Why can't I remove an object from a material set?

I'm trying to fix that awful "bug" in Maya whereby if you extract or seperate faces from an object with multiple materials on it, the newly extracted object has connections to all the same materials, even though it may only use ONE of those materials.
This problem is causing us big problems with some of our models and combining them into a single object is actually causing Maya to crash is some cases. However, if we apply ONE material to the objects before combining it's fine (and about 4-6 minutes faster)... and there's only about 14 materials in the whole scene..?

What I want to do to fix this problem (and as it happens an older one I gave up on) is to disconnect or disassociate any object with shaders it isn't actually using... eg:

I have a mesh object (polySurfaceShape1) that consists of ONLY ONE QUAD. it has NO history or intermediates or anything else...
If I type this:

listSets -o polySurfaceShape1;

I get:

// Result: lambert2SG lambert3SG lambert4SG lambert5SG
(because the cube I extracted it from earlier had FOUR materials on it)

If I type:

listSets -o polySurfaceShape1.f[0];

I get:

// Result: lambert4SG
(which IS actually the only material on the object)

So I thought I'd do this:

sets -remove lambert2SG polySurfaceShape1;
sets -remove lambert3SG polySurfaceShape1;
sets -remove lambert5SG polySurfaceShape1;

which, needless to say does absolutely nothing.

Can anyone tell me why?, or if there's a better way to solve this problem...? bear in mind that my objects may have MORE than a single material on, so I can't just re-apply the correct material to it.. (which does fix this problem as it happens)..

:nathaN

Removing SG nodes from sets isn't enough. There are more connections involved. Look in hypergraph - shape nodes, material nodes, groupId nodes, all that is connected to shading groups and interconnected.

It's pretty messy - It takes a bit of a effort to disconnect everything properly.

I would take another path and do something like this:
- put problematic objects in a new scene
- list all materials in scene
- iterate thru list of materials checking if there are faces that have this material assigned
- if there are no such faces - delete the material and shading group node.

Something like this:

global proc deleteUnusedMaterials(){

string $mats[] = `ls -materials`;
string $m;
for( $m in $mats ){
	hyperShade -objects $m;
	string $obj[] = `ls -sl`;
	if( size ( $obj ) == 0 ){
		string $sg[] = `listConnections -d true -type "shadingEngine" $m`;
		delete $m;
		delete $sg;
	}
}

}

Note that this will still leave unused 'groupId' nodes connected to the shape node.
Script can be upgraded to handle this - when deleting SG node just check if there are incoming 'message' connections from 'groupId' nodes. If yes, just delete them ( groupId nodes ) too.

it's a bit more complicated than this, I don't actually have any unused materials in my scene. What I have is many, many objects with attributte connections to multiple lambertSGs that they are simply not using. those shading groups are being used by neighbouring objects so I don't want to touch them. I just want to automatically disconnect every object from certain lambertSGs...

if I'm not really making myself or the problem clear try this:

create a cube.
Assign a new (yellow) material to every face.
select a single quad face.
assign a new (red) material to this face.
extract and separate this face, so it becomes an entirely new, unique object.
open the hyperShade window and, with the face object selected, graph up and downstream connections.

You'll see the object is connected to BOTH the yellow AND the red material's SG.. even though it is only using the red shader.

and, if you type:
listSets -o pCubeShape1;
it returns BOTH shaders too...

