Recording keyframes from Python plugin

On 25/08/2014 at 19:57, xxxxxxxx wrote:

Hello,

I have a plugin that tells objects to change a parameter. For example, a 'while' loop that changes a cubes length from 0 to 100. I want to be able to press play and automatic key framing and have it store frames of the cubes length as it changes. How would I do this?

Would it need to record a frame each time the value changes? What's a way to add this to the code?

Thanks for the help,
Joe

On 26/08/2014 at 12:39, xxxxxxxx wrote:

Hello,

What kind of plugin do you have? Depending on the plugin type there may be different ways to do what you want.

To add keyframs from Python you need Ctracks, Curves and Ckeys:

        # define some value for the key  
      someValue = random.random() * 200;  
        
      #define the time  
      keyTime = c4d.BaseTime(1)  
        
      # get the active object to add some keys  
      activeObject = doc.GetActiveObject()  
        
      if activeObject != None:  
            
          # look if there is already a track  
          track = activeObject.FindCTrack(c4d.ID_BASEOBJECT_REL_POSITION)  
            
          # if not create the track  
          if track == None:  
              track=c4d.CTrack(activeObject, c4d.DescID(c4d.DescLevel(c4d.ID_BASEOBJECT_REL_POSITION, c4d.DTYPE_VECTOR, 0), c4d.DescLevel(c4d.VECTOR_X, c4d.DTYPE_REAL, 0)))  
              activeObject.InsertTrackSorted(track)  
                
          # now get the track's curve  
          curve = track.GetCurve()  
            
          # and add a key at the specified time  
          added = curve.AddKey(keyTime)  
            
          if added != None:  
            
              # now define the actual value of the key  
              key = added["key"]  
              key.SetValue(curve,someValue)  

First you check if the track already exist. If not you can create the track yourself. When you have the track, you access it's curve and add the key. Finally you can set the key's value.

Best wishes,
Sebastian

On 26/08/2014 at 17:59, xxxxxxxx wrote:

Thanks for the reply. This gets me in the right direction.

Now, I want to be able to click play, and have it store a keyframe whenever it is changed.

To give you insight as to what I'm doing.... I have external software that sends data to cinema at any given time. I want to take what it sends, and write a key frame for the object that changes (example: a cubes length). So for your method to work, it would have to know which keyframe is active in order to know which to write to.. keyTime = c4d.BaseTime(1) wouldn't necessarily work for this..

Any other ideas?

thanks

On 27/08/2014 at 12:06, xxxxxxxx wrote:

Hello,

to create a key for the current frame you can simply get the current time by accessing the document:

keyTime = doc.GetTime()

You may also want to check if for the current frame a key already exits:

# check if key already exists  
keyDict = curve.FindKey(keyTime);  
            
if keyDict == None:  
  # and add a key at the specified time  
  keyDict = curve.AddKey(keyTime)  
            
# now define the actual value of the key  
key = keyDict["key"]  
key.SetValue(curve,someValue)

best wishes,
Sebastian