PDA

View Full Version : Exporting custom values to a text file.


Ian00
04-17-2008, 08:40 PM
Hello, I am fairly new to maxscript, so any help, would of course be greatly appreciated. I have searched through cgtalk and scriptspot, but I couldn't find much. I am trying to find a way of exporting values, such as values in an attribute holder modifier, vertex positions in an object, and object transforms in max, to a text file to aid in game modding. Out of these I would mostly need to export custom attribute values.

JHN
04-18-2008, 12:00 PM
Maybe you should have searched the maxscript manual first :)
Search for "write to file", it will get you the "FileStream Values" topic to start with.
Also have a look at "Accessing INI File Keys".

Good luck!
-Johan

Ian00
04-18-2008, 12:18 PM
Thanks for the reply. :) It looks like this may be alot more complicated than I thought. ^^ But the example about weapondataCA may be what I am looking for. Its still seems pretty complicated, maybe if I had an example of a sphere, with just the radius value exported to a txt file on the C:\ drive would help.^^

ZeBoxx2
04-18-2008, 01:16 PM
assuming your sphere is Sphere01:


f = createFile "c:\\somefile.ext" -- create a file handle ready for output
format "Sphere radius: %\n" $Sphere01.radius to:f -- output the given string + radius to the file
close f -- make sure we close the file once we're done with it


That's pretty much the basics of it :)

If "c:\\somefile.ext" already exists, createFile will fail, and you'll have to use openFile instead:


f = openFile "c:\\somefile.ext" mode:"at" -- open the file, append to end, text
format "Sphere radius: %\n" $Sphere01.radius to:f -- output the given string + radius to the file
close f -- make sure we close the file once we're done with it


( Of course, openFile fails if the file does not exist. Isn't that fun? )

JHN
04-18-2008, 01:25 PM
-- Create a new file and get the fileStream
f = createFile "c:\\sphere_radius.txt"
-- Format a string to the fileStream
format "sphereRadius = %" $sphere01.radius to:f
-- Flush the fileStream from memory to disk
flush f
-- Close the fileStream
close f


Something like this?
-Johan

JHN
04-18-2008, 01:27 PM
He ZeBoxx2,

Saw you posted right before me :)
Don't you need to flush the filestream before closing?

-Johan

Ian00
04-18-2008, 01:31 PM
Tanx you! :D Ok one more thing I need to ask, how would you export the same sphere with a new attribute holder modifier on it, also exporting the value from a single spinner called float01? Thanks again for your help.

JHN
04-18-2008, 01:37 PM
-- Create a new file and get the fileStream
f = createFile "c:\\attribute_name.txt"
-- Format a string to the fileStream
format "attribute_name = %" $sphere01.modifiers['modifier_name'].attribute_name to:f
-- Flush the fileStream from memory to disk
flush f
-- Close the fileStream
close f

Ian00
04-18-2008, 01:52 PM
Thanks that it seems like that should work but when I name the spinner, attribute_name and the modifier, modifier_name on Sphere01, it gives me and error doing the code in maxscript window and evaluating it the listener says-

-- Unknown property: "pos" in undefined-- Create a new file and get the fileStream
<File:c:\attribute_name.txt>
-- Error occurred in anonymous codeblock; filename: ; position: 115
-- Type error: Modifier array index must be number or name, got: undefined
OK
OK

-- No ""close"" function for undefined

and the txt file is blank.

Oh and here is what I put into the maxscript editor also btw,

f = createFile "c:\\attribute_name.txt"
format "attribute_name = %" $sphere01.modifiers['modifier_name'].attribute_name to:f
flush f
close f
and it still gives an error saying

<File:c:\attribute_name.txt>
-- Error occurred in anonymous codeblock; filename: ; position: 77
-- Type error: Modifier array index must be number or name, got: undefined
OK
OK

ZeBoxx2
04-18-2008, 01:58 PM
Don't you need to flush the filestream before closing?
It's good form to do so, but close() flushes as well. Using flush() can be good if you're writing a lot of data and A. you think it might die somewhere along the way, then at least you'll have the data up to that flush() guaranteed, and B. you have another application accessing that file (while open and being written to - eek) and it needs the latest and greatest in there.

Ian00
04-18-2008, 02:23 PM
Ok, I tried renaming the modifier, and attribute, and put in,

f = createFile "c:\\attribute_name.txt"
format "float01 = %" $sphere01.modifiers['custom01'].float01 to:f
flush f
close f

and the error still is,

<File:c:\attribute_name.txt>
-- Error occurred in anonymous codeblock; filename: ; position: 70
-- Type error: Modifier array index must be number or name, got: undefined
OK
OK

and the file still is blank. :/ I also made sure to delete the file, before I evaluated btw.

handiklap
04-18-2008, 02:48 PM
Ok, I tried renaming the modifier, and attribute, and put in,

f = createFile "c:\\attribute_name.txt"
format "float01 = %" $sphere01.modifiers['custom01'].float01 to:f
flush f
close f

and the error still is,

<File:c:\attribute_name.txt>
-- Error occurred in anonymous codeblock; filename: ; position: 70
-- Type error: Modifier array index must be number or name, got: undefined
OK
OK


and the file still is blank. :/ I also made sure to delete the file, before I evaluated btw.

You need to put in the actual name of your modifier you're trying to access, not 'custom01'. Alternately, since the mod stack index is 1-based starting at the top of the stack, if it's always in the same place in the mod stack, you could use the index of that modifier, like

$sphere01.modifiers[1].float01

In this case, "float01" should be replaced by whatever name you've given the value you're trying to retrieve from the modifier at the top of the stack (modifiers[1])


Also, you're probably going to want your formatted string to include the "\n" escape character;
format "float01: %\n"
This will add a new line, otherwise it's going to be hard to decipher, as it will be all on the same line without any breaks.

edit: and afaik, it's not necessary to delete your txt file before running the script. I've got a log file that gets "created" every day, and I've never had to delete it before it gets created in the script. Having it open, well that may be another matter.

Ian00
04-18-2008, 03:10 PM
^^ Yaaay! thanks alot, that works like a charm :D My spinner was called float01 btw. Here is what I put,

f = createFile "c:\\attribute_name.txt"
format "float01: %\n" $sphere01.modifiers[1].float01 to:f
flush f
close f

Listener says,

OK<File:c:\attribute_name.txt>
OK
OK
OK

File says,

float01: 0.00

and I say, yaaaaay handiklap! :D :D Thanks everyone!

Ian00
04-18-2008, 03:29 PM
Oh and for anyone else needing to know how to output more than one value, you can type this for 2 values, one for the radius value and one for the float01 value

f = createFile "c:\\attribute_name.txt"
format "sphereRadius = %, float01: %\n" $sphere01.radius $sphere01.modifiers[1].float01 to:f
flush f
close f

and the file will say this,

sphereRadius: 12.671, float01: 8.1

(I changed the values to make sure they were working) Ok thanks again! :D

CGTalk Moderation
04-18-2008, 03:29 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.