User avatar
By deadalvs
#196699
CRAP !

i just spent so much time with trying to make a small UI with buttons and some procedures that create stuff.

i just don't get it with the variable scope and global/local definition. do i have to redeclare every time all the vars when i get into a new for loop, while loop or a new procedure ??

*HELP*

could one post some examples that show this in a simple way ?

and what the f. is that error based on:
// Error: Line 29.37: Initialization of global variable "$selShape" requires a constant value. //

help very much appreciated... :cry:

* * *

deadalvs
User avatar
By DrMerman
#196701
Hey man. If you either post or PM me your script I'll run you through it.

Cheers,
Dr Merman
User avatar
By deadalvs
#196702
thanks... i just could smash my laptop !!!

hours and hours of frustration !

* * *

i'll just write the stuff in english, so You will understand the script. it would be so easy... :(

* * *

deadalvs
User avatar
By deadalvs
#196707
i am wanting to create an UI with buttons that will create different space trusses based on the cvs of two shapable nurbs surfaces (degree 1)

* * *

question one:

what is here the problem:

/*****/

// first, create two nubs planes and select them.
string $sel[] = `ls -sl`;
pickWalk -d down;
string $selShape[] = `ls -sl`;

int $U1 = `nurbsPlane -q -patchesU $selShape[0]`;
// Result: 5 //
int $V1 = `nurbsPlane -q -patchesV $selShape[0]`;
// Result: 5 //
int $U2 = `nurbsPlane -q -patchesU $selShape[1]`;
// Error: line 13: No object was specified to query //
int $V2 = `nurbsPlane -q -patchesV $selShape[1]`;
// Error: line 13: No object was specified to query //

/*****/

the objects are cast into the string array.
the command nurbsPlane -q -patchesU gets the number of patches of the selected object's shape node.
why can the command not be executed with the 1st element in the array when it does work with the zero-th ?

* * *

deadalvs
User avatar
By deadalvs
#196708
ok. here it is.

script BEGIN:

///////////////



// space truss script, first approach with randomized cv access of two nurbs surfaces (eg. deg = 1; 5 by 5 patches)

if (`window -exists "TrussWindow"` == 1){deleteUI "TrussWindow";}

window -title "TrussCreateWindow" -mnb 0 -mxb 0 TrussWindow;
columnLayout -adjustableColumn 1;
setParent ..;
setParent ..;

frameLayout -label "Create Trusses" -cll true;
columnLayout;
button -label "Get two selected Nurbs" -command "getNurbs();";
separator -w 300;
text -l "Number of randomized sticks";
intField stickCount;
button -label "Simple randomized Truss" -command "randTruss `intField -q -v stickCount`";
separator -w 300;

showWindow TrussWindow;


/*************************************************************************/
/*************************************************************************/
/*************************************************************************/

global proc getNurbs()
// gets the names of the two selected surfaces and casts them into the string array.
// one array for the transform nodes and one array for the shape nodes. (easier to access the nodes later on)
{
string $sel[] = `ls -sl`;
pickWalk -d down;
string $selShape[] = `ls -sl`;
print $sel;
}

/*************************************************************************/

global proc randTruss(int $stickCount)
{
int $stickCount;
string $sel[];
string $selShape[];


//select "STAB*";
//delete;

vector $vectorP1;
vector $vectorP2;

int $U1 = `nurbsPlane -q -patchesU $selShape[0]`;
int $V1 = `nurbsPlane -q -patchesV $selShape[0]`;
int $U2 = `nurbsPlane -q -patchesU $selShape[1]`;
int $V2 = `nurbsPlane -q -patchesV $selShape[1]`;



int $counter = $stickCount;


//sticks are created until the stickCount is reached. they only get created if the randomized points of the two surfaces are less than
//2.5 units away from each other.

while ($counter < $stickCount)
{
string $sel[];
vector $vectorP1;
vector $vectorP2;

int $ZufallU1 = `floor (rand (0, ($U1 + 3)))`;
int $ZufallV1 = `floor (rand (0, ($V1 + 3)))`;
int $ZufallU2 = `floor (rand (0, ($U2 + 3)))`;
int $ZufallV2 = `floor (rand (0, ($V2 + 3)))`;

$vectorP1 = `pointPosition ($sel[0] + ".cv" + "[" + $ZufallU1 + "][" + $ZufallV1 + "]")`;
$vectorP2 = `pointPosition ($sel[1] + ".cv" + "[" + $ZufallU2 + "][" + $ZufallV2 + "]")`;


$squaredDist =`pow ($vectorP2.x - $vectorP1.x) 2` + `pow ($vectorP2.y - $vectorP1.y) 2` + `pow ($vectorP2.z - $vectorP1.z) 2` ;
$dist = `sqrt $squaredDist`;

if ($dist >= 2.5)
{
curve -d 1 -p ($vectorP1.x) ($vectorP1.y) ($vectorP1.z) -p ($vectorP2.x) ($vectorP2.y) ($vectorP2.z) -k 0 -k 1 ;
rename "STAB_";
$counter ++;
}

}
}

