Derek Wolfe
07-26-2007, 06:48 PM
For a file at: C:\tmp\positions.txt
with comma-sepatated values like this:
1,2,3
1,1,1
2,2,2
3,3,3
float $x, $y, $z;
int $index = 0;
$fileId=`fopen "C:/tmp/positions.txt" "r"`;
while ( !feof($fileId) ) {
$nextLine = fgetline($fileId);
string $buffer[];
tokenize $nextLine "," $buffer;
$x = $buffer[0];
$y = $buffer[1];
$z = $buffer[2];
$index++;
$sphere = `sphere -r 2 -name ("sphere_" + $index)`;
setAttr ($sphere[0] + ".translateX") $x;
setAttr ($sphere[0] + ".translateY") $y;
setAttr ($sphere[0] + ".translateZ") $z;
}
fclose $fileId;
strarup
11-02-2007, 08:10 AM
Hi,
well might be a quite late reply.. however hope it can be to some use... e.g. also if someone else with a similar problem should struckle with it...
and looking for some ideas to how to solve it... :)
will try to explain what I have done...
well first I have added a filedialogUI thingie... makes it more flexible... and you can choose from where you want to get your file from...'
from within the UI and when opening the file... 2 processes occurs... first it get the values for the position of the sphere it's going to create...
creates the sphere... and then it goes get the keyframes and set them... if there is any... could also have added a thingie for the radius of the sphere...
however seemed you want to keep it at a radius of 2... so haven't added it...
the keyframe example is just an example on how it can be done... the way I have chosen to do it in this example... is kinda like multi dividing a string...
I have chosen 3 token chars... @,§ and £... also modified the tokenizelist cmd from maya which can make a string into an string array... to be able to choose
which tokenchar I wanna to use instead of only use the comma as tokenchar to get this function...
e.g. I used @ to get the line divided into the 3 positions and the keyframe string...
whereafter I use § to divide the keyframe string... and at last divide that with £ to get the framenr/time, values and attributes...
e.g. this line 1.0 @ 2.0 @ 3.0@ fr1£tx1.0£ty2.0£tz3.0£rx0.0£ry0.0£rz0.0£§fr5£tx-2.0£ty4.0£tz6.0§fr10£tx-6.0£ty7.0£tz8.0£§@
will make a sphere and move it to the position xyz[1.0, 2.0, 3.0] and set keyframes at frame 1 with the given values and attributes... and also the same for frame 5 and 10...
e.g. §fr10£tx-6.0£ty7.0£tz8.0£§ will set a key at frame 10 for translateX with a value of -6.0, translateY with a value of 7.0 and for translateZ with a value of 8.0...
if you want to add extra stuff just remember what the different stuff does.... e.g. @ divides it into the first indexes that is beeing used...
so e.g. let's say we want to add a radius to it... in this case I choose to put it after the keyframe string... so I don't need to change much in the example
and make it more confusing... :)
1.0 @ 2.0 @ 3.0@ fr1£tx1.0£ty2.0£tz3.0£rx0.0£ry0.0£rz0.0£§fr5£tx-2.0£ty4.0£tz6.0§fr10£tx-6.0£ty7.0£tz8.0£§@ 2.0 @
and in the script add this
if($i == 4)
{
//index 4 = the line with radius info...
$daRadius = $curLine[$i];
}
and to the sphere creation line change it to...
$daSphere = `sphere -r $daRadius -name $daSphereName`;
I have also added an little example of how to produce/make the textfile with mel... would be pretty anoying to type in those values and tokenchars...
might not be the best example but the best I could make at 4 o'clock in the night with no more coffee...
and no options to get any new coffee because my cell door is locked :(
not sure where the data that the values you use comes from... however if it's from maya... you can use the "writeDaPlotFile"...
otherwise I'm not so sure how useful it is... :)
hope it helps a bit thou anyway... :)
damn still a couple of hours before I can get some coffee... so sorry for not explaining the write file script thingie...
just to tired... wrote a couple of comments thou...
regards
Strarup
textfile used as example for the code script example...
1.0 @ 2.0 @ 3.0@ fr1£tx1.0£ty2.0£tz3.0£rx0.0£ry0.0£rz0.0£§fr5£tx-2.0£ty4.0£tz6.0§fr10£tx-6.0£ty7.0£tz8.0£§@
1.0 @ 1.0 @ 1.0@ fr1£tx1.0£ty1.0£tz1.0£rx0.0£ry0.0£rz0.0£§fr5£tx2.0£ty-4.0£tz-6.0§fr10£tx6.0£ty-7.0£tz-8.0£§@
2.0 @ 2.0 @ 2.0@ fr1£tx2.0£ty2.0£tz2.0£rx0.0£ry0.0£rz0.0£§fr5£tx10.0£ty6.0£tz8.0§fr10£tx4.0£ty3.0£tz10.0£§@
3.0 @ 3.0 @ 3.0@ fr1£tx3.0£ty3.0£tz3.0£rx0.0£ry0.0£rz0.0£§fr5£tx0.0£ty0.0£tz0.0§fr10.0£tx0.0£ty10.0£tz0.0£§@
and the code for the script... :)
proc readAndPlotDaSpheres( )
{
if ( (`window -exists readAndPlotDaSpheresUI`) == true ) deleteUI readAndPlotDaSpheresUI;
window -title "Open up your textfile" -wh 400 150 readAndPlotDaSpheresUI;
columnLayout -adjustableColumn true;
//Start of the column.
textFieldButtonGrp
-cw 1 96 -label "File Path" -buttonLabel "Get it"
-bc "string $daPath = `fileDialog`;textFieldButtonGrp -e -text $daPath daRaPSFilepath;runAndPlotDaFile($daPath)"
daRaPSFilepath;
//textField daRaPFfilecontent;
//End of the column.
setParent..;
showWindow readAndPlotDaSpheresUI;
}
proc runAndPlotDaFile(string $daPath)
{
$fileId=`fopen $daPath "r"`;
string $nextLine = `fgetline $fileId`;
//$nextLine = `fgetline $fileId`; // skipping the first line and read in the next
float $pos[3];
string $daKeyFrameStr;
int $lineNr;
string $curLine[];
while ( size( $nextLine ) > 0 )
{
// makes the line in to an array
//tokenizeList($nextLine, $curLine);
//using modified Tokenizelist cmd to be able to choose
//which tokenchar to use... in this case the @
daTokenizeList($nextLine, $curLine, "@");
if (size($curLine) < 2)
{
//print("finished...");
break;
}
int $i, $j;
for($i=0; $i< size($curLine); $i++)
{
//remove "whitespaces" from the current object...
$curLine[$i] = `strip $curLine[$i]`;
if($i == 0)
{
//index 0 = the X position...
$pos[$lineNr] = $curLine[$i];
}
if($i == 1)
{
//index 1 = the Y position...
$pos[$lineNr] = $curLine[$i];
}
if($i == 2)
{
//index 2 = the Z position...
$pos[$lineNr] = $curLine[$i];
}
if($i == 3)
{
//index 3 = the line with keyframes info...
$daKeyFrameStr = $curLine[$i];
}
}
$daSphereName = ("sphere_" + ($lineNr+1));
$daSphere = `sphere -r 2 -name $daSphereName`;
move -ws $pos[0] $pos[1] $pos[2] $daSphere;
//or use this... -->
//setAttr ($daSphereName+".t") $pos[0] $pos[1] $pos[2];
//make keyframes if any exist...
if($daKeyFrameStr != "")
{
doDaPlotDaKeyframesForDaSphere($daSphereName,$daKeyFrameStr);
}
$daKeyFrameStr = "";
$nextLine = `fgetline $fileId`;
$lineNr++;
}
fclose $fileId;
}
proc doDaPlotDaKeyframesForDaSphere(string $daObj, string $daKeyframeStr)
{
string $daKeyLine[];
string $daVal[];
daTokenizeList($daKeyframeStr, $daKeyLine, "§");
string $daNrVal, $daAttrN, $daObjAttr;
int $daTime;
float $daAttrVal;
int $i, $j;
for($i=0; $i<size($daKeyLine); $i++)
{
daTokenizeList($daKeyLine[$i], $daVal, "£");
for($j=0; $j<size($daVal); $j++)
{
//remove "whitespaces" from the current object...
$daVal[$j] = `strip $daVal[$j]`;
//get the number value...
$daNrVal = endString($daVal[$j], (size($daVal[$j])-2));
//remove number values and get attr name...
$daAttrN = substitute($daNrVal, $daVal[$j], "");
if($j == 0)
{
//"remove" fr and keep number value only...
$daVal[0] = $daNrVal;
}
else
{
$daAttrVal = $daNrVal;
$daObjAttr = ($daObj+"."+$daAttrN);
setKeyframe -v $daAttrVal -t $daVal[0] $daObjAttr;
}
}
}
}
//modified Alias Maya tokenizelist cmd script function to include the token char...
proc int daTokenizeList( string $list, string $tokenizedList[], string $daTChar )
{
string $regExpr = $daTChar; // <--- modification... = ",";
string $replaceWith = " ";
string $substitutedList = "";
// Remove the commas, if any, and replace them with spaces
//
while( $substitutedList != $list ) {
// Do this only after the first time.
//
if( $substitutedList != "" ) {
$list = $substitutedList;
}
$substitutedList = `substitute $regExpr $list $replaceWith`;
}
// Check if $llist is empty.
//
if( size( $list ) == 0 ) {
return 0;
}
tokenize( $list, $tokenizedList );
return 1;
}
readAndPlotDaSpheres;
and the code for the writing of the file... :)
proc writeDaPlotFile()
{
fileBrowserDialog -m 1 -fc "saveDaPlotFile" -ft "text" -an "save" -fl "text,;*.txt" -ds 2 -wt "Save Da Plot File...";
}
proc int saveDaPlotFile( string $daFilename, string $daFileType )
{
string $daExt = ".txt";
//getting the file name wtriten in the browser thingie
file -rn -s ($daFilename+$daExt);
string $daCmd = `makeDaPlotSpheresFile`;
//making file with the data from the cmd...
$fileId=`fopen $daFilename "w"`;
fwrite $fileId $daCmd;
fclose $fileId;
return 1;
}
proc string makeDaPlotSpheresFile()
{
string $daSel[] = `ls -sl -l`;
int $daStart = `playbackOptions -q -min`;
int $daEnd = `playbackOptions -q -max`;
int $curTime = `currentTime -q`;
if($curTime != $daStart)
{
//if it isn't at start move there...
//to get the start position to place the sphere...
currentTime -e $daStart;
}
float $daPos[];
string $daSelPlotStr[];
string $daPosStr, $daKeyStr, $daCmd;
for($i=0; $i<size($daSel); $i++)
{
$daKeyStr="";
$daPos = `xform -q -ws -rp $daSel[$i]`;
$daPosStr = $daPos[0]+" @ "+$daPos[1]+" @ "+$daPos[2]+" @ ";
$daKeyStr = getDaPlotKeyframes($daSel[$i]);
$daSelPlotStr[$i]= $daPosStr+$daKeyStr;
}
for($j=0; $j<size($daSelPlotStr); $j++)
{
$daCmd = $daCmd+$daSelPlotStr[$j]+"\r\n";
}
return $daCmd;
}
proc string getDaPlotKeyframes(string $daObj)
{
float $flKeyList[], $flSoKeyList[], $daAttrVal[];
string $stKeylist[], $stRD_KeyList[], $daKeyedAttr[], $daKeyAttrStr[];
int $daKey;
string $daTempStr;
//get all the keyframes for the selected objects...
$flKeyList = `keyframe -q -tc $daObj`;
//and sort them...
$flSoKeyList = sort($flKeyList);
//append the keyframe float value to a string
//to make it able to remove redundant data...
for($i=0; $i<size($flKeyList); $i++)
{
$stKeylist[$i] = (""+$flSoKeyList[$i]);
}
//remove redundant keyframes from the list
$stRD_KeyList = stringArrayRemoveDuplicates($stKeylist);
for($j=0; $j<size($stRD_KeyList); $j++)
{
$daKey = $stRD_KeyList[$j];
//frame number at the start of the string...
$daKeyAttrStr[$j]= "fr"+$daKey+"£";
//getting the attributes which has keys on the current frame
$daKeyedAttr = getKeyedAttrNameAtTime($daObj, $daKey);
for($k=0; $k<size($daKeyedAttr); $k++)
{
//getting the value for the attribute at the given frame/time
$daAttrVal = `keyframe -t $daKey -q -vc($daObj+"."+$daKeyedAttr[$k])`;
//adding the snortname attribute together with the value and the £ tokenscar...
$daTempStr = $daKeyedAttr[$k]+$daAttrVal[0]+"£";
$daKeyAttrStr[$j] = $daKeyAttrStr[$j]+$daTempStr;
}
}
//making the array into an string and adding the § tokenchar...
$daTempStr =stringArrayToString($daKeyAttrStr, "§");
$daTempStr = $daTempStr+"@";
return $daTempStr;
}
proc string[] getKeyedAttrNameAtTime(string $daObj, int $daTime)
{
//bah... couldn't find an easier way to get those bloody attribut names...
//don't know if an easier way or new cmd's has become availible in maya 8.5
//or if it's lack of coffee... was considering doing a token "_" on the
//animcurves that the cmd spits out... however found this a bit more reliable...
string $daAnimCurves[] = `keyframe -t $daTime -q -n $daObj`;
string $daAttr[], $daSNattr[], $daKeyedAttr[];
for($i=0; $i<size($daAnimCurves); $i++)
{
//the attribute connected/affected by the animcurve
$daAttr = `listConnections -p 1 $daAnimCurves[$i]`;
//converting the attibute to it's short name...
$daSNattr = `listAttr -sn $daAttr[0]`;
$daKeyedAttr[$i]= $daSNattr[0];
}
return $daKeyedAttr;
}
writeDaPlotFile;
CGTalk Moderation
11-02-2007, 08:10 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.
vBulletin v3.0.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.