PDA

View Full Version : variable creation with EVAL not working


dvelasco
12-15-2012, 05:14 AM
Hello!

I need to create some variables with the eval command on-the-fly, and I must be doing something wrong! If I use "print" instead of eval, it will print exactly what I need (If I executed the printed commands one by one by hand, it would work). However, the command seems to not be interpreting whats inside. It almost seems like I need to eval again. Is this possible? The script follows:

select -r pSphere1;

global proc stretch (int $div)
{
// cria lattice
string $lat[] = eval ("lattice -divisions 2 " + $div + " 2 -cp -objectCentered true -ldv 2 2 2");
string $latTop[] = `pickWalk -d up`;
CenterPivot;
scale -r 1.2 1.2 1.2 ;
TemplateObject;

// variaveis
string $Clusters = `group -em`;
string $latClusters[];
string $sel[] = `ls -sl`;

// LATTICE -------------------------------------------------

int $latLoop = 0;
int $latLayer = $div - 1;
int $clusterNum = 1;

while ($latLoop < $div)
{

eval ("select -r " + $lat[0] + "Lattice.pt[0:1][" + $latLayer + "][0] " + $lat[0] + "Lattice.pt[0:1][" + $latLayer + "][1] ") ;
print ("select -r " + $lat[0] + "Lattice.pt[0:1][" + $latLayer + "][0] " + $lat[0] + "Lattice.pt[0:1][" + $latLayer + "][1] \n") ;

eval ("string $C" + $latLoop + "[] = `newCluster \"-relative -envelope 1\"`");
print ("string $C" + $latLoop + "[] = `newCluster \"-relative -envelope 1\"` \n");

eval ("$latClusters[" + $latLoop + "] = $C" + $latLoop + "[0]");
print ("$latClusters[" + $latLoop + "] = $C" + $latLoop + "[0] \n");

print ("Cluster " + $clusterNum + " = \"$C" + $latLoop + "[0]\" \n \n \n");

eval ("select -add " + $Clusters);
parent;

$latLoop = $latLoop + 1;
$latLayer = $latLayer -1;
}


// why does this next line not work? I want it to be a working variable -------------------------------
print $C6[0];
}
stretch 7;

dvelasco
12-15-2012, 05:23 AM
Heres another way that I tried. In my mind, I should get a list of clusters when I ask the script to print $latClusters, but it just gives me an empty response.


select -r pSphere1;

global proc stretch (int $div)
{
// cria lattice
string $lat[] = eval ("lattice -divisions 2 " + $div + " 2 -cp -objectCentered true -ldv 2 2 2");
string $latTop[] = `pickWalk -d up`;
CenterPivot;
scale -r 1.2 1.2 1.2 ;
TemplateObject;

// variaveis
string $Clusters = `group -em`;
string $latClusters[];
string $sel[] = `ls -sl`;

// LATTICE -------------------------------------------------

int $latLoop = 0;
int $latLayer = $div - 1;
int $clusterNum = 1;

while ($latLoop < $div)
{

string $temp1 = ("select -r " + $lat[0] + "Lattice.pt[0:1][" + $latLayer + "][0] " + $lat[0] + "Lattice.pt[0:1][" + $latLayer + "][1] ") ;
print ($temp1 + "\n");
eval $temp1;

string $temp2 = ("string $C" + $latLoop + "[] = `newCluster \"-relative -envelope 1\"`");
print ($temp2 + "\n");
eval $temp2;

string $temp3 = ("$latClusters[" + $latLoop + "] = $C" + $latLoop + "[0]");
print ($temp3 + "\n");
eval $temp3;

print ("Cluster " + $clusterNum + " = \"$C" + $latLoop + "[0]\" \n \n \n");

eval ("select -add " + $Clusters);


$latLoop = $latLoop + 1;
$latLayer = $latLayer -1;
}


print ("--------------------");
print $latClusters;

}
stretch 7;

dvelasco
12-16-2012, 01:01 AM
OK, this should make it pretty simple. What I want is the Cluster name, not "C0[0]" to be spit out. How can I get this? Select any object and run this script to get the result.

// ---------------------------------------- isolating problem

int $latLoop = 0;
string $temp;

eval ("string $C" + $latLoop + "[]");
eval ("$C" + $latLoop + " = `newCluster \"-relative -envelope 1\"`;");
$temp = ("$C" + $latLoop + "[0]");
print $temp;

// ----------------------------------------

zaskar
12-16-2012, 08:19 AM
If you want to create variables on the fly, you need to declare them globally:
int $latLoop = 0;
string $temp;

eval ("global string $C" + $latLoop + "[]");
eval ("$C" + $latLoop + " = `newCluster \"-relative -envelope 1\"`;");
eval("print $C" + $latLoop);

But i would suggest you doing it the common way:
int $latLoop = 0;
string $cluster[] = {};
string $clusterHandle[] = {};

string $tmp[] = `cluster -relative -envelope 1`;
$cluster[$latLoop] = $tmp[0];
$clusterHandle[$latLoop] = $tmp[1];

print("clusterNode: " + $cluster[$latLoop] + " / clusterHandle: " + $clusterHandle[$latLoop] + "\n");

dvelasco
12-16-2012, 08:31 PM
Thanks Zaskar! I felt like quite the newbie, but indeed I rarely code, and forgot about the global variable option! Im going to go back into the script with that in mind, thanks for the technique! Im sure that itll work now.

cheers!

--Diego

CGTalk Moderation
12-16-2012, 08:31 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.