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: 

(Python) Using if and else to toggle between a weighted tangent and a non-weighted tangent not working.

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
amaterasu-qbb
354 Views, 3 Replies

(Python) Using if and else to toggle between a weighted tangent and a non-weighted tangent not working.

 

import maya.cmds as cmds

# Non-Weighted Tangents. This is depends on preferences.
cmds.keyTangent(e = True, wt = False)

# set to Weighted Tangents.
cmds.keyTangent(e = True, wt = True)

# set to Non-Weighted Tangents, False.
if cmds.keyTangent(q = True, wt = True):
    cmds.keyTangent(e = True, wt = False)

# toggle Weighted Tangents.
else:
    cmds.keyTangent(e = True, wt = True)

 

It works until if. But I am unable to toggle Weighted Tangents and Non-Weighted Tangents using else. Any kind of suggestions are appreciated. I want to register and use this code as a hotkey.

Thanks,
Labels (1)
3 REPLIES 3
Message 2 of 4

Hi! everyone.
I found a code that works on Twitter and I want to share it.
The author of the code is @Anonymous on Twitter.

 

 

import maya.cmds as cmds

# Toggle Weighted Tangent, Non-Weighted Tangents.
cmds.keyTangent(e = True, wt = not(cmds.keyTangent(q = True, wt = True)[0]))
"""
There are other suggested codes.
"""
import maya.cmds as cmds

selectedCurves = cmds.keyframe(q=True,name = True,selected = True) or []
for selectedCurve in selectedCurves:
    cmds.keyTangent(selectedCurve,e = True,wt = not(cmds.keyTangent(selectedCurve,q = True, wt = True)[0]))

 

 

The last code is a great code that allows toggle Weighted Tangents and Non Weighted Tangents for each Graph in the Graph Editor.
Any advice or feedback you can give me would be appreciated.

Thanks,
Message 3 of 4
Kahylan
in reply to: amaterasu-qbb

Hi!

 

Your code works for me. Could it be that you left lines 2-8 in so you automatically set it to weighted before the if statement every time?

 

import maya.cmds as cmds

# set to Non-Weighted Tangents, False.
if cmds.keyTangent(q = True, wt = True) == [True]:
    cmds.keyTangent(e = True, wt = False)

# toggle Weighted Tangents.
else:
    cmds.keyTangent(e = True, wt = True)

 

 

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

Hi! @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.
Maybe I am missing some basics. I tried to make a flowchart, but I was confused and could not write the code.

This led me to create 2 to 8 lines of code.
I thought I could toggle it with 9 to 15 lines of code, but in reality it was tough.

I didn't know about the 4-line technique in your code where you put == [True]: after the if.

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

Post to forums  

Autodesk Design & Make Report