/*************************************************************************/


////////

script END

* * *

problem:

in line 54. the error message is:
// Error: line 54: No object matches name: //

this means that the string array $sel[] and $selShape[] are not defined in this procedure ! how can i do this ?

the architectural / technical sense of this created truss is still zero of worth, i just need this first thing to work before i can create real trusses with iterations of for-loops.
if You have questions: HUNT ME ! :)

* * *

thanks !

* * *

deadalvs
User avatar
By DrMerman
#196710
Cheers man. I'll take a look in my break and get back to you :)
User avatar
By DrMerman
#196721
Hey man. I took a quick look at those variables and have pasted the fixed code below. Basically, to use variables between procs you have to declare them as global. I have explained this in the comments below.

Cheers and good luck :)
Dr Merman

p.s I added a print command at the bottom, just to make sure everything was running all of the way through.
Code: Select all
///////////////

// space truss script, first approach with randomized cv access of two nurbs surfaces (eg. deg = 1; 5 by 5 patches)

if (`window -exists "TrussWindow"` == 1){deleteUI "TrussWindow";}

window -title "TrussCreateWindow" -mnb 0 -mxb 0 TrussWindow;
columnLayout -adjustableColumn 1;
setParent ..;
setParent ..;

frameLayout -label "Create Trusses" -cll true;
columnLayout;
button -label "Get two selected Nurbs" -command "getNurbs();";
separator -w 300;
text -l "Number of randomized sticks";
intField stickCount;
button -label "Simple randomized Truss" -command "randTruss `intField -q -v stickCount`";
separator -w 300;

showWindow TrussWindow;



/*************************************************************************/
/*************************************************************************/
/*************************************************************************/
 
global proc getNurbs()
//gets the names of the 2 nurbs surfaces, and adds these names to 2 global variables. repeat for the shape nodes.
{
//first define the 4 global variables needed. global variables can be used to transfer data between procs.
//in order to avoid the "must be given a constant value" error, you first create the variable, and then give it a value.

global string $nurbsTrans1;
global string $nurbsTrans2;
global string $nurbsShape1;
global string $nurbsShape2;

//find the 2 selected nurbs surfaces
string $sel[] = `ls -sl`;

//add those two nurbs surfaces to the global variables we have already created.
$nurbsTrans1 = $sel[0];
$nurbsTrans2 = $sel[1];

//now repeat the process for the shape nodes
pickWalk -d down;
string $selShape[] = `ls -sl`;
$nurbsShape1 = $selShape[0];
$nurbsShape2 = $selShape[1];
print $sel;
}

/*************************************************************************/

