Skip to content

Ivy Animator Script – Progress and lessons learned

January 22, 2013

So, I’ve been working some more on the Ivy growth animator script, gaining progress in making this a proper add on.

The first goal was to have the script do all the required preliminary steps, i.e. convert the ivy branches into meshes, sort their faces properly and add build modifiers to all of them. The rest was already implemented (setting the start and length of each build), so this wasn’t a lot of work but it taught me some important lessons about the Blender Python API.

I also refactored the code significantly, splitting the code into functions which add a lot of order and adding documentation and comments.

API Mechanics 101

The most important thing I gleaned from this experience was that the python API reflects quite accurately the normal workflow in Blender. For instance, if a certain command you do through the visual interface works only in edit mode, then you also need to run it in edit mode through the python API (meaning: toggle edit mode with a python command, then run the editmode-only command you want afterwards).

Also, many editing commands (transformations, conversion, adding modifiers, etc) will only work on the active, selected object – again, just like the visual interface.

So, writing a python script in Blender is in essence like writing a macro in simpler programs, which makes it straightforward and more familiar if you know Blender well, but also requires you to think about the visual interface when coding.

So these were some general thoughts, and here are some more specific useful commands I used.

Setting the position of the 3D cursor

I found several different commands that allow you to see the 3D cursor’s position, but only one that worked for setting its position.

bpy.context.scene.cursor_location = Vector( [ x, y, z ] )

If you try assigning a vector from some other source (coordinates for instance) to the cursor_location attribute, it might fail if that vector has more than 3 values. This happened to me when I tried putting the 3D cursor on the location of a bezier curve control point, using its coordinates (.co) attribute which is a vector. Since it has 4 values rather than 3, it failed miserablym until I finally understood the error message and isolated only the x, y and z values as specified above.

Adding a new modifier to an object

This is rather simple, but note that you give the modifier both a name and specify its type:

bpy.context.object.modifiers.new( NAME, TYPE )

Example:

bpy.context.object.modifiers.new( 'GROW', 'BUILD' )

That unique name is what you will use to access and manipulate the modifier’s values later on. For instance, if you created a new Build modifier named “GROW”, to change its properties you can use this:

bpy.data.objects[0].modifiers['GROW'].frame_start = 12
# sets the start frame of the modifier named "GROW" on the first object to 12

Simple commands

Here are a few simple but useful commands that I used in the script.

Selecting an object:

bpy.data.objects(OBJECT_NAME).select = True

Activating an object:

myobj = bpy.data.objects(OBJECT_NAME)   # create object reference
scene.objects.active = myobj

Toggle edit mode on selected object:

bpy.ops.object.editmode_toggle()

Going to a specific component selection mode while in edit mode:

bpy.ops.mesh.select_mode(use_extend=False, use_expand=False, type='FACE')
#type could also be 'VERT' or 'EDGE' of course

Select all mesh elements (faces / edges / verts) – depends on select mode (see above)

bpy.ops.mesh.select_all()

Convert curve object to mesh:

bpy.ops.object.convert(target='MESH')

Separate mesh to objects (same as the “P” shortcut key):

bpy.ops.mesh.separate(type='LOOSE')
# could also be ‘SELECTED’ or ‘MATERIAL’ to separate the selection only or by variations in materials, accordingly

Count the number of faces on the active (mesh) object:

bpy.context.object.data.update(calc_tessface=True)     # calculate face data so that...
face_count = len(bpy.context.object.data.tessfaces)    # we can obtain the total number of faces

What’s next?

The last two things required for this to be released as a proper add on, is:

1. animating the leaves (which proved to be more complex than I initially thought, still wip).

2. Creating some interface elements which will enable users to set values to some of the important variables (which control the overall length of the animation and how it progresses). It will also enable users to choose the ivy branches curve object and leaves mesh object via an object browser, then press a button to run the script and create the animation.

This is going to be interesting!

Post_3_TitleImage

Advertisements
Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: