Help: Selecting mesh vertices through the Python 2.0 API

Help: Selecting mesh vertices through the Python 2.0 API

ejcarroll007
Explorer Explorer
6,275 Views
4 Replies
Message 1 of 5

Help: Selecting mesh vertices through the Python 2.0 API

ejcarroll007
Explorer
Explorer

Hey everyone,

 

I'm trying to figure out how to select vertices through the Maya Python 2.0 API, instead of using the maya.cmds. I'm testing to see how much of a performance boost I can get when using the API.

 

Example of how I would normally approach selecting vertices in maya.cmds

import maya.cmds as cmds

vertices = [0, 1, 2, 3, 4]

vertex_select_list = []    
for vertex in vertices:
    vertex_select_list.append("{0}.vtx[{1}]".format('pSphere1', vertex))
    
cmds.select(vertex_select_list)

 

 

Sample of my Python 2.0 API code for trying to select vertices. (This is what I scrounged up from how the internet does it)

import maya.api.OpenMaya as omapi2

vertices = [0, 1, 2, 3, 4]

sel = omapi2.MSelectionList()
sel.add("pSphere1")
dag, mObject = sel.getComponent(0)
mfn_components = omapi2.MFnSingleIndexedComponent(mObject)
mfn_object = mfn_components.create(omapi2.MFn.kMeshVertComponent)
mfn_components.addElements(vertices)
selection_list = omapi2.MSelectionList()
selection_list.add(mfn_object)
omapi2.MGlobal.setActiveSelectionList(selection_list)

The above code does not work as I get the following error...

# Error: RuntimeError: file <maya console> line 12: (kInvalidParameter): Object is incompatible with this method #

 

My 2 questions...

  1. In the error above, I get an incompatible error. But I'm not exactly sure what the issue is, as the MSelectionList.add() method requires an MObject. Which I am passing through when I used the MFnSingleIndexedComponent.create() method. Does anyone why I'm getting this error and what it means exactly?
  2. Is their a better/different way of selecting vertices through the Maya Python 2.0 API? I don't even know if this is the intended way of doing so.

Thanks,

 

-Eric

0 Likes
Accepted solutions (1)
6,276 Views
4 Replies
Replies (4)
Message 2 of 5

stuzzz
Collaborator
Collaborator
from maya.OpenMaya import * #import in this space for better visualisation

mesh = "pSphere1" 
vertices = MIntArray()
indices = [4,2,1]
[vertices.append(x) for x in indices] #there might be a better solution for initializing a MIntArray with an tuple. 

comp = MFnSingleIndexedComponent()
compObj = comp.create(MFn.kMeshVertComponent)
comp.addElements(vertices)

# we query the MDagPath of the mesh
dag = MDagPath()
sel = MSelectionList()
sel.add(mesh)
sel.getDagPath(0, dag) 

# we then include a MSelection with the dag and its component(MObject)
sel2 = MSelectionList()
sel2.add(dag, compObj)
MGlobal.setActiveSelectionList(sel2)
Message 3 of 5

stuzzz
Collaborator
Collaborator

To Answer your questions:

  1. The MObject you pass to the MSelectionList is a component type. MSelectionList::add expect a MObject of kMesh type (since you deal with mesh)
  2. You might have a great reason for selecting vertices with the api. I'm not sure there's a better way. As you can see you can use MObject as container for a selection. If you need to deal with such objects just attach them to some class functions (i.e MFnMesh, MItGeometry) instead.

 

Message 4 of 5

ejcarroll007
Explorer
Explorer
Accepted solution

Hello stuzz,

 

Thank you for the demonstration with the Python 1.0 API, it works great. But I was looking for the 2.0 haha

 

But it did help me find out what my issue was and it's a slight difference in the MSelection.add()

 

When I was looking at the documentation, I thought I couldn't add more than 1 argument. I had missed that I could add a tuple (MDagPath, MObject))

 

So modifying it like this now works!

import maya.api.OpenMaya as omapi2

vertices = [0, 1, 2, 3, 4]

sel = omapi2.MSelectionList()
sel.add("pSphere1")
dag, mObject = sel.getComponent(0)
mfn_components = omapi2.MFnSingleIndexedComponent(mObject)
mfn_object = mfn_components.create(omapi2.MFn.kMeshVertComponent)
mfn_components.addElements(vertices)
selection_list = omapi2.MSelectionList()
selection_list.add((dag, mfn_object))
omapi2.MGlobal.setActiveSelectionList(selection_list)

 

Thanks for the help!

 

-Eric

Message 5 of 5

stuzzz
Collaborator
Collaborator
I'm glad it helped