PDA

View Full Version : undeclaired variables but yet they are?? HELP


Apophis3d
02-15-2008, 01:05 AM
ok i am not sure why maybe keeps telling me this

line 37.23: "$sel" is an undeclared variable. //

it also says

Error: int $num = `size($sel)`;

global proc ObjectNamer()
{
//Checks to see if window exists

if (`window -query -exists nameme` ==1)
deleteUI nameme;

//Window
string $window = `window -title "Object Rename" nameme`;

columnLayout
-columnAttach "both"5
-rowSpacing 5
-columnWidth 250;
textFieldGrp
-label "Object Name"
-text "Name me";
button
-label "Rename Object"
-command "namehere();";
button
-label "Exit"
-command ("deleteUI -window " + $window);

showWindow $window;

//edits window
window -edit -widthHeight 270 130 $window;
window -edit -tlc 180 50 $window;
}
}

global proc namehere ()
{
//Varables
string $sel[0] = `button ls -sl Rename Object`;
int $num = `size($sel)`;

//Loop
for ($i = 0; $i<$num; $i++){
rename |pCube1|pCubeShape1 "cr" ;
}

This line rename |pCube1|pCubeShape1 "cr" ;

Has not yet been edited correctly just ignore it

Diamant
02-15-2008, 06:25 AM
Hey Apophis3d...


your first problem, $sel is an undeclared variable, is caused because.... you didn't declare the variable.. :)

think of it like this.. you are trying to store the button into the first array position.. but you haven't created the array yet..

you aren't completely off though.. you dont need to create the array before hand.. just leave out the array position..

so...

string $sel[] = `button ls -sl Rename Object`;

would be correct.. this is declaring a new array and storing the result into the array.. saying string $sel[0] is saying store it into the first position of an array that exists.. which causes an error, $sel is undeclared.., because it doesn't exist yet..

hope this makes sense..

the second error, int $num = `size($sel)`; should resolve itself after fixing the first part



hopefully this helps,
-Rich

HowardB
02-15-2008, 06:48 AM
how about something like this.

global proc ObjectNamer()
{
//Checks to see if window exists

if (`window -query -exists nameme` ==1)
deleteUI nameme;

//Window
string $window = `window -title "Object Rename" nameme`;

columnLayout
-columnAttach "both"5
-rowSpacing 5
-columnWidth 250;
textFieldGrp
-label "Object Name"
-text "Name_Me"
someTextField;

button
-label "Rename Object"
-command "namehere();";
button
-label "Exit"
-command ("deleteUI -window " + $window);

showWindow $window;

//edits window
window -edit -widthHeight 270 130 $window;
window -edit -tlc 180 50 $window;
}

global proc namehere ()
{
string $sel, $temp[], $newName;

$temp = `ls -sl`;
$sel = $temp[0];

$newName = `textFieldGrp -q -text someTextField`;
rename $sel $newName;
}


edit:

whoops i totally missed the loop in there!

ewerybody
02-16-2008, 09:24 AM
hey Apophis3d!
writing a good renamer is a tough task! :]

you can browse at highend3D for quite a bunch of them (http://www.highend3d.com/maya/downloads/mel_scripts/search.php?searchtext=rename&filter=3%7C11&search.x=0&search.y=0)!
but sure.. downloading doesn't teach you anything ;]

// be consistent throughout the script. ObjectNamer or nameme!? nameme is better I think ;]
global proc nameMe()
{
// if its 0 or 1 you don't need "== 1" / "==0" - just a tip
if (`window -q -ex nameMe_window`)
deleteUI nameMe_window;

// as you delete "nameme" first the name is free! You don't need to put it in a variable
window -title "Object Rename" nameMe_window;
columnLayout
-columnAttach "both" 5
-rowSpacing 5
-columnWidth 250;
// you must name the control! so that you can query it at all!
textFieldGrp
-label "Object Name"
-text "Name me"
nameMe_nameField;
button
-label "Rename Object"
-command "nameMe_perform";
button
-label "Exit"
-command "deleteUI nameMe_window";

// do the edit before you show the window otherwise it may pop through the screen
window -edit -widthHeight 270 130 nameMe_window;
window -edit -tlc 180 50 nameMe_window;

showWindow nameMe_window;
}

global proc nameMe_perform ()
{
// here you get what you typed in the field:
string $newName = `textFieldGrp -q -text nameMe_nameField`;

// button what?!!?!? ls -sl tells you whats selected!
// -l is for "long". double name convenience
string $sel[] = `ls -sl -l`;

// in this case you don't need no count. use that handy "for in loop"!:
for ($x in $sel)
rename $x ($newName); // if 1-lined you can omit the scope
}

Apophis3d
02-16-2008, 05:52 PM
string $sel[]= `button ls -sl Rename Object`;
int $num = `size($sel)`;

changed the code to this and well it's still saying:

line 37.23: "$sel" is an undeclared variable. //

which is this line int $num = `size($sel)`;

Diamant
02-16-2008, 09:13 PM
odd... i dont get that error at all.. i do get other errors because you aren't calling a correct command...

string $sel[] = `button ls -sl Rename Object`;

"button ls -sl Rename Object" is not a command..

however, i did notice you have an extra bracket lodged in there.. maybe that is causing your error..

try this..

btw, you dont need to say if window exists == 1... its removed in the code pasted.. if its not there the code thinks its the same thing as == 1..

"if (`window -query -exists nameme`)"


global proc ObjectNamer()

{

//Checks to see if window exists



if (`window -query -exists nameme`)

deleteUI nameme;



//Window

string $window = `window -title "Object Rename" nameme`;



columnLayout

-columnAttach "both"5

-rowSpacing 5

-columnWidth 250;

textFieldGrp

-label "Object Name"

-text "Name me";

button

-label "Rename Object"

-command "namehere();";

button

-label "Exit"

-command ("deleteUI -window " + $window);



showWindow $window;



//edits window

window -edit -widthHeight 270 130 $window;

window -edit -tlc 180 50 $window;



}



global proc namehere ()

{



string $sel[] = `button ls -sl Rename Object`;

int $num = `size($sel)`;

}

Apophis3d
02-16-2008, 11:32 PM
yeah heres the errors i am getting

ObjectNamer;
// Error: }
//
// Error: "C:/Users/Chris/Documents/maya/2008-x64/scripts/ObjectNamer.mel" line 31.1: Syntax error //
// Error: int $num = `size($sel)`;
//
// Error: "C:/Users/Chris/Documents/maya/2008-x64/scripts/ObjectNamer.mel" line 37.23: "$sel" is an undeclared variable. //
// Error: } //
// Error: "C:/Users/Chris/Documents/maya/2008-x64/scripts/ObjectNamer.mel" line 42.2: Syntax error //

Apophis3d
02-16-2008, 11:36 PM
ok I just ran your edited version and it works. I am not seeing how yours differes from mine though...

Diamant
02-16-2008, 11:57 PM
this is your code...

//edits window
window -edit -widthHeight 270 130 $window;
window -edit -tlc 180 50 $window;
}
}

