for example,I have 3 cameras In Maya,I want to get all of them,How Can I Get them In my C++ Code.Very Thx.
How To Get All Cameras In Maya API.
If you have their names you can just add them to an MSelectionList by name and reference them that way:
// Example camera names
MStringArray cameraNames;
cameraNames.append("persp");
cameraNames.append("front");
MSelectionList selList;
int i;
for( i = 0; i < cameraNames.length(); i++ )
{
selList.add( cameraNames[i] );
}
MDagPathArray cameraPaths;
for( i = 0; i < selList.length(); i++ )
{
MDagPath path;
selList.getDagPath( i, path );
if( path.isValid() )
{
cameraPaths.append( path );
}
}
Alternately if you don’t know the name and just want to iterate through all cameras, you can use MItDependencyNodes to iterate through all nodes of a given type:
MItDependencyNodes dgIter( MFn::kCamera );
MObjectArray cameraObjs;
while( !dgIter.isDone() )
{
cameraObjs.append( dgIter.thisNode() );
dgIter.next();
}
thx for your Answer,But When I Use the Secend Way,I Found there are four cameras,but
I don’t put anything in my scene.Deeply Thx for your help.
The second way finds ALL cameras (including the default cameras: persp, front, top, side), which is why you find 4 without adding anything in your scene. If you want to filter those out, then you’ll want to filter them by name using the exact names above.
Via MEL:
createNode camera;
Or MEL via API:
MString cmd = "createNode camera;";
MGlobal::executeCommand( cmd )
Strictly API:
MDGModifier mod;
mod.createNode( "camera" );
mod.doIt();
// You can save a reference to this MDGModifier if you want to support undo. Otherwise you can discard it.
Hello!
Could you please help me with cameras too? I’ve read this thread and tried something myself too, but it still doesn’t work the way I want.
I want to get all the cameras’ names (pers, top, side, font). I wrote this and the result is:
perspShape/|pers|perspShapetopShape/|top|topShapefrontShape/|front|frontShape**sideShape/|side|sideShape
But I want just persp, top, front, side.
Could you help me with this please??
MItDag it(MItDag::kDepthFirst, MFn::kCamera, &stat);
while(!it.isDone()){
MFnCamera f(it.item());
fprintf(fp,"*%s/%s*", f.name().asChar(), f.fullPathName().asChar());
it.next();
}
I copied your code with MObjectArray, but I don’t know how to get a single object’s name out of there. 
To get the node name only:
Did you try to use partialPathName() instead of fullPathName()?
Or simply split the string by “|” and use the last item.
Concerning object names form a MObjectArray. You can simply get the name with the help of a dependency node:
MObjectArray moa;
MFnDependencyNode depFn(moa[0]);
depFn.name();
Hi,
first of all thank you for your help.
When I use all of these things I get “perspShape”, but I need only “persp”. I hoped that it would work if I change MFn::kCamera to MFn::kTransform it would work but it doesn’t (I thought that “persp” node is a transformation node because there is transformation information in it)
MItDependencyNodes dgIter( MFn::kCamera );
MObjectArray cameraObjs;
while( !dgIter.isDone() )
{
cameraObjs.append( dgIter.thisNode() );
dgIter.next();
}
Have a look at the post from Keilun above. You need to go up in the hierarchy what means you need a dag path object:
selList.getDagPath( i, path );
Then you can go up by:
path.pop();
The path object now contains the transform node of the camera and a partialPathName() should give you the “persp” instead of “perspShape”.