PyMEL Documentation

repository·master·Indexed 19 days ago

https://github.com/lumapictures/pymel

An object-oriented Python module for Autodesk Maya that provides a more intuitive alternative to the standard maya.cmds and maya.mel modules. PyMEL represents nodes and attributes as Python objects, offering a cleaner interface for MEL integration, simplified attribute management via the Attribute class, and a design philosophy aimed at making Maya's procedural commands more Pythonic.

Tokens
25.1K
Snippets
86
Records
114
Agent score
67%

What's inside PyMEL

  1. Use the Attribute class to manage Maya attributes

    master

    The Attribute class in pymel.core.general provides a unified interface for all attribute-related operations, replacing the various MEL commands. Method names follow a simplified pattern based on standard Maya commands:

    • setAttr becomes Attribute.set
    • getAttr becomes Attribute.get
    • connectAttr becomes Attribute.connect

    Example usage on a camera node:

    from pymel.core import *
    import pymel.core.general as general
    
    cam = general.PyNode('persp')
    if cam.visibility.isKeyable() and not cam.visibility.isLocked():
        cam.visibility.set(True)
        cam.visibility.lock()
    
    print cam.v.type()  # shortnames also work
    from pymel.core import *
    cam = general.PyNode('persp')
    if cam.visibility.isKeyable() and not cam.visibility.isLocked():
        cam.visibility.set(True)
        cam.visibility.lock()
  2. What is the Bridge in PyMEL?

    master

    The Bridge is a specialized control mechanism (managed via a GUI) that governs the interaction between MEL and the Maya API when generating PyNode classes. It allows developers to:

    • Create manual overrides to correct input and output types.
    • Map MEL commands to equivalent API commands.
    • Specify whether a specific PyMEL method should be implemented using MEL or the API.
    • Define the specific name of the resulting Python method.
  3. Understand the difference between maya.cmds and PyMEL

    master

    Maya provides two primary ways to interact with its core via Python, which serve different purposes:

    1. maya.cmds: An automatic wrapper around MEL commands. While powerful and covering thousands of commands, it is procedural and often feels 'unpythonic' or awkward because it is a direct translation of MEL functions into Python. It often returns types like None where a Pythonic API might return an empty list.

    2. PyMEL: A 'restructured wrap' that uses the existing Maya Python modules as building blocks to create an intuitive, object-oriented API. PyMEL is designed to be more legible, follows Python idioms, and provides a complete object-oriented design for working with nodes and attributes.

    PyMEL's primary goals are to fix bugs in maya.cmds, improve its behavior to be more Pythonic, and provide a structured object-oriented interface.

  4. How PyMEL improves procedural (maya.cmds) commands

    master

    PyMEL reorganizes existing maya.cmds functionality following specific practical guidelines to ensure a better developer experience:

    • Query/Edit Symmetry: A value returned by a query flag is accepted as a valid argument by the corresponding edit flag.
    • Consistent Return Types: Functions that return lists (like ls or listRelatives) return an empty list [] instead of None when no matches are found.
    • Single Item Returns: Functions that always return a single item return that item directly, rather than wrapping it in a list or tuple (e.g., spaceLocator).
    • Object Returns: Wherever possible, PyMEL returns PyMEL/Python objects instead of raw strings.
    • Mapping Mechanisms: Functions providing mapping mechanisms have dictionary-like counterparts (e.g., FileInfo for fileInfo).
    • Data Structures: Functions returning lists of pairs return a 2D array or a dictionary (e.g., ls(showType=1)).
    • UI Callbacks: Arguments provided by UI callbacks are of the appropriate type to be used to set the value of the control.
    • Node Arguments: If a function queries or edits Maya nodes, the node is passed as a positional argument rather than a keyword argument (e.g., sets).
  5. Understand the PyMEL design philosophy

    master

    PyMEL is designed to bridge the gap between the procedural nature of MEL/maya.cmds and a true Pythonic, object-oriented experience. It aims to solve three core problems:

    1. Fixing bugs found in maya.cmds.
    2. Improving workflow by making maya.cmds behavior more Pythonic (e.g., returning empty lists instead of None).
    3. Providing a complete object-oriented design for interacting with Maya nodes, attributes, and other structures.

    PyMEL strikes a balance between the verbosity of the Maya C++ API and the unorganized, procedural nature of MEL.

  6. Import Python modules vs. sourcing MEL scripts

    master

    In MEL, procedures might be automatically available if the script is on the MAYA_SCRIPT_PATH. In Python, you must be explicit: any .py file on your PYTHONPATH is a module and must be imported using the import statement in every script that uses it.

    # MEL
    source "myScript.mel";
    
    # Python
    import myModule
  7. Use PyMEL data, node, and UI type shortcuts

    master

    PyMEL provides convenient aliases for the specialized classes returned by pymel.core functions. Instead of importing the full paths, you can use these short aliases which are automatically imported into the pymel.core namespace:

    • Data Types: Use pymel.core.dt to access classes in pymel.core.datatypes.
    • Node Types: Use pymel.core.nt to access classes in pymel.core.nodetypes (classes corresponding to Maya node types).
    • UI Types: Use pymel.core.ui to access classes in pymel.core.uitypes (classes corresponding to Maya UI types).
  8. How PyMEL handles MEL integration

    master

    PyMEL provides a much cleaner interface for calling MEL procedures compared to maya.mel.eval. It allows you to call MEL procedures as if they were Python functions, passing arguments directly. Additionally, PyMEL provides specific MEL error messages with line numbers in Python tracebacks and allows interacting with MEL global variables via a dictionary-like interface.

    # Calling a MEL procedure with arguments
    values = ['one', 'two', 'three', 'four']
    pm.mel.stringArrayRemoveDuplicates(values)
    
    # Accessing and setting MEL global variables
    print(pm.melGlobals['gMainFileMenu'])
    pm.melGlobals['gGridDisplayGridLinesDefault'] = 2
  9. Manipulate names of non-existent objects

    master

    To allow for name parsing and string manipulation of objects that are not yet in the scene, PyMEL provides specialized classes in the other module. These classes contain methods for string parsing and existence testing without requiring the node to exist in Maya.

    Available classes in pymel.other:

    • other.NameParser
    • other.AttributeName
    • other.DependNodeName
    • other.DagNodeName
  10. Compare PyMEL vs maya.cmds and MEL

    master

    PyMEL significantly reduces boilerplate compared to maya.cmds and MEL. It uses object-oriented design to allow direct attribute access (e.g., x.sx.connect(x.sy)) and provides built-in support for vector math and list/vector arguments.

    # PyMEL approach to common tasks
    from pymel import *
    for x in ls(type='transform'):
        # object oriented design
        print(x.longName())
    
        # make and break some connections
        x.sx.connect(x.sy)
        x.sx.connect(x.sz)
    
        # disconnect all connections to .sx
        x.sx.disconnect()
    
        # add and set a string array attribute with the history of this transform's shape
        x.setAttr('newAt', x.getShape().history(), force=1)
    
        # get and set some attributes
        x.rotate.set([1, 1, 1])
        trans = x.translate.get()
        
        # vector math and list/vector args
        trans *= x.scale.get()
        x.translate.set(trans)
    
        # call a mel procedure
        mel.myMelScript(x.type(), trans)
  11. Build User Interfaces with PyUI classes

    master

    PyMEL provides an object-oriented approach to building Maya GUIs. Every UI command available in maya.cmds has a corresponding class in PyMEL derived from PyUI <pymel.core.uitypes.PyUI>.

    When you use procedural UI commands (like window() or button()), PyMEL automatically returns a PyUI class instance instead of a simple string name. These instances allow you to get and set properties directly using methods on the object, rather than passing names back into commands.

    from pymel.core import *
    
    # Procedural commands return PyUI objects
    win = window(title="My Window")
    layout = columnLayout()
    chkBox = checkBox(label="My Checkbox", value=True, parent=layout)
    btn = button(label="My Button", parent=layout)
    
    # You can interact with the objects directly
    print(chkBox.getValue())
    btn.setLabel("New Label")
    
    win.show()