2
$\begingroup$

I have a very basic script which I hope to do a couple things at once, but I am stuck at removing the constraint, and the group_link fails.

bpy.ops.constraint.delete()

and

obj.group_link(group='Ties')

What am I doing wrong? Note: the group does exist, it even has 1 object in it just to be sure. :)

EDIT:

Everything here works, now only to solve the constraint removal. :( :)

import bpy

for obj in bpy.context.selected_objects:

    ### get last 3 digits from object name and save them as objNumber
    objNumber = obj.name[-3:]

    ### set object as Active
    bpy.context.scene.objects.active = bpy.data.objects['Cube.'+objNumber]

    ### add Boolean and set the target to Cube
    bpy.ops.object.modifier_add(type='BOOLEAN')
    obj.modifiers["Boolean"].object = bpy.data.objects["Cube"]


    ### remove current constraints and add Copy Transforms, with target set to an Empty with the same number as original object
    bpy.ops.object.constraint_delete()
    bpy.ops.object.constraint_add(type='COPY_TRANSFORMS')
    bpy.context.object.constraints["Copy Transforms"].target = bpy.data.objects["Empty."+objNumber]

    ### moving to layer 10 and grouping to group Ties
    #bpy.context.space_data.context = 'OBJECT'
    obj.layers[10] = True
    obj.layers[1] = False
    bpy.ops.object.group_link(group='Ties')
$\endgroup$
  • 1
    $\begingroup$ bpy.ops generally works on the active context object, which is only one of context.selected_objects generally index 0, always context.active_object. You can set the context object in a script with context.scene.objects.active = obj and use the operators. However you can add a constraint via con = obj.contstraints.new(name, type='COPY_TRANSFORMS') and then set its properties with con.target = empty. Will put together as an answer if required. $\endgroup$ – batFINGER Oct 7 '16 at 11:30
  • $\begingroup$ Thank you for your reply! Everything except the constraint removal works for me now, but the bpy.ops.constraint.delete() still throws an error about wrong context ... though the bool modifier, new constraints and grouping all works $\endgroup$ – V453000 Oct 10 '16 at 10:14
  • $\begingroup$ Found a solution! I don't understand the code yet but it works :D blenderartists.org/forum/… $\endgroup$ – V453000 Oct 10 '16 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.