How to use Maya-MEL command "select" in c++

828 Views Asked by At

i'd like to know how to use "select" in Maya-MEL. Question is: when i use the software select an blendshape object(here is a pair of eye in FBX format), i can select it in many ways,such as "select -r |eye|eye","select eye_blendShapes","select eyeShape","select eye|eye"... just like this

But, In my c++ program it can't work

MGlobal::executeCommand(MString("file -import -namespace \"NTemp\" -mergeNamespacesOnClash true") + "\"" + eye_file + "\"");
//MGlobal::executeCommand(MString("file -import ") + "\"" + eye_file + "\"",result);
MGlobal::executeCommand(MString("select -r eyeShape"));

i don't know how to select the eye in my c++ code, anyone can help me? Thanks a lot!

2

There are 2 best solutions below

3
On

you should use that python code

import maya.OpenMaya as om

object_name = ""
om.MGlobal.selectByName(object_name, om.MGlobal.kReplaceList)

c++ code is almost same

0
On

The API works a little differently to MEL. For a start, rather than executing the file command, prefer using the MFileIO interface instead.

In terms of selecting things in C++, umm, don't !?!

Selection is a user driven action. If you're issuing commands to modify the selection list in C++, you're doing it wrong!

  1. Are you sure you want to actually select the item? Are you actually just trying to get hold of an Object or MDagPath for the object? If it's the latter, then use MSelectionList to perform the selection, and query the result without modifying the global selection list (which will cause a display refresh, and additional undo step to be stored, and will quickly annoy your users!).
MSelectionList sl;
if(sl.add("eyeShape")) {
  MObject eyeShape;
  sl.getDependNode(0, eyeShape);

  /* do thing */
}
  1. Are you absolutely sure you want to select eyeShape? I get a little worried if I ever see someone selecting a single node in C++ (or MEL for that matter!). I'm going to hazard a guess that eyeShape is a staggeringly common name for a mesh in Maya!

a). Are you sure selecting the shape is correct? What about the DAG path? Do you want to select every instance of eyeShape, or are you concerned with a single instance?

b). Since you've just imported the file into a namespace, why are you not qualifying what you're trying to select, with the namespace you defined? If your users have a character in the scene already, with an eyeShape, your selection command will simply return that node first. Your imported shape will be returned second. result: you probably have a bug?

  1. So if you're sure that selecting eyeShape is the right thing to do, then it is possible to modify the global select list to do it. Please prefer MGlobal::selectCommand over MGlobal::select/selectByName though. It just means your users can undo the operation.

  2. If you are doing this because you are about to issue another MEL call immediately afterwards, that operates on the eyeShape (e.g. are you porting some MEL code into an MPxCommand?). Stop. Just use MEL.

"select -r |eye|eye" --> MSelectionList::add(MDagPath(), kReplaceList)
"select eye_blendShapes" --> MSelectionList::add(MString(), kAddList)
"select eyeShape" --> MSelectionList::add(MObject()/MString(), kAddList)
"select eye|eye" --> MSelectionList::add(MObject()/MString(), kAddList)