Created
July 17, 2017 20:30
-
-
Save gr4ph0s/18688498dc96b678150bad39450df68c to your computer and use it in GitHub Desktop.
[c4d]Set axis rotation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Set axis rotation of an object (didn't support spline in bezier mode) | |
#17/07/2017 | |
#More informations about csv structure => http://frenchcinema4d.fr/showthread.php?81748-Besoin-d-aide-pour-un-code-python | |
import c4d | |
def LocalToGlobal(obj, local_pos): | |
obj_mg = obj.GetMg() | |
return obj_mg * local_pos | |
def GetPointGlobal(obj): | |
all_points_pos = obj.GetAllPoints() | |
for i in xrange(len(all_points_pos)): | |
all_points_pos[i] = LocalToGlobal(obj, all_points_pos[i]) | |
return all_points_pos | |
def SetPointGlobal(obj, global_point): | |
points = obj.GetAllPoints() | |
for i in range(len(points)): | |
points[i] = GlobalToLocal(obj, global_point[i]) | |
obj.SetAllPoints(points) | |
obj.Message(c4d.MSG_UPDATE) | |
def GlobalToLocal(obj, global_pos): | |
obj_mg = obj.GetMg() | |
return ~obj_mg * global_pos | |
def SetGlobalRotation(obj, rot): | |
old_pos = GetPointGlobal(obj) | |
m = obj.GetMg() | |
pos = m.off | |
scale = c4d.Vector(m.v1.GetLength(), | |
m.v2.GetLength(), | |
m.v3.GetLength()) | |
m = c4d.utils.HPBToMatrix(rot) | |
m.off = pos | |
m.v1 = m.v1.GetNormalized() * scale.x | |
m.v2 = m.v2.GetNormalized() * scale.y | |
m.v3 = m.v3.GetNormalized() * scale.z | |
obj.SetMg(m) | |
SetPointGlobal(obj, old_pos) | |
def main(): | |
op = doc.GetActiveObject() | |
if not op: return | |
doc.StartUndo() | |
doc.AddUndo(c4d.UNDOTYPE_CHANGE, op) | |
h = 0 | |
p = 90 | |
b = 0 | |
hpb = c4d.Vector(c4d.utils.Rad(h), c4d.utils.Rad(p), c4d.utils.Rad(b)) | |
SetGlobalRotation(op, hpb) | |
doc.EndUndo() | |
c4d.EventAdd() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment