/maya-scripts-zyf

Useful information about scripting in Autodesk Maya. And some scripts for operations in Maya, Mel or Python.

Primary LanguagePython

Scripting in Autodesk Maya

Author: Zhao Yafei (email: zhaoyafei0210@gmail.com)

  1. Some useful information about scripting in Autodesk Maya.
  2. Some Scripts (MEL or Python) for operations in Maya.

Table of Contents:

Maya Application Content

Content Tree

For Maya 2019 on MacOS:

/Applications/Autodesk/maya2019/Maya.app/Contents
├── Examples
├── Frameworks
├── Info.plist
├── MacOS
├── PkgInfo
├── Resources
├── _CodeSignature
├── assets
├── bin
├── brushImages
├── brushShapes
├── icons
├── lib
├── libexec
├── modules
├── presets
└── scripts

Python Interpretor Path

For Maya 2019 on MacOS, Maya's python interpretor is under '/Applications/Autodesk/maya2019/Maya':

/Applications/Autodesk/maya2019/Maya.app/Contents/Frameworks/Python.framework
├── Headers -> Versions/Current/Headers
├── Python -> Versions/Current/Python
├── Resources -> Versions/Current/Resources
└── Versions
    ├── 2.7
    └── Current -> 2.7

Maya APIs

MEL

MEL short for Maya Embedded Language, is a scripting language written for Maya, and with which much of Maya’s functionality and user interface is built. It was designed to be concise, simple, and Maya-specific.

C++ API

C++ API provides deeper access to Maya’s internals. With the API you can create new node types and new MEL commands. Prior to the introduction of python in Maya, the API could only be used with C++.

Python API

Offical (but confusing) Python APIs

PyMel docs give an clear introduction for the confusing official Maya Python APIs on this page ( Why PyMEL?):


  Rather than reinvent the wheel, Autodesk opted to provide “wrappers” around their pre-existing toolsets: the C++ API and MEL. By wrapping them they provided an alternate python interface for each, but the core code that comprises the API and MEL remained largely the same. The wrappers serve as a (hopefully) thin layer to communicate between python and the Maya code being wrapped. The nature of the wraps is slightly different for each.

  - maya.OpenMaya
  In the case of the C++ API, Autodesk went with an open source wrapper called swig which generates python functions and classes from C++ counterparts. The python layer rests on top of C++, which remains the native execution language. During execution, swig marshals data back and forth between python and C++.
  MEL itself is split into two components: commands, and everything else.

  - maya.cmds
  In the case of MEL commands, which are (effectively) written using the C++ API, Autodesk wrote the wrapping mechanism themselves such that MEL commands could also be registered and used in python as functions. The same command executed in MEL and python ultimately end up triggering the same underlying C++ API calls.
  
  - maya.mel.eval
  In order to allow execution of arbitrary MEL code such as procedures, Autodesk provided high level access to the MEL interpreter. This is as simple of a wrapper as you can get: evaluate a string representing a chunk of MEL code in the MEL interpreter and convert the result to a python object (string, float, int, and lists thereof).
  So now that python is available in Maya all of our problems are solved, right? Well, not quite. Since these new modules are just wraps of the same underlying API and MEL code that we’ve had all along and neither were intended to eventually become “pythonified”, the syntax that results from this layering of python over MEL and C++ tends to be awkward, especially to those familiar with python’s idioms.

  This syntactical awkwardness, particularly in maya.cmds, was one of the initial inspirations behind PyMEL. Think of it this way: would you rather read a book that was translated from japanese into english by a software program like babelfish or by a human who is fluent in both languages? That is a key difference between an automatic wrap like maya.cmds and a “restructured wrap” like PyMEL, which uses the maya python modules as building blocks to construct an intuitive, insightful, and pythonic API.
PyMel

PyMel Docs

What is PyMel:

PyMEL makes python scripting in Maya work the way it should. Maya’s command module is a direct translation of MEL commands into python functions. The result is a very awkward and unpythonic syntax which does not take advantage of python’s strengths – particularly, a flexible, object-oriented design. PyMEL builds on the cmds module by organizing many of its commands into a class hierarchy, and by customizing them to operate in a more succinct and intuitive way.

Which Python API to use?

The official ones: maya.cmds/maya.mel

