On 04/03/2013 at 06:18, xxxxxxxx wrote:
I created a method for autokeying object psr changes.
It seems to work well and behave exactly like in Cinema autokey.
But maybe some of you much more experienced guys could have a look at the code and tell me if there´s something wrong, missing or could be done in a much better manner.
It currently works on a single object, I wonder if it would be preferable to call the method within a object list loop or to iterate an object list within the function itself?
def Autokey(self, obj_list, psr_track, xyz_vector, value, min_value) :
self.psr_track = psr_track
self.xyz_vector = xyz_vector
self.obj_list = obj_list
self.value = value
self.min_value = min_value
# Get the psr track type:
desc_id = c4d.DescID(c4d.DescLevel(self.psr_track, c4d.DTYPE_VECTOR, 0, ),
c4d.DescLevel(self.xyz_vector, c4d.DTYPE_REAL, 0))
doc = c4d.documents.GetActiveDocument()
current_time = doc.GetTime()
start_time = doc.GetMinTime()
a_track = self.obj_list.FindCTrack(desc_id) # find the track for selection
if not a_track: # no animation yet, sets a key at start time
if current_time == start_time: # if at start time, set no new track/key
return True
new_track = c4d.CTrack(self.obj_list, desc_id)
a_track = self.obj_list.InsertTrackSorted(new_track)
a_track = self.obj_list.FindCTrack(desc_id)
a_curve = a_track.GetCurve()
keydict = a_curve.AddKey(start_time)
key = keydict["key"]
key.SetValue(a_curve, self.min_value)
a_curve.SetKeyDefault(doc, keydict["nidx"])
if a_track: # there is animation
a_curve = a_track.GetCurve()
keyfind = a_curve.FindKey(current_time)
if not keyfind: # if no key is already at current time, set a key
keydict = a_curve.AddKey(current_time)
key = keydict["key"]
key.SetValue(a_curve, self.value)
a_curve.SetKeyDefault(doc, keydict["nidx"])
c4d.EventAdd()
return True
else: # if there is a key already, just modify the value
found_key = keyfind["key"]
found_key.SetValue(a_curve, self.value)
c4d.EventAdd()
return True
return True
The arguments could be and the method could be called with:
obj_list = c4d.documents.GetActiveDocument().GetActiveObject()
psr_track = c4d.ID_BASEOBJECT_POSITION
xyz_vector = c4d.VECTOR_X
value = 100 # received form a Number Edit field
min_value = 0 # initial value of a Number Edit field
if c4d.IsCommandChecked(12425) == True: # check for Autokey State
Autokey(obj, psr_track, xyz_vector, value, min_value)