2
$\begingroup$

I'm facing an apparently well known issue with Blender that was already described here.

Basically, when I create a new object at world level and then make it the child of another object, its coordinates stay coherent with world and there's no easy way to update them relative to the parent. I need positional data of my objects in relation to how the hierarchy is organized because I have to plot them outside.

A pratical example is when I generate bounding boxes for one or more objects. I used this add-on by sambler to make the BBs, though they get created at world level by default, whereas I'd like to create them as parents of the related items, just one level up, regardless of where they're found in the hierarchy. If I make the BB then manually move it to the desired position in the hierarchy, I come across the "freezed" coordinates issue described above.

Do you have any suggestion? Thank you!

$\endgroup$

1 Answer 1

1
$\begingroup$

Insert a bounding box as parent.

Does a child object inherit the matrix from the parent?

Can slip in a new parent

  • Copy the object, clear modifiers, constraints and animation data
  • Make the copy the parent
  • Set the copy matrix local to identity, in this case with basis and parent_inverse both identity. As shown in link above when calculating the world matrix by chaining the matrices up the parents, an identity local matrix does not contribute to the result.
  • All local transforms that were on input mesh are now transferred to the bbox parent.

Test script, select a mesh object, run script.

import bpy
from mathutils import Matrix, Vector

def bbox2mesh(ob):
    bbox = ob.copy()
    bbox.constraints.clear()
    bbox.modifiers.clear()
    bbox.animation_data_clear()
    faces = ((0, 1, 2, 3),
             (4, 7, 6, 5),
             (0, 4, 5, 1),
             (1, 5, 6, 2),
             (2, 6, 7, 3),
             (4, 0, 3, 7),
            )
    me = bpy.data.meshes.new("BBox")
    me.from_pydata([Vector(b) for b in ob.bound_box], [], faces)
    bbox.display_type = 'WIRE'
    bbox.data = me
    return bbox
    
context = bpy.context
ob = context.object
coll = context.collection
bbox = bbox2mesh(ob) # make a copy
ob.parent = bbox
ob.matrix_basis = ob.matrix_parent_inverse = Matrix()
coll.objects.link(bbox)
context.view_layer.objects.active = bbox
$\endgroup$
1
  • $\begingroup$ This seems to work pretty well, though it can only manage single objects. Is there a way to create a single bounding box for multiple entities too (like sambler's does)? BTW huge thanks for the effort! $\endgroup$
    – Marcello_L
    Commented Feb 7, 2021 at 17:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .