Maya API: MSelectionList::getSelectionStrings() - Getting a flattened list?

Question about the MSelectionList object in Maya API.
I see that the getSelectionStrings() function returns selection on the format of pCube1.vtx[0:7] instead of a flattened list which you can get via maya.cmds.ls(flatten=True). Is there any smart way around this?

Problem I have with this is that you can’t really loop thru a component selection (without getting the DAG-path and running a mesh iterator on the entire mesh - which will go through ALL the components if I am not mistaken?) I want an easy way of storing partial component selections and the ability to loop thru these at a later stage.

I tried doing maya.cmds.ls(selectionList.getSelectionStrings(), flatten=True) but ran into some performance problems (100k vertices) where it would lag for several seconds. I also tried making my own flattened string list using regular expressions (which was 5x faster but still slow). It feels like I’m fighting this from the wrong end here.

Okay so I found this:

Code from the blog post:

sel = OpenMaya.MGlobal.getActiveSelectionList()
__, vertices = sel.getComponent(0)
fn_vertices = OpenMaya.MFnSingleIndexedComponent(vertices)
vertex_indices = fn_vertices.getElements()

So getComponent(index) will return the DAG path and an MObject of the components - in this case vertices.
Then you can create a function set for retrieving the indices.

PROBLEM is multi-selections. The MObject that getComponent(index) return only holds data about the FIRST component type you selected. Running apiTypeStr() on it will return “kMeshPolygonComponent”, “kMeshVertComponent”, “kMeshVtxFaceComponent” - whatever thing that was first selected. This is not what I expected. :frowning:

Ok problem solved.
MSelectionList is limited. But MItSelectionList can be used for iterating over multiple component types.

1 Like