Python button visibility

Python button visibility

miharu_s
Participant Participant
3,642 Views
9 Replies
Message 1 of 10

Python button visibility

miharu_s
Participant
Participant

Hi, I've been trying to find solve this for weeks, but now I'm growing more desperate than ever each day. So here's the deal!

 

I'm trying to toggle the visibility of a button on the click of another one. I don't know if I'm just horrible at doing research but it seems like nobody is running into that issue, so I have to assume it's something really simple and I'm just way to dumb to figure it.

 

Here a snippet of it explaining the general idea:

 

def CharaPickerUI():
    if window('PickerUI', exists = True):
        deleteUI('PickerUI')
        
    PUI = window('PickerUI')
    
    with formLayout( width = 727  # Form Layout to create a divider
                
        CharaForm = formLayout()# Form Layout to put the button in Place
        
        HeadBTN = button( label = 'Button A' )# Button creation
        
        formLayout( CharaForm, edit = True, attachForm = [(HeadBTN , 'top', 123 ), (HeadBTN , 'left', 319 )])# edit of the form to put button in place
        
        SHFacialBTN = button ( label = 'Show / Hide Button A', width = 150, height = 72, command = 'SHFacialBTN()' )# Button creation
    
    showWindow('PickerUI')

def LLegSwitch(  
    
    if getAttr("bacon") == 1: # False condition but I use a real one

        button ('HeadBTN', edit = True, visible = False)

    
    else:
        
        button ('HeadBTN', edit = True, visible = True)

   
CharaPickerUI()

 

Here's  a picture of my whole UI the button circled in orange are supposed to switch the visibility of the IK/FK controler in the UI 

Picker.png

 

 

 

0 Likes
Accepted solutions (1)
3,643 Views
9 Replies
Replies (9)
Message 2 of 10

stuzzz
Collaborator
Collaborator

Hi,

 

It's hard to say with the pseudo code but you are using Pyside or PyQt:

button.setVisible(False)

 

 

0 Likes
Message 3 of 10

miharu_s
Participant
Participant

Hey,

 

first thanks for your answer but sadly it doesn't work, I'm quite new to the whole python stuff so maybe I bite more than I could chew.

 

Anyways, as far as the language I'm using I couldn't be less sure. I started with basic Maya commands then proceed to Import pymel and functools as I was going on. In the end, the first lines of my program look like this!

( By the way, is it useful to say that I'm coding the whole thing directly into the script editor of Maya and not a tertiary coding software like Notepad++ )

 

import maya.cmds as cmds
from pymel.core import *
from functools import partial

 

 As for the code sorry, I didn't want to post like 1300 codes line here and be marked as spam ( which happen after I edited the post just to add the picture ) I also realized that the whole thing wasn't making any sense since I was just copying bits and pieces of my main code.

 

Not sure if this will be more helpful but, here is the line of the creation of the button and the ones of the function it has to execute.

 

for clarification sakes, I'm going to Identify each name that is truly relevant to the problem, not sure it will be that much more helpful tough. If not I guess I'll just throw the whole thing in here, after adding like billions of comments 'cuz there's aren't that many not to say none in the code

 

LLegIKFKBTN = Name of the button

LLegSwitch = Name of the function

L_Leg_Switch_CTRL.translateX = Name of the attribute controlling the IK - FK Switch manually on the model 

LKneeIKBTN = Name of one of the IK button in the picker ( One of the many I'm trying to hide )

 

LLegIKFKBTN = button ( label = 'IK / FK Switch', width = 150, height = 45, command = 'LLegSwitch()' )

def LLegSwitch( ):
    
    if getAttr("L_Leg_Switch_CTRL.translateX") == 1:
        #IK TO FK
        matchTransform("L_Upper_Leg_CTRL", "L_Upper_Leg_JNT")
        matchTransform("L_Lower_Leg_CTRL", "L_Lower_Leg_JNT")
        matchTransform("L_Ankle_Twist_CTRL", "L_Ankle_Twist_JNT")
        setAttr("L_Leg_Switch_CTRL.translateX", 0)
        
        #LKneeIKBTN = button( visible = False )
        #LKneeIKBTN ( visible = False )
        #button('LKneeIKBTN', visible = False)
        #button ('LKneeIKBTN', edit = True, visible = False)
        button.setVisible(False)

    
    else:
        #FK TO IK
        matchTransform("L_Knee_CTRL", "L_Lower_Leg_JNT", rotation = False)
        matchTransform("L_Ankle_Twist_CTRL", "L_Ankle_Twist_JNT")
        matchTransform("L_Toe_Flap_CTRL", "L_Toes_JNT")
        setAttr("L_Leg_Switch_CTRL.translateX", 1)
        
        #LKneeIKBTN = button( visible = True )
        #LKneeIKBTN ( visible = True )
        #button('LKneeIKBTN', visible = True)
        #button ('LKneeIKBTN', edit = True, visible = True)
        button.setVisible(True)

 

 

Also, you have each and every test I've made as a comment to hide the knee button.

 

0 Likes
Message 4 of 10

stuzzz
Collaborator
Collaborator

Hi,

 

So if you are new to this thing I would recommend to have an understanding of Pyside/PyQt which are the Maya's GUI framework since many years.

 

I used to do my interface with melscript but finally switch to avoid headhaches

0 Likes
Message 5 of 10

miharu_s
Participant
Participant

Hey,

 

so yeah... not to sound disrespectful for all the help you provided up until now, but that is not an option, that UI is due for Monday 11h59 PM. With Pyside/PyQt it must be 10x easier to make the whole layout thing but, for now, at my school  they want us to stick with basic Maya command ( the one we can find in the technical documentation help )

 

I don't have the time to learn a whole new language and I especially don't want to be more penalize for using to much-advanced stuff ( I'm not even sure how my grade on this will be affected already with the use of Pymel and functools )

 

I mean there must be a way right. This is the last thing I have to make work for the whole UI to be ready.

0 Likes
Message 6 of 10

stuzzz
Collaborator
Collaborator

It's definitely not disrespectul my friend, I understand that.

So here the solution, I've just tried, it should work!

 

# -- make sure to store the output of the button creation in a variable 
cmds.window( width=150 )
cmds.columnLayout( )
myBut = cmds.button( label='Button 1' )
cmds.showWindow()

MyBut contains the direct location of your button which gives you a direct access to it for modifications later.

then:

cmds.button(myBut, e=True, visible=False)

 As you can see above, I used my handle variable "myBut" to edit my button behaviors. The flag "visible" should help with that boolean value.

 

If you use a python class, you may go for a global class variable, or a property accessor.

Please tell me if that works.

0 Likes
Message 7 of 10

miharu_s
Participant
Participant

hi,

so I took what you've written and edited it just a bit to work the same way the rest of my program written and it look like this 

# -- make sure to store the output of the button creation in a variable 
cmds.window( width=150 )
cmds.columnLayout( )

myBut = cmds.button( label='Button 1', visible=False )
myButA = cmds.button( label='Hider 2', command = 'Hider()' )

cmds.showWindow()

def Hider():
    
    if button('myBut', visible = False):
        cmds.button(myBut, e=True, visible=True)

    else:
        cmds.button(myBut, e=True, visible=False)

 

as a stand-alone what you've written work perfectly but when I add an if statement the if part works great but for the else which is when I click on the button again I get this error message

 

# Error: RuntimeError: file C:\Program Files\Autodesk\Maya2018\Python\lib\site-packages\pymel\internal\pmcmds.py line 134: Object's name 'myBut' is not unique. #

 

0 Likes
Message 8 of 10

stuzzz
Collaborator
Collaborator
Accepted solution

when using Maya's command you mainly have 3 properties flags( Create, Query, Edit )

When you first create the button you implicitly use the create flag:

myBut = cmds.button() 

 then you when you set the button as visible you use the edit flag (e):

cmds.button(myBut, e=True, visible = False)

if you want to know if the button is visible you need to use the Query flag  (q):

cmds.button(myBut, q=True, visible=True)

This will return a boolean (True if visible otherwise False).

 

At your if statement at your  Hider function, you implicitly create a new button instead of questionning (query) the visible state of myBut existing button.

Please make sure that you put the variable and not a string also

 

so you should have instead:

def Hider():
    if button(myBut, q=True, visible = True):
      ...

 

 

 

 

0 Likes
Message 9 of 10

miharu_s
Participant
Participant

You're a genius really, it works marvelously so I'll just take it from there and try to make it work with my shenanigans of a code 'cuz up to now Maya give me this error when I try to run it

 

# Error: NameError: file <maya console> line 1285: global name 'LKneeIKBTN' is not defined

 

But I'll consider the problem solve since this was the major issue and at least it gives an answer for the one who will be searching for it like I was.

 

Thousands of tanks again and again

 

If I still can't make it work I'll guess I'll just come to cry here again 🙂

0 Likes
Message 10 of 10

stuzzz
Collaborator
Collaborator

That's a please to help.

Hopefully you will finish the UI before the deadline.