PDA

View Full Version : textScrollList Help


nofxsapunk
11-04-2006, 05:10 PM
I am having a problem with a rename script I am trying to write.

The problem is that I need to be able to get the objects that I select from my scroll list to all be renamed.

Example: Say I select 4 spheres and then run my script. The script shows the 4 spheres in a textScrollList. I then select the 4 spheres in the textScrollList. Next I go to the rename textField and enter the name "ball", what i want to happen when i click the rename button is that it renames all 4 spheres "ball, ball1, ball2, ball3. but it only renames the first selected. can someone please help.

Mikademius
11-04-2006, 09:13 PM
When you click your rename button, make the proc go through a loop that renames based on selection.

nofxsapunk
11-04-2006, 10:21 PM
how do i get my loop to go through that selection?

my code is this:


global proc amRenameUI(){
window;
rowColumnLayout -height 100 -numberOfRows 5 ;
textScrollList -numberOfRows 10 -allowMultiSelection true "list";


rowColumnLayout -numberOfRows 1;
button -label "Prefix" -command "amRename 1";
textField "prefixTF";
setParent..;
rowColumnLayout -numberOfRows 1;
button -label "Rename" -command "amRename 2";
textField "renameTF";
setParent..;
rowColumnLayout -numberOfRows 1;
button -label "Suffix" -command "amRename 3";
textField "suffixTF";
setParent..;


string $objects[] = `ls -sl`;
string $object;
for ($object in $objects){
textScrollList -e -append $object list;

string $tslInfo[] = `textScrollList -query -allItems list`;
string $selAll;
for ($selAll in $tslInfo){


}

}

showWindow;

}
global proc amRename(int $renameType){
// gets information from what is currently selected

string $sel[] = `ls -sl`;
string $firstItem = $sel[0];
print $sel;

string $scroll = `textScrollList -q list`;


switch($renameType){
case 1:
//PREFIX CODE HERE
string $prefix = `textField -q -text prefixTF`;
string $newPre = `rename $firstItem ($prefix + " " + $firstItem)`;
break;

case 2:
//RENAME CODE HERE

string $rename = `textField -q -text renameTF`;
// renames object with renameTF's information
string $newName = `rename $nameIt $rename`;
break;

case 3:
//SUFFIX CODE HERE
string $suffix = `textField -q -text suffixTF`;
string $newSuf = `rename $firstItem ($firstItem + " " + $suffix)`;
break;
}
// creates a warning telling you what the objects new name is
print ("Your object is now named" + " " + $newName);

// close window
};

------------------------------------------------------------
the script isnt pretty right now, i am more concentrated on getting it to work then i will work with the layout.

here is a link to the actual mel script

http://www.HekaEnt.com/Help/renameIt.mel

Mikademius
11-04-2006, 10:32 PM
I'd guess a loop like this could work?



string $rename = `textField -q -text renameTF`;
srting $selObj[] = `ls -sl`;

for ($i = 0; $i<=(`size($selObj)`); $i++){
rename $selObj[$i] $rename;
}



I'm not sure if the $rename can stand outside the loop, don't have maya open now. Try it out and tell me how it went :)

nofxsapunk
11-04-2006, 10:52 PM
i forgot to mention that i needed it to be able to add a suffix and a prefix with this loop. so how do i change

