Skip to content

Instantly share code, notes, and snippets.

@gr4ph0s
Created May 11, 2017 17:43
Show Gist options
  • Save gr4ph0s/90fd9cfd751c6480eca9a4731b177597 to your computer and use it in GitHub Desktop.
Save gr4ph0s/90fd9cfd751c6480eca9a4731b177597 to your computer and use it in GitHub Desktop.
"""
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* Gr4ph0s wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return :)
* ----------------------------------------------------------------------------
"""
import c4d
doc = c4d.documents.GetActiveDocument()
def GetGlobalPosition(obj):
return obj.GetMg().off
def GetGlobalRotation(obj):
return c4d.utils.MatrixToHPB(obj.GetMg())
def GetGlobalScale(obj):
m = obj.GetMg()
return c4d.Vector( m.v1.GetLength(),
m.v2.GetLength(),
m.v3.GetLength())
def SetGlobalPosition(obj, pos):
m = obj.GetMg()
m.off = pos
doc.AddUndo(c4d.UNDOTYPE_CHANGE, obj)
obj.SetMg(m)
def SetGlobalRotation(obj, rot):
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
doc.AddUndo(c4d.UNDOTYPE_CHANGE, obj)
obj.SetMg(m)
def SetGlobalScale(obj, scale):
m = obj.GetMg()
m.v1 = m.v1.GetNormalized() * scale.x
m.v2 = m.v2.GetNormalized() * scale.y
m.v3 = m.v3.GetNormalized() * scale.z
doc.AddUndo(c4d.UNDOTYPE_CHANGE, obj)
obj.SetMg(m)
def getXYZtrack(obj, trackID, channelID):
#Find the Track
param = c4d.DescID(c4d.DescLevel(trackID, c4d.DTYPE_VECTOR,0),
c4d.DescLevel(channelID, c4d.DTYPE_REAL,0)
)
track = obj.FindCTrack(param)
#Create if no track found
if not track:
track = c4d.CTrack(obj, param)
doc.AddUndo(c4d.UNDOTYPE_NEW, track)
obj.InsertTrackSorted(track)
return track
def addKey(obj, track, frame, value, check_value_before=False):
#init var
time = c4d.BaseTime(frame, doc.GetFps())
curve = track.GetCurve()
#Find the key
key = curve.FindKey(time)
found = False
#If there is no key create one
if not key:
#init our key
key = c4d.CKey()
track.FillKey(doc, obj, key)
#Set value for our key
key.SetTime(curve, time)
key.SetValue(curve, value)
#Insert our key
doc.AddUndo(c4d.UNDOTYPE_NEW, key)
curve.InsertKey(key)
#If we found an already existant at current time, jsut set the the value.
else:
key = key["key"]
#Set new key value
doc.AddUndo(c4d.UNDOTYPE_CHANGE, key)
key.SetValue(curve, value)
def main():
obj = op
doc.StartUndo()
#Get tracks
posTrackX = getXYZtrack(obj, c4d.ID_BASEOBJECT_POSITION, c4d.VECTOR_X)
posTrackY = getXYZtrack(obj, c4d.ID_BASEOBJECT_POSITION, c4d.VECTOR_Y)
posTrackZ = getXYZtrack(obj, c4d.ID_BASEOBJECT_POSITION, c4d.VECTOR_Z)
scaleTrackX = getXYZtrack(obj, c4d.ID_BASEOBJECT_SCALE, c4d.VECTOR_X)
scaleTrackY = getXYZtrack(obj, c4d.ID_BASEOBJECT_SCALE, c4d.VECTOR_Y)
scaleTrackZ = getXYZtrack(obj, c4d.ID_BASEOBJECT_SCALE, c4d.VECTOR_Z)
rotTrackX = getXYZtrack(obj, c4d.ID_BASEOBJECT_ROTATION, c4d.VECTOR_X)
rotTrackY = getXYZtrack(obj, c4d.ID_BASEOBJECT_ROTATION, c4d.VECTOR_Y)
rotTrackZ = getXYZtrack(obj, c4d.ID_BASEOBJECT_ROTATION, c4d.VECTOR_Z)
#Get world data
current_frame_world_pos = GetGlobalPosition(obj)
current_frame_world_scale = GetGlobalScale(obj)
current_frame_world_rot = GetGlobalRotation(obj)
#Get relative data
current_frame_local_pos = obj.GetRelPos()
current_frame_local_scale = obj.GetRelScale()
current_frame_local_rot = obj.GetRelRot()
#Add Key for current value
current_frame = doc.GetTime().GetFrame(doc.GetFps())
addKey(obj, posTrackX, current_frame, current_frame_local_pos.x)
addKey(obj, posTrackY, current_frame, current_frame_local_pos.y)
addKey(obj, posTrackZ, current_frame, current_frame_local_pos.z)
addKey(obj, scaleTrackX, current_frame, current_frame_local_scale.x)
addKey(obj, scaleTrackY, current_frame, current_frame_local_scale.y)
addKey(obj, scaleTrackZ, current_frame, current_frame_local_scale.z)
addKey(obj, rotTrackX, current_frame, current_frame_local_rot.x)
addKey(obj, rotTrackY, current_frame, current_frame_local_rot.y)
addKey(obj, rotTrackZ, current_frame, current_frame_local_rot.z)
#Go To next Frame
after_frame = current_frame + 1
doc.SetTime(c4d.BaseTime(after_frame, doc.GetFps()))
c4d.DrawViews(c4d.DRAWFLAGS_FORCEFULLREDRAW)
c4d.GeSyncMessage(c4d.EVMSG_TIMECHANGED)
#Set same world pos
SetGlobalPosition(obj, current_frame_world_pos)
SetGlobalScale(obj, current_frame_world_scale)
SetGlobalRotation(obj, current_frame_world_rot)
#Get relative data
after_frame_local_pos = obj.GetRelPos()
after_frame_local_scale = obj.GetRelScale()
after_frame_local_rot = obj.GetRelRot()
#Add Key for frame+1
addKey(obj, posTrackX, after_frame, after_frame_local_pos.x, True)
addKey(obj, posTrackY, after_frame, after_frame_local_pos.y, True)
addKey(obj, posTrackZ, after_frame, after_frame_local_pos.z, True)
addKey(obj, scaleTrackX, after_frame, after_frame_local_scale.x, True)
addKey(obj, scaleTrackY, after_frame, after_frame_local_scale.y, True)
addKey(obj, scaleTrackZ, after_frame, after_frame_local_scale.z, True)
addKey(obj, rotTrackX, after_frame, after_frame_local_rot.x, True)
addKey(obj, rotTrackY, after_frame, after_frame_local_rot.y, True)
addKey(obj, rotTrackZ, after_frame, after_frame_local_rot.z, True)
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