Maya API and textures.

Goragoth

Regular
I'm writing a simple Maya (6.0) exporter using the Maya API (following a tutorial on GameDev.net) and I've managed to extract vertices and polygons all ok so but I'm really struggling getting the texture filenames (and there isn't much on the web about the API). Anyone here have experience with the Maya API?

My exact problem: I've found the material that I want and then I've found the color plug for that but when I ask it if its a fileTexture it says no (printing out its API type gave me kAttribute3Float). Now I'm sure that the material is connected to a fileTexture node in Maya so why does it give me this???

Relevant code snippet:
Code:
      MFnDependencyNode fnDependNode( srcNode );
		MPlug plug = fnDependNode.findPlug( MString( "color" ), &stat );
		if( stat  )
		{
			MPlugArray cc;
			plug.connectedTo( cc, true , false );

			if ( cc.length() == 1 )
			{
				// As a destination there can be only one source.
				MObject src = cc[0];

				// Is it a file texture?
				if( src.hasFn( MFn::kFileTexture ) )
				{
					MFnDependencyNode fnFile( src );
					MPlug ftnPlug = fnFile.findPlug( MString( "fileTextureName" ), &stat );
					if( stat )
					{
						MString fileTextureName;
						ftnPlug.getValue( fileTextureName );
						cout << "Texture: " << fileTextureName.asChar() << endl;
					}
					else
					{
						cout << "Couldn't connect to tex_name plug to get filetexture." << endl;
					}
				}
srcNode is a the Lambert shader that I want.
"if( src.hasFn( MFn::kFileTexture ) )" is what fails although it really shouldn't.

There are a few things left that I can try to get this thing to work but maybe someone here has worked with the Maya API and has some idea what is going on (or not).
 
Nevermind, I ended up finding something in the API samples that worked. Boy, the Maya API is complicated but at least they do provide lots of samples and documentation. :D
 
This might be of help later, if you find that the API isn't working as expected. Artists will do some strange things.

Code:
MStatus getFileTexturePath( const MObject &shaderNode, const MString &COLOR_ATTR, // The material node.
						   MString &filePath ) 
{
	/// begin texture extraction
	using MS::kSuccess;
	using MS::kFailure;

	printf("Getting texture for %s\n", MFnDependencyNode(shaderNode).name().asChar());

	if (shaderNode == MObject::kNullObj)
		return kFailure;;
	MStatus status;

	//outColor for bake sets, color for regular textures
	//MPlug colorPlug = MFnDependencyNode(shaderNode).findPlug("color", &status);
	MPlug colorPlug = MFnDependencyNode(shaderNode).findPlug("outColor", &status);
	if (status == kFailure) {
		printf("%s: can't find plug named 'outColor' for shader node %s\n", __FUNCTION__, MFnDependencyNode(shaderNode).name().asChar() );
		return kFailure;
	}

	MItDependencyGraph dgIt(colorPlug, MFn::kFileTexture,
		MItDependencyGraph::kUpstream, 
		MItDependencyGraph::kBreadthFirst,
		MItDependencyGraph::kNodeLevel, 
		&status);

	if (status == kFailure) {
		printf("%s: can't filter upstream for kFileTexture on plug named 'outColor' for shader node %s\n", __FUNCTION__, MFnDependencyNode(shaderNode).name().asChar() );
		return kFailure;
	}

	dgIt.disablePruningOnFilter();

	// If no texture file node was found, just continue.
	if (!dgIt.isDone()) {
		// Print out the texture node name and texture file that it references.
		MObject textureNode = dgIt.thisNode();
		MPlug filenamePlug = MFnDependencyNode(textureNode).findPlug("fileTextureName");
		MString textureName;
		filenamePlug.getValue(textureName);
		
		filePath = textureName;
		//MFnSet fnSet(set);
		//printf("Set: %s\n", fnSet.name().asChar() );
		printf("Texture Node Name: %s\n", MFnDependencyNode(textureNode).name().asChar() );
		//printf("Texture File Name: %s\n", textureName.asChar() );
		return kSuccess;
	}

	printf("%s: nothing in the filter upstream for kFileTexture on plug named 'outColor' for shader node %s\n", __FUNCTION__, MFnDependencyNode(shaderNode).name().asChar() );
	// end texture extraction
	return kFailure;
}
 
Yeah that looks similar to what I'm doing now. The original code was from the Bryan Ewert Maya API tutorials online and I still have no idea why it didn't work but the code from the API sample is all good. Thanks for that code though, I'll take a look at it (and I may have other questions later, good to know someone around here has knowledge of the Maya API).
 
Thanks Popnfresh, those links should help a lot. A quick google didn't give me any helpful links and the Maya API is pretty umm... comprehensive. :D
 
Back
Top