Code snippets. Introduction to Python scripting in Blender 2.5x

Oops, I forgot to convert tabs to spaces in quite a few scripts. The scripts run all right, but the indentation in the pdf file was lost. Fixed link:
http://rapidshare.com/files/425538067/Code_snippets_updated_for_Blender_254_2010_10_17.zip

I have not made any of the suggested changes, because I had already uploaded to rapidshare when I read the comments. Maybe for a later version.

i’m going to open a new thread for this layout panel
cause i a few questions for this one

may be you can help to anwer the questions

Thanks

Can’t wait to find this on Blender projects or BlenderWiki as Meta-Androcto suggest.

Rapidshare transform the Web in a Minitel, pay for download fast or wait!

Cannot download the file.

Hi ThomasL,
Your .pdf does look very useful and interesting, but could you please make it available from somewhere else than RapidShare ? I tried to download it twice this morning, and at the second attempt I got kicked off their site with a rude message…
Thanks in advance,
Thierry

I confirm for Rapidshare. I thought I had to register, I tried to but could not validate my account. The Java scripts on their page seem to be shot, you get rude messages, these people SUCK. Please do try to find somebody else.
Thanks,
Thierry

some little changes for
blender 2.55 r32944

in shapekey.py i had to change 3 times this line (usage without Argument)
bpy.ops.object.shape_key_add() #False)

and in batch.py
i changed the version-check to
version = [2, 55, 0]
and
because it is only possible for me to run batch.py one time with the default scene (thats the scene with 1 cube, lamp, cam)
i changed:
#for ob in bpy.data.objects:
for ob in scn.objects:

to look only for objects in the scene, but even then it was
not possible to delete all objects created with the batch.
it says a lot “cyclic” and maybe it is necessary to have
different loops: one first setting all objects to mode=OBJECT
and then unlinking and deleting the list of objects for several times
until the cyclic dependency is solved by deleting the “parent”-object
to free the childs, so those will be deleted in the next run --?–
Would this work?

@test-dr which shapekey script did you have to make those changes to?

from the date of my posting,
there was this version(check the first posting of this thread, where he makes the updates):
Code_snippets_updated_for_Blender_254_2010_10_17.zip
and i dont know about any newer version.
Thats why there ist the change vor the version-number-check too (in my changes)

I just wanted to thank you for this. I’ve been using it since the first version, and it has helped me tremendously.

I have a question about curve creation. When I use the code snippet from the pdf it works and does exactly what it should until I change the handle type. The code I used is below:

import bpy

from mathutils import Vector

# Add curve and object.
aCurve = bpy.data.curves.new('aCurve','CURVE')
aObj = bpy.data.objects.new('aObj',aCurve)
bpy.context.scene.objects.link(aObj)

# Set attributes
aCurve.dimensions = '3D'
aCurve.use_fill_back = False
aCurve.use_fill_front = False

# Set the control coordinates
points = [(0,0,0),(2,2,-2),(3,-1,5)]


spline = aCurve.splines.new('BEZIER')
numPoints=len(points)
spline.bezier_points.add(numPoints-1) # Make sure only numPoints points in the curve, 1 is created with the new Bezier.

for n in range(numPoints):
    newPoint = spline.bezier_points[n] # New point
    newPoint.co = points[n] # Set the coordinates

    newPoint.handle_left = newPoint.co+Vector((0,0,1)) # Set the handles above and below the points
    newPoint.handle_right = newPoint.co+Vector((0,0,-1))
    
    newPoint.handle_right_type='AUTO' # Set the handle type
    newPoint.handle_left_type='AUTO'

However, if you enter edit mode the handles are clearly not in AUTO mode. If you select a point, press ‘G’ and then ‘ESC’ they will fix themselves.

Does anyone know why this happens and why they are not already correctly in AUTO mode?

Thanks in advance,
Truman

Here is an alternative download location:

www.makehuman.org/download/Code_snippets_updated_for_Blender_254_2010_10_17.zip

The shapekey problem is due to an api change after 2.55 was released. It will be corrected eventually, but I want to wait a while and see if there will be more api changes.

@ Truemanblending

you should open a new thead for this subject

might be intereting to find a good solution for this!

happy 2.5

here are a couple of threads giving lots of info on some basic things
which would give more info to newbies in your PDF intro file snippets

thread for filepath filename

http://blenderartists.org/forum/showthread.php?t=202664&p=1741105#post1741105

thread for panels with rows of int floats
and using operators buttons properties ect.
and also some example using box with operators properties ect.

http://blenderartists.org/forum/showthread.php?t=202459&p=1741103#post1741103

Theses other example would definitively fit very well in your PDF intro with snippets
and help newbies understand how to do things in 2.5

Thanks and happy 2.5

Thankyou for this great thread, would you know how to get the colour info from an image/ texture.
so in 2.49 I have: r,g,b,a = zoneMap.getPixelF(rowPix, colPix)
what would it be in 2.5?

are you going to come up with a new vesion soon ?
there has been many changes to API in last 2 months
and hope it will stabilize soon!

also would it be possible to add more snippet for curves
like how do you identify curves
poly bezier spline
and also some examples on how to read/write the points for theses different
type of curves

then how to convert curves to another type of curve let say from bezier to poly or psline
and convert to mesh controlling the REso for a curve or bevel curve

another exmaple would be nice for adding a bevel and taper curve!

thanks and keep up the good work
happy 2.

Looks like the add lattice and a few other of the scripts are not functioning. Seems “assign” isn’t an attribute anymore. Any ideas on what the current method would be?

This is a sample of the code in question. The ob.vertex_groups.assign is resulting in an error and an invalid attribute.

for v in ob.data.vertices:
     if v.co[o] > 0.001:
          ob.vertex_groups.assign([v.index], left, 1.0, 'REPLACE')

Watch here:
http://blenderartists.org/forum/showthread.php?t=209935&p=1795800&viewfull=1#post1795800

Basically the script create a vertex group and assign to it a material.
Ciao
VB

Looks like this snippet is what we need:

Group1 = obj.vertex_groups.new('SnowLevel') #Create a Vertex Group
Group1.add(vgroup2, 1.0, 'ADD') #Add the vertices to the vertex group

Provided it works. I’ll have to check.

A lot of the scripts use the assign attribute. So this will fix a good portion of them.

Yes. I can confirm that works to fix the problem. Thanks for the link :slight_smile:

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. New examples have been added in the following areas.

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