Showing posts with label Script. Show all posts
Showing posts with label Script. Show all posts

Sunday, March 28, 2010

Python 3, Part 6 (Functions)



In this tutorial, we are going to see how to write and call functions in Python 3, as well as how to pass arguments, both as keyword and value, to a Python function. I will also introduce you to some of the neat features of the Blender 2.5 text editor, such as source code color coding, which help make coding Python scripts much easier. This video is based on the Python 3.1 function tutorial at: http://docs.python.org/release/3.0.1/tutorial/controlflow.html#more-on-defining-functions.

Wednesday, March 3, 2010

2.50 Alpha 1 Python Part 4



Up to this point, we have been looking at the Blender 2.5 Python environment and to scripts to control windows, panels, and operators. We haven't done anything with actual Blender objects. It's time to see how to change and even create new Blender objects in Python.

Tuesday, March 2, 2010

2.50 Alpha 1 Python Part 3



In Part 2, we saw how windows are built from building blocks called Panels in Blender 2.5. You can lay out a panel and get data from the user. The next step is to actually do something with the data. For that, Blender 2.5 lets you define operators. Once you create an operator, it works anywhere within Blender. In this tutorial, we will create a simple operator, using the simple_operator.py template, one of a number of pre-designed templates that makes it easy for you to just add the functionality you need.

2.50 Alpha 1 Python Part 2



In Part 1 of this series on Blender 2.50 Python, we explored the new Blender 2.50 Python User Interface. In Part 2, we will look at the new user interface, consisting of windows and panels. You are already familiar with windows -- things like the 3D window, the Outline Window, and the UV / Image Editor Window. These still behave as 2.4x type windows, which you can resize, split, join, and so on.

However, the way windows are built is completely different. In Blender 2.5, windows are assembled in units called Panels. You program each panel as a Python class. A window is a group of panels, with each panel in the window registered to it. In this tutorial, we will explore how Blender 2.5 windows are put together by both looking at the scripts that build the windows in the user interface, and actually changing these windows. Then we will create a panel based on Blender's panel template script, and register it to a window, adding our own panel to a Blender window. In the process, we will see where the python scripts for the windows shipped with Blender are kept and examine how the user interface is created. The goal is to show you how to find the code for the windows, as a starting point for you to change the Python code to add panels to existing windows. The way of building the User Interface has changed so much in 2.50 that just about all Blender Python 2.4x scripts that use windows need to be rewritten, basically from the ground up.

This tutorial is based on the Blender 2.5 Tour 9, Python tutorial, by Michael Fox (mfoxdogg), at: .

So start up Blender 2.5 Alpha 1. The 3D window has a default cube. We won't be doing anything with any Blender objects in this tutorial. We'll only be looking at the user interface scripts. First off, where are they? To find out, switch the 3D window to a Text window by going to the window type buttons menu. The text icon is the same as in Blender 2.4x. From the Text menu, select Open. Bundled into the distribution files is a .blender folder. It may be hidden on your computer, so enable unhiding folders in your operating system, if you don't see that folder. The Blender Python scripts are in the Scripts folder. By the way, there are other Python scripts in the Python/lib folder. You can look at these, which are part of the Python 3.1 distribution, to learn more about Python itself. Today, we're going to look at the Scripts folder.

The Scripts folder itself contains a number of folders: extensions, IO, modules, op, presets, templates, and ui. We're going to start by going to the ui folder, which contains the scripts for the windows in Blender's user interface. The script names start with either "properties_" or "space_". The ones starting with "properties_" are the scripts for the various properties windows. The scripts starting with "space_" are scripts for Blender's windows, such as console, dopesheet, and 3D. Space_view3d.py, for example, is the script for the 3D window.

We're going to start with the script for the Material window, properties_material.py. In the window on the far right, which you may need to expand a bit, click on the big spherical icon. This is the Material window, which is the window produced by this script. The window is divided into sections, which are coded as Property panels. Hide the details of each panel, so that only the headers -- things like Preview, Diffuse, Specular and so on -- show.

Corresponding to each of these panels is a Python class of starting with the words MATERIAL_PT. In the code, the panels are not coded in the order that you see them. In fact, there are panels that you don't see. The panel for the Diffuse settings is labeled "Diffuse", and the code to build the user interface elements, such as the list of shaders, is in the class called MATERIAL_PT_diffuse, specifically in the Draw function, which is required for any panel, some of the code of which is highlighted in pink. There's a class called MATERIAL_PT_specular for the specular panel, with its Draw function, a class called MATERIAL_PT_shading for the Shading panel, and so on.

All the way at the bottom is a list of the panel classes in this window, which are highlighted. This is the order that the panels will display in, although not all of them may display. These classes are registered when the register function, highlighted here, is called. Note that the Physics panel is not displayed. That's because Physics is not appropriate to the Blender Render material window. However, if we switch to Blender Game, where Physics is relevant, the panel displays. In the code for the Physics panel, we see that the panel only should display for the Blender Game engine.

