Return to Snippet

Revision: 57417
at May 25, 2012 03:32 by adarpodracir


Initial Code
# CGTalk Example...
# By: Ricardo Prada.

"""
State at frame 0:
+ Position: (0,0,0) # in cm
+ Rotation: (0,0,0) # in XYZ-degrees format
+ Size: (100,200,200) # in cm
+ Segments: (10,20,20)
+ Material: Blue
+ Bend (Deformer): 
	- Position: (0,0,0) # in cm
	- Rotation: (0,0,0) # in XYZ-degrees format
	- Size: (100,200,200) # in cm
	- Mode: "Limited"
	- Strength: 45 # in degrees
	- Angle: 30 # in degrees

State at frame 120:
+ Position: (900,1200,2500) # in cm
+ Rotation: (45,80,120) # in XYZ-degrees format
+ Size: (200,300,700) # in cm
+ Segments: (20,30,70)
+ Material: Red
+ Bend (Deformer):
	- Position: (900,1200,2500) # in cm
	- Rotation: (45,80,120) # in XYZ-degrees format
	- Size: (200,300,700) # in cm
	- Mode: "Limited"
	- Strength: 120 # in degrees
	- Angle: 60 # in degrees
"""

import c4d
import math

def CreateKey(op,id,value,frame):
	if not op.GetDocument(): raise Exception, "object must be in a document"
	
	# First check if the track type already exists, otherwise create it...
	track=op.FindCTrack(id)
	if not track:
		track=c4d.CTrack(op,id)
		op.InsertTrackSorted(track)
	
	curve=track.GetCurve()
	key=curve.AddKey(c4d.BaseTime(frame,doc.GetFps()))
	
	if type(value)==int or type(value)==float:
		key["key"].SetValue(curve,value)
	else:
		key["key"].SetGeData(curve,value)