Reasons:

Scripting in Maya

Cloud Help for Scripting in Maya 2019 on knowledge.autodesk.com.

MEL and Expressions

Cloud Help for MEL and Expressions in Maya 2019 on knowledge.autodesk.com.

Python in Maya

Cloud Help for Python in Maya 2019 on knowledge.autodesk.com.

Python scripting can be used for many tasks in Maya, from running simple commands to developing plug-ins, and several different Maya-related libraries are available targeting different tasks. The following is a brief overview of the Python libraries shipped with Maya:

maya.cmds

This is a Python wrapper for MEL commands, and can be used in place of MEL. For more information, see Using Python and the Python Commands Reference.

pymel.core

Pymel is an alternative wrapper for MEL, developed by a third party. It is shipped with Maya, but not supported by Autodesk. It organizes commands differently, and takes an object-oriented approach compared to the procedural approach of maya.cmds. For more information, see Using PyMEL and the PyMel Reference.

PyMel Docs

maya.OpenMaya

This is a Python wrapper for the Maya C++ API, and referred to as Python API 1.0. It is suitable for developing plug-ins, and other tasks that require functionality not exposed by MEL. To understand the exposed classes, you should refer to the conceptual topics and the "C++ API Reference" in the Maya Developer Help. For more information, see "Maya Python API 1.0" in the Maya Developer Help.

maya.api.OpenMaya

This is a Python wrapper for the Maya C++ API, and referred to as Python API 2.0. This wrapper has better performance and is more "Pythonic" than the Python API 1.0. It is also a newer API, and is still under development, so not all classes exposed in 1.0 are available. For more information, see "Maya Python API 2.0" and "Maya Python API 2.0 Reference" in the Maya Developer Help.

Maya Scripts

In-product MEL scripts

On MacOS, Maya 2019 in-product scripts (.MEL) are under /Applications/Autodesk/maya2019/Maya.app/Contents/scripts. The content tree is as follows:

/Applications/Autodesk/maya2019/Maya.app/Contents/scripts
├── AETemplates
├── FBX
├── NETemplates
├── fur
├── gameFbxExporterPresets
├── muscle
├── others
├── paintEffects
├── shelves
├── startup
├── turtle
└── unsupported

nodes/objects

  • Get name lists of nodes/objects using cmds.ls()
    • Python Script with maya.cmds: maya_get_nodes_list.py (Tested in Maya2019) functions:
      • get_current_scene_name()
      • export_nodes_list()
      • get_node_types()
      • get_all_node_list()
      • get_all_shape_nodes()
      • get_all_transform_nodes()
      • get_all_mesh_nodes()
      • get_all_joint_nodes()
      • get_all_blendshape_nodes()
      • get_all_geometry_nodes()
      • get_all_material_nodes()
      • get_all_texture_nodes()
      • get_all_camera_nodes()
      • get_all_light_nodes()
      • get_all_partition_nodes()
      • get_all_set_nodes()
      • get_all_container_nodes()
      • get_all_plane_nodes()
  • Get node/object name list of the specified node type using cmds.ls()
    • Python Script with maya.cmds: maya_get_nodes_list_of_type.py (Tested in Maya2019) functions:
      • get_current_scene_name()
      • export_nodes_list()
      • get_node_types()
      • get_all_node_list()
      • get_node_list_of_type(): succeeded on types: mesh/blendShape/joint/camera/constraint/..., failed on types: material/texture/light

blendshapes

  • Duplicate blendshape targets
    • default: When operating in Shape Editor, this will automatically call /Applications/Autodesk/maya2019/Maya.app/Contents/scripts/blendShapeEditorDuplicateTargets.MEL
  • Export name list of target shape of the specified blendshape into .txt file
  • Export name list of target shape of all blendshape nodes into .txt file
  • Export target shapes of specified blendshape into .obj file
  • Export keyframe blendshape weight values into .json file

expressions

joints

keyframe

Maya Commands Reference and Node Types Reference

The following links were accessible as of June 2020.

Maya 2020

Maya 2019

Maya 2018

Maya 2017

Maya 2016

Maya 2015

Maya 2014

Maya 2013

Maya 2012

Maya 2011

Maya 2010

Maya 2009

Maya 2008