bpy.ops.constraint.delete - How?

bpy.ops.constraint.delete() doesn’t seem to work as expected. I was wondering what I may have missed.

The code you see below works up until the bpy.ops.constraint.delete() … that function seems unresponsive. Any thoughts?


    to_add = bpy.context.active_object
    
    add_constraint = to_add.constraints.new('TRACK_TO')

    target = bpy.data.objects['Golden Points Object']
    
    #set tracking settings
    bpy.context.active_object.constraints[0].target = target
    bpy.context.active_object.constraints[0].up_axis = 'UP_Y'
    bpy.context.active_object.constraints[0].track_axis = 'TRACK_NEGATIVE_Z'
   
    #apply transform
    bpy.ops.object.visual_transform_apply()
    bpy.ops.constraint.delete

Once again, it is almost easier to write your own solution than to try to integrate bpy.ops operations into your script.

Here is code that operates directly upon the data itself rather than relying upon bpy.ops for functionality.


import bpy

to_add = bpy.context.active_object

add_constraint = to_add.constraints.new('TRACK_TO')

target = bpy.data.objects['Cone']

#set tracking settings
bpy.context.active_object.constraints[0].target = target
bpy.context.active_object.constraints[0].up_axis = 'UP_Y'
bpy.context.active_object.constraints[0].track_axis = 'TRACK_NEGATIVE_Z'
   
#apply transform
bpy.ops.object.visual_transform_apply()

#remove constraint (NOTE: this removes all constraints from the Cube)
ob = bpy.data.objects["Cube"]

if ob != None:
    cl = len(ob.constraints)
    if cl > 0:
        # Remove existing constraints.
        for c in ob.constraints:
            ob.constraints.remove(c)

Hi

If there are constraints already then the new one may not be constraints[0]. You declare the add_constraint var, why not use it.



import bpy

to_add = bpy.context.active_object

add_constraint = to_add.constraints.new('TRACK_TO')

target = bpy.data.objects['Golden Points Object']

#set tracking settings
add_constraint.target = target
add_constraint.up_axis = 'UP_Y'
add_constraint.track_axis = 'TRACK_NEGATIVE_Z'
   
#apply transform
bpy.ops.object.visual_transform_apply()

to_add.constraints.remove(add_constraint)

1 Like

Thanks you guys. That works beautifully. I’m still learning python so these elegant little solutions don’t come so easily yet.

the constraints.remove() I did not know about. I had found the .delete one on the documentation. Is there a way I could have discovered .remove() quicker? I had googled for quite some time just to find delete().

Atom - I’d love to write my own solutions out. The main hurdle I have right now is not knowing how the data is structured/named to be able to grab it to work with it. Its still a very bump-in-the-dark process for me.

Is there a way to learn the data hierarchy more easily? Maybe a chart somewhere?

Thanks.

@MrOrange: I use the search for the API quite a bit. It is under the Help menu of the Blender application. For instance, I did a search on ‘up_axis’ this lead me to constraints material quite quckly.

Is there a way to learn the data hierarchy more easily?

print(dir(myVar)) is your friend.


import bpy

print(dir(bpy))
print(dir(bpy.data))
print(dir(bpy.data.objects))

After each print view the console for the results. dir() prints out all the methods and properties of the variable provided.

Sometimes you have to spend some time just digging down into variables. Like in your code if you did…

print(dir(add_constraint))

Then you examined the contents of the console you will see all the options that are possible to operate upon add_constraint.

That is very good. I would have been using that all along if I knew it existed! Thats a time saver.