Converting a script from 2.49b to 2.6x

I have been trying to update a script I wrote for 2.49b to 2.6x. Right now, I have been trying to create an empty object with a specific name, location, and rotation. I found the bpy.ops.object.add function that will create an object with a specified location and rotation, but not a specified name. There is bpy.ops.object.add_named, but with it I cannot specify a specific location or rotation.

I have in my code:


  e = bpy.ops.object.add(type="EMPTY",location=(x,0.0,-10.0),rotation=(0.0,0.0,0.0))

It appears that e is a set that contains the string “FINISHED” instead of being the newly created object like it was in 2.49b. It would be sufficient if I can rename the object once it has been created (or reset the location/rotation) but I don’t know how to access the newly created empty object since bpy.ops.object.add doesn’t seem to return it.

As I continue updating my code, I’ll probably use this thread for a lot more questions about how to update my script from 2.49b to 2.6x, so I hope nobody minds.

cause of Bmesh there is an update n API for primitives

bpy.ops.mesh.primitive_cylinder_add(vertices=32, radius=1, depth=2, end_fill_type=‘NGON’,
view_align=False, enter_editmode=False, location=(0, 0, 0), rotation=(0, 0, 0),
layers=(layers))

salutations

Thanks, but I wanted to add an empty, not a cylinder.

it just give the general idea and what changes were made

look in api for the empty one

but also like this

import bpy
bpy.ops.object.add()
object = bpy.context.object
object.name = ‘new_empty’

layers = [False]*20
layers[0] = True

bpy.ops.object.add(type=‘EMPTY’, view_align=False, enter_editmode=False, location=(0, 0, 0), rotation=(0, 0, 0), layers=(layers))

salutations

Thanks. Using “object = bpy.context.object” worked.

My next question is that as I edit my python scripts I am frequently getting errors that say I am using inconsistent indentation, even though I only use the tab key to indent and never use spaces. I can select Format > Convert Whitespace > To Tabs, but this becomes tedious. Is there a way to change my settings so that a tab key always produces a tab character and never several spaces in place of a tab? Also, I wonder if when I push return on an indented line and the next line comes up indented, if the indentions that it produces are of tabs or spaces. I can’t really tell because when I use the arrow keys, it acts as if they are tabs, but when I use the backspace key to delete the indentation, it acts as if they are spaces.

i prefer to use an external text editor like notepade2 or notepad ++ which are free and easy to use
then i load or reload the py file in the text editor

much easier to read and modify !

in these editor you can select tab or space and they show the text color for python programs too!

salutations

You can also do this if you don’t want to use bpy.ops:


empty = bpy.data.objects.new('empty', None)
context.scene.objects.link(empty)

As for the text editor… yeah, it’s a mess if you want to try to use tabs instead of spaces.

there is a format command to go from spaces to tabs which seems to work fine in text editor then you can save it to a py file!

salutations

My creates an empty and then assigns it the variable ‘e’. The value of c[“name”] is “0”.


  bpy.ops.object.add(type="EMPTY",location=(x,0.0,-10.0),rotation=(0.0,0.0,0.0))
  e = bpy.context.object
  e.name = c["name"]

Later, it loads three objects from another file and I want to assign it the value ‘obW’.


  for i in [ ["head",cfile] , ["arm",pfile] , ["face",pfile] ]:
   ...
    bpy.ops.wm.link_append(directory=i[1]+'/Object/', link=False, autoselect=True, files=to_load)
    obW = bpy.context.object
    print("e.name is ", e.name)
    print("obW.name is ", obW.name)
    obW.name = c["name"] + "." + i[0]
    constr = obW.constraints.new("CHILD_OF")
    constr.name = "ChildOfEmpty"
    constr.target = e

Here is the result of the print statements:


e.name is  0
obW.name is  0
e.name is  0.head
obW.name is  0.head
e.name is  0.arm
obW.name is  0.arm

The constraints were added to the empty object, not the objects that I loaded from the other file, which is where I wanted the constraints. It looks to me like when I assign obW to bpy.context.object, I am assigning it to the empty object that I initially created instead of the new objects that I loaded from the other file. How can I assign obW to the object that I loaded with bpy.ops.wm.link_append?

the object has to be the active object it think !

salutations

The link_append op doesn’t return the added object/objects as the context or active object. Use context.selected_objects instead.

On another note the docs here http://www.blender.org/documentation/blender_python_api_2_62_1/info_gotcha.html#library-collisions
allude that this is possible


# library object name look up using a pair
# where the second argument is the library path matching bpy.types.Library.filepath
obj = bpy.data.objects["my_obj", "//my_lib.blend"]

I’ve had absolutely no joy with this.

Thanks for the help. context.selected_objects seems to have worked.

My next question is where do I find the possible keying sets to use with bpy.ops.anim.keyframe_insert? In particular, I want to know how to insert a keyframe for the start and end clip values on a camera. I tried searching through the API reference but couldn’t find anything. I tested it with a few values like “CLIP_START” and “STARTCLIP” but none of them worked.

Right now I’m trying to figure out which fcurve controls the X location, which controls the Y, etc. I am trying to replace the following code from a script that works in 2.49b:


    LocX = 0
    LocY = 0
    LocZ = 0
    for i in ipo:
     if i.name == "LocX": LocX = i
     if i.name == "LocY": LocY = i
     if i.name == "LocZ": LocZ = i
    x = LocX[f]
    y = LocY[f]
    z = LocZ[f]

where f is an integer. I have been exploring through the python console in Blender and found that I can access act.fcurves[i].data_path (where act is an action) and determine if the fcurve controls location or rotation, but it does not specify which curve is x, y, or z.

It’s the the fcurve.array_index prop. 0 for x, 1 for y etc.