global proc randTruss(int $stickCount)
{

//as you have already done for the $stickCount variable, you need to reassign the global variables in this proc.
//in the rest of the code, I have replaced instances of '$selShape' with the global variables created above.
//$sel[0] becomes $nurbsTrans1
//$sel[1] becomes $nurbsTrans2
//$selShape[0] becomes $nurbsShape1
//$selShape[1] becomes $nurbsShape2

int $stickCount;
global string $nurbsTrans1;
global string $nurbsTrans2;
global string $nurbsShape1;
global string $nurbsShape2;


//select "STAB*";
//delete;

vector $vectorP1;
vector $vectorP2;

int $U1 = `nurbsPlane -q -patchesU $nurbsShape1`;
int $V1 = `nurbsPlane -q -patchesV $nurbsShape1`;
int $U2 = `nurbsPlane -q -patchesU $nurbsShape2`;
int $V2 = `nurbsPlane -q -patchesV $nurbsShape2`;



int $counter = $stickCount;


//sticks are created until the stickCount is reached. they only get created if the randomized points of the two surfaces are less than
//2.5 units away from each other.

while ($counter < $stickCount)
{
string $sel[];
vector $vectorP1;
vector $vectorP2;

int $ZufallU1 = `floor (rand (0, ($U1 + 3)))`;
int $ZufallV1 = `floor (rand (0, ($V1 + 3)))`;
int $ZufallU2 = `floor (rand (0, ($U2 + 3)))`;
int $ZufallV2 = `floor (rand (0, ($V2 + 3)))`;

$vectorP1 = `pointPosition ($nurbsTrans1 + ".cv" + "[" + $ZufallU1 + "][" + $ZufallV1 + "]")`;
$vectorP2 = `pointPosition ($nurbsTrans2 + ".cv" + "[" + $ZufallU2 + "][" + $ZufallV2 + "]")`;


$squaredDist =`pow ($vectorP2.x - $vectorP1.x) 2` + `pow ($vectorP2.y - $vectorP1.y) 2` + `pow ($vectorP2.z - $vectorP1.z) 2` ;
$dist = `sqrt $squaredDist`;

if ($dist >= 2.5)
{
curve -d 1 -p ($vectorP1.x) ($vectorP1.y) ($vectorP1.z) -p ($vectorP2.x) ($vectorP2.y) ($vectorP2.z) -k 0 -k 1 ;
rename "STAB_";
$counter ++;
}

}

print "\nEnd of Proc";
}

/*************************************************************************/

////////
User avatar
By deadalvs
#196745
to SunlightRocker:

it does something like «smörebröd»... :)

* * *

now let's have a look at the new script... ! thanks, DrMerman... You're «da merman» :)

i'll surely have a few more questions... :roll:

* * *

deadalvs
User avatar
By DrMerman
#196754
No worries man. Just let me know.

SunlightRocker: Nobody knows man, nobody :D
User avatar
By deadalvs
#196757
oh... i get an error message at line 78 in this part (near bottom):


/************************/

//select "STAB*";
//delete;

vector $vectorP1;
vector $vectorP2;

int $U1 = `nurbsPlane -q -patchesU $nurbsShape1`;
int $V1 = `nurbsPlane -q -patchesV $nurbsShape1`;
int $U2 = `nurbsPlane -q -patchesU $nurbsShape2`;
int $V2 = `nurbsPlane -q -patchesV $nurbsShape2`;

/************************/

error message:
// Error: line 79: No object was specified to query //

* * *

did You try it with a newly restarted maya (cleared vars)?

let me look again... !

* * *

deadalvs
User avatar
By deadalvs
#196768
hmm...

it is strange... but i have still the problem with the object that isn't found. i have searched again through the script but could not find a reason.

this is very strange !!
there's something wrong with that flag command i think... (?)

((
the same case as i mentioned before with this:
int $U1 = `nurbsPlane -q -patchesU $selShape[0]`;
// Result: 5 //
int $V1 = `nurbsPlane -q -patchesV $selShape[0]`;
// Result: 5 //
int $U2 = `nurbsPlane -q -patchesU $selShape[1]`;
// Error: line 13: No object was specified to query //
int $V2 = `nurbsPlane -q -patchesV $selShape[1]`;
// Error: line 13: No object was specified to query //
))

* * *

deadalvs
User avatar
By DrMerman
#196769
Hey man, I've checked it through and all is working this end. If you don't do the 'Get 2 Selected Nurbs' part, then you could have problems. To make the script run, I do the following.

*Paste the code into the script editor.
*Create 2 nurbs objects and select them both
*Run the script
*With the 2 objects still selected, press 'Get Selected Objects'
*Set the int value to anything you wish
*Press the 'Simple Randomised Truss' button.

Let me know mate.

Cheers,
Dr Merman
User avatar
By Joss
#196810
Probably "$selShape" contains only 1 shape.
I think generally it's not good idea to get shapes with "pickWalk". It's better to do this with "listRelatives -s", although it can cause problems too in some rare cases(transforms with multiple shapes).

And, you have to use more fail-safe checks like:
if (size($selShape) < 2){print("bla-bla-bla\n"); return(stuff);}

Actually to make script more bulletproof, you also have to be sure, that there's _real_ transforms selected before getting shapes.
User avatar
By deadalvs
#196818
HA !

* * *

1) the $counter wasn't defined before the while loop thus it was zero and didn't create the lines.

2) Joss gave me an idea. i always only created the first plane and copied it (i'm a lazy bastard). this never worked. solution: just create two planes completely newly.

thanks until now... i work on now... :)

* * *

