Search code examples
python-2.7attributesmayaverticesmaya-api

How can you get a vertex's attribute through Open Maya


I want an Open Maya getter and setter for locking a vertex's pnt attribute.

I am currently using Maya's standard cmds, but it is too slow.

This is my getter:

mesh = cmds.ls(sl=1)[0]
vertices = cmds.ls(cmds.polyListComponentConversion(mesh, toVertex=True), flatten=True)  

cmds.getAttr("{}.pntx".format(vertices[0]), lock=True)

This is my setter:

mesh = cmds.ls(sl=1)[0]
vertices = cmds.ls(mc.polyListComponentConversion(mesh, toVertex=True), flatten=True)

cmds.setAttr("{}.pntx".format(vertices[0]), lock=False)

This is what I have so far, in Open Maya:

import maya.api.OpenMaya as om

sel = om.MSelectionList()
sel.add(meshes[0])
dag = sel.getDagPath(0)
fn_mesh = om.MFnMesh(dag)

I think I need to pass the vertex object into an om.MPlug() so that I can compare the pntx attribute against the MPlug's isLocked function, but I'm not sure how to achieve this.

I have a suspicion that I need to get it through the om.MFnMesh(), as getting the MFnMesh vertices only returns ints, not MObjects or anything that can plug into an MPlug.


Solution

  • My suspicion was correct; I did need to go through the MFnMesh. It contained an MPlug array for pnts. From there I was able to access the data I needed.

    import maya.api.OpenMaya as om
    
    meshes = mc.ls(type="mesh", long=True)
    bad_mesh = []
    
    for mesh in meshes:
        selection = om.MSelectionList()
        selection.add(mesh)
        dag_path = selection.getDagPath(0)
        fn_mesh = om.MFnMesh(dag_path)
    
        plug = fn_mesh.findPlug("pnts", True)
    
        for child_num in range(plug.numElements()):
            child_plug = plug.elementByLogicalIndex(child_num)
    
            for attr_num in range(child_plug.numChildren()):
                if child_plug.child(attr_num).isLocked:
                    bad_mesh.append(mesh)
                    break