Create UI using pymel

Hi,

I’m trying to learn pymel and now creating window in maya but it is not coming out as it is with
cmds,

camPan
‘’’
from pymel.core import *

template = uiTemplate(‘ExampleTemplate’, force=True)

with window(title=“PanCam Tool”,menuBar=True,menuBarVisible=True) as win:

# start the template block

with template:

    with columnLayout( rowSpacing=5 ):

        with frameLayout(label="Zoom Options"):

            with rowLayout(adjustableColumn=2):

                text("Camera to Zoom")

                with optionMenuGrp():

                    menuItem(label = "red")

            

            with rowLayout(adjustableColumn=2):

                text("X")

                floatSliderGrp(label='horizontal', step=6, field=True, minValue=-1.0, maxValue=1.0, value=0)

            with rowLayout(adjustableColumn=2):

                text("Y")

                floatSliderGrp(label='Vertical', step=6, field=True, minValue=-1.0, maxValue=1.0, value=0)

    

            with rowLayout(adjustableColumn=2):

                text("Z")

                floatSliderGrp(label='depth', step=6, field=True, minValue=-1.0, maxValue=1.0, value=0)

‘’’

I want to create a window with the layout such as in the attachment. could help me where I’m going wrong

Hi Nik,

I have several Maya tools which you can use as an example on PyMel UI setup, hope it can help a bit (I don’t think you need all this nested with statements)

Also, If you want to push boundaries with UI a bit you can take a look at PySide (PySide2 starting from Maya 2018 I guess). So you can use QT Designer to build interfaces easily. Though for your example it might be overkilled…

You have weird quotes sometimes, that fix allows window to pop up but still with errors:

from pymel.core import *

template = uiTemplate('ExampleTemplate', force=True)

with window(title='PanCam Tool',menuBar=True,menuBarVisible=True) as win:

    # start the template block
    
    with template:
    
        with columnLayout( rowSpacing=5 ):
    
            with frameLayout(label="Zoom Options"):
    
                with rowLayout(adjustableColumn=2):
    
                    text("Camera to Zoom")
    
                    with optionMenuGrp():
    
                        menuItem(label = "red")
    
                
    
                with rowLayout(adjustableColumn=2):
    
                    text("X")
    
                    floatSliderGrp(label='horizontal', step=6, field=True, minValue=-1.0, maxValue=1.0, value=0)
    
                with rowLayout(adjustableColumn=2):
    
                    text("Y")
    
                    floatSliderGrp(label='Vertical', step=6, field=True, minValue=-1.0, maxValue=1.0, value=0)
    
        
    
                with rowLayout(adjustableColumn=2):
    
                    text("Z")
    
                    floatSliderGrp(label='depth', step=6, field=True, minValue=-1.0, maxValue=1.0, value=0)
1 Like

Thank you so much I’ll definitely look for the PySide attributes. as you suggested no need for the nested with statements. I tried the redular method but when i use the optionmenuGrp command I’m not able to add the floatsliderGrp command. I checked for examples, found separate examples of each but nothing together. so could you guide me what went wrong or what am i missing?

‘’’
from pymel.core import *

winname = “CtexWin”

if window(winname,query=True,exists=True):

deleteUI(winname)

changewindow = window(winname,title=“PanCam Tool”,widthHeight=(350,250))

columnLayout()

frameLayout(label=“Zoom Options”,width= 350)

rowLayout(numberOfColumns=2)

optionMenuGrp(label=“Camera To Zoom”,columnAlign= (1,‘left’))

menuItem(label=“Cameras”)

floatSliderGrp(label=“X”,field=True,step=6)

floatSliderGrp(label=“Y”,field=True,step=6)

floatSliderGrp(label=“Z”,field=True,step=6)

showWindow(winname)
‘’’

