Maya C++ API -


#1

Could anyone help me with accessing attributes such as radius of a sphere or color of the material?

I was trying to do this with findPlug but nothing.

Thanks


#2

Could you give us a couple examples of what you tried and didn’t work, so we can point you in the right direction based on your existing code?

You could use MSelectionList from a string to get an MPlug.

thanks
-Dan


#3

The radius of a sphere wouldn’t be on the transform or on the shape, it would be on the generator connected to the shape.
The colour of a material, same thing, you’d need to traverse to the right node and know the right attribute name (and some attributes might display as one in the AE, but in reality be separate and separately named or compounded in the actual raw plugs).

It sounds like you might want to first find what you want to do in a more agile environment, like using OM2 in Python, to understand how to operate on the graph and by what parameters first. Your problem doesn’t sound C++ specific or API related, as much as about the notion of how to traverse to data in Maya and where that data is.


#4

I tried this:

  MStatus stat;
	MObject cmp;
	MSelectionList slist;
	MGlobal::getActiveSelectionList(slist);
	slist.getDagPath(0, mdagPath, cmp);
	int i;
	
	mdagPath.extendToShape();
	
	int instanceNum = 0;
	if (mdagPath.isInstanced())
		instanceNum = mdagPath.instanceNumber();
	
        MFnMesh fnMesh(mdagPath);
	MObjectArray sets;
	MObjectArray comps;
	if (!fnMesh.getConnectedSetsAndMembers(instanceNum, sets, comps, true))
		cerr << "ERROR: MFnMesh::getConnectedSetsAndMembers
";
	
	for (  i=0; i<sets.length(); i++ ) {
		
		MObject set = sets[i];
		MObject comp = comps[i];
		
		MFnSet fnSet( set, &stat );
		if (stat == MS::kFailure) {
            cerr << "ERROR: MFnSet::MFnSet
";
            continue;
        }
		
      
		MItMeshPolygon piter(mdagPath, comp, &stat);

			

		MObject shaderNode = findShader(set);
		if (shaderNode == MObject::kNullObj)
			continue;
		
		MPlug materialPlug = MFnDependencyNode(shaderNode).findPlug("color", true, &stat);
		if (stat == MS::kFailure)
			continue;
		
		MItDependencyGraph dgIt(materialPlug,
						   MFn::kMaterial,
						   MItDependencyGraph::kUpstream, 
						   MItDependencyGraph::kBreadthFirst,
						   MItDependencyGraph::kNodeLevel, 
						   &stat);
		
		if (stat == MS::kFailure)
			continue;
		
		dgIt.disablePruningOnFilter();

		MObject materialNode =  dgIt.rootNode();
	
		 MPlug plug = MFnDependencyNode(materialNode).findPlug("color");
		 MString col;
		 plug.getValue(col);
		

That findShader:

MObject findShader( MObject& setNode )

{
	MFnDependencyNode fnNode(setNode);
	MPlug shaderPlug = fnNode.findPlug("surfaceShader");
			
	if (!shaderPlug.isNull()) {			
		MPlugArray connectedPlugs;
		bool asSrc = false;
		bool asDst = true;
		shaderPlug.connectedTo( connectedPlugs, asDst, asSrc );

		if (connectedPlugs.length() != 1)
			cerr << "Error getting shader
";
		else 
			return connectedPlugs[0].node();
	}			
	
	return MObject::kNullObj;
}

I am not sure about if I am using write attribute name or maybe I am doing this completely wrong.


#5

I think the problem is that you are trying to get the value of a compound attribute as a string.
You need to get the numChildren()of the plug, and iterate through them as .child(i).

This is python API, but you should get the idea.

import maya.OpenMaya as om

def toApiPlug( *args ):
    if len( args ) == 1:
    	attribute = args[0]
    
    selectList = om.MSelectionList()
    selectList.add(attribute)
    plug = om.MPlug()
    selectList.getPlug(0, plug)
    return plug


color = toApiPlug('lambert1.color')
num = color.numChildren()
for i in range(0,num):
    color.child(i).asDouble()

#6

Thank you very much!