Home Technical Talk

Maxscript write and read .txt file

polycounter lvl 9
Offline / Send Message
Pinned
elias leoanrd polycounter lvl 9

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 = ""
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 doesFileExist (maxRootDir +"\\ExportPath.txt") == false do
(
global TextFile = (createFile TextFileName)
format MyPath to:TextFile
close TextFile
)
if doesFileExist (maxRootDir +"\\ExportPath.txt") == true do
(
local replaceTxt = ""
MyPath = (openFile TextFileName mode:"r") as string
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

Replies

  • PolyHertz
    Options
    Offline / Send Message
    PolyHertz polycount lvl 666
    edit: Ok, now I see what you're trying to do...

  • monster
    Options
    Offline / Send Message
    monster polycounter
    For this it would be much easier to use an INI file.
    Max Help INI Files: http://goo.gl/Ex7LVx
    
    --SET INI SETTING
    (
    	MyPath = getSavePath caption:"path..."
    	if MyPath != undefined do
    	(
    		MaxINI = getMAXIniFile()
    		setINISetting  MaxINI "EliasSettings" "ExportDirectory" MyPath
    	)
    )
    
    --GET INI SETTING
    (
    	MaxINI = getMAXIniFile()
    	MyPath = getINISetting  MaxINI "EliasSettings" "ExportDirectory"
    )
  • Swordslayer
    Options
    Offline / Send Message
    Swordslayer interpolator
    monster: Agreed, for reading/writing path to file, that's perfect. For the rest of the question, as it's supposed to be an exporter (if I get it right), some reading/writing in a custom format is necessary anyway. In that case, something like this would be a better approach (from the top of my head, untested):

    try destroyDialog ::exportStuff catch()
    
    rollout exportStuff
    (
    	label labelExportFolder "Export folder"
    	editText folderPathTxt across:2
    	button btnBrowse "..."
    	button btnExport "Export"
    
    	on btnBrowse pressed do 
    	(
    		local path = getSaveFileName types:"Text(*.txt)|*.txt|"
    		if path != undefined do folderPathTxt.text = path
    	)
    
    	on btnExport pressed do
    	(
    		local fileName = folderPathTxt.text
    		local textFile = if doesFileExist fileName then openFile fileName mode:"r+" else createFile fileName
    
    		-- do the export here
    		-- i.e. format ... to:textFile
    
    		close textFile
    	)
    )
    createDialog exportStuff
    There's a 'How To ... Read/Write Geometry Data From/To Text File' topic in the help file, definitely read also 'Scope of Variables' and 'FileStream Values'.
  • elias leoanrd
    Options
    Offline / Send Message
    elias leoanrd polycounter lvl 9
    @MONSTER thanks a lot, i'll try that!
    also for future reference, how do you put code in a separate window like you did?

  • monster
    Options
    Offline / Send Message
    monster polycounter
    The paragraph icon has Code formatting as an option. 
  • elias leoanrd
    Options
    Offline / Send Message
    elias leoanrd polycounter lvl 9
    @MONSTER  I tried to understand the .ini structure, but got totally lost.
    I found some info on the MAXScript help but this didn't make any sense to me 
    :# 

    setINISetting <filename_string> <section_string> <key_string> <key_value_string> [ forceUTF16:<boolean> ]
    the <filename_string> is used to give the file a name and place.
    I don't understand what the 
     <section_string> , <key_string> and <key_value_string> do exactly. Couldn't wrap my head arround it.
  • elias leoanrd
    Options
    Offline / Send Message
    elias leoanrd polycounter lvl 9
    also still working on the .txt way of working and i think I almost got it
    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

    fs = openFile TextFileName mode:"r+" --opens the file    --  <--- THIS IS WHERE THE ERROR HAPPENS
    format "put this in" to:fs --this writes your text to the now open file
    close TextFile
    )
  • elias leoanrd
    Options
    Offline / Send Message
    elias leoanrd polycounter lvl 9
    @MONSTER So i tried you .ini method of working.
    but i get this error: --Unable to convert: undefined to type: FileName

    global MyPath = ""
    -- Set .ini
    MyPath = getSavePath caption:"Choose export directory"
    if MyPath != undefined do
    (
    	local maxINI = getMAXIniFile()
    	setINISetting  maxINI "EliasScriptSettings" "ExportDirectory" MyPath
    )
    
    --Get .ini
    (
    	local maxINI = getMAXIniFile()
    	MyPath = getINISetting  maxINI "EliasSettings" "ExportDirectory"
    )
    
    rollout TestDialog "Dialog"
    (
    label labelExportFolder "Export folder" pos:[10,10] 
    edittext folderPathTxt "" text:MyPath pos: [10,30] width:140 text: MyPath
    button btnBrowse "..." pos: [160,30]
    on btnBrowse pressed do 
    (
    	local dir = getSavePath caption:"Choose export directory"
    	if (dir != undefined) do ( folderPathTxt.text = dir )
    	MyPath = (folderPathTxt.text) as string     -- get edittext content and put in MyPath
    	setINISetting  MaxINI "EliasScriptSettings" "ExportDirectory" MyPath -- !!ERROR HERE!!
    )
    )
    createdialog TestDialog width: 200 height: 200


  • elias leoanrd
    Options
    Offline / Send Message
    elias leoanrd polycounter lvl 9
    ITS ALIVE!
    code is working, here it is:
    fn setFOKINPATH =
    (
    if MyPath != undefined do
    (
    	local maxINI = getMAXIniFile()
    	setINISetting  maxINI "EliasScriptSettings" "ExportDirectory" MyPath
    )
    )
    
    --Get .ini
    (
    	local maxINI = getMAXIniFile()
    	MyPath = getINISetting  maxINI "EliasScriptSettings" "ExportDirectory"
    	messagebox "GOT IT!"
    	print MyPath
    )
    
    rollout TestDialog "Dialog"
    (
    	
    	label labelExportFolder "Export folder" pos:[10,10] 
    	edittext folderPathTxt "" text:MyPath pos: [10,30] width:140
    	button btnBrowse "..." pos: [160,30]
    	on btnBrowse pressed do 
    	(
    		MyPath = getSavePath caption:"Choose export directory"
    		if (MyPath != undefined) do 
    		(
    			folderPathTxt.text = MyPath 
    		)
    		-- setINISetting  MaxINI "EliasScriptSettings" "ExportDirectory" "test" -- !!ERROR HERE!!
    		setFOKINPATH()
    	)
    )
    createdialog TestDialog width: 200 height: 200

Sign In or Register to comment.