SimplyMaya Forums

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

Home / forum / Animation / Mel Scripts & Programming / find object's material via mel
Register FAQ Members List Calendar Mark Forums Read

New Thread
 
Thread Tools
Old 13-01-2003, 02:26 AM   #1
dannyngan
Registered User
 
dannyngan's Avatar
 
Join Date: Dec 2002
Location: Seattle, WA
Posts: 1,154
Thanks: 0
Thanked 1 Time in 1 Post
Default 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 02:40 AM.
dannyngan is offline   Reply With Quote
Old 13-01-2003, 04:37 AM   #2
dannyngan
Registered User
 
dannyngan's Avatar
 
Join Date: Dec 2002
Location: Seattle, WA
Posts: 1,154
Thanks: 0
Thanked 1 Time in 1 Post
Default

Nevermind, I found it.
__________________
Danny Ngan
Animator | Amaze Entertainment
my website | my blog | my job
dannyngan is offline   Reply With Quote
Old 13-01-2003, 04:51 AM   #3
mark_wilkins
Registered User
 
mark_wilkins's Avatar
 
Join Date: Jan 2003
Posts: 161
Thanks: 0
Thanked 1 Time in 1 Post
Default

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
mark_wilkins is offline   Reply With Quote
Old 13-01-2003, 04:52 AM   #4
mark_wilkins
Registered User
 
mark_wilkins's Avatar
 
Join Date: Jan 2003
Posts: 161
Thanks: 0
Thanked 1 Time in 1 Post
Default

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
mark_wilkins is offline   Reply With Quote
Old 13-01-2003, 05:05 AM   #5
Darkware
Subscriber
 
Darkware's Avatar
 
Join Date: Oct 2002
Location: USA
Posts: 1,172
Thanks: 0
Thanked 4 Times in 4 Posts
Default

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 05:13 AM.
Darkware is offline   Reply With Quote
Old 13-01-2003, 05:09 AM   #6
mark_wilkins
Registered User
 
mark_wilkins's Avatar
 
Join Date: Jan 2003
Posts: 161
Thanks: 0
Thanked 1 Time in 1 Post
Default

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
mark_wilkins is offline   Reply With Quote
Old 13-01-2003, 05:10 AM   #7
mark_wilkins
Registered User
 
mark_wilkins's Avatar
 
Join Date: Jan 2003
Posts: 161
Thanks: 0
Thanked 1 Time in 1 Post
Default

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
mark_wilkins is offline   Reply With Quote
Old 13-01-2003, 05:57 AM   #8
dannyngan
Registered User
 
dannyngan's Avatar
 
Join Date: Dec 2002
Location: Seattle, WA
Posts: 1,154
Thanks: 0
Thanked 1 Time in 1 Post
Default

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
dannyngan is offline   Reply With Quote
Old 13-01-2003, 10:39 AM   #9
mumbojumbo_13
Subscriber
 
mumbojumbo_13's Avatar
 
Join Date: Oct 2002
Location: The Land Down Under
Posts: 1,047
Thanks: 0
Thanked 6 Times in 6 Posts
Default

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.
mumbojumbo_13 is offline   Reply With Quote
Old 13-01-2003, 11:11 PM   #10
dannyngan
Registered User
 
dannyngan's Avatar
 
Join Date: Dec 2002
Location: Seattle, WA
Posts: 1,154
Thanks: 0
Thanked 1 Time in 1 Post
Default



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
dannyngan is offline   Reply With Quote
Old 14-01-2003, 02:22 AM   #11
mark_wilkins
Registered User
 
mark_wilkins's Avatar
 
Join Date: Jan 2003
Posts: 161
Thanks: 0
Thanked 1 Time in 1 Post
Default

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

-- Mark
__________________
Mark R. Wilkins
author of MEL Scripting for Maya Animators
www.melscripting.com
mark_wilkins is offline   Reply With Quote
Old 14-01-2003, 02:58 AM   #12
dannyngan
Registered User
 
dannyngan's Avatar
 
Join Date: Dec 2002
Location: Seattle, WA
Posts: 1,154
Thanks: 0
Thanked 1 Time in 1 Post
Default

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 03:01 AM.
dannyngan is offline   Reply With Quote
New Thread

« Previous Thread | Next Thread »

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem solving many dynamic objects in a pile? rohiniduggal Dynamics & Special Effects 3 14-01-2011 06:26 PM
MEL headache! happymat27 Members Lounge 5 05-12-2010 08:43 AM
Strange Problem - Unable to change attributes for multiple objects anthonysw3 Technical Issues 4 04-12-2010 10:56 PM
MEL headache!! happymat27 Mel Scripts & Programming 1 03-12-2010 03:41 PM

1 Month

$34

Month-to-month billing
$34/month
Unlimited access to all videos
Project files to download
Easy to cancel at anytime
 
 

3 months

$87

Billing every 3 months
$29/month
Unlimited access to all videos
Project files to download
Easy to cancel at anytime
Save $5 per month or $15 in total compared to monthly billing with this package.

12 months

$288

Billing every 12 months
$24/month
Unlimited access to all videos
Project files to download
Easy to cancel at anytime
Save $10 per month or $120 in total compared to monthly billing with this package.

Your plan will automatically renew. You can cancel at any time via your PayPal account to avoid being charged again.

You can view the FAQ on our video plans here. You can contact us directly here. By clicking one of the buttons above, you agree to our Terms and Privacy Policy.