PDA

View Full Version : Script works, but not within procedure


mlefevre
05-18-2009, 11:00 AM
Hi!

I'm having a bit of trouble with this. Executing the code without the procedure will work fine, but executing the procedure won't.

Here's a basic example. Something to do with eval.
Any sage advice appreciated! :)


global proc doThis()
{
string $tokenList[];
string $guideLoc[];
string $locPosList[];
string $locPosListString = "$locPosList = {\"-3 0 3\", \"3 0 3\", \"3 0 -3\", \"-3 0 -3\"};";
eval $locPosListString;

for ($i = 0; $i < size($locPosList); $i++)
{
tokenizeList($locPosList[$i], $tokenList);

//typecast locator positions to floats
float $guideLocPos[3] = { float($tokenList[0]), float($tokenList[1]), float($tokenList[2]) };

//position the locators
$guideLoc = `spaceLocator -p $guideLocPos[0] $guideLocPos[1] $guideLocPos[2]`;
}
}

strarup
05-18-2009, 11:48 AM
Hi Matt,


eh... do you need the eval for something in your script?

otherwise why not just put the values directly into $locPosList[] ?


proc doThis()
{
string $tokenList[];
string $guideLoc[];
string $locPosList[] = {"-3 0 3", "3 0 3", "3 0 -3", "-3 0 -3"};
for ($i = 0; $i < size($locPosList); $i++)
{
tokenizeList($locPosList[$i], $tokenList);
//typecast locator positions to floats
float $guideLocPos[3] = { float($tokenList[0]), float($tokenList[1]), float($tokenList[2]) };
//position the locators
$guideLoc = `spaceLocator -p $guideLocPos[0] $guideLocPos[1] $guideLocPos[2]`;
}
}
doThis;


kind regards

Strarup

mlefevre
05-18-2009, 12:27 PM
Hi Strarup

Thanks for the reply, your solution works great!
I went with eval, because it was possible the list of positions, e.g.
string $locPosList[] = {"-3 0 3", "3 0 3", "3 0 -3", "-3 0 -3"};
may have been huge, and I didn't want to end up with a very long line of code. I couldn't work out a way to concatenate stringArray's, but I've just found

appendStringArray

which does the trick nicely! I was having trouble with vector arrays, but I must have mistyped something. This also seems to do the trick, and gets around the tokenize / type casting.

vector $myVectorArray[] = {<<1.0,1.0,1.0>>, <<2.0,2.0,2.0>>};
vector $myVectorArrayAppend[] = {<<3.0,3.0,3.0>>, <<4.0,4.0,4.0>>};

for ($i = 0; $i < size($myVectorArrayAppend); $i++)
{
$myVectorArray[size($myVectorArray)] = $myVectorArrayAppend[$i];
}

Is there a better way to append to vector arrays?

Thanks again Strarup :)

strarup
05-18-2009, 11:04 PM
Hi Matt,

hehe... yeah eval can be quite useful... for an example on how to build up strings and use eval...
you can e.g. take a look at this post/thread... "polygon duplicate/writing/output/rebuilder something script... here ya go... :) (http://forums.cgsociety.org/showthread.php?t=559155)"...
also a bit with tokennizing making strings and split them up... maybe some of it from this post/thread might be useful... "File i/o question (newb) (http://forums.cgsociety.org/showthread.php?t=522598)"...
an example of using match and tokenize in this post/thread... "version control (http://forums.cgsociety.org/showthread.php?t=522772)"...
daScrubKeys function from the script at this thread/post... "scrub keyframe script availible... :-) (http://forums.cgsociety.org/showthread.php?t=559154)"... gives an example of sorting, appending and removing stuff from a array...

well there is lot of ways to stuff... ussually it depends on how you wanna use it... and what the values are based on...
here are some commands that might be useful aswell...

command: stringArrayCatenate

proc printDaStringArrayCatenateTest()
{
string $locPosList1[] = {"-3 0 3", "3 0 3"};
string $locPosList2[] = {"3 0 -3", "-3 0 -3"};
string $locPosList[]= `stringArrayCatenate $locPosList1 $locPosList2`;
print($locPosList);
}
printDaStringArrayCatenateTest;


modifying the stringArrayCatenate script from maya to make it work on vectorArray's...

proc vector[] vectorArrayCatenate(vector $vector1[], vector $vector2[] )
{
vector $vecArr[] ;
int $i ;
for( $i = 0 ; $i < size($vector1) ; $i++ )
{
$vecArr[$i] = $vector1[$i] ;
}
int $nr = size($vecArr) ;
for( $i = 0 ; $i < size($vector2) ; $i++ )
{
$vecArr[$nr+$i] = $vector2[$i] ;
}
return $vecArr;
}

