PDA

View Full Version : Data Import Help Needed.


srafx
11-20-2008, 01:31 PM
I used a sample script and modified it. I sware at one point that this code actually opened a dialog so I can select the data file. Currently when I run this code nothing happens. I even tried to set the strings to the specific data file and objectname.


global proc getAnim(string $fileName, string $objectName)

{

//open the file DADs data file

$fileId=`fopen $fileName "r"`;


//read line one

string $nextLine = `fgetline $fileId`;


//Looks for end
while ( size( $nextLine ) > 0 ) {

//split line into separate elements

string $rawAnimArray[];

tokenize ($nextLine, " ",$rawAnimArray);

//place each element of the array into separate variables

print $rawAnimArray;
float $frame=$rawAnimArray[0];
float $tx=$rawAnimArray[1];
float $ty=$rawAnimArray[2];
float $tz=$rawAnimArray[3];
float $rx=$rawAnimArray[4];
float $ry=$rawAnimArray[5];
float $rz=$rawAnimArray[6];

//change the currentTime
currentTime $frame ;

//sets objects translation x,y,z

setAttr ($objectName+".tx") $tx;
setKeyframe ($objectName+".tx");

setAttr ($objectName+".ty") $ty;
setKeyframe ($objectName+".ty");

setAttr ($objectName+".tz") $tz;
setKeyframe ($objectName+".tz");


//sets objects rotation x,y,z

setAttr ($objectName+".rx") $rx;
setKeyframe ($objectName+".rx");

setAttr ($objectName+".ry") $ry;
setKeyframe ($objectName+".ry");

setAttr ($objectName+".rz") $rz;
setKeyframe ($objectName+".rz");


//next line and loop

$nextLine = `fgetline $fileId`;

}

//close file

fclose $fileId;

}



This is the data I am trying to import (t,x,y,z,h,p,r)


0.00000000 0.00000000 0.00000000 0.00000000 -0.00000000 0.00000000 -0.00000000
0.05000000 0.00000000 0.25000000 0.00000000 -0.00000000 0.00000000 -0.00000000
0.10000000 0.00000000 0.49999999 0.00000000 -0.00000000 0.00000000 -0.00000000
0.15000001 0.00000000 0.75000001 0.00000000 -0.00000000 0.00000000 -0.00000000
0.20000000 0.00000000 0.99999999 0.00000000 -0.00000000 0.00000000 -0.00000000
0.25000000 0.00000000 1.25000004 0.00000000 -0.00000000 0.00000000 -0.00000000
0.30000001 0.00000000 1.50000002 0.00000000 -0.00000000 0.00000000 -0.00000000
0.34999999 0.00000000 1.75000000 0.00000000 -0.00000000 0.00000000 -0.00000000
0.40000001 0.00000000 1.99999998 0.00000000 -0.00000000 0.00000000 -0.00000000
0.44999999 0.00000000 2.24999995 0.00000000 -0.00000000 0.00000000 -0.00000000
0.50000000 0.00000000 2.50000008 0.00000000 -0.00000000 0.00000000 -0.00000000
0.55000001 0.00000000 2.74999991 0.00000000 -0.00000000 0.00000000 -0.00000000
0.60000002 0.00000000 3.00000004 0.00000000 -0.00000000 0.00000000 -0.00000000
0.64999998 0.00000000 3.24999987 0.00000000 -0.00000000 0.00000000 -0.00000000
0.69999999 0.00000000 3.49999999 0.00000000 -0.00000000 0.00000000 -0.00000000
0.75000000 0.00000000 3.75000012 0.00000000 -0.00000000 0.00000000 -0.00000000
0.80000001 0.00000000 3.99999995 0.00000000 -0.00000000 0.00000000 -0.00000000
0.85000002 0.00000000 4.25000008 0.00000000 -0.00000000 0.00000000 -0.00000000
0.89999998 0.00000000 4.49999991 0.00000000 -0.00000000 0.00000000 -0.00000000
0.94999999 0.00000000 4.75000003 0.00000000 -0.00000000 0.00000000 -0.00000000
1.00000000 0.00000000 5.00000016 0.00000000 -0.00000000 0.00000000 -0.00000000
1.04999995 0.00000000 5.24999999 0.00000000 -0.00000000 0.00000000 -0.00000000
1.10000002 0.00000000 5.49999982 0.00000000 -0.00000000 0.00000000 -0.00000000
1.14999998 0.00000000 5.75000024 0.00000000 -0.00000000 0.00000000 -0.00000000
1.20000005 0.00000000 6.00000007 0.00000000 -0.00000000 0.00000000 -0.00000000
1.25000000 0.00000000 6.24999991 0.00000000 -0.00000000 0.00000000 -0.00000000
1.29999995 0.00000000 6.49999974 0.00000000 -0.00000000 0.00000000 -0.00000000
1.35000002 0.00000000 6.75000016 0.00000000 -0.00000000 0.00000000 -0.00000000
1.39999998 0.00000000 6.99999999 0.00000000 -0.00000000 0.00000000 -0.00000000
1.45000005 0.00000000 7.24999982 0.00000000 -0.00000000 0.00000000 -0.00000000
1.50000000 0.00000000 7.50000024 0.00000000 -0.00000000 0.00000000 -0.00000000


