View Full Version : if statement


shine77
10 October 2005, 12:32 PM
Hello.
Is it not possible to assign a varible inside a if statement an call it later.
Thanx.

int $a = 1;
if ($a == 1){
int $b = 2;
} else {
int $b = 1;
}
print $b;

Ourouk
10 October 2005, 12:55 PM
Yes it's possible ! You must declare the variable outside the if statement.
( If you declare it inside, the $b variable will be a "local" variable only active between 2 {} )

int $a = 1;
int $b;

if ($a == 1)
{
$b = 2;
}
else
{
$b = 1;
}

print $b;

shine77
10 October 2005, 01:25 PM
Yes it's possible ! You must declare the variable outside the if statement.
( If you declare it inside, the $b variable will be a "local" variable only active between 2 {} )

int $a = 1;
int $b;

if ($a == 1)
{
$b = 2;
}
else
{
$b = 1;
}
print $b;

Thanx for the quick reply.
But if I run the script again and $b has something in it, $b will be cleared.
thats the problem Im runinning into :(

Ourouk
10 October 2005, 01:29 PM
Yes if you run the script again, $b will be overwritten.
explain what you want to do...I don't understand.

drGonzo
10 October 2005, 01:36 PM
Hello.
Is it not possible to assign a varible inside a if statement an call it later.
No. Thats the whole deal with variable scope. As mentioned, you have to declare it outside the brackets.

shine77
10 October 2005, 01:53 PM
Yes if you run the script again, $b will be overwritten.
explain what you want to do...I don't understand.

This is what I wanted to do.

{

string $currentPanel = `getPanel -withFocus`;
string $panelType = `getPanel -to $currentPanel`;
string $chme = `modelEditor -query -displayLights $currentPanel`;
print $chme;

string $AllLights[] = `ls -lights`;
//returns viewport with black
//float $bgColor[3];

if ( $chme != "all"){
float $bgColor[3] = `displayRGBColor -query background`;
displayRGBColor background 1.0 1.0 1.0;
hide $AllLights;
if ($panelType == "modelPanel") {
modelEditor -edit -dl "all" $currentPanel;
} else if (`isTrue "MayaCreatorExists"` && `scriptedPanel -ex $currentPanel` && `scriptedPanel -q -type $currentPanel` == "dynPaintScriptedPanelType") {
dynPaintEditor -e -dtx 1 -dsl "all" $gDynPaintEditorName;
}
} else {
modelEditor -e -dl "default" $currentPanel;
displayRGBColor background $bgColor[0] $bgColor[1] $bgColor[2];
showHidden -a;
}
};

But it dosent work because of the varible {} limit and if I put float $bgColor in the beginning it will return black in the viewport.
Basicly what I want to do is to be able to see the silluette (use all lights in viewport with all lights hidden) on a white background and return out of it with the same color as I had in the viewport.

Robert Bateman
10 October 2005, 03:10 PM
declare it global... ie,

global float $bgcolor[] = {1,0,0};

proc myFunc() {
// have to have this here to affect the global
global float $bgcolor[];

// At this point ....
//
// first time around, $bgcolor will be red, the next time round it'll be whatever
// it was last set to.....
//

if( $someCondition ) {
$bgcolor = `someFunctionCall`;
}
else {
$bgcolor = `someOtherFunctionCall`;
}
}

the other way is to use optionVar to store the colour values (which will be retained for when you next run Maya)

shine77
10 October 2005, 03:57 PM
declare it global... ie,

global float $bgcolor[] = {1,0,0};

proc myFunc() {
// have to have this here to affect the global
global float $bgcolor[];

// At this point ....
//
// first time around, $bgcolor will be red, the next time round it'll be whatever
// it was last set to.....
//

if( $someCondition ) {
$bgcolor = `someFunctionCall`;
}
else {
$bgcolor = `someOtherFunctionCall`;
}
}

the other way is to use optionVar to store the colour values (which will be retained for when you next run Maya)

thanx IŽll try that

CGTalk Moderation
10 October 2005, 03:57 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.


1