Introduction Blender is a powerful 3D application for creating 3D images and animations and bring your creativity to life. The strength of Blender is its philosophy to provide a smooth and seamless work flow and tools for the artist. By interacting with the multitude of buttons, menu's using keyboard and mouse you can create objects, sculpt them, rig those objects etc., and eventually render your scene composed of aforementioned objects.
Apart from creating objects using devices, Python also enables you to create objects and other things using a scripting interface.
Now, If you are absolutely new to Programming, please go through the excellent and free online e-book Learning With Python. (Note: I am in the process of writing a quick primer covering Python 3.1 which Blender 2.5 and up will be using)
In a nutshell, in order for us to program, one should be comfortable with creating, extracting and manipulating data with operations. Python provides us with very nice ways of dealing with data and get out job done. Accessing Blender's Built-in Python Console By pressing SHIFT+F4 in any Blender Window (3D View, Timeline etc.,) you can change it to a Console
Note: Python is a case-sensitive language, so make sure you type the commands as shown in this tutorial (using the same case)To check what symbols are loaded into the default environment, type dir() and press ENTER-KEY Now, type bpy. and then press CTRL+SPACE-KEY and you will see the Console auto-complete feature in action. We will make use of this a lot to help our learning the API faster. You will notice that a list of sub-modules inside of bpy appear. These modules encapsulate all that we can do with Blender Python API and are very powerful tools. Exercise: List all stuff inside the bpy.app module
We can now access the stuff (that was listed previously) one by one by typing bpy.app.stuff1and pressing ENTER-KEY Now that you got a hang of this, lets proceed to investigate the more useful modules in bpy. Basic BPY API Modules
If you look at the 3D Viewport in the default Blender scene, you will notice 3 objects: Cube, Lamp and Camera.
For all the scenarios listed above (not all were listed, mind you..) the bpy module provides functionality to access and modify data.
Let's start with the Contextual access of information in a Blender file/scene. bpy.context Type the following in the Console (>>> stands for Console input prompt)
>>> bpy.context. and press CTRL+SPACE-KEY to list everything inside it.
For the above commands to show the proper output, make sure you have selected objects in your 3D view.
bpy.context.mode Will print the current 3D View mode (Object, Edit, Sculpt etc.,)
bpy.context.object bpy.context.active_object
Will give access to the active object in the 3D View
Exercise: Try out.. (Enter following commands and press ENTER-KEY after each one) >>> bpy.context.object.location.x = 1
Change x location to a value of 1
>>> bpy.context.object.location.x += 0.5 Move object from previous x location by 1 unit
>>> bpy.context.object.location = [1, 2, 3]Changes x, y, z location
>>> bpy.context.object.location.xyz = [1, 2, 3]Same as above
>>> type(bpy.context.object.location)Data type of objects location>>> dir(bpy.context.object.location)Now that is a lot of data that you have access to
bpy.context.selected_objects
Will give access to a list of all selected objects (including the active object)
Exercise: Try out the following commands in the Blender console window. >>> bpy.context.selected_objects then press CTRL+SPACE-KEY
>>> bpy.context.selected_objects[0]Prints out name of first object in the list
>>> [object for object in bpy.context.selected_objects if object != bpy.context.object]
Complex one.. But this prints a list of objects not including the active objectbpy.data
|









