Skip to content

Instantly share code, notes, and snippets.

@andreberg
Last active May 9, 2020 19:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save andreberg/885210 to your computer and use it in GitHub Desktop.
Save andreberg/885210 to your computer and use it in GitHub Desktop.
[CINEMA 4D: Python Set Global Axis Rotation Func] A function for setting the orientation of the object axis withouth modifying the points. #cinema4d #c4d #python #function #computergraphics #cg
def SetGlobalAxisRotation(obj, rot, depth=0):
"""
Set the global rotation of the object axis (i.e. keeping points in place).
obj object
rot vector
"""
# unfortunately tail recursion and depth guard
# seem needed because of the MatrixToHPB(HPBToMatrix(Rad(angle))
# roundtrip round-off error, so we need to improve
# our solution with each recursion level
depth += 1
if obj is None: return False
if not isinstance(rot, c4d.Vector):
raise TypeError("expected c4d.Vector, got %s" % type(rot))
mg = obj.GetMg()
# calc difference to current orientation
# (we want absolute value opposed to relative increment)
currot = MatrixToHPB(obj.GetMg())
diff = rot-currot
if VectorEqual(rot, currot) or depth >= 50:
return False
else:
if DEBUG:
print "%d: rot = %s, currot = %s, diff = %s" % (depth, rot, currot, diff)
mat = HPBToMatrix(diff)
print "mat = %s" % (mat)
# orient whole object (axis + points)
obj.SetMg(mg * mat)
# get inverse rotation matrix for the points
pntsrm = mat.__invert__()
allpnts = obj.GetAllPoints()
newpnts = []
for pnt in allpnts:
# write back points to previous positions
# by applying the inverse matrix
newpnts.append(pntsrm * pnt)
obj.SetAllPoints(newpnts)
obj.Message(c4d.MSG_UPDATE)
if DEBUG:
currot = MatrixToHPB(op.GetMg())
diff = rot-currot
print "%d: rot = %s, currot = %s, diff = %s" % (depth, rot, currot, diff)
return SetGlobalAxisRotation(obj, rot, depth)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment