CGTalk > Software Specific Forums > Autodesk Maya > Maya Programming
Login register
Thread Closed share thread « Previous Thread | Next Thread »  
 
Thread Tools Search this Thread Display Modes
Old 10-28-2005, 12:32 PM   #1
shine77
Veteran
 
Join Date: May 2003
Posts: 59
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;
 
Old 10-28-2005, 12:55 PM   #2
Ourouk
Veteran
3D graphist
 
Join Date: Feb 2004
Posts: 63
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;
 
Old 10-28-2005, 01:25 PM   #3
shine77
Veteran
 
Join Date: May 2003
Posts: 59
Quote:
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 :(
 
Old 10-28-2005, 01:29 PM   #4
Ourouk
Veteran
3D graphist
 
Join Date: Feb 2004
Posts: 63
Yes if you run the script again, $b will be overwritten.
explain what you want to do...I don't understand.
 
Old 10-28-2005, 01:36 PM   #5
drGonzo
Know-it-All
k|m
FX TD
Los Angeles, USA
 
Join Date: May 2005
Posts: 497
Quote:
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.
 
Old 10-28-2005, 01:53 PM   #6
shine77
Veteran
 
Join Date: May 2003
Posts: 59
Quote:
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.
 
Old 10-28-2005, 03:10 PM   #7
Robert Bateman
Lord of the posts
 
Robert Bateman's Avatar
Robert Bateman
Software Engineer
United Kingdom
 
Join Date: Aug 2004
Posts: 1,160
declare it global... ie,
Code:
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)
 
Old 10-28-2005, 03:57 PM   #8
shine77
Veteran
 
Join Date: May 2003
Posts: 59
Quote:
Originally Posted by Robert Bateman
declare it global... ie,
Code:
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
 
Old 10-28-2005, 03:57 PM   #9
CGTalk Moderation
Lord of the posts
CGTalk Forum Leader
 
Join Date: Sep 2003
Posts: 1,066,481
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 11:30 AM.


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