Community
Maya Programming
Welcome to Autodesk’s Maya Forums. Share your knowledge, ask questions, and explore popular Maya SDK topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Please feedback my code. increase or decrease the value of the selected key

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
amaterasu-qbb
355 Views, 4 Replies

Please feedback my code. increase or decrease the value of the selected key

Hi!
 
I am an aspiring animator who wants to improve my programming skills. I have created a code that allows me to increase or decrease the value of a selected key. I am in the process of asking for feedback on the code in various places.

The reason I created this code is that in the graph editor, you can use the SHIFT key and the middle button to move the key horizontally or vertically to make various adjustments, I wanted to get away from the hassle of making judgments with the mouse if I could do it with a keyboard shortcut.

The expected problems with this code include
Since this command increases or decreases the value of the key by 0.1 in the graph editor, the value may be too much or too little in some cases.
 
Please teach me how to solve this problem.
My code can be assigned to a hotkey. Here is the command. It is written in Python and works with Maya 2024.
Thanks,

 

 

import maya.cmds as cmds

# Relative value increase
cmds.keyframe(edit = True, iub = True, relative = True, vc = 0.1)
import maya.cmds as cmds

# Relative value decrease
cmds.keyframe(edit = True, iub = True, relative = True, vc = -0.1)

 

 

Tags (2)
Labels (3)
4 REPLIES 4
Message 2 of 5
Kahylan
in reply to: amaterasu-qbb

Hi!

 

It works, so that is good. It's a good start into coding.

Two minor suggestions to your code:

1) The flag iub is set to True by default, so you don't need to add it to your command to get the desired effect.

2) I would either have all your flags in long form or all of them in short form. It doesn't make a difference, it just makes it seem more professional.

import maya.cmds as cmds

# like this
cmds.keyframe(e = True, r = True, vc = 0.1)

# or this
cmds.keyframe(edit = True, relative = True, valueChange = 0.1)

 

Now as you mentioned correctly, a big problem with your command is that it feeds a static value at the moment, which depending on scene scale could be way to high or way too low. I thought I'd present a possible solution to this problem.

 

Creating an option variable (a variable that gets stored in Mayas preferences) to scale the value up and down, combined with a UI that would be called via script or shelf button to easily set the option variable. It would look something like this.

 

Make value bigger hotkey command:

import maya.cmds as cmds

#check if optionVar exists, if it doesn't set it to default value
if cmds.optionVar(ex = "animationValueChangeFloat")== 0:
    cmds.optionVar(fv = ("animationValueChangeFloat", 0.1))
    
#get Value from optionVar
changeValue = cmds.optionVar(q= "animationValueChangeFloat")   

#change keyframe value
cmds.keyframe(e = True, r = True, vc = changeValue)

Make value smaller hotkey command:

import maya.cmds as cmds

#check if optionVar exists, if it doesn't set it to default value
if cmds.optionVar(ex = "animationValueChangeFloat")== 0:
    cmds.optionVar(fv = ("animationValueChangeFloat", 0.1))
    
#get Value from optionVar
changeValue = cmds.optionVar(q= "animationValueChangeFloat")   

#change keyframe value
cmds.keyframe(e = True, r = True, vc = (-1 * changeValue))

 

Command to call UI from shelf button:

import maya.cmds as cmds


def buildChangeUI():
    windowName = "animationValueChangeUI"
    
    if cmds.window(windowName, ex = True):
        cmds.deleteUI(windowName)
    
    cmds.window(windowName)
    v = getValue()
    cmds.columnLayout("mainLayout")
    sliderGrp = cmds.floatSliderGrp("ChangeSliderGRP" , l= "Change Value", f = True, min=0, max = 10, fmx = 100000,pre= 3, v= v)
    cmds.floatSliderGrp(sliderGrp , l= "Change Value", e=True, cc = lambda x = cmds.floatSliderGrp(sliderGrp, q= True, v= True): changeOptionVar(value = x))
    cmds.showWindow(windowName)

def getValue():
    if cmds.optionVar(ex = "animationValueChangeFloat")== 0:
        cmds.optionVar(fv = ("animationValueChangeFloat", 0.1))
        changeValue= 0.1
    else:
        changeValue = cmds.optionVar(q= "animationValueChangeFloat")
    return changeValue    
    
def changeOptionVar(value = None):
    cmds.optionVar(fv= ("animationValueChangeFloat", value))

buildChangeUI()

 

Option Variables are stored in the preferences, so this approach will change the value in all scenes on the same machine, you could also approach it more localised by storing the value on a node in the scene.

 

I hope it helps!

 

Message 3 of 5
amaterasu-qbb
in reply to: Kahylan

Sorry for the delay in responding, thanks for your reply.
I appreciate your compliments on my poorly written code. I have asked for feedback in many places, but only you have given me feedback.

 

You have always helped me.

 

Here is a list of what I have learned.
  • I was able to confirm that the default value for keyframe iub is True.
  • I did not know about optionVar. Thank you for teaching me.
  • From now on I will use the same notation for flag names.
  • I confirmed that the Make Value bigger and Make Value Smaller hotkeys also work by setting animationValueChangeFloat in the UI and selecting the key in the graph editor.

 

Thanks for creating the UI. The UI is very well done.

 

I would like to get permission from you.
  • Your code can help others. Can I share the link to this page on social networks?
  • Can I quote your post with the link I attached and share the link to this page?

 

Many thanks,
Message 4 of 5
Kahylan
in reply to: amaterasu-qbb

Happy to help!

 

Sure feel free to share this page wherever you want 🙂

 

Message 5 of 5
amaterasu-qbb
in reply to: Kahylan

Thank you.😊

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report