for ($i = 0; $i<=(`size($selObj)`); $i++){
rename $selObj[$i] $rename;

to do so?

the first line of that script looks like it is what i need but the second line is where i am confused.

Buexe
11-04-2006, 11:39 PM
i forgot to mention that i needed it to be able to add a suffix and a prefix with this loop. so how do i change

for ($i = 0; $i<=(`size($selObj)`); $i++){
rename $selObj[$i] $rename;

to do so?

the first line of that script looks like it is what i need but the second line is where i am confused.


string $prefix = "somePrefix";
string $suffix = "someSuffix";
string $rename= "newName";
string $newName;

for ($i = 0; $i<=(`size($selObj)`); $i++)
{
$newName = $prefix+$rename + $suffix + i; // constructs the new name string
rename $selObj[$i] $newName ;
}

2 other things:
1. I think your loop should be rather this
for ($i = 0; $i<size($selObj); $i++)
because this will loop once on every object, yours does it one time too much
2. use "textScrollList -q -si list" to query the selected items on your textScrollList

Mikademius
11-04-2006, 11:52 PM
I guess there are several ways to do this. some require more code, but might look better. Some might have a strange way to store things etc. I'll write down what pops up in my head. You'll have to decide how you wish to strip the code for your script :)



string $ah_prefix = `textField -q -text ah_prefixTF`;
string $ah_suffix = `textField -q -text ah_suffixTF`;

string $ah_rename = `textField -q -text ah_renameTF`;

if ($ah_prefix && $ah_suffix){
// added _ between the prefix and suffix. You change it as you like :)
rename $selObj[$i] ($ah_prefix + "_" + $ah_rename + "_" + $ah_suffix);
}

else if ($ah_prefix && !$ah_suffix){
rename $selObj[$i] ($ah_prefix + "_" + $ah_rename);
}

else if (!$ah_prefix && $ah_suffix){
rename $selObj[$i] ($ah_rename + "_" + $ah_suffix);
}

//This could probably be done with a switch too.




It feels a bit clumsy to do it like this, but the theory behind it should work.
You might encounter some syntax errors here, but you'll nkow how to get them out of there ;)

nofxsapunk
11-05-2006, 12:26 AM
ok, now im getting confused, i put in the script from Buexe and im getting errors, i think i put it in the wrong place, and im not sure where to put your part of the script to try it out Mikademius. im new. im taking a class right now but i am having real troubles grasping MEL.


here is the script at the point im at now

-----------------------------------------------------------------------------------------

global proc amRenameUI(){
window;
rowColumnLayout -height 100 -numberOfRows 5 ;
textScrollList -numberOfRows 10 -allowMultiSelection true "list";


rowColumnLayout -numberOfRows 1;
button -label "Prefix" -command "amRename 1";
textField "prefixTF";
setParent..;
rowColumnLayout -numberOfRows 1;
button -label "Rename" -command "amRename 2";
textField "renameTF";
setParent..;
rowColumnLayout -numberOfRows 1;
button -label "Suffix" -command "amRename 3";
textField "suffixTF";
setParent..;


string $prefix = "somePrefix";
string $suffix = "someSuffix";
string $rename= "newName";
string $newName;

string $objects[] = `ls -sl`;
string $object;
for ($object in $objects){
textScrollList -e -append $object list;


for ($i = 0; $i<size($selObj); $i++)
$newName = $prefix+$rename + $suffix + $i; // constructs the new name string
rename $selObj[$i] $newName ;
}



}

showWindow;

}
global proc amRename(int $renameType){
// gets information from what is currently selected

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

print $sel;

string $scroll = `textScrollList -q -si list`;


switch($renameType){
case 1:
//PREFIX CODE HERE
string $prefix = `textField -q -text prefixTF`;
string $newPre = `rename $firstItem ($prefix + " " + $firstItem)`;
break;

case 2:
//RENAME CODE HERE

string $rename = `textField -q -text renameTF`;
// renames object with renameTF's information
string $newName = `rename $selObj[$i] $rename`;
break;

case 3:
//SUFFIX CODE HERE
string $suffix = `textField -q -text suffixTF`;
string $newSuf = `rename $firstItem ($firstItem + " " + $suffix)`;
break;
}
// creates a warning telling you what the objects new name is
print ("Your object is now named" + " " + $newName);

// close window
};

----------------------------------------------------------------

nofxsapunk
11-05-2006, 12:38 AM
btw i have only been working with mel for 5 weeks, i got a book to help me and im learning it, its just im at a block and all i am doing is :banghead: :wip: :cry:

Mikademius
11-05-2006, 12:55 AM
Ok, I started the script all over again to get it the way I'd write it. Feel free to copy/paste and use whatever code you find interesting :)

Crap. Girlfriend wants us to "go to sleep". I'll finish it first thing in the morning. I've already written half of it.


In the meantime, you should read my 6 page mel tutorial:

http://www.final3.com/html/tutorials/mel_ShapeEditor_01.htm

BlackHwk4
11-05-2006, 07:18 AM
Here's something I made really quick to give you an example of what you'd need to do to make the script you want:


string $wndName = "bla";

// if window exists
if (`window -ex $wndName`)
deleteUI $wndName; // kill it

window -t "Blah blah" -wh 190 210 -s 0 $wndName;

columnLayout -cw 182 -cat "both" 3 mainLayout;

button -l "Refresh List" -c "refreshList()";
textScrollList -nr 8 -ams 1 myTSL;
textField myTxtField;
button -l "Rename" -c "renameProc()";

setParent..;

showWindow $wndName;