and this is mine
//edits window

window -edit -widthHeight 270 130 $window;

window -edit -tlc 180 50 $window;

}



notice the extra bracket you have in there.. so you are telling your code to end twice... that will cause errors all over the place..

Apophis3d
02-17-2008, 12:44 AM
oh lol dang that explains it

thanks for the help

Apophis3d
02-17-2008, 04:20 PM
ok so i am trying to make this section of my code work right

global proc namehere ()

{
string $sel[] = `button ls -sl Rename Object`;
int $num = `size($sel)`;

//Loop
for ($i = 0; $i<$num; $i++){
rename |pCube1|pCubeShape1 "cr" ;
}
}

But i am slightly confused on how to make the loop work for this function. Would I do something like string $sel = `rename +"$sel"`; ?

Buexe
02-17-2008, 06:32 PM
I don`t know what this thing is supposed to do : button ls -sl Rename Object

But renaming could be done this way:
string $sel[] = `ls -sl -l `;
int $no = 0;
for ( $no = 0 ; $no < size ($sel ) ; $no++)
{
rename $sel[$no] bla2;
}


or shorter:
for ( $o in `ls -sl -l`) rename $o bla2;

Diamant
02-17-2008, 08:31 PM
i guess i'm not 100% sure what you are trying to do... if all you want to do is rename the object based off of the text field you provide.. then its really easy to do..

first you need to query the text field. in order to do that you actually need to give the textfield a name.. lets say you called it "rename_TF";

you'd do that here btw..
textField -tx "name me" rename_TF;

you dont need a textFieldGrp since its a single textfield..

next just query what the user entered into the textfield..

string $newName = `textField -q -tx rename_TF`;

after that.. get the list of selected objects.

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

then do a loop.. you have 2 choices really but the easiest is the "for in" loop..

so define a new string..

string $ind_obj;

then do your loop..

for ($ind_obj in $sel)
{
rename $ind_obj $newName;
}

its really that simple.. hope this helps..

as you can see.. there really are many ways to solve a problem.. if you look at the code a bunch of people posted here already the answers are in there.. hopefully this helps clear it up a bit though..

-Rich

Apophis3d
02-18-2008, 01:12 AM
Wow thats alot easer than I thought. I seem to have a habit of over complicating my coding thanks again!

Apophis3d
02-18-2008, 01:30 AM
Ok I have one final question, i am trying to get my error message to work. I am using an if statement but it seems to be ignoring it.

//checks for selected objects
string $sel[] = `ls -sl`;
int $num = `size($sel)`;
string $ind_obj;

//Loop
for ($ind_obj in $sel)
{
rename $ind_obj $txRename;

if ($num ==0)
error "No objects selected.";
}
}

ticket01
02-18-2008, 05:25 AM
Ok I have one final question, i am trying to get my error message to work. I am using an if statement but it seems to be ignoring it.

//checks for selected objects
string $sel[] = `ls -sl`;
int $num = `size($sel)`;
string $ind_obj;

//Loop
for ($ind_obj in $sel)
{
rename $ind_obj $txRename;

if ($num ==0)
error "No objects selected.";
}
}

Should this be in the loop? I don't thinks so, cause it wouldn't make any sense. If size($sel) > 0 your if statement will never be true. Just put it out of the loop and it should work. But a small piece of advise: error statements interrupt the MEL interpreter. If you want to do something else after an error, use a warning.

ewerybody
02-18-2008, 08:05 AM
Wow thats alot easer than I thought. I seem to have a habit of over complicating my coding thanks again!And over-reading my replys, it seems :D

if you do that "for in loop" there is actually no need for a count check. As the loop will only start if there is something in the given array!. Otherwise it just passes.

CGTalk Moderation
02-18-2008, 08:05 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.