View Full Version : Preference File (reading/saving data)

06 June 2007, 05:10 PM
Hi again fellow nerds!

As my MEL application is beginning to take its form, i am now
in the need of programming the preferences for this tool. That
means reading preference data from a file, making the MEL
application act/change according to that data and finally storage
preference configuration data back to this file.

I've been trying to find a way to do this, but i've done something
wrong, it seems. As i see it, the procedure for reading preference
data from another file, would look somewhat like:

- defining a string for the dir/filename
- using the fGetLine and cycle through every line in the file and execute those individual lines (eg. $StartupSubject = 2; - this is supposed to decide which subject should be shown at startup (for instance, subject 1 is 'Animation', subject 2 is 'Modeling' etc.))
- when all lines have been executed it's a matter of attaching the strings to the appropriate controls in the MEL application

writing preference data back to a file, i haven't thought about it yet, but i suppose it would be something like using the fOpen to open the file, deleting it's content, writing all the new content by a print command or something like that.

But, i have a problem with the cycle-through the lines in reading the data. That's my question, how would such code look like? .. mine returns only the first line in the file, it won't cycle through all the lines .. i won't put in my code in this thread as it is not on this computer. But if you know how to get this to work, please tell me! :)

06 June 2007, 01:06 PM
This should get you going on the right track. Don't have Maya to test it with, but all the commands should be the ones you need to use.

int $fileId = `fopen "C:/yourPreferencesFile.txt"`;

while(!feof $fileId)
string $line = `fgetline $fileId`;

Robert Bateman
06 June 2007, 09:39 AM
see optionVar in the mel command reference. Your prefs will then get added to the userPrefs.mel file.

06 June 2007, 12:53 PM
Alright, i've tried the first piece of code you wrote, just fairly more complex, because, yes it reads it, but doesn't execute the code within the pref file, apparently .. i'm gonna try again, i might have wrote it wrong or something (the first time i got it to work somewhat like it should it only read half the file and then it said it couldn't find procedure "g", which was caused by it didn't read the rest, it came to a global string from which it only read the g, thereby trying to find a procedure called "g", which it of course couldn't find).

optionVar .. hmm, i've been having some difficulties finding out exactly how to use that command, so i've kinda been avoiding it. I'll try and learn how to use it then. I actually don't want the prefs to be stored in the UserPrefs file, i rather want everything to be stored in the MEL app's own pref-file.

Thanks so far, hope it gets to work :)

06 June 2007, 01:15 PM
it seems like i started out with a mistake of some sort, but i did actually try that exact code you wrote, and then when it didn't work i build it into a fairly complex thing doing the exact same thing, although it read only half of it, why i don't know, but going back to basics for this instance worked. Thanks again! :)

CGTalk Moderation
06 June 2007, 01:15 PM
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.