Introduction to Maya - Modeling Fundamentals Vol 1
This course will look at the fundamentals of modeling in Maya with an emphasis on creating good topology. We'll look at what makes a good model in Maya and why objects are modeled in the way they are.
# 1 23-09-2007 , 06:25 AM
Eelco's Avatar
Registered User
Join Date: Oct 2004
Location: In my dreams
Posts: 152

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?

# 2 24-09-2007 , 01:05 AM
t1ck135's Avatar
Registered User
Join Date: May 2004
Location: UK
Posts: 1,991
Nice challenge user added image
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 user added image
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 01:19 AM.
# 3 24-09-2007 , 01:30 AM
t1ck135's Avatar
Registered User
Join Date: May 2004
Location: UK
Posts: 1,991
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 user added image

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
# 4 24-09-2007 , 05:00 AM
Eelco's Avatar
Registered User
Join Date: Oct 2004
Location: In my dreams
Posts: 152
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


# 5 24-09-2007 , 12:23 PM
t1ck135's Avatar
Registered User
Join Date: May 2004
Location: UK
Posts: 1,991
this should do the trick user added image
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
# 6 24-09-2007 , 06:12 PM
Eelco's Avatar
Registered User
Join Date: Oct 2004
Location: In my dreams
Posts: 152
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. user added image


Last edited by Eelco; 24-09-2007 at 06:16 PM.
# 7 25-09-2007 , 01:07 AM
t1ck135's Avatar
Registered User
Join Date: May 2004
Location: UK
Posts: 1,991
cool user added image


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

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
# 8 30-09-2007 , 03:17 PM
Funky Bunnies's Avatar
Subscriber
Join Date: Mar 2005
Location: Illinois, USA
Posts: 250
I always just use 'displaySurface' for this effect as it doesnt rely on the default shader

# 9 01-10-2007 , 01:53 AM
t1ck135's Avatar
Registered User
Join Date: May 2004
Location: UK
Posts: 1,991
lol, thats a useful shortcut way of doing it funky user added image
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 01:55 AM.
# 10 01-10-2007 , 02:10 AM
t1ck135's Avatar
Registered User
Join Date: May 2004
Location: UK
Posts: 1,991
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
# 11 01-10-2007 , 06:30 AM
Funky Bunnies's Avatar
Subscriber
Join Date: Mar 2005
Location: Illinois, USA
Posts: 250
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;
	}
   }
}

# 12 01-10-2007 , 11:54 AM
t1ck135's Avatar
Registered User
Join Date: May 2004
Location: UK
Posts: 1,991
Hehe, already there funky. Immediately after posting the last message I added in the object checking to my script user added image

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 user added image
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
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