global proc refreshList()
{
string $selection[] = `ls -sl`; // get a list of what's selected

for ($current in $selection)
textScrollList -e -a $current myTSL;
}

global proc renameProc()
{
// get a list of the selected items in the textScrollList
string $tslSelection[] = `textScrollList -q -si myTSL`;

for ($current in $tslSelection)
rename $current (`textField -q -tx myTxtField`);
}

Buexe
11-05-2006, 10:47 AM
ok, now im getting confused, i put in the script from Buexe and im getting errors, i think i put it in the wrong place, and im not sure where to put your part of the script to try it out Mikademius. im new. im taking a class right now but i am having real troubles grasping MEL.


here is the script at the point im at now

-----------------------------------------------------------------------------------------


----------------------------------------------------------------

Well, you put my part into the GUI proc, but it should actually go into the renaming proc since it is doing the renaming. the errors come from differently named variables ($objects and $sel).Before we do things twice, let`s see what Mikademius comes up with.
Nice weekend everybody

Mikademius
11-05-2006, 11:51 PM
Phew, sorry I'm late. I have two jobs so weekends are just a perfect time for...more work!!
Here's what I got so far. The explainations are in the code. If you have any questions, just ask and I'll try my best to answer them :)


//First off make sure you check if your UI is already open.
//Run this to delete the UI if it is:

if (`window -ex ah_renameUI`)deleteUI ah_renameUI;

//UI check done

//UI creation; START:
window -t "ah_renameUI" -wh 300 165 ah_renameUI;

rowColumnLayout -nc 2 -cw 1 150 -cw 2 150 ah_mainLayoutHolder;
//Layout to the left to hold selected objects:

frameLayout -l "objectList" -collapsable 0 -bs "etchedIn" ah_leftFrame;
columnLayout -adj 1;
textScrollList -w 135 -nr 19 -ams 1 ah_objTSL;
button -l "refresh selection" -c "ah_updateTSL";

setParent..;
setParent..;

//Layout to the rigth to hold all the editing:
frameLayout -l "editFields" -w 200 -collapsable 0 -bs "etchedIn" ah_rightFrame;
columnLayout -adj 1 ah_rightSection;

//I'll use textFieldGrps to get a better looking- , and easier to understand UI.
textFieldGrp -l "prefix:" -cw2 41 100 ah_prefix;
textFieldGrp -l "suffix:" -cw2 41 100 ah_suffix;
//Separator to make it easier for the eye to see what you are editing
separator -h 10 -style "out";

textFieldGrp -l "rename:" -cw2 50 91 ah_rename;

separator -h 17 -style "out";
//The button to do all the math :)
button -l "rename selected" -c "ah_rename";

//UI creation; END
showWindow ah_renameUI;

ah_updateTSL; //run this proc to update selection:

//--Proc: update proc --//

global proc ah_updateTSL (){
string $ah_selected[] = `ls -sl`; //get selected objects
if (!(`size($ah_selected)`)) //check if there is a selection at all
{
warning "nothing selected. List will not display any objects";
}

else
{
//cycle through the objects and add them to the list
textScrollList -e -ra ah_objTSL;
for ($ah_sel in $ah_selected){
textScrollList -e -a $ah_sel ah_objTSL;
}
}
}



//--Proc: rename proc --//

global proc ah_rename(){
//Get all the userInput:
string $ah_getObj[] = `textScrollList -q -si ah_objTSL`;

string $ah_getPrefix = `textFieldGrp -q -text ah_prefix`;
string $ah_getSuffix = `textFieldGrp -q -text ah_suffix`;
string $ah_getRename = `textFieldGrp -q -text ah_rename`;

for ($ah_editObj in $ah_getObj){
//if the obj already exists, Maya will add a num at the end ;)
rename $ah_editObj ($ah_getPrefix + "_" + $ah_getRename + "_" + $ah_getSuffix);
}


ah_updateTSL;
}




What the script lacks is a way to check if there is any input in the textFields at all. So therefore the script will rename stuff a bit odd with the underlines etc. I was thinking that some ifs or switches could deal with this. However I couldn't make it right now. I have to sleep too ;) 2 AM here...

nofxsapunk
11-06-2006, 02:50 AM
thank you Mikademius, im going to play with it some more and then i will ask questions.

Mikademius
11-06-2006, 09:26 AM
No problem.
Hope you get it to work :)

CGTalk Moderation
11-06-2006, 09:26 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.