SHARE
TWEET

CacheObject - py4d

nux95 Jun 26th, 2011 237 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import  c4d
  2.  
  3. def CacheObject(op, frameStart = None, frameEnd = None):
  4.     """ Caches 'op' about it's position, scale and rotation. """
  5.     doc     = op.GetDocument()
  6.     if not doc:
  7.         return False
  8.     fps     = doc.GetFps()
  9.     if frameStart is None:
  10.         frameStart      = doc.GetMinTime().GetFrame(fps)
  11.     if frameEnd is None:
  12.         frameEnd        = doc.GetMaxTime().GetFrame(fps)
  13.  
  14.     currentTime     = doc.GetTime()
  15.  
  16.     null            = c4d.BaseObject(c4d.Onull)
  17.     tracks          = (
  18.         c4d.CTrack(null, c4d.DescID(c4d.DescLevel(c4d.ID_BASEOBJECT_POSITION), c4d.DescLevel(c4d.VECTOR_X))),
  19.         c4d.CTrack(null, c4d.DescID(c4d.DescLevel(c4d.ID_BASEOBJECT_POSITION), c4d.DescLevel(c4d.VECTOR_Y))),
  20.         c4d.CTrack(null, c4d.DescID(c4d.DescLevel(c4d.ID_BASEOBJECT_POSITION), c4d.DescLevel(c4d.VECTOR_Z))),
  21.         c4d.CTrack(null, c4d.DescID(c4d.DescLevel(c4d.ID_BASEOBJECT_SCALE), c4d.DescLevel(c4d.VECTOR_X))),
  22.         c4d.CTrack(null, c4d.DescID(c4d.DescLevel(c4d.ID_BASEOBJECT_SCALE), c4d.DescLevel(c4d.VECTOR_Y))),
  23.         c4d.CTrack(null, c4d.DescID(c4d.DescLevel(c4d.ID_BASEOBJECT_SCALE), c4d.DescLevel(c4d.VECTOR_Z))),
  24.         c4d.CTrack(null, c4d.DescID(c4d.DescLevel(c4d.ID_BASEOBJECT_ROTATION), c4d.DescLevel(c4d.VECTOR_X))),
  25.         c4d.CTrack(null, c4d.DescID(c4d.DescLevel(c4d.ID_BASEOBJECT_ROTATION), c4d.DescLevel(c4d.VECTOR_Y))),
  26.         c4d.CTrack(null, c4d.DescID(c4d.DescLevel(c4d.ID_BASEOBJECT_ROTATION), c4d.DescLevel(c4d.VECTOR_Z))),
  27.     )
  28.     for track in tracks:
  29.         null.InsertTrackSorted(track)
  30.     curves          = tuple((track.GetCurve() for track in tracks))
  31.  
  32.  
  33.     for frame in xrange(frameStart, frameEnd, 1):
  34.         # set document time
  35.         time        = c4d.BaseTime(frame, fps)
  36.         doc.SetTime(time)
  37.         # update cinema 4d
  38.         c4d.DrawViews(c4d.DRAWFLAGS_ONLY_ACTIVE_VIEW|c4d.DRAWFLAGS_NO_THREAD|c4d.DRAWFLAGS_NO_REDUCTION|c4d.DRAWFLAGS_STATICBREAK)
  39.  
  40.         pos         = op.GetAbsPos()
  41.         sca         = op.GetAbsScale()
  42.         rot         = op.GetAbsRot()
  43.  
  44.         keys        = tuple((curve.AddKey(time)["key"] for curve in curves))
  45.         keys[0].SetValue(keys[0].GetCurve(), pos.x)
  46.         keys[1].SetValue(keys[1].GetCurve(), pos.y)
  47.         keys[2].SetValue(keys[2].GetCurve(), pos.z)
  48.         keys[3].SetValue(keys[3].GetCurve(), sca.x)
  49.         keys[4].SetValue(keys[4].GetCurve(), sca.y)
  50.         keys[5].SetValue(keys[5].GetCurve(), sca.z)
  51.         keys[6].SetValue(keys[6].GetCurve(), rot.x)
  52.         keys[7].SetValue(keys[7].GetCurve(), rot.y)
  53.         keys[8].SetValue(keys[8].GetCurve(), rot.z)
  54.  
  55.         c4d.GeSyncMessage(c4d.EVMSG_TIMECHANGED)
  56.  
  57.     doc.SetTime(currentTime)
  58.     c4d.EventAdd(c4d.EVENT_ANIMATE)
  59.  
  60.     null.SetName(op.GetName() + ".baked")
  61.  
  62.     null.Message(c4d.MSG_UPDATE)
  63.  
  64.     return null
  65.  
  66.  
  67. def main():
  68.     if op:
  69.         doc.InsertObject(CacheObject(op))
  70.     else:
  71.         c4d.gui.MessageDialog("Please select an object.")
  72.  
  73. if __name__ == "__main__":
  74.     main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top