One last thing. The material window class has the following properties:
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "material"

Remember this when we build our panel, to add to the Material window. To build a panel, Blender has a template script called panel_simple.py, in the Templates folder. This script, by default, adds a "Hello World" panel, which displays the active object name, to the Object panel. Since we want to add this panel to the material window, we change bl_context to "material". The draw function actually draws the layout on the panel. The row() function creates a row. Three rows are created, one of which displays "Hello World" with an icon. The second row displays the name of the active object. The third row is an input area where we can change the name of the cube. Finally, this new class is registered.

If we press the Run Script button, the script runs. Look at the Material window. The Hello World panel was added at the bottom. Change the name of the cube to MyCube. Look in the outliner. The name of the object has changed.

Of course, there's a lot more, such as the details of how panels are drawn. We'll leave that for another tutorial. For now, I hope you have a better idea of how the new 2.5 interface works and how the scripts are organized. If you liked this tutorial, please hit the Subscribe button on Youtube, and discuss this video at forum.irakrakow.com. Happy Blendering!

Sunday, February 28, 2010

2.49 Camera Changer



Adding cameras, which have different views of the scene, and then changing them during your animation, can add more drama and realism to your animations. Blender lets you have as many cameras as you want, and to position them anywhere in your scene. Blender also has many Python scripts. You've probably used a few of them. For example, if you have ever saved the UV face layout from the UV/Image editor,to export the UV face layout to an image editor, you've used a Python script.

Saturday, September 12, 2009

2.49a Color Picker



If you've used any imaging software such as Photoshop, you're probably familiar with selecting a color using the RGB sliders. Of course, you can do this in Blender as well. However, there is a feature called the Color Picker which gives you a number of other ways of choosing colors. Anywhere Blender asks you for a color, you can use the color picker.

We start by firing up Blender with its default scene. We're going to delete the default cube (right click to select, then X or the delete key to delete), and add a UV Sphere (Space - Add - Mesh - UVSphere). The standard way of selecting a color is to use the RGB sliders. We'll add a Material by selecting the UVSphere (Right Click) in Object Mode. To add a material, click the F5 (Shading) button, then the Materials button. Go to solid mode (press the Z key if you are in wireframe mode) so we can see the color. Then click the Add New button. To make the material red, drag the R slider all the way up to 1 and the G and B sliders all the way down to 0.

Another way to choose a color is by the combination of Hue, Saturation, and Value, or HSV. The color picker lets you do this either visually or by using the sliders in a similar fashion as RGB.

The rectangle to the left of the Col text has turned red to show the UVSphere's color. Left click on it to bring up the Color Picker.

Drag the little circle on the color picker swatch to change the hue. Dragging to the right increases the hue value from 0 to 1. Dragging to the left decreases the hue value from 1 to 0. As you drag, notice that the Hue settings, on the right, change as well.

There are two boxes, in the lower right part of the color picker. The lower box shows the old color. The box on top of it shows the new color. The color hasn't been applied yet. There are two ways to apply the color. You can drag the cursor out of the color picker box, or you can press Enter. In either case, the color is applied and the new color shows in the color rectangle.

Click on the color rectangle to bring up the color picker. You can also drag the little circle in the Saturation and Value square. The saturation ranges from 0 to 1 along the y axis, and the value ranges from 0 to 1 along the x axis. Note how the saturation and value sliders change as you move the circle. Drag the cursor out of the color picker into the 3D viewport to select the color.

Click on the color rectangle to bring up the color picker. There's another way to choose a color, by direct entry of a hexadecimal RGB value. Each RGB value can range from 0 to 255, which is 00 to FF in hexadecimal. Here's a cream color, for example, with a hexadecimal value of FFCC99. We can enter this value in the hexadecimal value area. Again, we drag the cursor out of the color picker into the 3D viewport to select the color.

Click on the color rectangle to bring up the color picker again. There are 16 presets which let you store custom colors in the color picker. You can select a color by clicking on one of the presets. Or, you can create your own preset, which Blender saves between sessions. That way, you can create your own palette. we'll create a color and then select a preset rectangle, then ctrl-left click to create our custom preset.

Finally, the Sample button lets you select a color from anywhere within your Blender screen. After you press the Sample button the cursor turns into an eyedropper. Move the eyedropper anywhere within Blender and then left click to select the color that way.

You can use the Sample button, combined with the Image Browser, to sample a color from an image on your hard drive. Split the screen (position the cursor on the border, then right click and select Split), choose the Image Browser. Navigate to the directory containing your images. Then go back into the Color Picker, click the Sample button, and sample an image from the thumbnails shown on the Image Browser.

Blender's color picker gives you a number of ways of choosing colors beyond using the RGB sliders. You can select from the hue swatches, from the Saturation and Value square, or enter the HSV values directly into the color picker. You can enter hexadecimal RGB value. Or you can sample a color by pressing the Sample button.