@NikDesh
If you use three ` (tilde) around all of your code it will show upas a full code block (much easier to quickly check things/copy paste etc.) It looks like you’re trying, but starting with an ’ first or something

Sorry for the tilde not being used properly, I have an issue saying there is a syntax error.
everything is correct still it is showing the error, and when I run the same script from the previous file it just displays till the optionmenuGrp the later floatsliderGrp’s are not being displayed.
Could you tell me where i’m going wrong with the script?

The code you mentioned above when I run it it is giving a error

Error: line 1: RuntimeError: file C:\Program Files\Autodesk\Maya2018\Python\lib\site-packages\pymel\internal\pmcmds.py line 134: Too many children in layout: rowLayout2

I don’t know how to fix your code, I don’t understand this design. If you need to fix somebody’s code it could be a tough thing.

But if you are building your script by yourself, you can re-build UI using the example code below (I guess this is the common way for this) and it would be easier:

import pymel.core as pm
import os, sys, glob


def baseUI(*args):
    
    if pm.window('ANMManager', q = 1, ex = 1):
        pm.deleteUI('ANMManager')
    win = pm.window('ANMManager', t= 'Animation manager', rtf=True, w = 280 )
    with win:
        mainLayout = pm.columnLayout() 
        
        inputLayout = pm.rowColumnLayout(nc=6, parent = mainLayout) 
        pm.text (l = '  PART  ')
        partField = pm.textField(tx = 'REEL_01', w= 60, h = 30)
        pm.text (l = '    SEQ ')
        sequenceField = pm.textField(tx = '010', w= 45, h = 30)
        pm.text (l = '    SHOT ')
        shotField = pm.textField(tx = '010', w= 45, h = 30)
        pm.separator(h = 5 , style = 'none')        
        createLayout = pm.rowColumnLayout(nc = 1, parent = mainLayout) 
        OPANM = pm.button(l = 'OPEN ANIMATION SCENE', w = 280, h = 35)
        # OPANM.setCommand(pm.Callback (dna.shotOpen, partField, sequenceField, shotField, listANM ))
        pm.separator(h = 6 , style = 'none')  
                           
        PBLayout = pm.rowColumnLayout(nc = 1, parent = mainLayout) 
        playblast = pm.button(l = 'PLAYBLAST', w = 280, h= 50)       
        playblast.setCommand(pm.Callback (PB, 'mov'))
        pm.separator(h = 5, style = 'none')
        
        camLayout = pm.rowColumnLayout(nc = 10, parent = mainLayout) # 12, 18, 27, 40, 65, 75, 100, 135, 180
        for i in listLens:
            LB = pm.button(l = str(i), w = 25, h= 40)
            LB.setCommand(pm.Callback (lens, i))
        lockCAM = pm.button(l = 'LCM', w = 55)
        lockCAM.setCommand(pm.Callback (lockCam))
        pm.separator(h = 8, style = 'none')
        
        editLayout = pm.rowColumnLayout(nc = 1, parent = mainLayout) 
        check =  pm.button(l = 'CHECK SCENE', w = 280, h= 45)
        # check.setCommand(pm.Callback (dna.checkScene))
        
        addLayout = pm.rowColumnLayout(nc = 3, parent = mainLayout)
        add_A = pm.button(l = 'ADD ASSSETS', w = 90, h= 30)
        del_A = pm.button(l = 'DEL ASSETS', w = 90)
        upd = pm.button(l = 'UPDATE REFS', w = 100)
        #add_A.setCommand(pm.Callback (dna.addAsset))
        #del_A.setCommand(pm.Callback (dna.delAsset))
        #upd.setCommand(pm.Callback (dna.updateRefs))       
        updLayout = pm.rowColumnLayout(nc = 2, parent = mainLayout)
        fixALL = pm.button(l = 'FIX ALL', w = 240, h = 30)
        snv =    pm.button(l = 'SNV', w = 40)
        # fixALL.setCommand(pm.Callback (dna.fixAll))
        # snv.setCommand(pm.Callback (saveNext))
   
    win.show()    

baseUI()

Run this code in Maya, see how it looks, try to understand how each section was built. Start with an empty window and add necessary widgets one by one.

def baseUI(*args):
    
    if pm.window('ANMManager', q = 1, ex = 1):
        pm.deleteUI('ANMManager')
    win = pm.window('ANMManager', t= 'Animation manager', rtf=True, w = 280 )
    with win:
        mainLayout = pm.columnLayout() 
        
    win.show()    

baseUI()

Thank you so much for the example u gave it cleared some of my doubts. At least I know I’m following the same path to create the UI as u mentioned. but when try to create a floatslidergrp after a optionmenuGrp it doesnt seem to work in rowlayout.

import pymel.core as pm

def baseUI(*args):
    
    if pm.window('CamPanTool', q = 1, ex = 1):
        pm.deleteUI('CamPanTool')
    win = pm.window('CamPanTool', t= 'Camera Pan Tool', rtf=True, w = 280 )
    with win:
        mainLayout = pm.columnLayout() 
        
        pm.rowLayout(parent = mainLayout)
        pm.optionMenuGrp(l='Camera to Zoom: ')
        pm.menuItem(label='Camera 1')
        pm.menuItem(label='Camera 2')
        
        pm.rowColumnLayout(nr = 3, parent = mainLayout) 
        X = pm.floatSliderGrp(label='X: ', field=True)
        Y = pm.floatSliderGrp(label='Y: ', field=True) 
        Z = pm.floatSliderGrp(label='Z: ', field=True)
        
        pm.rowColumnLayout(nc = 3, parent = mainLayout) 
        pm.checkBox(label='Camera Pan ON/OFF' )
        pm.checkBox(label='Stereo ON/OFF' )
        pm.button(l = 'Reset')
        
    win.show()    

baseUI()

Hey Thanks for the clarification it worked for but in the optionmenu Grp when i add items to the menu
it is not being displayed. could you please let me know where I’m wrong.?

from pymel.core import *

def showUI():
    
    #get cameras in the scene
    panelwf = getPanel(withFocus=True)
    whichCam = modelPanel(panelwf,query=True, camera=True)
    whichCamShape[] = ls(whichCam,dag=True,allPaths=True,shapes = True)
    
    cameras[]=ls(cameras=True)

    
    #Create New Window
    winname = "CtexWin"
    if window(winname,query=True,exists=True):
        deleteUI(winname)
    changewindow = window(winname,title="PanCam Tool",widthHeight=(350,250),resizeToFitChildren=True,sizeable=True)
    columnLayout()
    frameLayout(label="Zoom Options",width= 350)
    columnLayout()
    optionMenuGrp(whichCam, label="Camera To Zoom",columnAlign= (1,'left'))
    menuItem(label=whichCamShape[0])
    for i in cameras:
        menuItem(label = cameras[i])

    setParent('..')
    x = floatSliderGrp(label="X",field=True,step=0.000001,minValue=-1.0,maxValue=1.0)

    y = floatSliderGrp(label="Y",field=True,step=0.000001,minValue=-1.0,maxValue=1.0)

    z = floatSliderGrp(label="Z",field=True,step=0.000001,minValue=-1.0,maxValue=1.0)
    
    checkBox(label="camPan On Off")
    columnLayout()
    button(label="Reset",width= 100,height = 20,command=reSet)
    changewindow.show()
    return x,y,z


def reSet(*args):
    floatSliderGrp(x,e=True,value=0)
    floatSliderGrp(y,e=True,value=0)
    floatSliderGrp(z,e=True,value=0)
    



        





x,y,z = showUI()