Beer glass scene creation
This course contains a little bit of everything with modeling, UVing, texturing and dynamics in Maya, as well as compositing multilayered EXR's in Photoshop.
# 1 13-01-2003 , 03:26 AM
dannyngan's Avatar
Registered User
Join Date: Dec 2002
Location: Seattle, WA
Posts: 1,154

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.


Danny Ngan
Animator | Amaze Entertainment
my website | my blog | my job

Last edited by dannyngan; 13-01-2003 at 03:40 AM.
# 2 13-01-2003 , 05:37 AM
dannyngan's Avatar
Registered User
Join Date: Dec 2002
Location: Seattle, WA
Posts: 1,154
Nevermind, I found it.


Danny Ngan
Animator | Amaze Entertainment
my website | my blog | my job
# 3 13-01-2003 , 05:51 AM
mark_wilkins's Avatar
Registered User
Join Date: Jan 2003
Posts: 161
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 R. Wilkins
author of MEL Scripting for Maya Animators
www.melscripting.com
# 4 13-01-2003 , 05:52 AM
mark_wilkins's Avatar
Registered User
Join Date: Jan 2003
Posts: 161
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


Mark R. Wilkins
author of MEL Scripting for Maya Animators
www.melscripting.com
# 5 13-01-2003 , 06:05 AM
Darkware's Avatar
Subscriber
Join Date: Oct 2002
Location: USA
Posts: 1,172
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.


Last edited by Darkware; 13-01-2003 at 06:13 AM.
# 6 13-01-2003 , 06:09 AM
mark_wilkins's Avatar
Registered User
Join Date: Jan 2003
Posts: 161
Sure... however I thought Danny was asking how to do it in a MEL script. :-)

-- Mark


Mark R. Wilkins
author of MEL Scripting for Maya Animators
www.melscripting.com
# 7 13-01-2003 , 06:10 AM
mark_wilkins's Avatar
Registered User
Join Date: Jan 2003
Posts: 161
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


Mark R. Wilkins
author of MEL Scripting for Maya Animators
www.melscripting.com
# 8 13-01-2003 , 06:57 AM
dannyngan's Avatar
Registered User
Join Date: Dec 2002
Location: Seattle, WA
Posts: 1,154
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:

https://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 user added image ), 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:

user added image

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.


Danny Ngan
Animator | Amaze Entertainment
my website | my blog | my job
# 9 13-01-2003 , 11:39 AM
mumbojumbo_13's Avatar
Subscriber
Join Date: Oct 2002
Location: The Land Down Under
Posts: 1,047
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 user added image


Thanks for wasting your time reading this line.
# 10 14-01-2003 , 12:11 AM
dannyngan's Avatar
Registered User
Join Date: Dec 2002
Location: Seattle, WA
Posts: 1,154
user added image

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!


Danny Ngan
Animator | Amaze Entertainment
my website | my blog | my job
# 11 14-01-2003 , 03:22 AM
mark_wilkins's Avatar
Registered User
Join Date: Jan 2003
Posts: 161
The one doesn't necessarily follow from the other... user added image

-- Mark


Mark R. Wilkins
author of MEL Scripting for Maya Animators
www.melscripting.com
# 12 14-01-2003 , 03:58 AM
dannyngan's Avatar
Registered User
Join Date: Dec 2002
Location: Seattle, WA
Posts: 1,154
Hehe... Well, I still think you know more about MEL than I do. I've only been using it for 8 months now. user added image

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

https://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...


Danny Ngan
Animator | Amaze Entertainment
my website | my blog | my job

Last edited by dannyngan; 14-01-2003 at 04:01 AM.
Posting Rules Forum Rules
You may not post new threads | You may not post replies | You may not post attachments | You may not edit your posts | BB code is On | Smilies are On | [IMG] code is On | HTML code is Off

Similar Threads