Sunday, December 23, 2007

Python and Blender

I was just working on a Blender project where I have 36 objects that I should have set the rigid body property for each. It took several minutes to do manually, was very tedious, and made me realize it was time to learn how to script Blender.

Blender is scriptable through the Python language. I've never scripted Blender and I've never used Python, so it took me a bit... Figuring out the appropriate Blender APIs and then learning the Python syntax (it is very different from what I'm used to like AS3, C# and Java). Here's my tiny, first script:
import Blender

objects = Blender.Object.GetSelected()

for curObj in objects:
curObj.rbFlags = curObj.rbFlags | Blender.Object.RBFlags.RIGIDBODY

Taking it apart, the first line imports the Blender module for the Blender APIs. The second line gets all of the selected objects in a scene using the Object module's GetSelected() method. The third and fourth lines iterate through the list of objects. Line 4 uses the bitwise OR operator to set the RIGIDBODY flag for the current object.

It took me a while to figure this little script out, longer than it took to do the operation manually. But the next time I need to set a property of a large number of objects -- any property, bitwise or otherwise -- it'll take seconds to type instead of minutes in the interface.

No comments:

Post a Comment