oliviano
2/7/2018 - 4:08 PM

C4D - Pyhton

C4D Python Snippets - import keyframe from txt file _ v01 no error checks

#bh_keyLightBrightnessFromTextFile

#select a light or lights and run to key the light intensity with the values in the text file

# NB - each value should be on a new line in the text file

# from https://www.c4dcafe.com/ipb/forums/topic/68561-python-example-create-animation-from-text-file/

####################

startKeyingFrame=1 # change this value to determine what frame number the keying will start on

####################



import c4d

from c4d import gui



keyVals=list()



def readFile(path):

	for line in open(path):

		print line

		## below just left in for example of searching for particular words

		#words = ['startTime', 'endTime']

		#for word in words:

		#	if word in line:

		#		print line

		readValue = float (line)# need to cast the variable as a float so we can get its numerical value			

		keyVals.append(readValue)# add each value to the keyVals list			

	return keyVals



def main():

	animFile = c4d.storage.LoadDialog()# open the C4D file browser to allow the user to choose a text file to read

	readFile(animFile)# run the read file proc

	

	keyCount= len (keyVals)# count how many values are in the list

	FPS=doc.GetFps()

	dBright = c4d.DescID(c4d.LIGHT_BRIGHTNESS) #assign the DescID to a var for convenience

	objList=doc.GetActiveObjects(True)# get the selected objects

	lstSize = len(objList)# count how many objects are selected

	

	c=0

	while (c<lstSize):# loop through each selected object

	   tBright = objList[c].FindCTrack(dBright) #find the light brightness track

	   if not tBright:#if track isn't found, create it

		   tBright = c4d.CTrack(objList[c],dBright)

		   objList[c].InsertTrackSorted(tBright)

	   curve = tBright.GetCurve()

	   k=0

	   while (k<keyCount):# loop through each value in the keyVals list

		   frameNo=(startKeyingFrame+k)

		   frameTime= c4d.BaseTime(frameNo,FPS)

		   keyDict = curve.AddKey(frameTime)

		   tBright.FillKey(doc, objList[c], keyDict["key"])

		   keyVal=keyVals[k]

		   keyDict["key"].SetValue(curve,keyVal)

		   k=k+1

	   c4d.EventAdd()	

	   c=c+1

	  

	c4d.EventAdd()

	c4d.CallCommand(12147)#redraw the scene



				  

if __name__=='__main__':

	main()