def main():
	# Initial Parameters:
	fps=30
	minFrame=0
	maxFrame=150
	
	doc.SetFps(fps)
	doc.SetMinTime(c4d.BaseTime(minFrame,fps))
	doc.SetMaxTime(c4d.BaseTime(maxFrame,fps))
	doc.SetTime(c4d.BaseTime(maxFrame,fps))
	
	# Materials:
	mat1=c4d.BaseMaterial(c4d.Mmaterial)
	mat1.SetName("Blue")
	mat1[c4d.MATERIAL_COLOR_COLOR]=c4d.Vector(0,0,1)
	doc.InsertMaterial(mat1)
	
	mat2=c4d.BaseMaterial(c4d.Mmaterial)
	mat2.SetName("Red")
	mat2[c4d.MATERIAL_COLOR_COLOR]=c4d.Vector(1,0,0)
	doc.InsertMaterial(mat2)
	
	# Texture:
	mtag=c4d.TextureTag() # Create a new texture tag...
	
	# Cube:
	cube=c4d.BaseObject(c4d.Ocube)
	cube.SetName("MyCube")
	cube.InsertTag(mtag)
	doc.InsertObject(cube)
	
	doc.SetActiveObject(cube) # The record command needs an active object...
	cube.Message(c4d.MSG_UPDATE)
	
	# Deformer:
	bend=c4d.BaseObject(c4d.Obend)
	bend.SetName("MyBend")
	bend[c4d.BENDOBJECT_MODE]=1 # 0=Within Box, 1=Limited, 2=Unlimited...
	doc.InsertObject(bend,cube) # Add it to the Object Manager as a child of the cube...
	
	doc.SetActiveObject(bend) # The record command needs an active object
	bend.Message(c4d.MSG_UPDATE)
	
	# ID Definitions:
	# + Position:
	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))
	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))
	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))
	
	# + Rotation:
	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))
	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))
	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))
	
	# + Size:
	ID_PRIMCUBELEN_X=c4d.DescID(c4d.DescLevel(c4d.PRIM_CUBE_LEN,c4d.DTYPE_VECTOR,0),c4d.DescLevel(c4d.VECTOR_X,c4d.DTYPE_REAL,0))
	ID_PRIMCUBELEN_Y=c4d.DescID(c4d.DescLevel(c4d.PRIM_CUBE_LEN,c4d.DTYPE_VECTOR,0),c4d.DescLevel(c4d.VECTOR_Y,c4d.DTYPE_REAL,0))
	ID_PRIMCUBELEN_Z=c4d.DescID(c4d.DescLevel(c4d.PRIM_CUBE_LEN,c4d.DTYPE_VECTOR,0),c4d.DescLevel(c4d.VECTOR_Z,c4d.DTYPE_REAL,0))
	
	# + Segments:
	ID_PRIMCUBESUBX=c4d.DescID(c4d.DescLevel(c4d.PRIM_CUBE_SUBX,c4d.DTYPE_LONG,0))
	ID_PRIMCUBESUBY=c4d.DescID(c4d.DescLevel(c4d.PRIM_CUBE_SUBY,c4d.DTYPE_LONG,0))
	ID_PRIMCUBESUBZ=c4d.DescID(c4d.DescLevel(c4d.PRIM_CUBE_SUBZ,c4d.DTYPE_LONG,0))
	
	# + Material:
	ID_TEXTURETAGMATERIAL=c4d.DescID(c4d.DescLevel(c4d.TEXTURETAG_MATERIAL,c4d.DTYPE_BASELISTLINK,0))
	
	# + Bend:
	#   - Position: Defined above.
	#   - Rotation: Defined above.
	#   - Size:
	ID_DEFORMOBJECTSIZE_X=c4d.DescID(c4d.DescLevel(c4d.DEFORMOBJECT_SIZE,c4d.DTYPE_VECTOR,0),c4d.DescLevel(c4d.VECTOR_X,c4d.DTYPE_REAL,0))
	ID_DEFORMOBJECTSIZE_Y=c4d.DescID(c4d.DescLevel(c4d.DEFORMOBJECT_SIZE,c4d.DTYPE_VECTOR,0),c4d.DescLevel(c4d.VECTOR_Y,c4d.DTYPE_REAL,0))
	ID_DEFORMOBJECTSIZE_Z=c4d.DescID(c4d.DescLevel(c4d.DEFORMOBJECT_SIZE,c4d.DTYPE_VECTOR,0),c4d.DescLevel(c4d.VECTOR_Z,c4d.DTYPE_REAL,0))
	#   - Mode: Constant over time, so not defined...
	#   - Strength:
	ID_DEFORMOBJECTSTRENGTH=c4d.DescID(c4d.DescLevel(c4d.DEFORMOBJECT_STRENGTH,c4d.DTYPE_REAL,0))
	#   - Angle:
	ID_DEFORMOBJECTANGLE=c4d.DescID(c4d.DescLevel(c4d.DEFORMOBJECT_ANGLE,c4d.DTYPE_REAL,0))
	
	
	# Keys:
	# At frame = 0:
	frame=0
	
	# + Position:
	CreateKey(cube,ID_IDBASEOBJECTGLOBALPOSITION_X,value=0,frame=frame)
	CreateKey(cube,ID_IDBASEOBJECTGLOBALPOSITION_Y,value=0,frame=frame)
	CreateKey(cube,ID_IDBASEOBJECTGLOBALPOSITION_Z,value=0,frame=frame)
	
	# + Rotation:
	CreateKey(cube,ID_IDBASEOBJECTGLOBALROTATION_X,value=0,frame=frame)
	CreateKey(cube,ID_IDBASEOBJECTGLOBALROTATION_Y,value=0,frame=frame)
	CreateKey(cube,ID_IDBASEOBJECTGLOBALROTATION_Z,value=0,frame=frame)
	
	# + Size:
	CreateKey(cube,ID_PRIMCUBELEN_X,value=100,frame=frame)
	CreateKey(cube,ID_PRIMCUBELEN_Y,value=200,frame=frame)
	CreateKey(cube,ID_PRIMCUBELEN_Z,value=200,frame=frame)
	
	# + Segments:
	CreateKey(cube,ID_PRIMCUBESUBX,value=10,frame=frame)
	CreateKey(cube,ID_PRIMCUBESUBY,value=20,frame=frame)
	CreateKey(cube,ID_PRIMCUBESUBZ,value=20,frame=frame)
	
	# + Material:
	CreateKey(mtag,ID_TEXTURETAGMATERIAL,value=mat1,frame=frame)
	
	# + Bend:
	#   - Position:
	CreateKey(bend,ID_IDBASEOBJECTGLOBALPOSITION_X,value=0,frame=frame)
	CreateKey(bend,ID_IDBASEOBJECTGLOBALPOSITION_Y,value=0,frame=frame)
	CreateKey(bend,ID_IDBASEOBJECTGLOBALPOSITION_Z,value=0,frame=frame)
	
	#   - Rotation:
	CreateKey(bend,ID_IDBASEOBJECTGLOBALROTATION_X,value=0,frame=frame)
	CreateKey(bend,ID_IDBASEOBJECTGLOBALROTATION_Y,value=0,frame=frame)
	CreateKey(bend,ID_IDBASEOBJECTGLOBALROTATION_Z,value=0,frame=frame)
	
	#   - Size:
	CreateKey(bend,ID_DEFORMOBJECTSIZE_X,value=100,frame=frame)
	CreateKey(bend,ID_DEFORMOBJECTSIZE_Y,value=200,frame=frame)
	CreateKey(bend,ID_DEFORMOBJECTSIZE_Z,value=200,frame=frame)
	
	#   - Strength:
	CreateKey(bend,ID_DEFORMOBJECTSTRENGTH,value=math.radians(45),frame=frame)
	
	#   - Angle:
	CreateKey(bend,ID_DEFORMOBJECTANGLE,value=math.radians(30),frame=frame)
	
	cube.Message(c4d.MSG_UPDATE)
	
	
	
	# At frame = 120:
	frame=120
	
	# + Position:
	CreateKey(cube,ID_IDBASEOBJECTGLOBALPOSITION_X,value=900,frame=frame)
	CreateKey(cube,ID_IDBASEOBJECTGLOBALPOSITION_Y,value=1200,frame=frame)
	CreateKey(cube,ID_IDBASEOBJECTGLOBALPOSITION_Z,value=2500,frame=frame)
	
	# + Rotation:
	CreateKey(cube,ID_IDBASEOBJECTGLOBALROTATION_X,value=math.radians(45),frame=frame)
	CreateKey(cube,ID_IDBASEOBJECTGLOBALROTATION_Y,value=math.radians(80),frame=frame)
	CreateKey(cube,ID_IDBASEOBJECTGLOBALROTATION_Z,value=math.radians(120),frame=frame)
	
	# + Size:
	CreateKey(cube,ID_PRIMCUBELEN_X,value=200,frame=frame)
	CreateKey(cube,ID_PRIMCUBELEN_Y,value=300,frame=frame)
	CreateKey(cube,ID_PRIMCUBELEN_Z,value=700,frame=frame)
	
	# + Segments:
	CreateKey(cube,ID_PRIMCUBESUBX,value=20,frame=frame)
	CreateKey(cube,ID_PRIMCUBESUBY,value=30,frame=frame)
	CreateKey(cube,ID_PRIMCUBESUBZ,value=70,frame=frame)
	
	# + Material:
	CreateKey(mtag,ID_TEXTURETAGMATERIAL,value=mat2,frame=frame)
	
	# + Bend:
	#   - Position:
	CreateKey(bend,ID_IDBASEOBJECTGLOBALPOSITION_X,value=900,frame=frame)
	CreateKey(bend,ID_IDBASEOBJECTGLOBALPOSITION_Y,value=1200,frame=frame)
	CreateKey(bend,ID_IDBASEOBJECTGLOBALPOSITION_Z,value=2500,frame=frame)
	
	#   - Rotation:
	CreateKey(bend,ID_IDBASEOBJECTGLOBALROTATION_X,value=math.radians(45),frame=frame)
	CreateKey(bend,ID_IDBASEOBJECTGLOBALROTATION_Y,value=math.radians(80),frame=frame)
	CreateKey(bend,ID_IDBASEOBJECTGLOBALROTATION_Z,value=math.radians(120),frame=frame)
	
	#   - Size:
	CreateKey(bend,ID_DEFORMOBJECTSIZE_X,value=200,frame=frame)
	CreateKey(bend,ID_DEFORMOBJECTSIZE_Y,value=300,frame=frame)
	CreateKey(bend,ID_DEFORMOBJECTSIZE_Z,value=700,frame=frame)
	
	#   - Strength:
	CreateKey(bend,ID_DEFORMOBJECTSTRENGTH,value=math.radians(120),frame=frame)
	
	#   - Angle:
	CreateKey(bend,ID_DEFORMOBJECTANGLE,value=math.radians(60),frame=frame)
	
	cube.Message(c4d.MSG_UPDATE)
	
	c4d.EventAdd()



if __name__=='__main__':
	main()

Initial URL

                                

Initial Description
Simple animation in C4D (R13) consisting of moving a cube from state 1 to state 2 with Python.

Initial Title
Simple animation in C4D (R13) via Python Programming Language

Initial Tags
python, animation

Initial Language
Python