Hi,
I want to get the world position of Obj A's position and set it into the world position of Obj B's positions. As a background, I cannot directly constraint Obj A and Obj B since they have different pivot axis.
The Get World Position
is okay. Thanks to this thread.
I'm having trouble with the Set World Position
. I initially thought that I can get it by reverse engineer in the previous code but I get errors.
Specifically, TypeError: unsupported operand type(s) for /: 'c4d.Vector' and 'c4d.Matrix'
in the GlobalToLocal function.
which make sense, but, uhm, I really don't know how to solve it.
Here is the code so far
import c4d
# Declare object variables
newObject = doc.SearchObject("newObj")
oldObject = doc.SearchObject("oldObj")
totalPoints = len(newObject.GetAllPoints())
'''
Get the new position
'''
def LocalToGlobal(obj, local_pos):
"""
Returns a point in local coordinate in global space.
"""
obj_mg = obj.GetMg()
return obj_mg * local_pos
def GetPointGlobal(point_object, point_index):
"""
Return the position of a point in Global Space
"""
ppos = point_object.GetPoint(point_index) # Get the point in local coords
return LocalToGlobal(point_object, ppos) # Return the point in global space
# Get the new world position
newPos = []
for idx in range(totalPoints):
newPos.append(GetPointGlobal(newObject, idx))
'''
Assign the new position
'''
# Set the new world position
def GlobalToLocal(obj, global_pos):
"""
Returns a point in local coordinate in global space.
"""
obj_mg = obj.GetMg()
return (global_pos / obj_mg)
newLocalPos = []
for idx in newPos:
newPos.append(GlobalToLocal(oldObject, idx))
print (newLocalPos)
oldObject.SetAllPoints(newLocalPos)
oldObject.Message(c4d.MSG_UPDATE)
c4d.EventAdd()
Thank you for looking at my problem.