Simply Maya User Community

Simply Maya User Community (https://simplymaya.com/forum/index.php)
-   Programming (https://simplymaya.com/forum/forumdisplay.php?f=32)
-   -   find object's material via mel (https://simplymaya.com/forum/showthread.php?t=3181)

dannyngan 13-01-2003 03:26 AM

find object's material via mel
 
Is there a way to determine an object's material via MEL? Even better, is there a way to find the material that is assigned to a specific face? I haven't been able to figure this one out.

Thanks.

dannyngan 13-01-2003 05:37 AM

Nevermind, I found it.

mark_wilkins 13-01-2003 05:51 AM

Well, it can get a little complicated. It's not that bad to find the shading group (or groups) to which an object or its components belong. For example, if you have an object called nurbsSphere1 with a shape node called nurbsSphereShape1, this will show you the connected shading groups:

listConnections -type shadingEngine nurbsSphereShape1

Once you have the shading group names, you can figure out from that what materials are assigned, by listing objects that are connected to the shading group's surfaceShader, displacementShader, and volumeShader attributes. Usually the surface shader is the one you're most concerned with, so let's just look at that:

For this code snippet, I'll assume that you've selected the object you're curious about AND that it only has one material assigned to the entire object:


proc string getSurfaceShader (string $objName) {

// This assumes you want ONLY the shape node at the selected level of the
// hierarchy, for the object whose transform name is passed in as the argument.

string $myShapeNode[] = `listRelatives -children -shapes $objName`;
string $mySGs[] = `listConnections -type shadingEngine $myShapeNode[0]`;
string $surfaceShader[] = `listConnections ($mySGs[0] + ".surfaceShader")`;
return $surfaceShader[0];
}


So, to see the surface shader on nurbsSphere1, type

getSurfaceShader("nurbsSphere1");

This isn't a general solution because different components of an object (such as individual polys in a mesh object) can have different shading groups assigned, and this would only give you the shader for the first one. Also, if for some reason you have multiple shapes under one transform (weird, but could happen) this will only give you one of their surface shaders.

-- Mark

mark_wilkins 13-01-2003 05:52 AM

actually Danny is there a better way than what I just did? What did you find? I'm curious! :-)

(now that I think about it there's probably a command that's a shortcut for this but I'm not sure I know it off the top of my head...)

-- Mark

Darkware 13-01-2003 06:05 AM

I always open hypershade and right click a shader and choose "select objects with materials." If you just have a shader assigned to a specific face, it will only select the face. If you're trying to find the material assigned to a face, however, you can run into problems. If I have a cube with two shaders on it, when I right click and choose material attributes, I will only get one of them. Thus, I only know what one of the shaders is and I cannot modify it. Simply put, organization can really pay off in situations like this; especially if you're working with a particularlly large scene. Here's the secret - NAME YOUR SHADERS! Window>Rendering Editors>Hypershade. You see all the shaders you have in your scene. Every time you create one, come to hypershade, right-click it, and rename it. That way, when you're looking for a texture, you just have to recognize the name and double-click it to bring up the attributes window.

mark_wilkins 13-01-2003 06:09 AM

Sure... however I thought Danny was asking how to do it in a MEL script. :-)

-- Mark

mark_wilkins 13-01-2003 06:10 AM

BTW I wasn't able to figure out in a brief look how to find materials associated with specific faces in a MEL script, but I'll look again when I have a little time.

-- Mark

dannyngan 13-01-2003 06:57 AM

Thanks, Darkware, but I'm looking for a way to do this in MEL. I'll explain below.

Mark, thanks for the suggestion. AFAIK, that's the one of the simplest ways to get the shader from an object. But, as you said, that's only for shapes with a single shader. I am looking for way to do this at the face level for shapes with multiple shaders applied.

This is what I've found so far:

http://www.ewertb.com/maya/mel/mel_a43.html

Doesn't look too complex, just a bit convoluted. I'll dig into it some more tomorrow night when I have the brain power to do it.

For those of you who are curious (other than Mark ;) ), I'm writing a series of scripts to help me with my UV mapping process. My method of UV unwrapping is very similar to Craig Daughtry's (see his VIP tutorial) and is actually something I was doing in 3ds max long before I moved over to Maya. What I do is create several shaders with random colors and then visually plan out my UV shells by applying different shaders to face selections. Once I have all my faces assigned with shaders, I'll go through and planar project best fit on each of the "patches".

Here's what my mesh looks like when I'm assigning materials:

http://www.dannyngan.com/temp/danny-uv1.jpg

At the moment, I have a script that will automatically apply the planar projections and layout the resulting shells, but that script depends on specific shader names (I have another script that generates a user-defined number of shaders that are named correctly). I'd like to make the script a little more flexible by not relying on specific shader names. Basically, I want to generate an array of all shaders that are applied to a particular shape and then use that list of shaders to run my script.

Another thing I'd like to do is create a HUD element that displays the shader of the currently selected face. This is mostly to help me in my shell planning phase. It starts getting a bit crazy when I have 50+ shaders applied to my model, and I have to change shader assignments to just one face.

Anyhow, this is the moderately long answer for why I started this thread. Any help you guys can offer would be greatly appreciated.

mumbojumbo_13 13-01-2003 11:39 AM

go danny no one touchs this forum for ages and then DANNY and now we all flock to it nice to see it being used, now i learning mel yay :D

dannyngan 14-01-2003 12:11 AM

:)

I think it's even better that Mark is on the forums now. He definitely knows a heck of a lot more about MEL than I do. I mean, he wrote a book on it!

mark_wilkins 14-01-2003 03:22 AM

The one doesn't necessarily follow from the other... :)

-- Mark

dannyngan 14-01-2003 03:58 AM

Hehe... Well, I still think you know more about MEL than I do. I've only been using it for 8 months now. :)

Anyhow, I did some more searching around the web and found this on Highend3D:

http://www.highend3d.com/maya/mel/?s...=rendering#948

The script seems to work fine as long as I strip out the section that tries to open a HyperShade window (line begins "if ($foundShaders)"). It's probably the way the author has the script written -- I get an error everytime the script hits that conditional statement. The main part of the script, however, returns the .outColor attribute name for any shader that is assigned to the selected faces (works for multiple faces and shaders). As it is right now, it'll work for what I need. I'll probably end up tweaking the script to do what I want. Ah, the joys of MEL...


All times are GMT. The time now is 07:13 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Simply Maya 2018