Announcements

Between mid-October and November, the content on AREA will be relocated to the Autodesk Community M&E Hub and the Autodesk Community Gallery. Learn more HERE.

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

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

amaterasu-qbb
Collaborator Collaborator
572 Views
3 Replies
Message 1 of 4

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

amaterasu-qbb
Collaborator
Collaborator

 

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,
0 Likes
Accepted solutions (1)
573 Views
3 Replies
Replies (3)
Message 2 of 4

amaterasu-qbb
Collaborator
Collaborator
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,
0 Likes
Message 3 of 4

Kahylan
Advisor
Advisor
Accepted solution

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
Collaborator
Collaborator
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.