proc printDaVectorArrayCatenateTest()
{
vector $myVectorArray1[] = {<<1.0,1.0,1.0>>, <<2.0,2.0,2.0>>};
vector $myVectorArray2[] = {<<3.0,3.0,3.0>>, <<4.0,4.0,4.0>>};
vector $myVectorArray[] = `vectorArrayCatenate $myVectorArray1 $myVectorArray2`;
print($myVectorArray);
}
printDaVectorArrayCatenateTest;


command: stringToStringArray
using your former example... without tokenizing... (the command does that)

proc doThis()
{
string $guideLoc[];
string $locPosList[] = {"-3 0 3", "3 0 3", "3 0 -3", "-3 0 -3"};

for ($i = 0; $i < size($locPosList); $i++)
{
string $guideLocPos[3] = stringToStringArray($locPosList[$i], " "); //<-----
//position the locators
$guideLoc = `spaceLocator -p (float($guideLocPos[0])) (float($guideLocPos[1])) (float($guideLocPos[2]))`;
}
}
doThis;


regarding appending vectorArray's here is an example by modifying the appendStringArray script from maya to make it work on vectorArray's...

proc appendVectorArray(vector $copyTo[], vector $copyFrom[], int $numberToCopy )
{
int $i;
int $j = size($copyTo);
for( $i = 0; $i < $numberToCopy; $i++, $j++ )
$copyTo[$j] = $copyFrom[$i];
}

proc printDaAppendVectorArrayTest()
{
vector $myVectorArray[] = {<<1.0,1.0,1.0>>, <<2.0,2.0,2.0>>};
vector $myVectorArrayAppend[] = {<<3.0,3.0,3.0>>, <<4.0,4.0,4.0>>};
appendVectorArray($myVectorArray, $myVectorArrayAppend, 2);
print($myVectorArray);
}
printDaAppendVectorArrayTest;


how would I do this... well really depends on what the purpose is...
e.g. let's say you have a selection of objects... and you want to place locators at their position...
you can either have it build up in a loop inside the proc... or by making an other proc do it...
like in this example...

proc doThis2()
{
string $daSel[] = `ls -sl -l`;
vector $locPosList[] = doDaFillUpDaVectorArray($daSel);
string $guideLoc[];
string $guideLocList[];
for($i = 0; $i < size($locPosList); $i++)
{
float $daPos[3] = $locPosList[$i];
//could just create the locator without a name variable...
//spaceLocator -p $daPos[0] $daPos[1] $daPos[2];
//however e.g I don't know if it's the meaning that the locators
//should have their pivot point at the origin 0 0 0
//or at the worldposition where they are placed...
//or you need to do more to the locators afterwards...
$guideLoc = `spaceLocator -p $daPos[0] $daPos[1] $daPos[2]`;
//xform -cp $guideLoc[0];
//if you e.g. need to do something to the locators afterwards
//place it in the $guideLocList[]
$guideLocList[$i] = $guideLoc[0];
}
for($j = 0; $j < size($guideLocList); $j++)
{
//e.g. center the pivot or something else...
xform -cp $guideLocList[$j];
}
}

//making an vector array based on the worldposition of the selected object's...
proc vector[] doDaFillUpDaVectorArray(string $daArr[])
{
vector $daVectorArray[];
float $daWpos[3];
for($i=0; $i<size($daArr);$i++)
{
$daWpos = `xform -q -ws -rp $daArr[$i]`;
$daVectorArray[$i] = <<$daWpos[0], $daWpos[1], $daWpos[2]>>;
}
return $daVectorArray;
}
doThis2;


however if the positions are based on components like vertices, cv's or something like that xform doesn't give
the worldpos but pointPosition does... so again depends on what it should be used for... and how the data is
gathered etc... lot's of posibilities... could make doDaFillUpDaVectorArray work with vertices and cv's...
just need to change and add some stuff for that...

if you e.g. could give a bit more info on what you want to archieve and do... it might be a bit easier to help...
however hope some of this has been helpful and you could use some of it... :)


kind regards

strarup

mlefevre
05-19-2009, 08:50 AM
Strarup!

Thanks for your detailed reply, it's been extremely helpful!

I checked out the links to some of your previous stuff, and the examples you've put on here!

Using the locators was just an example, but accessing and using position data in the ways you've outlined is again, very helpful for all kinds of tasks.

Thanks again!

strarup
05-20-2009, 12:27 AM
Hi Matt,


you're welcome... and glad some of it was helpful and to any use... :)

I have an idea... to make a generic position function script... so you e.g. can get the position data back as either a float array or vector array... and working on components and objects... had a little brainfart... so far the idea is just in my head but tired... so bumping it here to remind me of writing it down... :D

kind regards

Strarup

CGTalk Moderation
05-20-2009, 12:27 AM
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.