Jump to content

Set keyframe from python


Recommended Posts

Hi,

Is there a simple way to just set a keyframe on a given parameter that will do the equivalent of clicking the bullet on the left of the param name in the attribute editor?

 

I know I can do it "the hard way" by getting the actual parameter value and then checking if a track exists, then add the track and set the keyframe from the value that I have retrieved earlier.... i.e. 1.

1. get value

2. see if there is a track and create if not, create curve

3. get track

4. get curve

5. add key

6. set curve back etc.

 

Is there a method that just creates the key as it is?

 

Thanks for any hints,

Cheers.

Link to post

What do you mean by "as it is"? Clicking the bullet circle doesn't do anything different... you need the whole hierarchy with track, curve, and key for this to work. AFAIK there is no all-in-one method that does that for you. But you only need to write it once and can reuse it then... not that much work?

 

Link to post

Maybe just use the

c4d.CallCommand(12410) # Record Active Objects

 ? 

You don't need step 1 - 6 as you mentioned. 
Just select the objects and run the command. 

Link to post

But that's keyframing all keyframeable parameters, not just a given one.

 

I just checked with R21, the script log doesn't show any command (no CallCommand, no parameter change) when I click the circle.

And the circle is not a standard button, so I don't see a way to trigger a specified one through scripting...?

Link to post
# Set keyframe on object "obj" parameter DescID "par_id", at a specific time and with a specific value
# If there is no track, add one
# If there is no key, add one based on the default project settings
# If the keyframe is already there, then just set the value

def set_k(obj, par_id, time, val):
    track = obj.FindCTrack(par_id)

    if track is None:
        track = c4d.CTrack(obj, par_id)
        obj.InsertTrackSorted(track)

    curve = track.GetCurve()

    found_key = curve.FindKey(time)

    if found_key:
        key = found_key['key']

    else:
        defkey, dub = doc.GetDefaultKey()
        key = defkey.GetClone()

    key.SetValue(curve, val)
    key.SetTime(curve, time)

    if not found_key:
        curve.InsertKey(key, True)
    else:
        curve.SetKeyDirty()

# then in the code

	# get the parameter DescID so much writing
    pid_px = c4d.DescID(c4d.DescLevel(c4d.ID_BASEOBJECT_REL_POSITION, c4d.DTYPE_VECTOR, 0), c4d.DescLevel(c4d.VECTOR_X, c4d.DTYPE_REAL, 0))
    
    # set the keyframe at current frame with the current parameter value
    val = obj[c4d.ID_BASEOBJECT_REL_POSITION, c4d.VECTOR_X]
	set_k(obj, pid_px, doc.GetTime(), val)

# usw... for other channels if necessary

	

Yeah I was just lazy. 

Link to post
17 hours ago, Cairyn said:

What do you mean by "as it is"? Clicking the bullet circle doesn't do anything different... you need the whole hierarchy with track, curve, and key for this to work. AFAIK there is no all-in-one method that does that for you. But you only need to write it once and can reuse it then... not that much work?

 

I was thinking along the lines of :

obj[c4d.ID_BASEOBJECT_REL_POSITION, c4d.VECTOR_X].SetKey()

Instead of writing all the mess in my post above 😉

I guess setting keyframes from a script is not really that common to justify SDK bloat 😄

Cheers!

 

Link to post
2 hours ago, turbid said:

I was thinking along the lines of :


obj[c4d.ID_BASEOBJECT_REL_POSITION, c4d.VECTOR_X].SetKey()

Instead of writing all the mess in my post above 😉

I guess setting keyframes from a script is not really that common to justify SDK bloat 😄

Cheers!

 

Yeah, haven't seen such a function yet. (That doesn't mean there isn't one, but it's well hidden...)

Keyframes are not the only thing neglected in the API though. We don't even have a function that could enumerate the visible tracks in one of the timelines - and actually no way to find out manually. Duh.

Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...