Speed up setting of keyframe

195 views
Skip to first unread message

yann19

unread,
Jul 19, 2018, 5:40:42 PM7/19/18
to Python Programming for Autodesk Maya
Hi, I am writing up a script that works in a similar fashion as quick bake, select controllers from rigA and have the animation within to be transferred over to rigB.

Currently the following code portion is taking quite some time, iterating through the frame range. Wondering if there are any other ways in which I can speed it up?

def _bake(selection, frame_range):
    start_time = cmds.timerX()

    # selection = controllers selected
    cmds.select(selection)

    # Isolate to get the bake faster
    panelFocus = cmds.getPanel(withFocus=True)
    visPanels = cmds.getPanel(visiblePanels=True)
    modelPanels = cmds.getPanel(type="modelPanel")
    if modelPanels:
        for pan in modelPanels:
            finder = pan in visPanels
            if finder:
                activePanel = pan
        cmds.setFocus(activePanel)
        cmds.scriptedPanel(
            "referenceEditorPanel1",
            edit=True,
            replacePanel=activePanel
        )
    total01 = cmds.timerX(startTime=start_time) # Took about 0.02

    # Mostly looking at only Translate and Rotate channels.
    channels = ["translate", "rotate"]
    axis = ["X", "Y", "Z"]
    for frame in range(frame_range[0], frame_range[1]):
        cmds.currentTime(frame)
        for channel in channels:
            for axi in axis:
                temp_attr = "".join((channel, axi))
                cmds.setKeyframe(attribute=temp_attr, time=frame)

    total02 = cmds.timerX(startTime=start_time) # Took about 27.66

    # Back to the prespective
    cmds.modelPanel(
        activePanel,
        edit=True,
        replacePanel="referenceEditorPanel1"
    )
    cmds.setFocus(panelFocus)

    # Filter baked curve
    cmds.filterCurve(filter="euler")

    totalTime = cmds.timerX(startTime=start_time) # Took 28.11


Justin Israel

unread,
Jul 19, 2018, 5:51:27 PM7/19/18
to python_in...@googlegroups.com
Is the cmds.currentTime(frame) necessary, if you are already passing a "time" value to setKeyframe()? I imagine you are wasting a bunch of cpu time changing the current frame in the scene and triggering updates. 

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/9399f1ee-04fd-498d-8c95-cb952abdab53%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Message has been deleted

yann19

unread,
Jul 19, 2018, 6:49:42 PM7/19/18
to Python Programming for Autodesk Maya
I added that command in, to ensure that animation is being baked properly in each frame across the frame range defined.

I tried commenting out that particular line, only the first frame is being baked while the rest of the frames are static....

Justin Israel

unread,
Jul 19, 2018, 6:58:21 PM7/19/18
to python_in...@googlegroups.com
On Fri, Jul 20, 2018 at 10:49 AM yann19 <yang...@gmail.com> wrote:
I added that command in, to ensure that animation is being baked properly in each frame across the frame range defined.

I tried commenting out that particular line, only the first frame is being baked while the rest of the frames are static....

Does it make a difference if you only change the current time once, after all the keyframes are set? Or what about using currentTime(frame, update=False) ?
 


On Thursday, July 19, 2018 at 2:51:27 PM UTC-7, Justin Israel wrote:
Is the cmds.currentTime(frame) necessary, if you are already passing a "time" value to setKeyframe()? I imagine you are wasting a bunch of cpu time changing the current frame in the scene and triggering updates.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

yann19

unread,
Jul 19, 2018, 7:08:57 PM7/19/18
to Python Programming for Autodesk Maya
Only the first frame is being baked while the rest is static, though the process is fast, when using "update=False" or commenting that line out..

yann19

unread,
Jul 20, 2018, 7:57:06 PM7/20/18
to Python Programming for Autodesk Maya
Perhaps it is something within my code that may needs to be refine.
I tried to bake it per controller but it seems to take even longer...

I have pasted my code here - https://pastebin.com/raw/25UhLt6C for reference.

Appreciate if anyone could help to take a look and feedback :) 

Nicolas Combecave

unread,
Jul 21, 2018, 4:16:35 AM7/21/18
to python_in...@googlegroups.com
Maybe not applicabl;e to your problem, but we got some significant improvements on some of our time-dependent tools using
cmds.refresh(suspend=True) at start of the process and cmds.refresh(suspend=False) at the end.


If you happen to use this concept more and more, it can be usefull to make a decorator out of it.


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

yann19

unread,
Jul 25, 2018, 5:16:01 PM7/25/18
to Python Programming for Autodesk Maya
Hi combi,

Sorry for the late reply. I do not think the use of `cmds.refresh()` may have any effects here.
Rather, it is the `setting` of the currentTime that seemingly needs to be action so that the baking of keyframes are done properly.

Nicolas Combecave

unread,
Jul 26, 2018, 4:44:37 AM7/26/18
to python_in...@googlegroups.com
Alright.. it seems your speed problem is located somewhere else... FWIW, here one of our dynamics baking tool (sequentially set current time, set some keys on some stuff, over a time range) got a *really* big boost in performance with this very simple step, 

I hope you'll find the bottleneck, because you'll probably be really surprised for the amount of speed you may gain is this kind of usecases.




--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

yann19

unread,
Jul 30, 2018, 1:46:36 PM7/30/18
to Python Programming for Autodesk Maya
Thank you all for the replies, really appreciate it :D

Will try to find the bottleneck issue which seemingly is pointing towards `cmds.currentTime` for the current code that is and spending more time to find out what is the exact cause..
Reply all
Reply to author
Forward
0 new messages