deadalvs

* * *

script:

if (`window -exists "TrussWindow"` == 1){deleteUI "TrussWindow";}

window -title "TrussCreateWindow" -mnb 0 -mxb 0 TrussWindow;
columnLayout -adjustableColumn 1;
setParent ..;
setParent ..;

frameLayout -label "Create Trusses" -cll true;
columnLayout;
button -label "Get two selected Nurbs" -command "getNurbs();";
separator -w 300;
text -l "Number of randomized sticks";
intField stickCount;
button -label "Simple randomized Truss" -command "randTruss `intField -q -v stickCount`";
separator -w 300;

showWindow TrussWindow;



/*************************************************************************/
/*************************************************************************/
/*************************************************************************/
 
global proc getNurbs()
//gets the names of the 2 nurbs surfaces, and adds these names to 2 global variables. repeat for the shape nodes.
{
//first define the 4 global variables needed. global variables can be used to transfer data between procs.
//in order to avoid the "must be given a constant value" error, you first create the variable, and then give it a value.

global string $nurbsTrans1;
global string $nurbsTrans2;
global string $nurbsShape1;
global string $nurbsShape2;

//find the 2 selected nurbs surfaces
string $sel[] = `ls -sl`;

//add those two nurbs surfaces to the global variables we have already created.
$nurbsTrans1 = $sel[0];
$nurbsTrans2 = $sel[1];

//now repeat the process for the shape nodes
pickWalk -d down;
string $selShape[] = `ls -sl`;
$nurbsShape1 = $selShape[0];
$nurbsShape2 = $selShape[1];
print $sel;
}

/*************************************************************************/

global proc randTruss(int $stickCount)
{

//as you have already done for the $stickCount variable, you need to reassign the global variables in this proc.
//in the rest of the code, I have replaced instances of '$selShape' with the global variables created above.
//$sel[0] becomes $nurbsTrans1
//$sel[1] becomes $nurbsTrans2
//$selShape[0] becomes $nurbsShape1
//$selShape[1] becomes $nurbsShape2

global int $stickCount;
global string $nurbsTrans1;
global string $nurbsTrans2;
global string $nurbsShape1;
global string $nurbsShape2;


//select "STAB*";
//delete;

vector $vectorP1;
vector $vectorP2;

int $U1 = `nurbsPlane -q -patchesU $nurbsShape1`;
int $V1 = `nurbsPlane -q -patchesV $nurbsShape1`;
int $U2 = `nurbsPlane -q -patchesU $nurbsShape2`;
int $V2 = `nurbsPlane -q -patchesV $nurbsShape2`;



int $counter = $stickCount;


//sticks are created until the stickCount is reached. they only get created if the randomized points of the two surfaces are less than
//2.5 units away from each other.

while ($counter < $stickCount)
{
global int $stickCount;
string $sel[];
vector $vectorP1;
vector $vectorP2;

int $ZufallU1 = `floor (rand (0, ($U1 + 3)))`;
int $ZufallV1 = `floor (rand (0, ($V1 + 3)))`;
int $ZufallU2 = `floor (rand (0, ($U2 + 3)))`;
int $ZufallV2 = `floor (rand (0, ($V2 + 3)))`;

$vectorP1 = `pointPosition ($nurbsTrans1 + ".cv" + "[" + $ZufallU1 + "][" + $ZufallV1 + "]")`;
$vectorP2 = `pointPosition ($nurbsTrans2 + ".cv" + "[" + $ZufallU2 + "][" + $ZufallV2 + "]")`;
print $vectorP1;
print $vectorP1;

$squaredDist =`pow ($vectorP2.x - $vectorP1.x) 2` + `pow ($vectorP2.y - $vectorP1.y) 2` + `pow ($vectorP2.z - $vectorP1.z) 2` ;
$dist = `sqrt $squaredDist`;

if ($dist <= 2.5)
{
curve -d 1 -p ($vectorP1.x) ($vectorP1.y) ($vectorP1.z) -p ($vectorP2.x) ($vectorP2.y) ($vectorP2.z) -k 0 -k 1 ;
rename "STAB_";
$counter ++;
}

}

print "\nEnd of Proc";
}

Hello everybody, We have just released a new vers[…]

Help with swimming pool water

Nothing beats observing the real world or, if that[…]

Sketchup 2026 Released

Considering how long a version for Sketchup 2025 t[…]

Greetings, One of my users with Sketchup 2025 (25[…]