Maya Training

Introduction to Hard Surface Modeling

Maya Training

Interiors and Furniture Vol 1 & 2 - Modeling & Cloth

Maya Training

Shading, Lighting and Rendering the Bedroom in MR

Maya Training

Robot volume one - Modeling with animation in mind

Maya Training

Spach-Alspaugh House the complete courseware

Maya Training

Burt The Cartoon Dinosaur Vol 01 - Modeling

New Maya Training

Robot Volume 02 - Hybrid Rigging
You are here > Home > SimplyMaya Community Forums
Loading

Welcome to Simply Maya

Please Sign in or Sign up for an account

Member Login

Lost your password?

Not a member yet? Sign Up!

Old 23-09-2007, 07:25 AM   #1
Eelco
Registered User
 
Eelco's Avatar
 
Join Date: Oct 2004
Location: In my dreams
Posts: 152
Thanks: 0
Thanked 3 Times in 3 Posts
Question MEL script to test and set shader transparency

Hey all,

to save time, I need a shortcut to toggle the transparency of the default shader. So I want to create a small MEL script to test and set shader transparency. But I just don't know enough to get it done.
It should go shomething like this:

double3 $d3Transparency;
if ( $d3Transparency != <<0,0,0>> )
setAttr "lambert1.transparency" -type double3 0 0 0; // make opaque
else
setAttr "lambert1.transparency" -type double3 0.5 0.5 0.5; // make transparent
Anybody who can help me with this?
__________________
Portfolio: http://www.sharecg.com/pf/Dieko
Eelco is offline   Reply With Quote
Old 24-09-2007, 02:05 AM   #2
t1ck135
Registered User
 
t1ck135's Avatar
 
Join Date: May 2004
Location: UK
Posts: 1,991
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Nice challenge
You're definitely on the right track there.

Dont know if you've got Maya 8.5+ but this little python script I've knocked up changes the transparency for the currently selected object's material.
To test it just create a new scene, add a sphere or similar, open the script editor and copy the following code in and run it:

Code:
def toggleTrans():
    # import and alias the relevant python libraries (only needed once in userSetup.py)
    # python libraries
    import maya.cmds as mc
    # get the currently selected object
    curSel = mc.ls(selection=True, dagObjects=True)
    # get the selections shading engine node
    curShader = mc.listConnections(curSel, source=True, type='shadingEngine')
    # get the current material node attached to it - note, this may not work on complex shaders and would need to be tested
    curMaterial = mc.listConnections(curShader, source=True, destination=False)
    # go through the returned nodes
    for item in curMaterial:
        # discard the transform node
        if mc.nodeType(item) != 'transform':
            # get the current transparency setting (quick hack here is just the red channel)
            curVal = mc.getAttr(item+'.transparency')
            # if its greater than zero then set it to zero (remove transparency)
            if curVal[0][0] > 0.0:
                mc.setAttr(item+'.transparency', 0.0, 0.0, 0.0, type='double3')
            # if its already zero then add some transparency
            else:
                mc.setAttr(item+'.transparency', 0.5, 0.5, 0.5, type='double3')
Now whenever you run the following command in the script editor it will change the transparency for the selected objects material:

Code:
toggleTrans()
You can also run it through Mel like so:

Code:
python("toggleTrans()");
I dont know how to add shelf items yet but if I can find some example code or something then I'll set it up
Oh and it might be helpful to convert it to Mel so that you can play with it (or in case you cant run python).
__________________
Examples of bTraffic - a traffic animation tool for Maya
bFlocking - a tool for Maya 8.5+ to generate flocking and swarming behaviours
Jan/Feb Challenge 2007 Entry and W.I.P
May/Jun Challenge 2006 Entry and W.I.P
Mar/Apr Challenge 2006 Entry and W.I.P
Jan/Feb Challenge 2006 Entry and W.I.P
Nov/Dec Challenge 2005 Entry and W.I.P
Sep/Oct Challenge 2005 Entry and W.I.P
Jul/Aug Challenge 2005 Entry
www.flash-fx.net

Last edited by t1ck135 : 24-09-2007 at 02:19 AM.
t1ck135 is offline   Reply With Quote
Old 24-09-2007, 02:30 AM   #3
t1ck135
Registered User
 
t1ck135's Avatar
 
Join Date: May 2004
Location: UK
Posts: 1,991
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ah, figured the shelf thing. Just type the 'toggleTrans() bit into the script editor, highlight it all and middle mouse drag onto your custom shelf. A window will popup asking if it is mel or python (choose python) and then you have the control all ready to use

Si
__________________
Examples of bTraffic - a traffic animation tool for Maya
bFlocking - a tool for Maya 8.5+ to generate flocking and swarming behaviours
Jan/Feb Challenge 2007 Entry and W.I.P
May/Jun Challenge 2006 Entry and W.I.P
Mar/Apr Challenge 2006 Entry and W.I.P
Jan/Feb Challenge 2006 Entry and W.I.P
Nov/Dec Challenge 2005 Entry and W.I.P
Sep/Oct Challenge 2005 Entry and W.I.P
Jul/Aug Challenge 2005 Entry
www.flash-fx.net
t1ck135 is offline   Reply With Quote
Old 24-09-2007, 06:00 AM   #4
Eelco
Registered User
 
Eelco's Avatar
 
Join Date: Oct 2004
Location: In my dreams
Posts: 152
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Thanks, but I'm working in 8.0. Besides I know even less of Python. I'd better stick to MEL for the time being.
I'm making game models and only use the defaul texture in the modeling process. A simple script which I can easily remember is sufficient.

I basically want to know what data type to use to store the transparency I get from getAttr "Lambert1.transparency". Vextor? And how to use it in a conditional expression.

vector $transparencyValue = `getAttr "lambert1.transparency"`; // result <<0, 0, 0>>
if ( $transparencyValue != ??? ) // this is my problem area
__________________
Portfolio: http://www.sharecg.com/pf/Dieko
Eelco is offline   Reply With Quote
Old 24-09-2007, 01:23 PM   #5
t1ck135
Registered User
 
t1ck135's Avatar
 
Join Date: May 2004
Location: UK
Posts: 1,991
Thanks: 0
Thanked 0 Times in 0 Posts
Default

this should do the trick
You need to declare it as a float but as it consists of multiple values you need to add them into an array

float $anArray[] = `getAttr lambert1.transparency`;
print $anArray[0];
print $anArray[1];
print $anArray[2];

Si
__________________
Examples of bTraffic - a traffic animation tool for Maya
bFlocking - a tool for Maya 8.5+ to generate flocking and swarming behaviours
Jan/Feb Challenge 2007 Entry and W.I.P
May/Jun Challenge 2006 Entry and W.I.P
Mar/Apr Challenge 2006 Entry and W.I.P
Jan/Feb Challenge 2006 Entry and W.I.P
Nov/Dec Challenge 2005 Entry and W.I.P
Sep/Oct Challenge 2005 Entry and W.I.P
Jul/Aug Challenge 2005 Entry
www.flash-fx.net
t1ck135 is offline   Reply With Quote
Old 24-09-2007, 07:12 PM   #6
Eelco
Registered User
 
Eelco's Avatar
 
Join Date: Oct 2004
Location: In my dreams
Posts: 152
Thanks: 0
Thanked 3 Times in 3 Posts
Default

That would require 3 seperate conditional expressions.
The transparency is a triple float. Just like a vector.
I tried it again and it seems to be working now. Must have mistyped something i guess.

But it works and it's sitting nicely on my shelf now.

vector $vTransparency = `getAttr "lambert1.transparency"`;

if ($vTransparency == <<0,0,0>> ){
setAttr "lambert1.transparency" -type double3 0.5 0.5 0.5 ;
print "\nSet transparent\n";
}else{
setAttr "lambert1.transparency" -type double3 0 0 0 ;
print "\nSet opaque\n";
}
Thanks for thinking along.
__________________
Portfolio: http://www.sharecg.com/pf/Dieko

Last edited by Eelco : 24-09-2007 at 07:16 PM.
Eelco is offline   Reply With Quote
Old 25-09-2007, 02:07 AM   #7
t1ck135
Registered User
 
t1ck135's Avatar
 
Join Date: May 2004
Location: UK
Posts: 1,991
Thanks: 0
Thanked 0 Times in 0 Posts
Default

cool


glad you got it working
you could always evaluate the first value and take the rest as expected

Si
__________________
Examples of bTraffic - a traffic animation tool for Maya
bFlocking - a tool for Maya 8.5+ to generate flocking and swarming behaviours
Jan/Feb Challenge 2007 Entry and W.I.P
May/Jun Challenge 2006 Entry and W.I.P
Mar/Apr Challenge 2006 Entry and W.I.P
Jan/Feb Challenge 2006 Entry and W.I.P
Nov/Dec Challenge 2005 Entry and W.I.P
Sep/Oct Challenge 2005 Entry and W.I.P
Jul/Aug Challenge 2005 Entry
www.flash-fx.net
t1ck135 is offline   Reply With Quote
Old 30-09-2007, 04:17 PM   #8
Funky Bunnies
Subscriber
 
Funky Bunnies's Avatar
 
Join Date: Mar 2005
Location: Illinois, USA
Posts: 250
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I always just use 'displaySurface' for this effect as it doesnt rely on the default shader
__________________
www.funkybunnies3d.com
Funky Bunnies is offline   Reply With Quote
Old 01-10-2007, 02:53 AM   #9
t1ck135
Registered User
 
t1ck135's Avatar
 
Join Date: May 2004
Location: UK
Posts: 1,991
Thanks: 0
Thanked 0 Times in 0 Posts
Default

lol, thats a useful shortcut way of doing it funky
Didnt even know it was there (as I'm still fairly new to Maya's programming capabilities).

I can easily add that in python like below:
Python:
Code:
# 1) open the script editor and copy the code below into it
def toggleXray():
    if mc.displaySurface(query=True, xRay=True) == [1]:
        mc.displaySurface(xRay=False)
    else:
        mc.displaySurface(xRay=True)


