Python Tools — Session 8A

PyMEL UI Creation

2020.02.25 — Session 8A

Session Navigation

Overview

This session introduces PyMEL UI creation by building a simple directory browser tool. It covers window management (creating, checking for, and deleting existing windows), layout systems using Python context managers, button callbacks, text field widgets, and file dialog integration. This forms the foundation for building custom Maya tools with graphical interfaces.

Key Concepts

  • Window Management — Creating windows with handles, checking for existing instances, and cleaning up preferences
  • Context Managers for UI — Using Python's with statement to build nested UI layouts cleanly
  • Layout Systems — Combining columnLayout and rowLayout to arrange UI elements in rows and columns
  • Button Callbacks — Connecting button clicks to class methods using pm.Callback
  • Text Fields — Using pm.textField to display and capture string input from the user
  • File Dialogs — Opening native file browser dialogs with pm.fileDialog2 for directory selection

Code

basic_ui.py — Simple Directory Browser UI

"""
#############################################################################
filename    basic_ui.py
author      Matt Osbond 
dP email    m.osbond@digipen.edu
course      CS115/CSX510
Brief Description:
    Simple Ui with browse dialog
    Digipen CS115/CSX510 Spring 2020
    Session 8A - Feb 25 2020
#############################################################################
"""

import pymel.core as pm
import os

   
class SimpleBrowserUI():
    def __init__(self):
        # Create a "handle" for our UI. Nobody will ever see it, but we want to control it so we can check for it
        self.handle = 'SimpleBrowser'

        # Delete the UI if it exists - bascially ensure we only ever have one instance running
        if pm.window(self.handle, exists=True):
            pm.deleteUI(self.handle)

        # Delete the prefs - this is sueful while building out your UI. It can be removed for deployment though
        if pm.windowPref(self.handle, exists=True):
            pm.windowPref(self.handle, remove=True)

        # PyMEL allows us to build UIs using a python context
        # the "with" context wraps everything, and makes building UIs in Maya much simpler
        with pm.window(self.handle, title='Simple Directory Browser', width=350, height=100):

            # Create a basic layout
            with pm.columnLayout(rs=10):  
                # Now we want two columns, we do this using a rowLayout and defining the numberOfColumns
                with pm.rowLayout(nc=2):
                    # Make a "browse" button and link the command using a callback
                    pm.button(label='Browse', command=pm.Callback(self.browse))
                    # A textField is where can save and visualize a string (our dir path)
                    self.target_dir = pm.textField(width=250)
                # Create our main button and link the command again
                pm.button(label='Go', width=345, command=pm.Callback(self.do_stuff))

            # Don't forget to show the window!
            pm.showWindow()

            
    def browse(self):
        # dialogStyle defines how the UI looks, fileMode determines what we browse for
        # See the docs for more info
        files = pm.fileDialog2(dialogStyle=2, fileMode=3)
        if files is not None:
            # fileDialog2 returns a list of files selected, but for fileMode=3 we only get one, so get the first element
            self.target_dir.setText(files[0])

            
    def do_stuff(self):
        print 'Doing stuff using directory : "%s"' %self.target_dir.getText()

        print 'Directory exists = %s' %os.path.exists(self.target_dir.getText())
← Prev Next →