0
$\begingroup$

I would like to use the Python API to:

  1. set the parent of a set of objects to an empty object
  2. then set the location of the empty object to the midpoint of the bounding box of all objects

but not move all the objects

Step 1 works ok - but I am stuck on the second.

I start with this situation:

enter image description here

and after grouping things look like this:

enter image description here

and

enter image description here

with the origin of e (the empty object) set at [0, 0, 0]

Now if I try to re-center the empty doing this:

def recenter(obj, point):
    try:
        obj.data.transform(mathutils.Matrix.Translation(-point))
    except:
        print('obj has no data')

    print('translation', obj.matrix_world.translation)
    print('location', obj.matrix_world.translation)    

    obj.matrix_world.translation += mathutils.Vector(point)

    print('translation', obj.matrix_world.translation)
    print('location', obj.matrix_world.translation)    

    return(obj)


tidy_names(locals())

recenter(e, [1.5, 1.5, 0.5]) 

I end up with:

enter image description here

Is there a shorthand way of doing this? I suspect I could recurse through all the children and reset, but that seems quite insane...

$\endgroup$
5
  • $\begingroup$ Related blender.stackexchange.com/questions/229292/… and blender.stackexchange.com/questions/223858/… $\endgroup$
    – batFINGER
    Aug 13, 2021 at 15:25
  • $\begingroup$ @batFINGER - thanks. So it looks like recurse it is, then? $\endgroup$
    – simone
    Aug 13, 2021 at 15:30
  • $\begingroup$ Why recurse? Example linked It is only iterating over immediate children, or if setting up for scene objects children will be those without a parent. $\endgroup$
    – batFINGER
    Aug 13, 2021 at 15:32
  • $\begingroup$ I thought: in a generalized solution I cannot rule out that there will be children of children. But I now realize that re-setting the position of the children will take the grandchildren along $\endgroup$
    – simone
    Aug 13, 2021 at 15:35
  • $\begingroup$ You could do it more easily if you calculate the midpoint first, move the empty there, and then parent, perhaps? $\endgroup$ Dec 13, 2021 at 1:14

0

You must log in to answer this question.

Browse other questions tagged .