Simply Maya User Community

Simply Maya User Community (https://simplymaya.com/forum/index.php)
-   Programming (https://simplymaya.com/forum/forumdisplay.php?f=32)
-   -   MEL script to test and set shader transparency (https://simplymaya.com/forum/showthread.php?t=27579)

Eelco 23-09-2007 06:25 AM

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:

Quote:

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?

t1ck135 24-09-2007 01:05 AM

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).

t1ck135 24-09-2007 01:30 AM

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

Eelco 24-09-2007 05:00 AM

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.

Quote:

vector $transparencyValue = `getAttr "lambert1.transparency"`; // result <<0, 0, 0>>
if ( $transparencyValue != ??? ) // this is my problem area

t1ck135 24-09-2007 12:23 PM

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

Eelco 24-09-2007 06:12 PM

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.

Quote:

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. :)

t1ck135 25-09-2007 01:07 AM

cool :)


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

Si

Funky Bunnies 30-09-2007 03:17 PM

I always just use 'displaySurface' for this effect as it doesnt rely on the default shader

t1ck135 01-10-2007 01:53 AM

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

t1ck135 01-10-2007 02:10 AM

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

Funky Bunnies 01-10-2007 06:30 AM

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;
        }
  }
}


t1ck135 01-10-2007 11:54 AM

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


All times are GMT. The time now is 08:38 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Simply Maya 2018