Extrait de code Maya API
Get active selection list
import maya.OpenMaya as om
sel = om.MSelectionList()
om.MGlobal.getActiveSelectionList(sel)
Get MObject
from node name
import maya.OpenMaya as om
sel = om.MSelectionList()
sel.add("pCube*|pCubeShape*")
m_obj = om.MObject()
for i in range(sel.length()):
sel.getDependNode(i, m_obj)
# do things with m_obj...
As you can guess, MSelectionList.add()
can use the same expression than MEL commands.
The sel.add(...)
could have been replaced by MGlobal.getSelectionListByName("pCube*|pCubeShape*", sel)
and do the exact same thing.
You can use MSelectionList.getDagPath()
instead of MSelectionList.getDependNode()
to get a MDagPath
.
Iterate over geometry vertex
import maya.OpenMaya as om
# mesh node to iterate vertices
node = 'BodyShape'
# get MObject from node name
sel = om.MSelectionList()
sel.add(node)
dag_path = om.MDagPath()
sel.getDagPath(0, dag_path) # 0 is index
# create vertex iterator
mesh_iter = om.MItGeometry(dag_path)
while not mesh_iter.isDone():
# get MPoint position in world space
pt = mesh_iter.position(om.MSpace.kWorld)
print(pt.x, pt.y, pt.z)
mesh_iter.next()
Get vertex count per face
We will use MFnMesh.getVertices().
import maya.OpenMaya as om
sel = om.MSelectionList()
sel.add('BodyShape')
dag_path = om.MDagPath()
sel.getDagPath(0, dag_path) # 0 is index
# create mesh function set
fn_mesh = om.MFnMesh(dag_path)
# get vertex count and vertex ids for each face.
vtxs_count = om.MIntArray()
vtxs_id = om.MIntArray()
fn_mesh.getVertices(vtxs_count, vtxs_id)
for vtx_count in vtxs_count:
print(vtx_count) # 4, 4, 4, 3, etc.
Convert MFloatMatrix to MMatrix
Using Maya API, you often have to convert a MFloatMatrix
type to MMatrix
type. The simpler way to do it in Python is this way:
import maya.OpenMaya as om
my_float_mtx = om.MFloatMatrix()
my_mtx = om.MMatrix(my_float_mtx.matrix)
This rely on the MFloatMatrix.matrix
property which is of type float [4][4]
using the MMatrix(const float m[4][4])
constructor.
Iterate over Maya ui as PySide objects
import maya.OpenMayaUI as omui
from PySide import QtCore, QtGui
import shiboken
mayaMainWindow = shiboken.wrapInstance(long(omui.MQtUtil.mainWindow()), QtGui.QWidget)
def print_children(widget, depth = 0):
for child in widget.children():
print(' '*depth, type(child), child.objectName())
print_children(child, depth + 1)
print_children(mayaMainWindow)
More infos about MQtUtil here
Dernière mise à jour : dim. 10 mars 2024