Code snippets. Introduction to Python scripting in Blender 2.5x

I agree the API is changing, in most cases for the better.

I also agree add_material appears to be a bug.

Thanks for the responce!

would be nice to have some snippets fro adding creating panesl in UI if possible
i know it will 10 to 20 pages but woudl helps noobies get around with how to add panels
with buttons ect,

hope you come out with enw version sson
thaks for the work you put into this its great intro to scripting
wish there was a lot more like this outthere!

happy 2.5

seems that mat and mat multi dont’ work anymore
if you know how to do it please let me know
i need that now !

also the add text
tcu.spacing = does not exist any more

would it be possible to add in your scnippet adding a poly curve if possible with some mat

i have an example of this if you need one but not really with mat yet !

keep up the good work even with API cahnges !

happy 2.5

The documentation for TextCurve can be found at There are now three attributes controlling spacing: space_character, space_line and space_word.

Boolean python attributes, which correspond to checkboxes ni the UI, now usually start with use_ or show_ .

i change my little script
but now i get an error on the size in version 626 which is only a few days old!

in which version theses changes apply ?

if would be nice if you could include in your snippets
how tio change the center of the created word or maybe add convert to text and then change the center locaton for right or middle or left ?
which might be very usefull for some application

hope to see your new tut soon

keep up the good work
Thanks & happy 2.5

I am using the following to add material to an object now: (build 32297)

mat =[stkmat]
ob.active_material = mat


texslot = mat.texture_slots[0]
texslot.texture_coords = 'UV'

for adding MaterialSlot parameters

This is really useful information.

very cool, thanks for sharing the book!
will you be doing updates to the book as the PyApi is finalized

don’t want to put any pressur but
can you tell us when your going to come out with a new update version for your PDF file?

can’t wait to see this new updated version for 2.54

Thanks happy 2.5

On popular request (on Ricki’s request anyway), here comes a second edition of my cookbook. The code snippets have been updated to run with the api of Blender 2.54. The scripts have been tested on r32510 under Ubuntu 10.04 64 bit.

There are quite a few new examples in two areas:

  • User interfaces (panels, buttons, properties, menus, polling).
  • Simulations (cloth, softbodies, particle fire, smoke, rigid bodies, fluids).

The cookbook can be downloaded from . Perhaps I will find some alternative hoster too, but not today.

Oh Thomas, just detected … API is different: ob.draw_name = True should be
ob.show_name = True

And another thing, seems wrong to me

version = [2, 53, 0]
(a,b,c) =
if b > version[1] or (b == version[0] and c > version[2]):
    msg = 'Blender too old: %s < %s' % ((a,b,c), tuple(version))
    raise NameError(msg)

my version is 2.54.0 and is rejected?!
> should be <? and the second b maybe an a?

Than there are inside the .blend the 3 default objects, but not visible in any screen
giving an error for the first loop to delete them!

So sorry, the unzip went a little bit wrong … so my remarks are not totally good

Question x_ray ???

Error from me … (code deleted is ok ) x_ray question stays.

Thank you vey much for this updated version
very nice new additions for panels

this intro to snippets in 2.5 should be added as a sticky at top of page
will be very usefull for lot’s of people who want to begin playing and learn 2.5 scripting

happy 2.54

Small suggestion for

bpy.types.Object.my_red = FloatProperty(
    name = "Red", default = 0.5, min = 0.0, max = 1.0)
bpy.types.Object.my_green = FloatProperty(
    name = "Green", default = 0.5, min = 0.0, max = 1.0)
bpy.types.Object.my_blue = FloatProperty(
    name = "Blue", default = 0.5, min = 0.0, max = 1.0)

RGB are between 0.0 and 1.0 aren’t they?

ran it in 31626 but not working but guess it’s not updated enough

so ran it in 32 385 and seems to work fine

following the recommaendations from PKHG

nice example showing how to change color of object!

now where is the swatch added ?


Ha ha, look at the code. Properties Panel and there: under Material!

EDIT (just discovered):
And the mesh must have at least one color to use the SWATCH Panel

i see the new sub panel in mat panel

but what are buttons for adding swatch and delete do ?
i mean is it adding a new button wit color and you can delete it
and where is it added?


[quote=RickyBlender;1721139]i see the new sub panel in mat panel

but what are buttons for adding swatch and delete do ?
i mean is it adding a new button wit color and you can delete it
and where is it added?

Click on the upper button, and you can see and select the ‘swatches’ you have!
In the beginning red … after adding a color it looks a little bit different.
And deleting deletes the selected one (I thought).

ok i see it i think now

when you add swatch this add a new menu item to the first menu button
sorry i tough this was a panel name but at the end there are the little arrows!
so it is really a menu button!

and you can then select it or other values already pre defined
and the delete button will remove this item from that menu list

nice example for dynamic menu


thanks for all your hard work Thomas.
I’m happy to host the Introduction on the Blender Wiki & the Files on Blender Projects would be ok too.

Also, we really need to fix the Make Human script.
I would really like to discuss this with you.
I am on IRC #freenode #blenderpython each day.
If you have some spare time, please drop in so we can fix things.
Thanks for all your great work.