How do I set an object parent without moving the child location as relative
Lets say I have an object relationship
- first
- second
- third
And I have absolute position/location/coordinates
first: 1.0, 0.0, 0.0
second: 0.0, 1.0, 0.0
third: 0.0, 0.0, 1.0
I read that we're supposed to use magic setter named matrix_parent_invert
, like so
child.matrix_parent_inverse = parent.matrix_world.inverted()
so as follows is a quick script to do that, and my object positions remain relative with various order of operations of the following"
import bpy
first = bpy.data.objects.new('first', None)
first.location = (1., 0., 0.)
bpy.context.collection.objects.link(first)
snd = bpy.data.objects.new('second', None)
snd.location = (0., 1., 0.)
snd.parent = first
snd.matrix_parent_inverse = first.matrix_world.inverted()
bpy.context.collection.objects.link(snd)
trd = bpy.data.objects.new('third', None)
trd.location = (0., 0., 1.)
trd.parent = snd
trd.matrix_parent_inverse = snd.matrix_world.inverted()
bpy.context.collection.objects.link(trd)