//-----------------------------------------------------------------------------
// Figures out the material path name from the maya shading group
//-----------------------------------------------------------------------------
MString ValveMaya::GetMaterialPath(
	const MObject &shadingGroupObj,
	MObject *pFileObj,
	MObject *pPlace2dTextureObj,
	MObject *pVmtObj,
	bool *pbTransparent,
	MString *pDebugWhy )
{
	MString materialPath( "debug/debugEmpty" );

	const MObject surfaceShaderObj( FindInputNode( shadingGroupObj, "surfaceShader" ) );
	if ( surfaceShaderObj.isNull() )
	{
		if ( pDebugWhy )
		{
			*pDebugWhy = MString( "Can't find surfaceShader node from shadingGroup: No input to " ) +
				MFnDependencyNode( shadingGroupObj ).name() + ".surfaceShader";
		}

		return materialPath;
	}

	if ( MFnDependencyNode( surfaceShaderObj ).typeName() == "vsVmt" )
	{
		MPlug vmtP = MFnDependencyNode( surfaceShaderObj ).findPlug( "vmtPath" );
		if ( !vmtP.isNull() )
		{
			vmtP.getValue( materialPath );
			return materialPath;
		}
	}


	const MObject fileObj( FindInputNodeOfType( surfaceShaderObj, "file", "color" ) );
	if ( fileObj.isNull() )
	{
		if ( pDebugWhy )
		{
			*pDebugWhy = MString( "Can't find file texture node from surfaceShader: No input to " ) +
				MFnDependencyNode( surfaceShaderObj ).name() + ".color";
		}

		return materialPath;
	}

	if ( pFileObj )
	{
		*pFileObj = fileObj;
	}

	if ( pbTransparent )
	{
		const MObject transObj( FindInputNodeOfType( surfaceShaderObj, "file", "transparency" ) );
		if ( fileObj == transObj )
		{
			*pbTransparent = true;
		}
	}

	if ( pPlace2dTextureObj )
	{
		MObject place2dTextureObj( FindInputNodeOfType( fileObj, "place2dTexture", "uvCoord" ) );
		if ( !place2dTextureObj.isNull() )
		{
			const MPlug uvCoordP( MFnDependencyNode( place2dTextureObj ).findPlug( "uvCoord" ) );
			if ( !( uvCoordP.isNull() || uvCoordP.isConnected() || uvCoordP.isLocked() ) )
			{
				*pPlace2dTextureObj = place2dTextureObj;
			}
		}
	}

	// Check to see if a vsVmtToTex node is driving the filename
	const MObject vsVmtToTexObj = FindInputNodeOfType( fileObj, "vsVmtToTex" );
	if ( !vsVmtToTexObj.isNull() )
	{
		if ( pVmtObj )
		{
			*pVmtObj = vsVmtToTexObj;
		}

		const MPlug materialPathP = MFnDependencyNode( vsVmtToTexObj ).findPlug( "materialPath" );
		if ( !materialPathP.isNull() )
		{
			materialPathP.getValue( materialPath );
			if ( materialPath.length() )
			{
				return materialPath;
			}
		}
	}

	// Otherwise do path munging to figure out 
	MString fileTexture;
	int fLen( 0 );

	const MFnDependencyNode fileFn( fileObj );
	const MPlug fileP( fileFn.findPlug( "fileTextureName" ) );
	fileP.getValue( fileTexture );
	fLen = fileTexture.length();
	if ( fLen == 0 )
	{
		if ( pDebugWhy )
		{
			*pDebugWhy = MString( "No texture filename specified on file texture node: " ) +
				MFnDependencyNode( fileObj ).name() + ".fileTextureName is empty";
		}

		return materialPath;
	}

	MStringArray path;
	const uint pLen( SplitPath( StripExtension( fileTexture ), path ) );

	if ( pLen == 0 )
		return fileTexture.asChar();

	materialPath = path[ pLen - 1 ];
	// Make the path into a relative path
	for ( int i = pLen - 2; i >= 0; --i )
	{
		if ( i > 1 && ( !stricmp( path[ i - 1 ].asChar(), "game" ) || !stricmp( path[ i - 1 ].asChar(), "content" ) ) )
			break;

		if ( !stricmp( path[ i ].asChar(), "materials" ) || !stricmp( path[ i ].asChar(), "materialsrc" ) )
			break;

		materialPath = path[ i ] + "/" + materialPath;
	}

	return materialPath;
}
示例#2
0
//
// Write the 'connectAttr' commands for all of a node's incoming
// connections.
//
void maTranslator::writeNodeConnections(fstream& f, const MObject& node)
{
	MFnDependencyNode	nodeFn(node);
	MPlugArray			plugs;

	nodeFn.getConnections(plugs);

	unsigned int		numBrokenConns = fBrokenConnSrcs.length();
	unsigned int		numPlugs = plugs.length();
	unsigned int		i;

	for (i = 0; i < numPlugs; i++)
	{
		//
		// We only care about connections where we are the destination.
		//
		MPlug		destPlug = plugs[i];
		MPlugArray	srcPlug;

		destPlug.connectedTo(srcPlug, true, false);

		if (srcPlug.length() > 0)
		{
			MObject				srcNode = srcPlug[0].node();
			MFnDependencyNode	srcNodeFn(srcNode);

			//
			// Don't write the connection if the source is not writable...
			//
			if (!srcNodeFn.canBeWritten()) continue;

			//
			// or the connection was made in a referenced file...
			//
			if (destPlug.isFromReferencedFile()) continue;

			//
			// or the plug is procedural...
			//
			if (destPlug.isProcedural()) continue;

			//
			// or it is a connection between a default node and a shared
			// node (because those will get set up automatically).
			//
			if (srcNodeFn.isDefaultNode() && nodeFn.isShared()) continue;

			f << "connectAttr \"";

			//
			// Default nodes get a colon at the start of their names.
			//
			if (srcNodeFn.isDefaultNode()) f << ":";

			f << srcPlug[0].partialName(true).asChar()
			  << "\" \"";

			if (nodeFn.isDefaultNode()) f << ":";

			f << destPlug.partialName(true).asChar()
			  << "\"";

			//
			// If the src plug is also one from which a broken
			// connection originated, then add the "-rd/referenceDest" flag
			// to the command.  That will help Maya to better adjust if the
			// referenced file has changed the next time it is loaded.
			//
			if (srcNodeFn.isFromReferencedFile())
			{
				unsigned int j;

				for (j = 0; j < numBrokenConns; j++)
				{
					if (fBrokenConnSrcs[j] == srcPlug[0])
					{
						f << " -rd \""
						  << fBrokenConnDests[j].partialName(true).asChar()
						  << "\"";

						break;
					}
				}
			}

			//
			// If the plug is locked, then add a "-l/lock" flag to the
			// command.
			//
			if (destPlug.isLocked()) f << " -l on";

			//
			// If the destination attribute is a multi for which index
			// does not matter, then we must add the "-na/nextAvailable"
			// flag to the command.
			//
			MObject			attr = destPlug.attribute();
			MFnAttribute	attrFn(attr);

			if (!attrFn.indexMatters()) f << " -na";

			f << ";" << endl;
		}
	}
}