PDA

View Full Version : Get material from face


Marcel
12-08-2003, 02:37 PM
I'm using the 'get SG from face' procedure from Brian Ewert (http://www.ewertb.com/maya/mel/mel_a43.html) Somehow when I try this on a object with only one material or with the standard Lambert it returns nothing. When an object has multiple materials it works fine.

Here is the code that works from the script editor (when one face is selected), can somebody see the problem?

string $currentFace[] = `ls -sl -fl`;
string $facetSG = "";

// Get array of all Shading Groups
string $shadingGroups[] = `ls -type shadingEngine`;

for ( $shadingGroup in $shadingGroups )
{
// If this facet is a member of the shading set for this
// Shading Group, tag this as the facet's shader

if ( `sets -isMember $shadingGroup $currentFace` )
{
$facetSG = $shadingGroup;
break;
}
}
print ("SG:" + $facetSG);

sbp
12-08-2003, 08:06 PM
Hi Marcel,

The set for the shading group won't contain component faces if it's a single material applied to an entire object. It only has the object as a member.

Marcel
12-08-2003, 08:43 PM
Hmmm, that makes a lot of sense. So if this procedure returns nothing I should query the SG of the whole object? I'll try that out.

Thanks a lot, this would have been something I'd never would have figured out by myself!

Marcel
12-09-2003, 10:01 AM
Tried it out today, works like a charm...thanks again!

BarbaPappa
02-04-2004, 05:04 PM
I'm trying to write a similar function. Would it be possible for you to post the finished working script?

Thanks

Marcel
02-05-2004, 08:58 AM
I am not allowed to post the whole script, because it's something I've written in my bosses time for the most part. I can post some snippets of code that show how it works.

Here is some code that takes the first face of a selection of faces and prints the Shading Group of that face:


string $facetSG = "";
string $sceneSelection[] = `ls -sl -fl`;
string $currentFace = $sceneSelection[0];

// Get array of all Shading Groups
string $shadingGroups[] = `ls -type shadingEngine`;
for ( $shadingGroup in $shadingGroups )
{
// If this facet is a member of the shading set for this
// Shading Group, tag this as the facet's shader
if ( `sets -isMember $shadingGroup $currentFace` )
{
$facetSG = $shadingGroup;
break;
}
}

print $facetSG;


If this code returns nothing, that means that there is only one material applied to the object. To get the material on the object in that case you can use this procedure (pass the object to the procedure and it will return a list of shadinggroups):


// ////////////////////////////////////////////////////////////////////
// getSGsFromObject
//
// Description: Returns an array of the Shading Groups (shadingEngine
// nodes) responsible for shading the specified object node.
// original code by Brian Ewert

proc string[] getSGsFromObject( string $currentObject )
{
string $shadingEngines[];

// get shape from object
string $currentShape[] = `listRelatives -s -path $currentObject`;

if ( `objExists $currentShape[0]` )
{
string $dest[] = `listConnections -destination true -source false -plugs false -type "shadingEngine" $currentShape[0]`;

// listConnections can return duplicates within its list.
if ( size( $dest ) )
{
$shadingEngines = stringArrayRemoveDuplicates($dest);
}
}

return $shadingEngines;
}



This procedure returns the actual material name attached to the shading group:


// ////////////////////////////////////////////////////////////////////
// getMaterialFromSG
//
// Description: Returns the Material node feeding the '.surfaceShader'
// attribute of the specified Shading Group node (shadingEngine).
// by Brian Ewert

proc string getMaterialFromSG( string $SG )
{
string $material = "";
if ( "shadingEngine" == `nodeType $SG` && `connectionInfo -id ( $SG + ".surfaceShader" )` )
{
string $buffer[];
$material = `connectionInfo -sourceFromDestination ( $SG + ".surfaceShader" )`;
// strip everything after the dot ('object.blabla' becomes 'object')
tokenize $material "." $buffer;
$material = $buffer[0];
}
return $material;
}


Let me know if this works for you. I'm rather busy the next two weeks but I'm sure I can help you out if you are stuck :beer:

BarbaPappa
02-05-2004, 09:04 AM
Marcel, thanks a lot. :beer:

I understand that you can't post all of it. Got the same issue here. But these codesnippets should get me started... thanks alot.

CGTalk Moderation
01-16-2006, 10:00 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.