Load variables from a text file (MEL)?

Load variables from a text file (MEL)?

Anonymous
Not applicable
1,315 Views
2 Replies
Message 1 of 3

Load variables from a text file (MEL)?

Anonymous
Not applicable

hello everyone, I need your help.
As indicated in the title, I want to load variables from a text file. I'm writing a single script.
At first, I generate a text file "C:/tmp/variable.txt" , (with a fopen) in which there is this:

string $VARMAX[] = {"nurbsSphere1","pCube1","nurbsCone2"};

secondly, I load it:
source "C:/tmp/variable.txt";
print $VARMAX;

it is now that the problem happens. If my file "C:/tmp/variable.txt" doesn't yet exist, Maya will tell me  'Cannot find file'. Logic.

So I try to put the command in a 'evalEcho',

pause -sec 1;
evalEcho "source \"C://xdk//variable.txt\";";
print $VARMAX;

I have this message: // Error: Line 3.14: "$VARMAX" is an undeclared variable. //
as if it was a procedure.

I need to have this text file to use it instantly or later.

0 Likes
Accepted solutions (2)
1,316 Views
2 Replies
Replies (2)
Message 2 of 3

vijaya.prakash
Alumni
Alumni
Accepted solution

Before executing the "source" command, you can get the file existing status using "filetest" command. Below is the exmaple:

 

if (`filetest -f "C://tmp//variable.txt"`) {
source "C://tmp//variable.txt";
print $VARMAX;
};

0 Likes
Message 3 of 3

SimonJWakley
Contributor
Contributor
Accepted solution

 

You could read out of the file and assign the line to you variable

 

// Here read them off disk if the file exists

// Open the file
// If we have a home directory, use that otherwise forget about it

 

string $setfile = getenv("HOME");

string $VARMAX[];


if (size($setfile))
{
    $setfile += "/myfile.txt";
    // Open the file
     $fileId = `fopen $setfile "r"`;
}

 

if ($fileId != 0)
{
    $varmax = `fgetline $fileId`;
    // "nurbsSphere1","pCube1","nurbsCone2" read from file hopefully 🙂
    fclose($fileId);
}

 

Not sure if it works or gives you what you want, but that's what I 'd try!

Simon

0 Likes