Scripting Blender

From PioneerWiki
Revision as of 13:34, 20 February 2013 by Mikehgentry (talk | contribs) (Set up Blender)
Jump to: navigation, search

Why bother?

It's actually really easy, and the kind of geometrically regular 'box modelling' building a spaceship entails is well suited to scripting.

Next time you find yourself repeatedly doing the same sequence of operations to lots of different meshes or objects, consider writing a script. It may take a bit longer the first time, but once you know how you'll be much more efficient.

Walk through

We're going to write a script (more of a macro really) which takes the selected object, duplicates it, moves one duplicate 10 units forward on one axis, then shrinks it 10% on the other two axes.

Set up Blender

  • You may need to install Python and place it in your $PATH first. I'm assuming if Blender works, Python works, but on Windows who knows...
  • Switch to 'Scripting' mode at the top, and in the Text pane's file widget hit 'New'. This is where our script will go. Type
import bpy

as the first line - that's the Blender PYthon module.

Perform the operations

Now we manually perform the operations we want the script to automate, to get an idea of the underlying python functions the interface uses. The arguments and output will appear in the top console pane. So, when I duplicate the starter box, move it ten units forwards on Y, then shrink it to .9 on X and Z, I get:

bpy.ops.object.duplicate_move(OBJECT_OT_duplicate={"linked":False, "mode":'TRANSLATION'}, TRANSFORM_OT_translate={"value":(0, 0, 0), "constraint_axis":(False, False, False), 
"constraint_orientation":'GLOBAL', "mirror":False, "proportional":'DISABLED', "proportional_edit_falloff":'SMOOTH', "proportional_size":1, "snap":False, "snap_target":'CLOSEST', "snap_point":(0, 0, 0), 
"snap_align":False, "snap_normal":(0, 0, 0), "texture_space":False, "release_confirm":False})
bpy.ops.transform.resize(value=(0.9, 1, 1), constraint_axis=(True, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', 
proportional_size=1, snap=False, snap_target='CLOSEST', snap_point=(0, 0, 0), snap_align=False, snap_normal=(0, 0, 0), texture_space=False, release_confirm=False)
bpy.ops.transform.resize(value=(1, 1, 0.9), constraint_axis=(False, False, True), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', 
proportional_size=1, snap=False, snap_target='CLOSEST', snap_point=(0, 0, 0), snap_align=False, snap_normal=(0, 0, 0), texture_space=False, release_confirm=False)

Test

Copy those three statements from the top pane by right-clicking on each, then hitting ctrl-c. Paste them with ctrl-v just after the 'import bpy' statement. Congratulations, you've just written a Blender Python Script.

Hit 'Run script' below and you'll note that it all works, apart from failing to move the object along Y. By looking at the 0.9 arguments to the resizes, we can work out that the second dimension represents Y (figures... X, Y and Z), so we try putting 10 in as the second argument to the duplicate_move function. We also reason that the second transform_resize statement is redundant - shrinking by different function calls for each axis might not do any harm, but it's needlessly messy. As long as we turn the constraint_axis arguments to false, we can feed in values on any axis. Test it again, and it works!

import bpy

bpy.ops.object.duplicate_move(OBJECT_OT_duplicate={"linked":False, "mode":'TRANSLATION'}, TRANSFORM_OT_translate={"value":(0, 10, 0), "constraint_axis":(False, False, False), "constraint_orientation":'GLOBAL', "mirror":False, "proportional":'DISABLED', "proportional_edit_falloff":'SMOOTH', "proportional_size":1, "snap":False, "snap_target":'CLOSEST', "snap_point":(0, 0, 0), "snap_align":False, "snap_normal":(0, 0, 0), "texture_space":False, "release_confirm":False})
bpy.ops.transform.resize(value=(0.9, 1, 0.9), constraint_axis=(False, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1, snap=False, snap_target='CLOSEST', snap_point=(0, 0, 0), snap_align=False, snap_normal=(0, 0, 0), texture_space=False, release_confirm=False)

Moving on...

Through a similar process - working out what the interface is doing when you click the buttons, then sticking it together into primitive scripts, and a bit of fiddling to get it going - you'll soon be Blending much faster.

Tutorials