Code snippets. Introduction to Python scripting in Blender 2.5x

Update 2011.06.26:
Updated and tested with B2.58:

Update 2011.05.07

ValterVB has ported my python cookbook to the wiki format. It is now available at

I wish to thank Valter for his work.

The pdf version has also been modified. There is no reason to download the new file if you have the old one, because the only difference is that the license statement in the text has been removed. The code snippets are now licensed under Creative Commons, to comply with the wiki requirements. The new pdf file can be found at

Update 2011.04.14. A new version of the code snippets can be downloaded from
All scripts have been updated for Blender 2.57 and work, or at least do not generate any errors, with the official release. New examples have been added in the following areas.

  • Custom properties.
  • Interface and operators.
  • Blender add-ons.
  • Multi-file packages
  • Nodes

Update 2010.10.17: The examples in the original document from August do not work anymore, due to changes in Blender’s python API. A second edition, updated for Blender 2.54 and with many new examples, can be downloaded from .

Major changes:

  • The scripts work with Blender 2.54. More precisely, they have been tested with Blender 2.54.0 rev 32510, compiled from svn, on Ubuntu 10.04 64 bit.
  • User interfaces (panels, buttons, properties, menus, polling).
  • Simulations (cloth, softbodies, particle fire, smoke, rigid bodies, fluids).

Since the API is now supposed to have stabilized, there is a fair chance that most scripts will continue to work for the foreseeable future.

Original post:
I have written a little text with small scripts examples in Blender 2.5x. The examples are not really usable (hence the word snippets), but illustrate how to access various features. All program do run in Blender 2.53.1 (at least on my computer), and there is a batch file which runs all examples in the text.

The pdf and python files can be downloaded from .
(Update 2010.08.07. New download. Added missing image file and corrected some spelling errors.)

The focus is on data creation and manipulation. Here is a list of the topics covered:

  • Meshes.
  • Vertex groups and shapekeys.
  • Armatures.
  • Rigged mesh.
  • Applying an array modifier.
  • Three ways to construct objects.
  • Materials.
  • Textures.
  • Multiple materials on a single mesh.
  • UV texture.
  • Object actions.
  • Posebone actions.
  • Parenting.
  • Drivers.
  • Particles.
  • Hair.
  • Editable hair.
  • Texts.
  • Lattices.
  • Curves.
  • Paths.
  • Camera and lights.
  • Layers.
  • Groups.
  • Worlds.
  • Render settings and viewports.
  • Batch run.

Some of the things that you will not find in these notes, at least not in the
present revision:

  • User interfaces, button layouts, etc.
  • Macros, acting on what is currently selected.
  • Nodes, for materials, compositing, etc.
  • Game engine stuff.
  • Brushes and sculpting.
  • Advanced rendering, video compositing, …

one of the first PDF intro to 2.5 scripting

thanks for sharing
will give this as a 2.5 intro scripting - it’s nice to see it

which there was something on the hierachy for the the API!
would help to understand it better and find things around and faster!

happy 2.5

Thanks ThomasL
Really great to see quality tutorials such as this.

Thank you ThomasL. Looks useful.

It’s great to see efforts in this direction, helping us understand better/quicker the Python API.

Keep it up!

wow, your pdf and examples (tried just a few) are indeed something one should have available!

Thanks a lot, makes my life to play with Blender much more pleasant!

Is’n it stuff for the Blender (2.5) wiki?

Just used
you forgot to include some files see error message:
img =
SystemError: Error: Can’t read: “C:\Users\Peter/snippets/scripts/color.png”, No
such file or directory.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “C:\Users\Peter\25Blender\allicons\alliconsSVN31095_2.blend/”, li
ne 60, in <module>
File “C:\Users\Peter/snippets/scripts”, line 14, in run
raise NameError(“Cannot load image %s” % realpath)
NameError: Cannot load image C:\Users\Peter/snippets/scripts/color.png

Just putting there some color.png ,no error occured (and a View3D full of objects … nice! )

Wow 75 pages of scripting goodies! This is simply a must-have for beginning 2.5+ scripters!

Thank you very much!

Added missing image file and corrected some spelling errors. New download

great material. thanks for this valuable gift.

Exactly what I need.

Hi Thomas,

it is great. I am looking all the years for that.

I hope you will maintain it.

Best Regards


great i’m going to dig into this very deep :slight_smile:

might i suggest that you add a revision number of this text to the first page, so you can refer people to
check if they are reading the most up-to-date version.

a quick typo spotted : on page 3
This is not to say that these are important topics.
should read something like :
This is not to say that these are not important topics. (or favourably : insignificant)

Thank you! this is beautiful.

  • in section 1.4
    maybe you could add that if the tooltip from a context menu is visible then you can ctrl+c it’s content directly to buffer, it is useful functionality and perhaps not obvious.

Are there any mirrors of this? The rapidshare link is broken. If you need a mirror I’ll volunteer to host a copy on my website.

Latex automatically puts a date on the first page, and the name of the zip file also comes with a date. Should be good enough to distinguish between versions.

I just tested downloading from the rapidshare link in the first post. No problems here.

Thanks for your support. It inspires me to look into the parts of the python api that I still don’t know anything about.

I like to have my ‘blender’ files in my 25blender directory,
and changing line 27 of like this:

path = os.path.expanduser('~/<b>25Blender/CodeSnippetsBlenderArtists/</b>scripts')

your finds the snippets.
Maybe an idea?

In the latest blender (of today) I have to change pathes of expanuser too in files where expanduser occurs .

@ ThomasL

It inspires me to look into the parts of the python api that I still don’t know anything about.

Take your time. There’s allready plenty for people to digest.

The most inspiring way to learn a new coding language is (for me) through examples, so your work really makes a difference. My guess is that more people will start scripting thanks to you.

BTW. did you notice the Blender Nation post?

I always have problems with rapidshare. The traffic is definitely shaped depending on region and doesn’t work with DownThemAll.


  • Many thanks for this document.

  • For the next version the chapter Game engine stuff,
    I have done the portage 2.49 > 2.53b of my project:
    … It may help you

  • A suggestion: Blender 2.5x version means 3.xx Python version some lines on this matter could be interesting (… I’m dealing actually with the big changes concerning Unicode stuff)

In recents builds (i.e 31660) the following fails:

Pick active object, remove its old material (assume exactly one old material).

ob = bpy.context.object

Add the two materials to mesh

me =

AttributeError: ‘Mesh’ object has no attribute ‘add_material’

I am assuming the API changed in the last 100 builds or so.
Does anyone know what code should now be used?

Hi Blue Beard,

A lot of the code has been broken by the recent API changes. I plan on releasing an updated and expanded version after the 2.54 beta has been released, but I want to wait until the API has become stable again (and until I find some time, we have just embarked on a kitchen renovation project).

As for add_material(), however, I think it is a bug. Mesh.add_material() has been gone from svn for three or four days, but it is still in the documentation, and I have not figured out any other way to add a material to a mesh.