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:

-------------------------------------------------------------------------------------------

  1. 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")
  2. 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
    Offline / Send Message
    PolyHertz polycount lvl 666
    edit: Ok, now I see what you're trying to do...

  • monster
    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
    1.  
    2. --SET INI SETTING
    3. (
    4. MyPath = getSavePath caption:"path..."
    5. if MyPath != undefined do
    6. (
    7. MaxINI = getMAXIniFile()
    8. setINISetting MaxINI "EliasSettings" "ExportDirectory" MyPath
    9. )
    10. )
    11.  
    12. --GET INI SETTING
    13. (
    14. MaxINI = getMAXIniFile()
    15. MyPath = getINISetting MaxINI "EliasSettings" "ExportDirectory"
    16. )
  • Swordslayer
    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):

    1. try destroyDialog ::exportStuff catch()
    2. rollout exportStuff
    3. (
    4. label labelExportFolder "Export folder"
    5. editText folderPathTxt across:2
    6. button btnBrowse "..."
    7. button btnExport "Export"
    8. on btnBrowse pressed do
    9. (
    10. local path = getSaveFileName types:"Text(*.txt)|*.txt|"
    11. if path != undefined do folderPathTxt.text = path
    12. )
    13. on btnExport pressed do
    14. (
    15. local fileName = folderPathTxt.text
    16. local textFile = if doesFileExist fileName then openFile fileName mode:"r+" else createFile fileName
    17. -- do the export here
    18. -- i.e. format ... to:textFile
    19. close textFile
    20. )
    21. )
    22. 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
    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
    Offline / Send Message
    monster polycounter
    The paragraph icon has Code formatting as an option. 
  • elias leoanrd
    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
    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
    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

    1. global MyPath = ""
    2. -- Set .ini
    3. MyPath = getSavePath caption:"Choose export directory"
    4. if MyPath != undefined do
    5. (
    6. local maxINI = getMAXIniFile()
    7. setINISetting maxINI "EliasScriptSettings" "ExportDirectory" MyPath
    8. )
    9.  
    10. --Get .ini
    11. (
    12. local maxINI = getMAXIniFile()
    13. MyPath = getINISetting maxINI "EliasSettings" "ExportDirectory"
    14. )
    15.  
    16. rollout TestDialog "Dialog"
    17. (
    18. label labelExportFolder "Export folder" pos:[10,10]
    19. edittext folderPathTxt "" text:MyPath pos: [10,30] width:140 text: MyPath
    20. button btnBrowse "..." pos: [160,30]
    21. on btnBrowse pressed do
    22. (
    23. local dir = getSavePath caption:"Choose export directory"
    24. if (dir != undefined) do ( folderPathTxt.text = dir )
    25. MyPath = (folderPathTxt.text) as string -- get edittext content and put in MyPath
    26. setINISetting MaxINI "EliasScriptSettings" "ExportDirectory" MyPath -- !!ERROR HERE!!
    27. )
    28. )
    29. createdialog TestDialog width: 200 height: 200


  • elias leoanrd
    Offline / Send Message
    elias leoanrd polycounter lvl 9
    ITS ALIVE!
    code is working, here it is:
    1. fn setFOKINPATH =
    2. (
    3. if MyPath != undefined do
    4. (
    5. local maxINI = getMAXIniFile()
    6. setINISetting maxINI "EliasScriptSettings" "ExportDirectory" MyPath
    7. )
    8. )
    9.  
    10. --Get .ini
    11. (
    12. local maxINI = getMAXIniFile()
    13. MyPath = getINISetting maxINI "EliasScriptSettings" "ExportDirectory"
    14. messagebox "GOT IT!"
    15. print MyPath
    16. )
    17.  
    18. rollout TestDialog "Dialog"
    19. (
    20. label labelExportFolder "Export folder" pos:[10,10]
    21. edittext folderPathTxt "" text:MyPath pos: [10,30] width:140
    22. button btnBrowse "..." pos: [160,30]
    23. on btnBrowse pressed do
    24. (
    25. MyPath = getSavePath caption:"Choose export directory"
    26. if (MyPath != undefined) do
    27. (
    28. folderPathTxt.text = MyPath
    29. )
    30. -- setINISetting MaxINI "EliasScriptSettings" "ExportDirectory" "test" -- !!ERROR HERE!!
    31. setFOKINPATH()
    32. )
    33. )
    34. createdialog TestDialog width: 200 height: 200

Sign In or Register to comment.