This may not seem like a big deal but we have car models consisting of 150-200 separate objects using 14-15 different shaders. Many of the objects are only using 1 or 2 of these shaders but have connections to ALL of them (because of they way they've been built and "separated"). Now, when we try and combine them together again Maya has a BIG problem with these material connections and either crashes outright or takes about 6 minutes using a test script we hacked up which combines objects in pairs instead of all together.

I've just gone back to Maya after typing all this and I've figured it out. I wasn't getting the connection info I needed because I never knew about the -plug flag!

I think I've sussed out what I'm trying to do. I'll post the results of my meddling when I'm certain..

:nathaN

A shading group is a set so deleting the object form that set should do the trick!

so getting the sg name and doing

	select -noExpand $sgname;
	ls -selection;

should get all materials in the set.

and likewise

sets -remove setname objects....

should also do the trick

well yeah.. I know that, but how exactly do you delete an object from a set? especially when Maya thinks it's not even IN THAT SET..?

try the cube example from my last post. see how far you get with your set theory cos if you can do it, it's probably going to be easier and safer than what I'm currently attempting...

:nathaN

Nasty.

I created cube with 3 faces having diferent materials. Then detached one face ( with "Duplicate face" ) - new object is called 'poly1'

Now querying to which sets 'poly1' belongs:

listSets -o polyShape1;

returns all three shading groups:
// Result: lambert2SG lambert3SG lambert4SG //

But asking if 'poly1' is member of a specific set:

sets -im lambert2SG polyShape1

returns false.

However, asking if face component is member of a set returns correct result:

sets -im lambert2SG polyShape1.f[ 0 ] // returns true;

So there are definitely some unholy connections in question here.

Now I typed all this for nothing because it seems that doing 'ctrl+d' ( good ol' duplicate ) on detached object will clean up the whole mess. Duplicated object is connected only to needed sg's

eh? I tried it and my duplicates still have all the bogus connections... I've tried it with all the different duplicate options set... but still the error..?

anyway, I've written a proc that seems to do the job...

proc disconnectUnusedShaders(string $shape)
{
string $allMats[] = listSets -ets -o $shape;
string $usedMat[] = listSets("-ets","-o",(polyListComponentConversion("-tf",$shape)));
string $delMats[] = stringArrayRemove $usedMat $allMats;

string $allDCnx[] = `listConnections -d 1 -s 0 -c 1 -p 1 -type "shadingEngine" $shape`;
string $allSCnx[] = `listConnections -d 0 -s 1 -c 1 -p 1 -type "shadingEngine" $shape`;

string $allGCnx[] = `listConnections -d 0 -s 1 -c 0 -p 0 -type "groupId" $shape`;
string $idDelList[];
int $n = 0;

for ($mat in $delMats)
{
	int $matNameSize = `size($mat)`;

	// first, delete destination connections:
	for ($c=0 ; $c<size($allDCnx) ; $c++)
	{
		string $attrCheck = `substring $allDCnx[$c] 1 $matNameSize`;
		if ($attrCheck == $mat)
			disconnectAttr $allDCnx[$c-1] $allDCnx[$c];
	}

	// second, delete source connections:
	for ($c=0 ; $c<size($allSCnx) ; $c++)
	{
		string $attrCheck = `substring $allSCnx[$c] 1 $matNameSize`;
		if ($attrCheck == $mat)
			disconnectAttr $allSCnx[$c] $allSCnx[$c-1];
	}

	// lastly, delete groupIds:
	for ($groupId in $allGCnx)
	{
		string $idMsg[] = `connectionInfo -dfs ($groupId+".message")`;
		string $attrCheck = `substring $idMsg[0] 1 $matNameSize`;
		if ($attrCheck == $mat)
		{
			if (`objExists $groupId`)
				$idDelList[$n++] = $groupId;
			break;
		}
	}
}	
delete $idDelList;

}

global proc purgeObjectMaterials()
{
string $sel[] = ls -sl -l -fl -s -dag;
for ($shape in $sel)
{
disconnectUnusedShaders $shape;
}
}

it's a bit clunky and it's only a first pass so if anyone can see any mistakes, oversights or ways to improve it please let me know.
This is the first thing I've had to do with material connections and attributes and I'm still not 100% sure how they all fit together...

:nathaN

duplicate -rr;

It does the job.
Don't forget to refresh the hypergraph before checking the results

no, it doesn't do the job!?

it doesn't do anything at all (except duplicate the object) and the -rr flag doesn't actually change the command behaviour either ("This flag controls only what is returned in the output string[], and it does NOT change the behaviour of the duplicate command.")

what version of Maya you trying this on failure? I can't get duplicate to fix this at all... and I've tried it on Maya 4.5 and Maya 6.0.1

:nathaN

version 5.0

Here's exactly what I've done:
- create poly cube and assign three lamberts to three different faces.
- detach one of the faces using "Duplicate face"
- delete the history on everything and duplicate the new one-face object.
- select the duplicate, open hypergraph and go "show output connections" - shape is connected only to needed shading groups ( and default sg )

weird... you're very lucky then cos I just get the same junk.. what does listSets -ets -o dupeShape return for it then?

It lists one shader - while same comand for original lists 4 shaders.

Duplicate options were: geo type - copy, group under world. Everthing else unchecked.

If I turn on "duplicate input graph" - shaders don't get deleted on duplicate.

how odd. it sounds like Alias fixed something in Maya 5 then broke it again in Maya 6.! wouldn't be the first time I guess...

can anyone else test or confirm this..??

Ouch. This is painful to watch. I know exactly of what you speak. Fortunately, our game exporter handles it well and none of our tools trip up on it.

I am pretty sure that if you apply a single material material to the whole object, it will wipe the the bogus connections. Assuming that is correct, I would get a some arrays matching the faces and SG. Then apply the first material to the whole object to "reset" it. Then loop through any additional materials applying them back to the face lists - skipping the empty lists, of course.

Probably not the speediest solution, but it should be fine on a reasonably sized scene.

Btw, I am unable to reproduce failure's results with duplicate -rr under Maya5 myself either.

--JeffD

hmm. yes, it's never actually been a problem in any of the studios I've worked before either, but the guys modelling the cars for Formula 1 here tell me they're always crashing Maya using "combine" which I'd never heard of Maya doing before... So after looking into it I find out it is exactly this problem and this problem alone. That script I posted seems to fix the problem outright though, and cleans up everything no problem.. no more crashing...! I've integrated it into our in-house "Extract" script so hopefully the problem just won't crop up for us again...

:nathaN