Maya Py

Note: most maya commands can be run over a list of nodes by adding the list to the beginning of the command

importing maya commands
    from maya import cmds

commands reference

node selection
    # Select a node, add to the selection, and clear"node_name")"node_name", add=True)

    # Get selected nodes
    # Get all nodes of a certain type"node_type")

Note: wildcards such as * can also be used with the ls command

working with nodes
    # Create and delete
    cmds.createNode("node_type", name="node_name")
    # Group nodes"group_name", empty=True)
    cmds.parent("node_name", "group_name")
    # Get node type
    type = cmds.nodeType("node_name")
check if a node exists
    if cmds.objExists("node_name"):
        # The node exists
add or remove nodes from a set
    cmds.sets("node_name", add="set_name")
    cmds.sets("node_name", remove="set_name")

working with attributes
    # Set an attribute
    cmds.setAttr("node.attr_name", "string", type="string")
    # Get an attribute's value
    value = cmds.getAttr("node.attr_name")
    # Get a list of all attributes on a node
    attrs = cmds.listAttr("node_name")
    # Add an attribute to a node
    cmds.addAttr(at="float", dv=0, max=1, min=-1, nn="Attr Name", ln="attrName")

get shape or transform nodes
    shape_node = cmds.listRelatives('transform_node', type='shape', c=True)[0]
    transform_node = cmds.listRelatives("shape_node", type="transform", p=True)[0]

center pivot
    cmds.xform("node_name", cp=True)
freeze transforms
    cmds.makeIdentity(["node_name"], apply=True, t=1, r=1, s=1, n=0, pn=1)
delete history
    # All construction history
    cmds.delete(all=True, ch=True)
    # Only selected ndoes

create and assign a shader
    cmds.shadingNode("shader_type", name="shader_name", asShader=True)
    cmds.sets(name="shader_group_name", em=True, r=True, nss=True)
    cmds.connectAttr("shader_name.outColor", "shader_group_name.surfaceShader")
    cmds.hyperShade(["node_name"], assign="shader_name")

create a keyframe
    cmds.setKeyframe("node_name", v=value, at="attr_name", t=frame_number)
create an expression
    cmds.expression(a="node.attr_name", s="expression")

get current viewed camera
    for viewport in cmds.getPanel(type="modelPanel"):
        camera = cmds.modelEditor(viewport, q=1, av=1, cam=1)
create a camera and image plane"camera_name", fl=50)
    cmds.setAttr("camera_name.renderable", True)
    cmds.imagePlane(name="imageplane_name", c="camera_name")
    cmds.setAttr("imageplane_name.depth", 1000)
    # Turn the image plane off
    cmds.setAttr("imageplane_name.displayMode", 0)
    # Turn the image plane on
    cmds.setAttr("imageplane_name.displayMode", 3)
fit image plane to resolution gate
    import maya.mel as mel
    mel.eval("source AEimagePlaneTemplate.mel")
    mel_command = "AEinvokeFitRezGate %s->%s.sizeX %s->%s.sizeY" % (cam, img_plane, cam, img_plane)

get current file name and path
    import os.path
    file_path = cmds.file(q=True, sn=True)
    file_name = os.path.basename(file_path)
set project and open a file
    import maya.mel as mel
    mel.eval('setProject \"' + '/full/project/path' + '\"')
    cmds.file("file_name", o=True, f=True)
save a file under a newname
    cmds.file(force=True, save=True, type="mayaBinary")

working with references
    # Create a reference
    cmds.file("/full/path/to/file", r=True, mnc=False, nc="namespace", op="v=0;")
    # Get a list of all references
    references ="reference")
    # Get a reference file path
    path = cmds.referenceQuery("reference_name", f=True, shn=True)
    # Replace a reference
    cmds.file("/path/to/new/reference/file", loadReference="reference_name")
    # Delete a reference
    reference = cmds.referenceQuery("reference_name", f=True)
    cmds.file(reference, rr=True)

export selected geometry
    import os.path
    cmds.file("full/path/to/file.mb", op="v=0;", type="mayaBinary", pr=False, es=True)

    import sys
    sys.stdout.write("# Result: message")
raise an error
    cmds.error("Error message")

To create custom tool uis for maya, use QT (or maya commands…)