Home Technical Talk

Mel Script: Toggle buttom (On/Off)

polycounter lvl 2
Offline / Send Message
Fran127 polycounter lvl 2
Hello Everyone

I have this two pieces of codes for turn on and off an arm in my rig:

////

select -r Jack:jack_ac_lf_elbowIK Jack:jack_ac_lf_index0 Jack:jack_ac_lf_index2 Jack:jack_ac_lf_index1 Jack:jack_ac_lf_index3 Jack:jack_ac_lf_middle1 Jack:jack_ac_lf_middle0 Jack:jack_ac_lf_middle2 Jack:jack_ac_lf_middle3 Jack:jack_ac_lf_ring3 Jack:jack_ac_lf_ring2 Jack:jack_ac_lf_ring1 Jack:jack_ac_lf_ring0 Jack:jack_ac_lf_pinkey0 Jack:jack_ac_lf_thumb1 Jack:jack_ac_lf_pinkey3 Jack:jack_ac_lf_pinkey2 Jack:jack_ac_lf_pinkey1 Jack:jack_ac_lf_thumb3 Jack:jack_ac_lf_thumb2 Jack:jack_ac_lf_elbow_bend Jack:jack_ac_lf_arm_bend Jack:jack_ac_lf_forearm_bend Jack:jack_ac_lf_clavicle Jack:jack_ac_lf_handIK ;

select -tgl Jack:jack_ac_lf_clavicle_offset ;

select -tgl Jack:jack_sk_lf_elbow ;

select -tgl Jack:jack_ac_lf_handIK_PH ;

select -tgl Jack:jack_lf_arm ;

select -tgl Pinta_labios_group ;

setAttr "lambert2_toggle_visibility_L.transparency" -type double3 1 1 1 ;

HideSelectedObjects;

select -cl ;

////

select -r Jack:jack_ac_rt_middle0 Jack:jack_ac_rt_index0 Jack:jack_ac_rt_index1 Jack:jack_ac_rt_index3 Jack:jack_ac_rt_index2 Jack:jack_ac_rt_middle2 Jack:jack_ac_rt_middle1 Jack:jack_ac_rt_middle3 Jack:jack_ac_rt_ring0 Jack:jack_ac_rt_ring3 Jack:jack_ac_rt_ring2 Jack:jack_ac_rt_ring1 Jack:jack_ac_rt_pinkey1 Jack:jack_ac_rt_pinkey0 Jack:jack_ac_rt_pinkey2 Jack:jack_ac_rt_elbow_bend Jack:jack_ac_rt_arm_bend Jack:jack_ac_rt_elbowIK Jack:jack_ac_rt_forearm_bend Jack:jack_ac_rt_thumb1 Jack:jack_ac_rt_pinkey3 Jack:jack_ac_rt_thumb3 Jack:jack_ac_rt_thumb2 Jack:jack_ac_rt_armPole Jack:jack_ac_rt_handIK Jack:jack_ac_rt_clavicle ;

select -tgl Jack:jack_ac_rt_clavicle_offset ;

select -tgl Jack:jack_sk_rt_elbow ;

select -tgl Jack:jack_ac_rt_handIK_PH ;

select -tgl Jack:jack_rt_arm ;

setAttr "lambert2_toggle_visibility_R.transparency" -type double3 0 0 0 ;

showHidden -a;

select -cl ;

///


I want to do just one buttom instead of always click in one and then in the another one. I found some solutions but if the code is just one simple line, by example this:


string $sel[] =`ls -sl`;    

    for ($thisObj in $sel){ 

        int $displaylevel[] = `displaySmoothness -query -polygonObject $thisObj`;

        if ($displaylevel[0] == 1) 

        {

            //Go to 3

            displaySmoothness -polygonObject 3;

        }

        if ($displaylevel[0] == 3) 

        {

            //Go to 1

            displaySmoothness -polygonObject 1;

        }

        

    }


I just want to do a better workflow for turn on and off the limbs in every rig i working, in the most simple way


Thanks in advanced




