PDA

View Full Version : xyz spread sheets to maya


Ironhalo
04-04-2007, 10:40 PM
hey everyone, i need to input data from a spread sheet and have it assign key frames to an object. specifically, a car's position at a given point in time corresponding to a xyz translate and scale. the data is similar to mocap data, theres values corresponding to fractions of a second. im not exactly a scripting wizard yet, so any suggestions on how to tackle this would be much appreciated!

isoparmB
04-05-2007, 05:04 AM
How is the text file formatted? Does it store the location information in X Y Z a frame per line?

If you can figure out how it's formatted and that formatting is consistent, you can basically read that document into Maya and parse through each line, while extracting the relavent data.

For example, say I have a text document named location.txt located on your hard drive which contains something like this:

1.00000 2.324 0
1.00000 3.324 2
1.00000 4.324 4
1.00000 5.324 6
1.00000 6.324 8
1.00000 7.324 10
1.00000 8.324 12


Where each line corresponds to a frame (assuming the first line is frame 1). If you wanted it to drive the movement of an object, say "carProxy" in maya, you could write an expression which parses through the line with the relavent information for the current frame:

int $locationFile = `fopen "D:/location.txt" "r"`;
float $locationResult[];

int $lineFrame = 1;
string $currentLine = `fgetline $locationFile`;

while(!`eof $locationFile`)
{
if(frame == $lineFrame)
{
tokenize(" ", $currentLine, $locationResult);
carProx.tx = $locationResult[0];
carProx.ty = $locationResult[1];
carProx.tz = $locationResult[2];
break;
};
string $currentLine = `fgetline $locationFile`;
$lineFrame++;
};

fclose $locationFile;


Although this code could be more efficient because it reopens the file per frame. You could feasibly store the file information in a big float array once and have the expression get the info from there. You could also check out if the syntax is correct, I'm not on a workstation right now. But that's one general idea of parsing through a text file for position information. The key thing is figuring out what the format of your file is so you can parse though it properly, maya has a good toolset of language parsing functions.

Ironhalo
04-05-2007, 03:21 PM
thanks a lot for the help! my only real mel experience is a couple poorly written scripts and a fair amount of particle expressions. im still wrapping my mind around the code, but im confident i can make it work!

Ironhalo
04-09-2007, 04:25 PM
sorry its taken me a while to get back on this, i havent had a chance to really test out the script yet. formatting the information isnt a problem, i can even access lines in the file. however, when i try the entire script i get the following error:

// Error: if(frame == $lineFrame)
//
// Error: Line 8.11: Invalid use of Maya object "frame". //
// Error: carProxy.tx = $locationResult[0];
//
// Error: Line 11.35: Invalid use of Maya object "carProxy.tx". //

i ended up copying all your script and pasting it into the script editor. ive been reading over the mel reference and im not sure whats causing the error.

Ironhalo
04-09-2007, 09:22 PM
well, a guy from my office and i managed to figure it out. i formated the file as:

1,2,3,
4,5,6,
7,8,9,

its important that i had an extra empty line. not entirely positive why, but without that last space the code wouldnt work. heres teh code:

int $locationFile = `fopen "Z:/test.txt" "r"`;
string $buffer[];
int $lineFrame = 1;
//string $currentLine = `fgetline $locationFile`;

while(!`feof $locationFile`)
{
currentTime $lineFrame;
string $currentLine = `fgetline $locationFile`;
$numTokens = `tokenize $currentLine "," $buffer`;
xform -wd -t $buffer[0] $buffer[1] $buffer[2];
$lineFrame++;
}

fclose $locationFile;

for soem reason theres an error on the xform line, but the code works regardless. i enabled autokey and set a key on the first frame. the script did the rest. im sure theres a better way to do it, if anyone has some input id love to get pointers.

isoparmB
04-10-2007, 05:26 AM
Sorry for the late reply, just got off holyweek.

The code was originally meant to be put in an expression and not meant to be executed in the script editor, though it seems you've fixed that issue. That form of syntax for the code only works for expressions.

For your text file:

1,2,3
4,5,6
7,8,9

should be sufficient, as you do not need the last comma for the tokenize to work. You could even go for:
tokenize $currentLine $buffer
and have each number separated by blank spaces, but that's just a preference suggestion.

For the code, you wouldn't need $numTokens if your not using it anywhere else, but otherwise your code is fine. You could try to find the command for turning autokey on and off and incorporating it into your code as well, just a suggestion.

int $locationFile = `fopen "Z:/test.txt" "r"`;
string $buffer[];
int $lineFrame = 1;

while(!`feof $locationFile`)
{
currentTime $lineFrame;
string $currentLine = `fgetline $locationFile`;
tokenize $currentLine "," $buffer;
xform -wd -t $buffer[0] $buffer[1] $buffer[2];
$lineFrame++;
}

fclose $locationFile;

CGTalk Moderation
04-10-2007, 05:26 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.