通过Python编程语言实现C4D(R13)中的简单动画

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过Python编程语言实现C4D(R13)中的简单动画相关的知识,希望对你有一定的参考价值。

Simple animation in C4D (R13) consisting of moving a cube from state 1 to state 2 with Python.
  1. # CGTalk Example...
  2. # By: Ricardo Prada.
  3.  
  4. """
  5. State at frame 0:
  6. + Position: (0,0,0) # in cm
  7. + Rotation: (0,0,0) # in XYZ-degrees format
  8. + Size: (100,200,200) # in cm
  9. + Segments: (10,20,20)
  10. + Material: Blue
  11. + Bend (Deformer):
  12. - Position: (0,0,0) # in cm
  13. - Rotation: (0,0,0) # in XYZ-degrees format
  14. - Size: (100,200,200) # in cm
  15. - Mode: "Limited"
  16. - Strength: 45 # in degrees
  17. - Angle: 30 # in degrees
  18.  
  19. State at frame 120:
  20. + Position: (900,1200,2500) # in cm
  21. + Rotation: (45,80,120) # in XYZ-degrees format
  22. + Size: (200,300,700) # in cm
  23. + Segments: (20,30,70)
  24. + Material: Red
  25. + Bend (Deformer):
  26. - Position: (900,1200,2500) # in cm
  27. - Rotation: (45,80,120) # in XYZ-degrees format
  28. - Size: (200,300,700) # in cm
  29. - Mode: "Limited"
  30. - Strength: 120 # in degrees
  31. - Angle: 60 # in degrees
  32. """
  33.  
  34. import c4d
  35. import math
  36.  
  37. def CreateKey(op,id,value,frame):
  38. if not op.GetDocument(): raise Exception, "object must be in a document"
  39.  
  40. # First check if the track type already exists, otherwise create it...
  41. track=op.FindCTrack(id)
  42. if not track:
  43. track=c4d.CTrack(op,id)
  44. op.InsertTrackSorted(track)
  45.  
  46. curve=track.GetCurve()
  47. key=curve.AddKey(c4d.BaseTime(frame,doc.GetFps()))
  48.  
  49. if type(value)==int or type(value)==float:
  50. key["key"].SetValue(curve,value)
  51. else:
  52. key["key"].SetGeData(curve,value)
  53.  
  54.  
  55. def main():
  56. # Initial Parameters:
  57. fps=30
  58. minFrame=0
  59. maxFrame=150
  60.  
  61. doc.SetFps(fps)
  62. doc.SetMinTime(c4d.BaseTime(minFrame,fps))
  63. doc.SetMaxTime(c4d.BaseTime(maxFrame,fps))
  64. doc.SetTime(c4d.BaseTime(maxFrame,fps))
  65.  
  66. # Materials:
  67. mat1=c4d.BaseMaterial(c4d.Mmaterial)
  68. mat1.SetName("Blue")
  69. mat1[c4d.MATERIAL_COLOR_COLOR]=c4d.Vector(0,0,1)
  70. doc.InsertMaterial(mat1)
  71.  
  72. mat2=c4d.BaseMaterial(c4d.Mmaterial)
  73. mat2.SetName("Red")
  74. mat2[c4d.MATERIAL_COLOR_COLOR]=c4d.Vector(1,0,0)
  75. doc.InsertMaterial(mat2)
  76.  
  77. # Texture:
  78. mtag=c4d.TextureTag() # Create a new texture tag...
  79.  
  80. # Cube:
  81. cube=c4d.BaseObject(c4d.Ocube)
  82. cube.SetName("MyCube")
  83. cube.InsertTag(mtag)
  84. doc.InsertObject(cube)
  85.  
  86. doc.SetActiveObject(cube) # The record command needs an active object...
  87. cube.Message(c4d.MSG_UPDATE)
  88.  
  89. # Deformer:
  90. bend=c4d.BaseObject(c4d.Obend)
  91. bend.SetName("MyBend")
  92. bend[c4d.BENDOBJECT_MODE]=1 # 0=Within Box, 1=Limited, 2=Unlimited...
  93. doc.InsertObject(bend,cube) # Add it to the Object Manager as a child of the cube...
  94.  
  95. doc.SetActiveObject(bend) # The record command needs an active object
  96. bend.Message(c4d.MSG_UPDATE)
  97.  
  98. # ID Definitions:
  99. # + Position:
  100. ID_IDBASEOBJECTGLOBALPOSITION_X=c4d.DescID(c4d.DescLevel(c4d.ID_BASEOBJECT_GLOBAL_POSITION,c4d.DTYPE_VECTOR,0),c4d.DescLevel(c4d.VECTOR_X,c4d.DTYPE_REAL,0))
  101. ID_IDBASEOBJECTGLOBALPOSITION_Y=c4d.DescID(c4d.DescLevel(c4d.ID_BASEOBJECT_GLOBAL_POSITION,c4d.DTYPE_VECTOR,0),c4d.DescLevel(c4d.VECTOR_Y,c4d.DTYPE_REAL,0))
  102. ID_IDBASEOBJECTGLOBALPOSITION_Z=c4d.DescID(c4d.DescLevel(c4d.ID_BASEOBJECT_GLOBAL_POSITION,c4d.DTYPE_VECTOR,0),c4d.DescLevel(c4d.VECTOR_Z,c4d.DTYPE_REAL,0))
  103.  
  104. # + Rotation:
  105. ID_IDBASEOBJECTGLOBALROTATION_X=c4d.DescID(c4d.DescLevel(c4d.ID_BASEOBJECT_GLOBAL_ROTATION,c4d.DTYPE_VECTOR,0),c4d.DescLevel(c4d.VECTOR_X,c4d.DTYPE_REAL,0))
  106. ID_IDBASEOBJECTGLOBALROTATION_Y=c4d.DescID(c4d.DescLevel(c4d.ID_BASEOBJECT_GLOBAL_ROTATION,c4d.DTYPE_VECTOR,0),c4d.DescLevel(c4d.VECTOR_Y,c4d.DTYPE_REAL,0))
  107. ID_IDBASEOBJECTGLOBALROTATION_Z=c4d.DescID(c4d.DescLevel(c4d.ID_BASEOBJECT_GLOBAL_ROTATION,c4d.DTYPE_VECTOR,0),c4d.DescLevel(c4d.VECTOR_Z,c4d.DTYPE_REAL,0))
  108.  
  109. # + Size:
  110. ID_PRIMCUBELEN_X=c4d.DescID(c4d.DescLevel(c4d.PRIM_CUBE_LEN,c4d.DTYPE_VECTOR,0),c4d.DescLevel(c4d.VECTOR_X,c4d.DTYPE_REAL,0))
  111. ID_PRIMCUBELEN_Y=c4d.DescID(c4d.DescLevel(c4d.PRIM_CUBE_LEN,c4d.DTYPE_VECTOR,0),c4d.DescLevel(c4d.VECTOR_Y,c4d.DTYPE_REAL,0))
  112. ID_PRIMCUBELEN_Z=c4d.DescID(c4d.DescLevel(c4d.PRIM_CUBE_LEN,c4d.DTYPE_VECTOR,0),c4d.DescLevel(c4d.VECTOR_Z,c4d.DTYPE_REAL,0))
  113.  
  114. # + Segments:
  115. ID_PRIMCUBESUBX=c4d.DescID(c4d.DescLevel(c4d.PRIM_CUBE_SUBX,c4d.DTYPE_LONG,0))
  116. ID_PRIMCUBESUBY=c4d.DescID(c4d.DescLevel(c4d.PRIM_CUBE_SUBY,c4d.DTYPE_LONG,0))
  117. ID_PRIMCUBESUBZ=c4d.DescID(c4d.DescLevel(c4d.PRIM_CUBE_SUBZ,c4d.DTYPE_LONG,0))
  118.  
  119. # + Material:
  120. ID_TEXTURETAGMATERIAL=c4d.DescID(c4d.DescLevel(c4d.TEXTURETAG_MATERIAL,c4d.DTYPE_BASELISTLINK,0))
  121.  
  122. # + Bend:
  123. # - Position: Defined above.
  124. # - Rotation: Defined above.
  125. # - Size:
  126. ID_DEFORMOBJECTSIZE_X=c4d.DescID(c4d.DescLevel(c4d.DEFORMOBJECT_SIZE,c4d.DTYPE_VECTOR,0),c4d.DescLevel(c4d.VECTOR_X,c4d.DTYPE_REAL,0))
  127. ID_DEFORMOBJECTSIZE_Y=c4d.DescID(c4d.DescLevel(c4d.DEFORMOBJECT_SIZE,c4d.DTYPE_VECTOR,0),c4d.DescLevel(c4d.VECTOR_Y,c4d.DTYPE_REAL,0))
  128. ID_DEFORMOBJECTSIZE_Z=c4d.DescID(c4d.DescLevel(c4d.DEFORMOBJECT_SIZE,c4d.DTYPE_VECTOR,0),c4d.DescLevel(c4d.VECTOR_Z,c4d.DTYPE_REAL,0))
  129. # - Mode: Constant over time, so not defined...
  130. # - Strength:
  131. ID_DEFORMOBJECTSTRENGTH=c4d.DescID(c4d.DescLevel(c4d.DEFORMOBJECT_STRENGTH,c4d.DTYPE_REAL,0))
  132. # - Angle:
  133. ID_DEFORMOBJECTANGLE=c4d.DescID(c4d.DescLevel(c4d.DEFORMOBJECT_ANGLE,c4d.DTYPE_REAL,0))
  134.  
  135.  
  136. # Keys:
  137. # At frame = 0:
  138. frame=0
  139.  
  140. # + Position:
  141. CreateKey(cube,ID_IDBASEOBJECTGLOBALPOSITION_X,value=0,frame=frame)
  142. CreateKey(cube,ID_IDBASEOBJECTGLOBALPOSITION_Y,value=0,frame=frame)
  143. CreateKey(cube,ID_IDBASEOBJECTGLOBALPOSITION_Z,value=0,frame=frame)
  144.  
  145. # + Rotation:
  146. CreateKey(cube,ID_IDBASEOBJECTGLOBALROTATION_X,value=0,frame=frame)
  147. CreateKey(cube,ID_IDBASEOBJECTGLOBALROTATION_Y,value=0,frame=frame)
  148. CreateKey(cube,ID_IDBASEOBJECTGLOBALROTATION_Z,value=0,frame=frame)
  149.  
  150. # + Size:
  151. CreateKey(cube,ID_PRIMCUBELEN_X,value=100,frame=frame)
  152. CreateKey(cube,ID_PRIMCUBELEN_Y,value=200,frame=frame)
  153. CreateKey(cube,ID_PRIMCUBELEN_Z,value=200,frame=frame)
  154.  
  155. # + Segments:
  156. CreateKey(cube,ID_PRIMCUBESUBX,value=10,frame=frame)
  157. CreateKey(cube,ID_PRIMCUBESUBY,value=20,frame=frame)
  158. CreateKey(cube,ID_PRIMCUBESUBZ,value=20,frame=frame)
  159.  
  160. # + Material:
  161. CreateKey(mtag,ID_TEXTURETAGMATERIAL,value=mat1,frame=frame)
  162.  
  163. # + Bend:
  164. # - Position:
  165. CreateKey(bend,ID_IDBASEOBJECTGLOBALPOSITION_X,value=0,frame=frame)
  166. CreateKey(bend,ID_IDBASEOBJECTGLOBALPOSITION_Y,value=0,frame=frame)
  167. CreateKey(bend,ID_IDBASEOBJECTGLOBALPOSITION_Z,value=0,frame=frame)
  168.  
  169. # - Rotation:
  170. CreateKey(bend,ID_IDBASEOBJECTGLOBALROTATION_X,value=0,frame=frame)
  171. CreateKey(bend,ID_IDBASEOBJECTGLOBALROTATION_Y,value=0,frame=frame)
  172. CreateKey(bend,ID_IDBASEOBJECTGLOBALROTATION_Z,value=0,frame=frame)
  173.  
  174. # - Size:
  175. CreateKey(bend,ID_DEFORMOBJECTSIZE_X,value=100,frame=frame)
  176. CreateKey(bend,ID_DEFORMOBJECTSIZE_Y,value=200,frame=frame)
  177. CreateKey(bend,ID_DEFORMOBJECTSIZE_Z,value=200,frame=frame)
  178.  
  179. # - Strength:
  180. CreateKey(bend,ID_DEFORMOBJECTSTRENGTH,value=math.radians(45),frame=frame)
  181.  
  182. # - Angle:
  183. CreateKey(bend,ID_DEFORMOBJECTANGLE,value=math.radians(30),frame=frame)
  184.  
  185. cube.Message(c4d.MSG_UPDATE)
  186.  
  187.  
  188.  
  189. # At frame = 120:
  190. frame=120
  191.  
  192. # + Position:
  193. CreateKey(cube,ID_IDBASEOBJECTGLOBALPOSITION_X,value=900,frame=frame)
  194. CreateKey(cube,ID_IDBASEOBJECTGLOBALPOSITION_Y,value=1200,frame=frame)
  195. CreateKey(cube,ID_IDBASEOBJECTGLOBALPOSITION_Z,value=2500,frame=frame)
  196.  
  197. # + Rotation:
  198. CreateKey(cube,ID_IDBASEOBJECTGLOBALROTATION_X,value=math.radians(45),frame=frame)
  199. CreateKey(cube,ID_IDBASEOBJECTGLOBALROTATION_Y,value=math.radians(80),frame=frame)
  200. CreateKey(cube,ID_IDBASEOBJECTGLOBALROTATION_Z,value=math.radians(120),frame=frame)
  201.  
  202. # + Size:
  203. CreateKey(cube,ID_PRIMCUBELEN_X,value=200,frame=frame)
  204. CreateKey(cube,ID_PRIMCUBELEN_Y,value=300,frame=frame)
  205. CreateKey(cube,ID_PRIMCUBELEN_Z,value=700,frame=frame)
  206.  
  207. # + Segments:
  208. CreateKey(cube,ID_PRIMCUBESUBX,value=20,frame=frame)
  209. CreateKey(cube,ID_PRIMCUBESUBY,value=30,frame=frame)
  210. CreateKey(cube,ID_PRIMCUBESUBZ,value=70,frame=frame)
  211.  
  212. # + Material:
  213. CreateKey(mtag,ID_TEXTURETAGMATERIAL,value=mat2,frame=frame)
  214.  
  215. # + Bend:
  216. # - Position:
  217. CreateKey(bend,ID_IDBASEOBJECTGLOBALPOSITION_X,value=900,frame=frame)
  218. CreateKey(bend,ID_IDBASEOBJECTGLOBALPOSITION_Y,value=1200,frame=frame)
  219. CreateKey(bend,ID_IDBASEOBJECTGLOBALPOSITION_Z,value=2500,frame=frame)
  220.  
  221. # - Rotation:
  222. CreateKey(bend,ID_IDBASEOBJECTGLOBALROTATION_X,value=math.radians(45),frame=frame)
  223. CreateKey(bend,ID_IDBASEOBJECTGLOBALROTATION_Y,value=math.radians(80),frame=frame)
  224. CreateKey(bend,ID_IDBASEOBJECTGLOBALROTATION_Z,value=math.radians(120),frame=frame)
  225.  
  226. # - Size:
  227. CreateKey(bend,ID_DEFORMOBJECTSIZE_X,value=200,frame=frame)
  228. CreateKey(bend,ID_DEFORMOBJECTSIZE_Y,value=300,frame=frame)
  229. CreateKey(bend,ID_DEFORMOBJECTSIZE_Z,value=700,frame=frame)
  230.  
  231. # - Strength:
  232. CreateKey(bend,ID_DEFORMOBJECTSTRENGTH,value=math.radians(120),frame=frame)
  233.  
  234. # - Angle:
  235. CreateKey(bend,ID_DEFORMOBJECTANGLE,value=math.radians(60),frame=frame)
  236.  
  237. cube.Message(c4d.MSG_UPDATE)
  238.  
  239. c4d.EventAdd()
  240.  
  241.  
  242.  
  243. if __name__=='__main__':
  244. main()

以上是关于通过Python编程语言实现C4D(R13)中的简单动画的主要内容,如果未能解决你的问题,请参考以下文章

C4D实现宜家闹钟建模

C4D实现宜家闹钟建模

python C4d - Python导出器

python C4D技巧和位

python C4D - Pyhton

python C4D - Pyhton