Code snippets. Introduction to Python scripting in Blender 2.5x

(ThomasL) #1

Update 2011.06.26:
Updated and tested with B2.58:
http://hotfile.com/dl/122285414/86dd604/Code_snippets_updated_for_Blender_258_2011_06_26.zip.html

Update 2011.05.07

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

http://wiki.blender.org/index.php/Dev:2.5/Py/Scripts/Cookbook/Code_snippets

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

http://hotfile.com/dl/116601588/a03f0bb/Code_snippets_updated_for_Blender_257_2011_05_03.zip.html

Update 2011.04.14. A new version of the code snippets can be downloaded from

http://hotfile.com/dl/114370840/3347eb7/Code_snippets_updated_for_Blender_257_2011_04_14.zip.html
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

http://www.makehuman.org/download/Code_snippets_updated_for_Blender_254_2010_10_17.zip

http://rapidshare.com/files/425538067/Code_snippets_updated_for_Blender_254_2010_10_17.zip .

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 http://rapidshare.com/files/411523215/Code_snippets_Intro_to_scripting_in_Blender_25x_2010_08_07.zip.html .
(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, …
(RickyBlender) #2

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

(Meta-Androcto) #3

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

(demohero) #4

Thank you ThomasL. Looks useful.

(Eclectiel) #5

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

Keep it up!

(PKHG) #6

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 batch.py
you forgot to include some files see error message:
img = bpy.data.images.load(realpath)
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/batch.py”, li
ne 60, in <module>
File “C:\Users\Peter/snippets/scripts exture.py”, 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! )

(tin2tin) #7

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

Thank you very much!

(ThomasL) #8

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

http://rapidshare.com/files/411523215/Code_snippets_Intro_to_scripting_in_Blender_25x_2010_08_07.zip.html

(nf3) #9

great material. thanks for this valuable gift.

(germano) #10

FANTASTIC!
Exactly what I need.
Thanks

(gkvoelkl) #11

Hi Thomas,

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

I hope you will maintain it.

Best Regards

Gerhard:ba:

(zeffii) #12

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.
(kattkieru) #13

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.

(ThomasL) #14

Zeffii:
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.

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

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

(PKHG) #15

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


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

your batch.py 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 .

(tin2tin) #16

@ 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?
http://www.blendernation.com/blender-2-5-python-code-snippets/

(Dorro) #17

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

(Keleido) #18

ThomasL,

  • 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:
    http://blenderartists.org/forum/showthread.php?t=95662
    … 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)

(Blue Beard) #19

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
bpy.ops.object.material_slot_remove()

Add the two materials to mesh

me = ob.data
me.add_material(mat)

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?

(ThomasL) #20

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.