I’m looking for a solution, that allows to set list of values
JavaScript
x
2
1
[0,1,2]
2
over given list of times
JavaScript
1
2
1
[0,1,2]
2
at once, without loop, like this:
JavaScript
1
3
1
for frame, value in zip([0,1,2], [0,1,2]):
2
cmds.keyframe(node, e=True, vc=value, t=frame)
3
There are commands
JavaScript
1
2
1
cmds.setKeyframe()
2
and
JavaScript
1
2
1
cmds.keyframe()
2
that allow to set animation keys at a given time But non of them allow to set range of value over the range of time (frames).
The same value can be put on the time range, but that’s not the case.
JavaScript
1
2
1
mel.eval("setKeyframe -e -v %s -t 0 -t 1 -t 2 %s" % (value, node))
2
I tried to get attributes of the animation curve node, that stores keys inside, but got an empty output.
JavaScript
1
15
15
1
node = '...'
2
types = cmds.listAttr(node)
3
4
for t in types:
5
if cmds.objExists(node+t):
6
try:
7
print t, cmds.getAttr(node+t)
8
except:
9
print 'failed with', t
10
continue
11
12
13
keyTimeValue [()]
14
15
Advertisement
Answer
Latest approach does not work in 2019 Maya. For those who will stumble upon, here is the proper code.
JavaScript
1
23
23
1
def add_keys(plugName, times, values, changeCache=None):
2
# Get the plug to be animated.
3
sel = om.MSelectionList()
4
sel.add(plugName)
5
plug = om.MPlug()
6
sel.getPlug(0, plug)
7
# Create the animCurve.
8
animfn = oma.MFnAnimCurve(plug)
9
timeArray = om.MTimeArray()
10
valueArray = om.MDoubleArray()
11
12
for i in range(len(times)):
13
timeArray.append(om.MTime(times[i], om.MTime.uiUnit()))
14
valueArray.append(values[i])
15
# Add the keys to the animCurve.
16
animfn.addKeys(
17
timeArray,
18
valueArray,
19
oma.MFnAnimCurve.kTangentGlobal,
20
oma.MFnAnimCurve.kTangentGlobal,
21
False,
22
changeCache)
23