Maxscript write and read .txt file
So I'm making an exporter in maxscript
I have troubles writing the export path into a .txt file and reading it.
This is what I have:
----------------------------------------------------------------------------------
label labelExportFolder "Export folder" pos:[10,600]
edittext folderPathTxt "" pos: [10,620] width:140
button btnBrowse "..." pos: [160,620]
on btnBrowse pressed do
(
Global MyPath = "" -- this stores the path in string form
local dir = getSavePath caption:"path..."
if (dir != undefined) do ( folderPathTxt.text = dir )
append MyPath folderPathTxt.text as string -
maxRootDir = getdir #maxroot
global TextFileName = (maxRootDir +"\\ExportPath.txt")
-- if file doesn't exist, create one and store MyPath in it
if doesFileExist (maxRootDir +"\\ExportPath.txt") == false do
(
global TextFile = (createFile TextFileName)
format MyPath to:TextFile
close TextFile
)
-- if the .txt file does exist, read it and store the content in MyPath and put it in the edittext
if doesFileExist (maxRootDir +"\\ExportPath.txt") == true do
(
local replaceTxt = ""
MyPath = (openFile TextFileName mode:"r+")
folderPathTxt.text = MyPath
close TextFile
)
)
-------------------------------------------------------------------------------------------
So I hope it's clear what I'm trying to do.
I'm not sure whether I should use the TxtFile or TextFileName
when i do "close TextFile" or "MyPath = (openFile TextFileName mode:"r")"
I guess the TextFileName just contains the path to the file that is created,
but I don't know what the TextFile variable is exactly. I'm confused
Comments
ITS ALIVE!
finally got it working :)
Unable to convert: undefined to type: FileName
So I tried making using the .ini file from Max which is easier to handle.
(someone on polycount gave me that tip)
But now I get another error.
I Think this should be easy to solve if I only Knew How :p
this is the error: -- Unable to convert: undefined to type: FileName
Update
Changed my code a bit, now I get this error:
--Runtime error: FileStream cannot create: E:\Max\3ds Max 2016\\ExportPath.txt
global MyPath = "no path"
maxRootDir = getdir #maxroot -- get max root file path
global TextFileName = (maxRootDir +"\\ExportPath.txt")
fn CreateTextFile =
(
global TextFile = (createFile TextFileName)
format MyPath to:TextFile
close TextFile
messagebox "created"
)
if doesFileExist (maxRootDir +"\\ExportPath.txt") == false do
(
CreateTextFile()
messagebox "file didn't exist, creating one"
)
label labelExportFolder "Export folder" pos:[10,600]
edittext folderPathTxt "" text:MyPath pos: [10,620] width:140 text: MyPath
button btnBrowse "..." pos: [160,620]
on btnBrowse pressed do
(
local dir = getSavePath caption:"path..."
if (dir != undefined) do ( folderPathTxt.text = dir )
MyPath = folderPathTxt.text as string -- get edittext content and put in MyPath
-- folderPathTxt.text = MyPath
fs = openFile TextFileName mode:"r+" --opens the file
format "put it iin" to:fs --this writes your text to the now open file
close TextFile
)
.
I think that your problem is with write persmission. Your OS(UAC) not allows you to create files in max root folder, which is in C drive(I suppose). Use this and the script should work:
Tools and Scripts for Autodesk 3dsMax