PDA

View Full Version : undeclared variable


meldu
01-15-2013, 12:38 AM
Hi, good night. I have a problem with a tool that i`m trying to make. The tool is for multiple parent constraints. The tool will have two buttons:

-1 the first one, will define who is the father.
-2 the second button, will define which are the childs, i mean, who will be constrained. And, will constraint this objects to the first one.

So:

1-You select the father, hit the button that says "father"
2-You select the childs, then hit the button that says "sons"

But i get this error:
// Error: "$papa" is an undeclared variable.

This its the script.
//global proc that assing the father

global proc setFather () {
//a variable that search and define who its the father
string $papa[] = `ls -sl`;
print $papa[0];
select $papa[0];

}


//global proc that for the sons

global proc setSons () {

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

$myNum = (size(`ls -sl`));

for ($laps = 0; $laps <= $myNum; $laps++){

select -r $papa[0];
select -add $sons
doCreateParentConstraintArgList 1 { "1","0","0","0","0","0","0","1","","1" };

}


}




//main window

window -rtf true -title "multiConstraint" multiCons;
columnLayout;

button -c "setFather" -label "father";

button -c "setSons" -label "sons";

showWindow multiCons;

I think its something about the global variables, but i cant figure out how to do it.

Thanks.

gmask
01-15-2013, 01:00 AM
you didn't declare $papa as a global variable

meldu
01-15-2013, 03:08 AM
you didn't declare $papa as a global variable

Yes, i did:

global string $papa[] = `ls -sl`;

But give me error:

// Error: Initialization of global variable "$papa" requires a constant value. //

I dont know why, and i dont know exactuly where to declare the global variable. I did it at the begining of the first global proc, but it shows that error.

zoharl
01-15-2013, 08:24 AM
I didn't test it, but first declare it as global, then set it:

global string $papa[];
$papa = `ls -sl`;

rgkovach123
01-15-2013, 01:27 PM
in general, I would avoid using global variables.

Instead of using Global Variables, write a function to return the father.

global proc string setFather () {
string $papa[] = `ls -sl`;
return $papa[0];
}

global proc setSons () {
string $sons[] = `ls -sl`;
string $papa = `setFather`;
$myNum = (size(`ls -sl`));
for ($laps = 0; $laps <= $myNum; $laps++){
select -r $papa;
select -add $sons;
doCreateParentConstraintArgList 1 { "1","0","0","0","0","0","0","1","","1" };
}
}

gmask
01-15-2013, 01:31 PM
Yes, i did

global string $papa[] = `ls -sl`;

But give me error:

// Error: Initialization of global variable "$papa" requires a constant value. //

I dont know why, and i dont know exactuly where to declare the global variable. I did it at the begining of the first global proc, but it shows that error.

you didn't mention that in your original post. :argh:

meldu
01-15-2013, 01:44 PM
I didn't test it, but first declare it as global, then set it:

global string $papa[];
$papa = `ls -sl`;


i declare it two times, before the two procs:

//global proc that assing the father and sons
global string $papa[];

global proc setFather () {
//a variable that search and define who its the father
string $papa[] = `ls -sl`;
print $papa[0];
select $papa[0];

}


//global proc that for the sons
global string $papa[];
global proc setSons () {

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

$myNum = (size(`ls -sl`));

for ($laps = 0; $laps <= $myNum; $laps++){

select -r $papa[0];
select -add $sons
doCreateParentConstraintArgList 1 { "1","0","0","0","0","0","0","1","","1" };

}


}




//main window

window -rtf true -title "multiConstraint" multiCons;
columnLayout;

button -c "setFather" -label "father";

button -c "setSons" -label "sons";

showWindow multiCons;


Same error: // Error: "$papa" is an undeclared variable. //

Thanks.

gmask
01-15-2013, 02:14 PM
i declare it two times, before the two procs:

It needs to be inside the procs!!!


global proc setFather () {
//a variable that search and define who its the father
global string $papa[];

string $papa[] = `ls -sl`;
print $papa[0];
select $papa[0];

}

zaskar
01-15-2013, 02:19 PM
Its not enough to declare a variable somewhere. You need to make a procedure familiar with this variable, so declare $papa inside the procedure.

//ok obsolete

zoharl
01-15-2013, 04:32 PM
Frankly, the doc explains it better than any of us:


http://download.autodesk.com/global/docs/maya2013/en_us/files/Procedures_Global_and_local_variables.htm

meldu
01-15-2013, 04:49 PM
Frankly, the doc explains it better than any of us:


http://download.autodesk.com/global/docs/maya2013/en_us/files/Procedures_Global_and_local_variables.htm


Thanks dude, i will keep trying

rgkovach123
01-15-2013, 05:35 PM
i recommend avoiding global variables, they are usually not necessary and a bad habit to develop if you are just starting out scripting. See my post above for how to setup your procs to avoid the use of global vars.

hominid
01-15-2013, 07:03 PM
your forgot the ; on the line "select -add $sons"

Cheers,
Pete

CGTalk Moderation
01-15-2013, 07:03 PM
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.