View Full Version : how to get what attribute is selected in channel box on right

11 November 2011, 01:29 PM

I want to build my own toggle switch for lock selected and key slected or unlock , to do that i just simply want to click a button to toggle in shelf so that if i select any attribute of the object i should be able to lock or unlock just with a click rather than doing right cliock and lock selected..please help...

11 November 2011, 07:56 PM
MEL? or python?
This has come up and been answered several times already on this forum, did you try a search? either way, you can do it in MEL like so:
string $selCBAttr[] = `selectedChannelBoxAttributes`;:nathaN

11 November 2011, 08:59 AM
I hate this but what i hav forget in concatinating

string $selection[] = `ls -selection`;

string $selCBAttr[] = `selectedChannelBoxAttributes`;

string $newStr = $selection +"."+ $selCBAttr;

print $newStr;

11 November 2011, 11:39 AM
$selection and $selCBAttr are arrays, so you can not use them like that in a string. Since there may be more than one object and channel selected you need to do something like loop through them.

You could do the first one in each array like this
string $newStr = $selection[0] +"."+ $selCBAttr[0]

or loop through the objects and channels
string $newStr;
for($s in $selection)
for($a in $selCBAttr) {
$newStr = ($s + "." + $a);
print ($newStr + "\n");


11 November 2011, 12:42 PM
so now it got

string $selection[] = `ls -selection`;

string $selCBAttr[] = `selectedChannelBoxAttributes`;
string $newStr = "\"" + $selection[0] +"."+ $selCBAttr[0] + "\"";

setAttr -lock true $newStr ;

// Error: No object matches name: "pCube1.rz" //

even when pCube1 's RotateZ is selected for instance...

11 November 2011, 03:46 PM
this is quite confusing if you don't fully appreciate the difference between variables and explicit strings, and how either is passed into functions.
you are creating a string variable that CONTAINS quotes (") and then passing it into the setAttr function, rather than using the quotes when denoting an explicit string.

If you want to pass strings as variables directly into functions do NOT add ". if you want to pass them in using eval or string catenation you DO add ".

// using a bare string variable:
string $string = $selection[0] + "." + $selCBAttr[0];
setAttr -lock true $string; // this works fine

// using a quoted string, must be eval-ed (directly or implicitely)
string $quotedString = "\"" + $selection[0] +"."+ $selCBAttr[0] + "\"";
eval ("setAttr -lock true " + $quotedString ); // this also works fine

// using NO variables at all (the preferred method)
setAttr -lock true ( $selection[0] + "." + $selCBAttr[0] ); // don't forget catenation braces
Hope that is some help.

11 November 2011, 05:19 PM
if i have multiple attributes selected I am trying to lock all of them one by one like this

string $selection[] = `ls -selection`;

string $selCBAttr[] = `selectedChannelBoxAttributes`;

for ($lstA in $selCBAttr)
print ($lstA + "\n");
int $i=0;

setAttr -lock true ( $selection[$i] + "." + $selCBAttr[$i] );
print ("\n " + $i + "\n") ;
print ("\n " + $i + "\n") ;


i guess i need to understand for looo better, but even if i try to do it like this

for ($i = 0; $i++)


setAttr -lock true ( $selection[$i] + "." + $selCBAttr[$i] );


I get error though as fas i remember i can skip the middle option of the for loop...

11 November 2011, 07:48 PM
for ($i = 0; $i++)

for() requires three operands, as in:

for( $i=0; $i<size(myArray); ++$i)

<* Wes *>

11 November 2011, 10:54 AM
i have a shelfButton for channel control that i've been using for quite some time. the command for editing selected channels looks like this:

global proc setSelectedCBattributes(int $lock, int $key, int $vis)
channelBox -e -exe ("setAttr -lock " + $lock + " -keyable " + $key + " -channelBox " + $vis + " \"#P.#A\";") true mainChannelBox;
// e.g. to lock and make unkeyable but keep visible :

hope that's what you were looking for ...

you could also add RMB menus to the shelfButton for the different options (lock/unlock, keyable/unkeyable, etc.) ...

for details on channelBox command & explanation of -execute flag see:

CGTalk Moderation
11 November 2011, 10:54 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.