Any help would be appreciated!

Thanks in advance.

Steve

kattkieru
11-20-2008, 04:51 PM
The data you cut and pasted has tabs, not spaces, as delimiters, so the function is failing. Also, I'm not so sure you want to be splitting it up when there are multiple spaces between data, as it could lead to trouble.

I fixed it by putting a single comma, no space, between each bit of data and then doing tokenize with a comma as the delimiter.

If you've written the exporter for this data, though, I'd highly recommend just having your exporter write out MEL code. Then you can source the MEL arrays in and just go instead of having to work with tokenize.

srafx
11-20-2008, 05:16 PM
I wish I could change the export but I have no control over that end. Is there anyway to modify my code to import the data the way it is?

Keilun
11-20-2008, 06:28 PM
I wish I could change the export but I have no control over that end. Is there anyway to modify my code to import the data the way it is?

You can tokenize about <tab> characters using the "\t" character.


tokenize( $nextLine, "\t", $buffer );

srafx
11-20-2008, 06:49 PM
Maybe I am not understanding...I get an error message:

// Error: Invalid call to "tokenize". Check number and types of arguments expected by the procedure. //

Keilun
11-20-2008, 08:02 PM
Double check your syntax.

You want to change this line:

tokenize ($nextLine, " ",$rawAnimArray);

To this line:

tokenize ($nextLine, "\t",$rawAnimArray);

I tested this with the following code and it works fine.

string $testStr = "Tab0 Tab1 Tab2 Tab3";
string $buffer[];
tokenize $testStr "\t" $buffer;
print $buffer;

Note: I just copied and pasted, so you'll want to insert actual tabs between the Tab# in the string to verify that it does work.

srafx
11-21-2008, 12:39 PM
Ok, thanks for that example. I'm not even sure what I was doing wrong but I got it to work this morning.

My question now is. Lets say I have 20 objects. I name the objects the same name as each of their data.txt file. For example:

pCube1 is the object and pCube1.txt is the data file.

What would be the easiest way to apply the data to each object accordingly. My idea would read the first file, then get the name of the file, select the object with that name, then apply it to the object. Then move to the next file in the directory.

Is there a better way?

Thanks!

NaughtyNathan
11-21-2008, 01:45 PM
guys, you usually don't need to distinguish between different whitespace charaters in most programming languages/functions and MEL tokenize is no exception. if you simply omit the delimiter flag the function defaults to whitespace. This is much safer and easier than specifying something.

tokenize $testStr $buffer;

as for your question about file/object names, no, what you've already suggested seems the most obvious and simple route to take. bear in mind though that short object names alone may not be enough if you have objects in heirarchies (e.g. you may have more than one "pCube1")

also, 99% of functions don't require you to explicitely select an object. Changing the selection during a script should always be avoided. You should be able to act upon an object by specifying it's name, don't rely on selection.

:nathaN

Keilun
11-21-2008, 03:18 PM
Yup that's a good point. Although in this case, he has tabbed spaces, which I don't believe are interpreted by MEL as whitespace.

NaughtyNathan
11-21-2008, 03:27 PM
Yup that's a good point. Although in this case, he has tabbed spaces, which I don't believe are interpreted by MEL as whitespace.

sure they are, and any number or combination of each... there'd be little point in designating a whitespace entity if it didn't include all whitespace... :)
:nathaN

Keilun
11-21-2008, 03:44 PM
Oh hey, whadya know. Didn't realize it captured tabs too. Cool.

ewerybody
11-24-2008, 10:46 AM
You can easily have both with stringToStringArray as well.
Now it does not work without givin a dilimiterString but just put " " and "\t" in it works:

{
// string with tabs and spaces:
string $bla = "asdfsad asdfasd sdfasdf";
string $array[] = stringToStringArray($bla," \t");
for ($a in $array)
print("$a: \"" + $a + "\"\n");
}

I avoid using tokenize because it feels bulky: You'd have to create the array first, then stuff it with the tokens. And it returns the number of tokens by default which you don't always need.
stringToStringArray of course uses tokenize in the background. But it works more convenient just like all other commands in maya.

Maybe one could actually override the stringToStringArray proc to make tokenize use the default whitespace if you say stringToStringArray $string "":

global proc string[] stringToStringArray(string $string, string $separationString)
{
string $array[] = {};
if (size($string))
{
// empty separationString: tokenize uses default whitespace
if ($separationString == "")
tokenize $string $array;
else
tokenize $string $separationString $array;
}

return $array;
}

CGTalk Moderation
11-24-2008, 10:46 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.