Button pressed execute code....

Button pressed execute code....

dg3duy
Collaborator Collaborator
695 Views
2 Replies
Message 1 of 3

Button pressed execute code....

dg3duy
Collaborator
Collaborator

How to call the create layer script when clicking the create layer button from the menu.

I don't know how to make the call from a button to the create layer code. And looking for help in the forums I'm lost because it's so vast.

dg3duy_0-1662681850204.png

 


MENU:

import maya.cmds as cmds

def showUI():
    myWin = cmds.window(title="Add layer", widthHeight=(260, 52))
    cmds.columnLayout()
    cmds.button(label="Add to Layer",width=258,align='center', command=buttonFunction)
    cmds.separator()                                                        
    cmds.button(label="test",width=258,align='center', command=buttonFunction)

    cmds.showWindow(myWin)
showUI()

CREATE LAYER:

import maya.cmds as cmds

def makeAnimLayer(layerName):

    baseAnimationLayer = cmds.animLayer(query=True, root=True)

    foundLayer = False

    if (baseAnimationLayer != None):
        childLayers = cmds.animLayer(baseAnimationLayer, query=True, children=True)

        if (childLayers != None) and (len(childLayers) > 0):
            if layerName in childLayers:
                foundLayer = True

    if not foundLayer:
        cmds.animLayer(layerName)
    else:
        print('Layer ' + layerName + ' already exists')

makeAnimLayer("Layer")

cmds.animLayer("Layer", edit=True, addSelectedObjects=True, selected=True)


 

0 Likes
Accepted solutions (1)
696 Views
2 Replies
Replies (2)
Message 2 of 3

Kahylan
Advisor
Advisor
Accepted solution

Hi!

 

With Maya UI's it helps to have the UI and the functions it calls on together in a class, so they are initialized properly at the same time, also for simple scripts like this, I like to put the function that calls the UI into the __init__ function of the class, so the UI gets loaded when the class gets called on.

Also, since you seem to want to use the default "button" command, you need to be aware that this command is quite old and has some kinks you need to be aware of. It returns a value of False as an argument in the function it calls on whenever the button is pressed (I'm not sure why, but that is just how it is). So you need to catch random arguments in the function you are calling if it doesn't require any arguments. And if it requires additional arguments, you need to call it using a lamda function, because there isn't another way to provide the arguments with this kind of button. In MEL you would need to call a dummy function that reads out/provides all the needed arguments to the function you actually want to call and calls it.

 

So here is how that looks like (I also added a textfield to your UI where a Layername can be specified, since you would have probably done that down the line anyways, since all your layers would have been called "Layer" otherwise):

import maya.cmds as cmds

class AnimLayerScript():
    
    def __init__(self):

        myWin = cmds.window(title="Add layer", widthHeight=(260, 52))
        cmds.columnLayout("mainColumn")
        cmds.rowLayout( numberOfColumns=2, columnAttach=(1, "right", 10), columnWidth=[(1,143),(2,245)] )
        cmds.text(label='Layer Name')
        textFld=cmds.textField('defaultAttr',tx ="Layer", width=240)
        cmds.separator(p = "mainColumn")  
        self.button = cmds.button(p = "mainColumn",label="Add to Layer",width=258,align='center', command= lambda x: self.makeAnimLayer(x, layerName = cmds.textField(textFld, q = True, tx= True) ))
                                                              
    
        cmds.showWindow(myWin)
    
    def execute():
        pass
    
    def makeAnimLayer(*args, layerName= None):

        baseAnimationLayer = cmds.animLayer(query=True, root=True)
    
        foundLayer = False
    
        if (baseAnimationLayer != None):
            childLayers = cmds.animLayer(baseAnimationLayer, query=True, children=True)
    
            if (childLayers != None) and (len(childLayers) > 0):
                if layerName in childLayers:
                    foundLayer = True
    
        if not foundLayer:
            cmds.animLayer(layerName)
        else:
            print('Layer ' + layerName + ' already exists')
        cmds.animLayer(layerName, edit=True, addSelectedObjects=True, selected=True)

AnimLayerScript()
    

 

I hope it helps!

Message 3 of 3

dg3duy
Collaborator
Collaborator

@Kahylan As usual, excellent explanation of how to implement the solution to the problem. Thank you very much for taking the time to clarify the doubts.

0 Likes