1
$\begingroup$

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.

$\endgroup$
2
2
$\begingroup$

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
$\endgroup$
2
  • $\begingroup$ Would probably be negligible in terms of performance in this case, but good to know empties can be created this way! $\endgroup$ – TLousky Nov 6 '15 at 12:11
  • $\begingroup$ can I chat with you somewhere? I have some questions... $\endgroup$ – Timoleon G Nov 6 '15 at 13:22
0
$\begingroup$

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.

$\endgroup$
6
  • $\begingroup$ 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 <module> File "Text.002", line 13, in <module> TypeError: bpy_struct: item.attr = val: Object.parent expected a Object type, not list Am i doing some thing wrong? $\endgroup$ – Timoleon G Nov 6 '15 at 11:23
  • $\begingroup$ Yup, had a small bug in the last line, fixed it $\endgroup$ – TLousky Nov 6 '15 at 11:25
  • $\begingroup$ @TimoleonG Had to make another small change to avoid additional transformation of the cubes by their parents, now works as expected. $\endgroup$ – TLousky Nov 6 '15 at 11:32
  • $\begingroup$ 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. $\endgroup$ – Timoleon G Nov 6 '15 at 12:04
  • $\begingroup$ @TimoleonG, upload your blend file here: 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. $\endgroup$ – TLousky Nov 6 '15 at 12:10

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.