SimplyMaya Forums

Welcome to the forum, feel free to ask questions here.


SM Forums / Maya Questions / Programming / find object's material via mel

Programming MEL Scripts, Expressions & Programming Related Topics

Join the conversation. Reply to post
 
Thread Tools
13-01-2003, 03:26 AM   #1
dannyngan
Registered User
 
dannyngan's Avatar
 
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.
  Reply with quote
13-01-2003, 05:37 AM   #2
dannyngan
Registered User
 
dannyngan's Avatar
 
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
  Reply with quote
13-01-2003, 05:51 AM   #3
mark_wilkins
Registered User
 
mark_wilkins's Avatar
 
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
  Reply with quote
13-01-2003, 05:52 AM   #4
mark_wilkins
Registered User
 
mark_wilkins's Avatar
 
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
  Reply with quote
13-01-2003, 06:05 AM   #5
Darkware
Subscriber
 
Darkware's Avatar
 
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.
__________________
www.Darkware3D.com

Last edited by Darkware; 13-01-2003 at 06:13 AM.
  Reply with quote
13-01-2003, 06:09 AM   #6
mark_wilkins
Registered User
 
mark_wilkins's Avatar
 
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
  Reply with quote
13-01-2003, 06:10 AM   #7
mark_wilkins
Registered User
 
mark_wilkins's Avatar
 
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
  Reply with quote
13-01-2003, 06:57 AM   #8
dannyngan
Registered User
 
dannyngan's Avatar
 
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:

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:



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
  Reply with quote
13-01-2003, 11:39 AM   #9
mumbojumbo_13
Subscriber
 
mumbojumbo_13's Avatar
 
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
__________________
Thanks for wasting your time reading this line.
  Reply with quote
14-01-2003, 12:11 AM   #10
dannyngan
Registered User
 
dannyngan's Avatar
 
Join Date: Dec 2002
Location: Seattle, WA
Posts: 1,154


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
  Reply with quote
14-01-2003, 03:22 AM   #11
mark_wilkins
Registered User
 
mark_wilkins's Avatar
 
Join Date: Jan 2003
Posts: 161
The one doesn't necessarily follow from the other...

-- Mark
__________________
Mark R. Wilkins
author of MEL Scripting for Maya Animators
www.melscripting.com
  Reply with quote
14-01-2003, 03:58 AM   #12
dannyngan
Registered User
 
dannyngan's Avatar
 
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.

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...
__________________
Danny Ngan
Animator | Amaze Entertainment
my website | my blog | my job

Last edited by dannyngan; 14-01-2003 at 04:01 AM.
  Reply with quote
Reply

Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
Selecting objects by name? timmons64 Maya Basics & Newbie Lounge 9 12-11-2018 11:52 AM
Some objects darker? (problem) Craftdragon Lighting & Rendering 1 28-04-2016 03:27 PM
objects missing from outliner Dpotato Maya Technical Issues 4 07-12-2015 04:52 PM
Objects move inconsistently when I move multiple MrSandman Maya Technical Issues 5 20-04-2015 08:13 PM
Making objects out of other objects Clod Maya Basics & Newbie Lounge 3 11-01-2004 12:25 PM