1

I have this script:

import bpy
track = bpy.context.selected_objects


for i in track:

    bpy.context.scene.cursor_location = i.location

    bpy.ops.object.empty_add(type='CUBE', radius=0.05)

Track are the trackers and I want to parent the trackes to the emptys that where created.I try this script but it isn 't working:

import bpy

track = bpy.context.selected_objects
em = []

for i in track:
    bpy.context.scene.cursor_location = i.location
    new = bpy.ops.object.empty_add(type='CUBE', radius=0.15)
    em.append(new)
    bpy.ops.object.parent_set(em)

I just want to make my life easier.Thanks for any responts.

F.Y.I. : I 'm doing facial motion capture. I track the markers reconstruct them and then I did constraints to F-curves.I want the trakers-empties to align to the bones of the face of the pitchipoly human, before I constaint the bones location to the trackers-empties(copy location). I thought that this solution is simple, but the trackers-empties where too many so I thought to make a script.

Timoleon G
  • 11
  • 4

2 Answers2

2

Much simpler without operators, using API methods

import bpy

context = bpy.context
scene = context.scene
for o in context.selected_objects:
    # create an empty
    empty = bpy.data.objects.new("%sEmptyParent" % o.name, None)
    empty.empty_draw_type = 'CUBE'
    empty.empty_draw_size = 0.15
    # link to scene
    scene.objects.link(empty)
    # make object parent
    empty.location = o.location
    o.location = (0, 0, 0)
    o.parent = empty

All transforms (loc, rot and scale)

    # make object parent
    empty.matrix_world = o.matrix_world
    o.matrix_world = Matrix.Identity(4)
    o.parent = empty
batFINGER
  • 84,216
  • 10
  • 108
  • 233
0

The problem is that you assumed the bpy.ops.object.empty_add operator returns a reference to the empty object it just created. But like most blender operators, it simply returned its status, in our case {'FINISHED'} - which means it was successful.

So, you need a different method to find the latest empty created. Since new empty objects are created with the name "Empty", plus an automatic number (after the first empty), we can simply search for the newest Empty's name (which is also the largest in string comparisons).

import bpy

track = bpy.context.selected_objects
em = []

for i in track:
     # Set location directly when creating the empty, no need to set the cursor first
    bpy.ops.object.empty_add(type='CUBE', radius=0.15, location = i.location )
    newName = max( [ e.name for e in bpy.data.objects if e.type == 'EMPTY' ] )
    new = bpy.data.objects[ newName ]

    em.append(new)

    # Set parent and keep original cube's transformation    
    bpy.ops.object.select_all( action = 'DESELECT' )
    i.select   = True
    new.select = True
    bpy.context.scene.objects.active = new
    bpy.ops.object.parent_set( keep_transform = True )

In addition to the main issue specified above, I also changed the way the parent is set, and the way you set the empty's location.

TLousky
  • 16,043
  • 1
  • 40
  • 72
  • Thanks for the ansewr but it 's giving me this:

    exec(compile(bpy.data.texts['Text.002'].as_string(), 'Text.002', 'exec'))

    Traceback (most recent call last): File "<blender_console>", line 1, in File "Text.002", line 13, in TypeError: bpy_struct: item.attr = val: Object.parent expected a Object type, not list Am i doing some thing wrong?

    – Timoleon G Nov 06 '15 at 11:23
  • Yup, had a small bug in the last line, fixed it – TLousky Nov 06 '15 at 11:25
  • @TimoleonG Had to make another small change to avoid additional transformation of the cubes by their parents, now works as expected. – TLousky Nov 06 '15 at 11:32
  • Thanks you realy for answering my question! I was doing the tests on cubes and it worked.The problem is that i have animation on the trackers(I track the markers reconstruct them and then I did constraints to F-curves) when I run the script it parents all the trackerss to the center of them.I don 't know if I can sent you the blender file from this page. Sorry for not being clear. I can understand if you lost your interest in this question. – Timoleon G Nov 06 '15 at 12:04
  • @TimoleonG, upload your blend file here: http://blend-exchange.giantcowfilms.com/ And update your question and add this link. Would also be useful if you explained what you hope to achieve by parenting empties to the trackers, other solutions might be out there. – TLousky Nov 06 '15 at 12:10
  • I 'm doing facial motion capture.I want the trakers-empties to align to the bones of the face of the pitchipoly human, before i constant the bones location to the trackers-empties(copy location). I thought that this solution is simple, but the trackers-empties where to many so I thought to make a script.Do you think it will be beter to make a new question?Maybe a question for how to link traker bones.Or just update and explane this better? – Timoleon G Nov 06 '15 at 12:30