python: moving selected keys only

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

 
Thread Tools Display Modes
  4 Weeks Ago
python: moving selected keys only

Hi,

I am writing a python script to offset some keys on objects.
I get the selected objects, then I want to go through the selected objects and offset each object's selected keyframes a certain amount.
I can get the selected keys using
cmds.keyframe (query=True, sl=True )

and I can move all selected key of all selected objects using:
cmds.keyframe(edit=True,relative=True,timeChange=2  )

but I can't figure out a way to move only selected keys of a single object from the selected objects list:

import maya.cmds as cmds
selected = cmds.ls(sl=True)
amount =2
for sel in range (0,len(selected)):
  cmds.keyframe(selected[sel],edit=True,relative=True,timeChange=sel*int(amount  ))


the above code moves all keys of selected objects, and not just the selected keys of the selected objects.
I tried getting the selected keys of the selected object inside the for-loop, and feed this variable to the "keyframe" command, instead of the object name (selected[sel]), but I get an error...
anyone can help me get this right?
__________________
Oz Adi

www.ozadi.com

 
  4 Weeks Ago
I think you will need to pull out the selected keys on each object and use them to specify what you want the keyframe command to operate on. Here's an example that operates on all animation of the given object at the times of its selected keys. That may or may not be too broad but it should give you an idea (this is simplified for testing purposes, I skipped your index-based multiplier on the time change):

import maya.cmds as cmds

selected = cmds.ls(sl=True)
amount = 2

for sel in selected:
 activeKeyTimes = cmds.keyframe(sel, q=True, sl=True)
 for t in activeKeyTimes:
 cmds.keyframe(sel, edit=True, time=(t,t), relative=True, timeChange=amount)
 
  4 Weeks Ago
Originally Posted by spire2: I think you will need to pull out the selected keys on each object and use them to specify what you want the keyframe command to operate on. Here's an example that operates on all animation of the given object at the times of its selected keys. That may or may not be too broad but it should give you an idea (this is simplified for testing purposes, I skipped your index-based multiplier on the time change):

import maya.cmds as cmds

selected = cmds.ls(sl=True)
amount = 2

for sel in selected:
 activeKeyTimes = cmds.keyframe(sel, q=True, sl=True)
 for t in activeKeyTimes:
 cmds.keyframe(sel, edit=True, time=(t,t), relative=True, timeChange=amount)
Hi, thanks but this works halfway.. it offsets selected keys, but it also offsets unselected keys on other channels, if they are on the same frame(s) as the selected ones that are offsetted.
__________________
Oz Adi

www.ozadi.com

 
  4 Weeks Ago
That's what I meant by "may or may not be too broad," it should be easy to account for individually selected channels though:

import maya.cmds as cmds

selected = cmds.ls(selection=True)
amount = 2

for obj in selected:
 nodes = cmds.keyframe(obj, q=True, name=True, sl=True)
 for node in nodes:
 activeKeyTimes = cmds.keyframe(node, q=True, sl=True, tc=True)
 for t in activeKeyTimes:
 cmds.keyframe(node, edit=True, time=(t,t), relative=True, timeChange=amount)

Last edited by spire2 : 4 Weeks Ago at 12:34 AM. Reason: realized I forgot the outer loop
 
  3 Weeks Ago
Thanks Spire2! that really helps me
__________________
Oz Adi

www.ozadi.com

 
reply share thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 10:38 AM.


Powered by vBulletin
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.