Replies

  • TTools
    Offline / Send Message
    TTools polycounter lvl 4
    I think you're doing too much work to achieve what you really want.  You've hard-coded your joint names into your Mel, which leaves your script very local to your current application.  Check out the code below which will toggle visibility on and off of descendant joints for any rig you are working on.

    // I've heavily commented the code below so you can see what I'm doing.




    //Select the topmost joints of your arms, and run this.

    string $selected[] = `ls -sl -long`; //Get the current selection.
    for ($i=0;$i<size($selected);$i++) // Start a loop so we can execute independently on each joint we selected.
    {
        select $selected[$i]; //Select the first joint in our selection of topmost joints.
        string $relatives[] = `listRelatives -fullPath -allDescendents $selected[$i]`; //List all descendant relatives of the selected joint.
        select -add $relatives; //Add all the descendant joints of the current joint to our current selection.  Now we have everything we want selected.
        if (`getAttr ($selected[$i] + ".visibility")` == 1) // If the visibility of our topmost joint is true, hide all of the joints in our selection.
        {
        HideSelectedObjects;
        }
        else // If the visibility of the topmost joint is false, show all of the joints in our selection.
        {
          ShowSelectedObjects;
        }
        select -cl; //We clear the selection here in preparation of the next loop cycle.
    }
    select $selected; // Re-select the first joints the user had selected.  It is often better to do a re-select than a clear, as the user typically wants that object selected again.


    I see that you're also attempting to set the visibility for a shader's transparency.  Not sure if that's really what your aiming for, but if so, you should be able to do that as needed in the If and Else statements respectively.

    Hope this helps! :)
  • Fran127
    Offline / Send Message
    Fran127 polycounter lvl 2
    TTools said:
    I think you're doing too much work to achieve what you really want.  You've hard-coded your joint names into your Mel, which leaves your script very local to your current application.  Check out the code below which will toggle visibility on and off of descendant joints for any rig you are working on.

    // I've heavily commented the code below so you can see what I'm doing.




    //Select the topmost joints of your arms, and run this.

    string $selected[] = `ls -sl -long`; //Get the current selection.
    for ($i=0;$i<size($selected);$i++) // Start a loop so we can execute independently on each joint we selected.
    {
        select $selected[$i]; //Select the first joint in our selection of topmost joints.
        string $relatives[] = `listRelatives -fullPath -allDescendents $selected[$i]`; //List all descendant relatives of the selected joint.
        select -add $relatives; //Add all the descendant joints of the current joint to our current selection.  Now we have everything we want selected.
        if (`getAttr ($selected[$i] + ".visibility")` == 1) // If the visibility of our topmost joint is true, hide all of the joints in our selection.
        {
        HideSelectedObjects;
        }
        else // If the visibility of the topmost joint is false, show all of the joints in our selection.
        {
          ShowSelectedObjects;
        }
        select -cl; //We clear the selection here in preparation of the next loop cycle.
    }
    select $selected; // Re-select the first joints the user had selected.  It is often better to do a re-select than a clear, as the user typically wants that object selected again.


    I see that you're also attempting to set the visibility for a shader's transparency.  Not sure if that's really what your aiming for, but if so, you should be able to do that as needed in the If and Else statements respectively.

    Hope this helps! :)
    Thank you so much!! 
  • Fran127
    Offline / Send Message
    Fran127 polycounter lvl 2
    TTools said:
    I think you're doing too much work to achieve what you really want.  You've hard-coded your joint names into your Mel, which leaves your script very local to your current application.  Check out the code below which will toggle visibility on and off of descendant joints for any rig you are working on.

    // I've heavily commented the code below so you can see what I'm doing.




    //Select the topmost joints of your arms, and run this.

    string $selected[] = `ls -sl -long`; //Get the current selection.
    for ($i=0;$i<size($selected);$i++) // Start a loop so we can execute independently on each joint we selected.
    {
        select $selected[$i]; //Select the first joint in our selection of topmost joints.
        string $relatives[] = `listRelatives -fullPath -allDescendents $selected[$i]`; //List all descendant relatives of the selected joint.
        select -add $relatives; //Add all the descendant joints of the current joint to our current selection.  Now we have everything we want selected.
        if (`getAttr ($selected[$i] + ".visibility")` == 1) // If the visibility of our topmost joint is true, hide all of the joints in our selection.
        {
        HideSelectedObjects;
        }
        else // If the visibility of the topmost joint is false, show all of the joints in our selection.
        {
          ShowSelectedObjects;
        }
        select -cl; //We clear the selection here in preparation of the next loop cycle.
    }
    select $selected; // Re-select the first joints the user had selected.  It is often better to do a re-select than a clear, as the user typically wants that object selected again.


    I see that you're also attempting to set the visibility for a shader's transparency.  Not sure if that's really what your aiming for, but if so, you should be able to do that as needed in the If and Else statements respectively.

    Hope this helps! :)
    Thank you so much!! 
Sign In or Register to comment.