# 2) Now copy this into the script editor, highlight it and middle drag to your shelf.
# When asked what type of code it is choose Python. You should now have a toggle for xray on the selected objects
toggleXray()
N.B, If anyone wants to use it you'll need to add it to a file and include it to run it whenever you use maya.

How do you go about it in Mel?
I tried with the following code and variations but it doesnt seem to like using attribute values as variable comparisions?
Mel:
Code:
int $xRayVar = `displaySurface -q -xRay`;
if ($xRayVar) {
    displaySurface -xRay false;
} else {
    displaySurface -xRay true;
};
Si
__________________
Examples of bTraffic - a traffic animation tool for Maya
bFlocking - a tool for Maya 8.5+ to generate flocking and swarming behaviours
Jan/Feb Challenge 2007 Entry and W.I.P
May/Jun Challenge 2006 Entry and W.I.P
Mar/Apr Challenge 2006 Entry and W.I.P
Jan/Feb Challenge 2006 Entry and W.I.P
Nov/Dec Challenge 2005 Entry and W.I.P
Sep/Oct Challenge 2005 Entry and W.I.P
Jul/Aug Challenge 2005 Entry
www.flash-fx.net

Last edited by t1ck135 : 01-10-2007 at 02:55 AM.
t1ck135 is offline   Reply With Quote
Old 01-10-2007, 03:10 AM   #10
t1ck135
Registered User
 
t1ck135's Avatar
 
Join Date: May 2004
Location: UK
Posts: 1,991
Thanks: 0
Thanked 0 Times in 0 Posts
Default

got it working by converting the attribute to a vector but I'm guessing this is more of a lucky hack than anything else?

Code:
vector $xRayVar3 = `displaySurface -q -xRay`;
if ($xRayVar3) {
    displaySurface -xRay false;
} else {
    displaySurface -xRay true;
};
Si
__________________
Examples of bTraffic - a traffic animation tool for Maya
bFlocking - a tool for Maya 8.5+ to generate flocking and swarming behaviours
Jan/Feb Challenge 2007 Entry and W.I.P
May/Jun Challenge 2006 Entry and W.I.P
Mar/Apr Challenge 2006 Entry and W.I.P
Jan/Feb Challenge 2006 Entry and W.I.P
Nov/Dec Challenge 2005 Entry and W.I.P
Sep/Oct Challenge 2005 Entry and W.I.P
Jul/Aug Challenge 2005 Entry
www.flash-fx.net
t1ck135 is offline   Reply With Quote
Old 01-10-2007, 07:30 AM   #11
Funky Bunnies
Subscriber
 
Funky Bunnies's Avatar
 
Join Date: Mar 2005
Location: Illinois, USA
Posts: 250
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It's because the command is trying to return an array. Also, you may want to go through all selected objects.

I wrote this a while back and probably stole bits and pieces from other people :p
just put this in your user commands in the hotkey editor.
Code:
{
//XRAY TOGGLE
string $cheese[] = `ls -sl -o -s -dag -l`;

for ($object in $cheese)
   {
        // lame test to see if it's geometry
	if ( `objExists ($object + ".doubleSided")`)
	{
		int $toggle[] = `displaySurface -q -xRay $object`;
		if ($toggle[0])
		   displaySurface -xRay 0 $object;
		else
	 	   displaySurface -xRay 1 $object;
	}
   }
}
__________________
www.funkybunnies3d.com
Funky Bunnies is offline   Reply With Quote
Old 01-10-2007, 12:54 PM   #12
t1ck135
Registered User
 
t1ck135's Avatar
 
Join Date: May 2004
Location: UK
Posts: 1,991
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hehe, already there funky. Immediately after posting the last message I added in the object checking to my script

Cheers for pointing out that it came back as an array, I'm more used to mel's structure than python's but you have to mess with type casting and checking them more than what I'm used to (PHP related background where you dont need to declare types).

I've now got two shelf icons, one for toggling the selected object(s) xray and another for doing the same with a selected objects material (e.g, selecting an object with a lambert3 material and making all objects that share the material semi-transparent). They might be useful for modelling at some point
Back onto particle scripting...

Si
__________________
Examples of bTraffic - a traffic animation tool for Maya
bFlocking - a tool for Maya 8.5+ to generate flocking and swarming behaviours
Jan/Feb Challenge 2007 Entry and W.I.P
May/Jun Challenge 2006 Entry and W.I.P
Mar/Apr Challenge 2006 Entry and W.I.P
Jan/Feb Challenge 2006 Entry and W.I.P
Nov/Dec Challenge 2005 Entry and W.I.P
Sep/Oct Challenge 2005 Entry and W.I.P
Jul/Aug Challenge 2005 Entry
www.flash-fx.net
t1ck135 is offline   Reply With Quote

A little bit about who we are
Links you might find useful
Catch up with SimplyMaya
SimplyMaya specialises in Maya tutorials. We offer over 1,000 individual Maya training videos, ranging from basic Maya tutorials through to intermediate Maya tutorials. Our tutorials are created by instructors with industry experience and are designed to get you up and running in Maya quickly without making it seem like hard work.

Copyright © 1999-2015 SimplyMaya - vBulletin® Copyright © 2000-2015, Jelsoft Enterprises Ltd.