[Closed] openFile() issue in 3ds 2013
Hello all, first time here for me ^^ !
I am french so, sorry if my english is bad :).
I am looking for some help to fix a script that doesn’t work on 2013 version of 3DS Max (and works perfect on 2012).
The code is from soxpostureblender_v0.725.ms, not a script of mine.
on uiBtnSave pressed do
(
if uiListPosture.items.count == 0 do return ()
local spbFileSave = (getSaveFileName caption:"Save spb file" types:"SOX Posture Blender (*.spb)|*.spb|All (*.*)|*.*|") as string;
if spbFileSave == "undefined" do return ()
newFileFlag = false;
tempStream = openFile spbFileSave mode:"r";
if tempStream == undefined then (createFile spbFileSave; newFileFlag = true;)
else close tempStream;
tempSPBStream = fnSetSPBVar ();
[u][i]writeStream = openFile spbFileSave mode:"wt";[/i][/u]
format (tempSPBStream as string) to:writeStream;
close writeStream;
)
The script crashes at the italic-underlined line and I don’t know why…
The getSaveFileName() function works, but the line:
tempStream = openFile spbFileSave mode:“r”;
returns undefined.
I know that the openFile() behaviour changes in 2013, but I don’t know how it works now, and how to fix my scripts.
Can someone help me?
i see very weird logic in this code. can you clean the scenario:
like…
#1 define a file name to save text data to
#2 if a file was not picked break
#3 if the file can’t be opened for write break (maybe delete it)
#4 write the data to the file
there is code on this scenario:
on uiBtnSave pressed do if uiListPosture.items.count == 0 then false else
(
file = (getSaveFileName caption:"Save spb file" types:"SOX Posture Blender (*.spb)|*.spb|All (*.*)|*.*|")
if (file != undefined) and (ss = createFile file) != undefined do
(
format "%
" (fnSetSPBVar() as string) to:ss
flush ss
close ss
true
)
)
Bonsoir!
FYI: No semicolons at the end of a maxscript line. They are only for separating two statements on the same line. Also, using return is slow. Defnitely don’t use return at the end of a function, the last value in a max script function is returned automatically.
fn printTwice = (
print “Salut”; print “Hi”
output = true
output
)
I noticed some of my scripts not working anymore and found that the file saving wasn’t working. CreateFile returns a handle to the filestream, the class name is File for this func. If you don’t capture that file handle, the file is stuck open. Then the only way to close the file is to restart Max.( I tried hitting <Esc> and running the garbage collector gc(), but it didn’t work.)
This is what I found that works.
[font=Consolas][size=2]fs = ""[/size][/font][font=Consolas][size=2][/size][/font]
[font=Consolas][size=2]fname = sysinfo.tempdir + "vertFileOld.txt"[/size][/font]
[font=Consolas][size=2]if (doesFileExist fname) == false then --check for file[/size][/font]
[font=Consolas][size=2] fs = createFile fname --create if its not there[/size][/font]
[font=Consolas][size=2]else[/size][/font]
[font=Consolas][size=2] fs = openFile fname mode:"w" --else open it mode w for writing[/size][/font]
myVar = 4.32
format "myVar = %
" myVar to:fs
close fs
I think createFile also opens the file.
You have to do:
fh = createFile file
or the file will appear open until you restart max.
MAX/Maxscript can sometimes be picky about language specific/special characters in paths/filenames, could that be the issue here?
Mmmh I don’t think so, I always name it “test”, in case “test.spb”.
If someone want to test himself, the script can be download right there: http://cafe.naver.com/pinksox/2894 .
Tried with a TXT file and it worked. the problem does not seem to be in
tempStream = openFile spbFileSave mode:"r";
because if it returns undefined then there is a check where it creates a new file in case that it is undefined so look at that instead.
if tempStream == undefined then (createFile spbFileSave; newFileFlag = true;) else close tempStream;
Wow dude, thank you very much, that works well (there are some errors in your code, maybe you wrote it too fast :P, but I’ve understood what you meant).
Very helpful 🙂 !!
errors? where?
edit... found :) there were two bugs. (not errors)
there is my favorite style for today:
on ui_save_bt pressed do if (data = setSPBVar()) != undefined do
(
if (file = getSaveFileName caption:"Save spb file" types:"SOX Posture Blender (*.spb)|*.spb|All (*.*)|*.*|") != undefined) do
(
(dotnetclass "System.IO.File").WriteAllText file (data as string)
file
)
)
two bugs. (not errors)
Haha yes indeed, maybe I didn’t use the correct word :).
Your second example seems very cool! I don’t use dotnet so much… maybe I could :D!
Btw, thank you much again !!