if statement

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

THREAD CLOSED
 
Thread Tools Search this Thread Display Modes
  10 October 2005
if statement

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;
 
  10 October 2005
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;
 
  10 October 2005
Originally Posted by Ourouk: 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 :(
 
  10 October 2005
Yes if you run the script again, $b will be overwritten.
explain what you want to do...I don't understand.
 
  10 October 2005
Originally Posted by shine77: 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.
 
  10 October 2005
Originally Posted by Ourouk: 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.
 
  10 October 2005
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)
 
  10 October 2005
Originally Posted by Robert Bateman: 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
 
  10 October 2005
Thread automatically closed

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.
__________________
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
 
Thread Closed share thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright ©2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 05:52 PM.


Powered by vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.