Home Technical Talk

Mel Script: Toggle Camera Based Selection

polycounter lvl 18
Offline / Send Message
Illusions polycounter lvl 18
I wrote a little Mel script to handle toggling Camera based selection in marquee mode on or off. Anyone know what I need to add or do so that the script can just be called by using something like cbsToggle in the command line?

Also is there any way to keep the code tag from removing certain portions of code?
cbsToggle wrote:

/*
November 11th, 2009
=======================================================
Name: cbsToggle.mel
Version: 1.00
Purpose:
This script will toggle the Camera based selection
checkbox for Marquee based selections in
Common Selection Options
=======================================================
*/

//Queries whether or not Camera based selection
//is ON or OFF, and sets cbsToggle to that value
int $cbsToggle = `selectPref -q -useDepth`;
if ($cbsToggle == 1)
{
//Turns Camera based selection OFF if it is already ON
selectPref -useDepth false;
print ("Camera based selection has been turned OFF\n");
}
else
{
//Turns Camera based selection ON if it is already OFF
selectPref -useDepth true;
print ("Camera based selection has been turned ON\n");
}

Replies

  • Whargoul
    Options
    Offline / Send Message
    Whargoul polycounter lvl 18
    Here's a hint to make things that toggle into really simple code:

    toggleCmd -flag (1-`toggleCmd -q -flag`);

    So in your case:

    selectPref -useDepth (1-`selectPref -q -useDepth`);

    To make it callable from the command line, you need to make it a global procedure and load it. Making it a procedure is simple:

    global proc YourProcName()
    {
    code here
    }

    Usually you can put it in a .mel file, and source that file. That file can contain many procedures,. Say you put it in myStuff.mel (bad name, but you get the idea) - then you would load the script by:

    source myStuff.mel;

    Now you can call your proc when you need. If you want - combine those two into a single command, and map it toa hotkey or shelf item.

    source myStuff.mel;YourProcName;

    Re-sourcing it isn't slow so no worries there. As a bonus, if you have a single proc inside a script, and it's named the same as the file - calling it directly works. So if you have a myDoStuff procedure inside a myDoStuff .mel file, calling myDoStuff will auto-source it and run the proc in one step.


    I have a large script for toggling many things since it's so common, I have a "CRtoggleStuff" command, and it takes an argument of what to toggle, and I just hard-code several of them in there. Something like this, off the top of my head:
    global proc CRtoggleStuff (string $ mode )
    {
         if ($ mode=="cameraSelect")
                selectPref -useDepth (1-`selectPref -q -useDepth`);
         else if ($ mode=="thingy")
               thingy -foo (1-`thingy-q -foo`);
         else
               error ("Unknown mode  n");
    
    }
    
    Works great, and I can put complex switches in there, for things that cycle through 3 or more, or commands that are difficult to toggle. Keeps them all in one nice place.

    Not sure why the code tags eat some stuff - I lost the $variable stuff in mine unless I separated the $ and the name and the \n I had to put a space in as well. So take that space out :)
  • Illusions
    Options
    Offline / Send Message
    Illusions polycounter lvl 18
    I feel rather silly now that it seems my huge mess 'o code can be reduced to a single line. Thanks for the write-up though, its very helpful.
  • MoP
    Options
    Offline / Send Message
    MoP polycounter lvl 18
    Whargoul wrote: »
    if you have a single proc inside a script, and it's named the same as the file - calling it directly works. So if you have a myDoStuff procedure inside a myDoStuff .mel file, calling myDoStuff will auto-source it and run the proc in one step.

    Yep, this is what I do all the time - it's how they do it with all the internal Maya scripts anyway... you just have your "master" proc as a global proc in there with the same name as the file, then all the "helper" procs not declared globally but in the same file - that way you can have a nice self-contained script in a single file that needs no "source" call, yet it can still have custom helper procs for itself (one you'll see a lot in Maya default files is "assembleCmd" for calling eval stuff - loads of separate files have different methods for assembleCmd but since they're local to each file, you never get any conflicts).

    Thanks for the tips, Whargoul.
  • jukke
    Options
    Offline / Send Message
    Hey could some of you guys tell me how Toggle between "Marquee" and "drag" in the Select Tool setting

    thank you
Sign In or Register to comment.