[Closed] 3ds Max 2016 crashes with this script
Good day to all gurus here.
I have lots of experience with programming, but until recently I never needed Max, so I am new in Max scripting in particular. If this is too simple to work around – please excuse my incompetence, I am just trying out still.
Here is my problem: I want to import a series of altitudes from a file, that is formatted as coma delimited:
0.0,0.0,0.393701,1,0.0,0.278389,0.278389,1
0.19685,0.19685,0.278389,1,-0.278389,0.0,0.278389,2,-0.19685,-0.19685,0.278389,2
0.0,-0.278389,0.278389,2,0.19685,-0.19685,0.278389,2
0.278389,0.0,0.278389,2,0.19685,0.19685,0.278389,3,0.0,0.393701,0.0,3,-0.278389,0.278389,0.0,3,1,0.0,0.0,4
0.278388,-0.278389,0.0,5,0.0,-0.393701,0.0,5,0.278388,-0.278389,0.0,5
0.393701,0.0,0.0,5,0.278389,0.278388,0.0,5,0.0,0.278389,-0.278389,5,-0.19685,0.19685,-0.278389,5,-0.278389,0.0,-0.278389,5,-0.19685,-0.19685,-0.278389,5
0.0,-0.278389,-0.278389,5,0.19685,-0.19685,-0.278389,5,0.278389,0.0,-0.278389,5
0.19685,0.19685,-0.278389,5,0.0,0.0,-0.393701,5
where each line is a row of altitudes and each altitude is separated with the next by “,”.
This is my test file.
I wrote a script as follows:
x0=0
y0=0
dX=0.1
dy=0.1
x=x0 as float
y=y0 as float
n=1
theShape = splineshape name:“imported”
fName = getOpenFileName caption:“Select a .csv file to import” filename:”.csv” types:”csv|.csv”
if fName != undefined then
(
addNewSpline theShape
addknot theShape 1 #smooth #line [x,y,0]
thefile = openFile fName mode:“r”
while not eof theFile do
(
theFileLine = readline theFile as stringstream
addNewSpline theShape
while not eof theFileLine do
(
z = readDelimitedString theFileLine “,” as float
addKnot theShape n #smooth #line [x,y,z]
y=y+dY
)
x=x+dX
n=n+1
updateShape theShape
)
updateShape theShape
close theFile
close theShape
)
the first four lines are to fix the starting poin of the insertion, and the steps by X and Y.
When I start this script, by “Evaluate All”, 3ds Max stops responding and even the debugger is blacking out. When I try to evaluate line by line, it gets stacked on line 21 – “while not eof theFileLine do”.
Please help. I really tried out…
i swiflty tested the script using your data, and here it does’nt lock up Max, but produces an empyt shape ( without knots ), so something is wrong with the script
Are you on the latest ServicePack with your Max 2016 ( i think it’s SP4 ) ?
I vaguely remember bugs with the maxscript file IO API in maxscript, but am not sure wether this was Max 2016 or Max 2015. But just for safety you should ensure that you on the latest service pack
The problem is with the code (Creation of empty new spline at the begining, not updating at the good place…)
Here’s a quick solution (I’ve changed file to array just for do it faster)
fileString = #(#(0.0,0.0,0.393701,1,0.0,0.278389,0.278389,1), \
#(0.19685,0.19685,0.278389,1,-0.278389,0.0,0.278389,2,-0.19685,-0.19685,0.278389,2), \
#(0.0,-0.278389,0.278389,2,0.19685,-0.19685,0.278389,2), \
#(0.278389,0.0,0.278389,2,0.19685,0.19685,0.278389,3 ,0.0,0.393701,0.0,3,-0.278389,0.278389,0.0,3,1,0.0,0.0,4), \
#(0.278388,-0.278389,0.0,5,0.0,-0.393701,0.0,5,0.278388,-0.278389,0.0,5), \
#(0.393701,0.0,0.0,5,0.278389,0.278388,0.0,5,0.0,0.278389,-0.278389,5,-0.19685,0.19685,-0.278389,5,-0.278389,0.0,-0.278389,5,-0.19685,-0.19685,-0.278389,5), \
#(0.0,-0.278389,-0.278389,5,0.19685,-0.19685,-0.278389,5,0.278389,0.0,-0.278389,5), \
#(0.19685,0.19685,-0.278389,5,0.0,0.0,-0.393701,5))
x0=0
y0=0
dX=0.1
dy=0.1
x=x0 as float
y=y0 as float
n=1
theShape = splineshape name:"imported"
fName = fileString
if fName != undefined then
(
while not n == fName.count do
(
theFileLine = fName[n]
addNewSpline theShape
m = 1
while not m == theFileLine.count do
(
z = theFileLine[m]
addKnot theShape n #smooth #line [x,y,z]
y=y+dY
m += 1
)
x=x+dX
n=n+1
)
updateShape theShape
)
forcecompleteredraw()
I am not sure if this will do exactly what you need, as you are adding several splines to the shape and then “closing” it at the end, so I assume that what you need is just one shape.
(
local dX = 0.1
local dY = 0.1
local x = 0.0
local y = 0.0
fName = getOpenFileName caption:"Select a .csv file to import" filename:"*.csv" types:"csv|*.csv"
if fName != undefined then
(
thefile = openFile fName mode:"r"
theShape = splineshape name:"imported"
addNewSpline theShape
while not eof theFile do
(
-- Create an array of the values in the line delimited by ","
points = filterstring (readline theFile) ","
-- If the array is not empty
if points.count > 0 do
(
for z in points do
(
addKnot theShape 1 #smooth #line [x, y, z as float]
y += dY
)
x += dX
)
)
updateShape theShape
close theFile
)
)
Thanks for the help aaandreas, really, I didn’t expect an answer so fast. I have tried your suggestion. Supposedly I can format the initial file in any way I want, I just need to import groups of vertexes (each group on a separate line in a file) in a grid manner and still to be able to choose a starting point for the grid. The real file I need to import is about 60Mb big, so it will be impractical to do this by just putting the data in a string format. Your way seems to have a problem as well, I just tried it out. The error code I see is “Type error: Call needs function or class, got: 0” in line 1, whatever does this mean.
Thing is if I make it work eventually, I would need to read the file in an array format…
I am open to any ideas.
Do you have any idea why this language Max is using is so weird? It’s freaking me out.
Dear gurus, thank you all, especially spacefrog.
I downloaded and installed the latest service pack and now the scripts are running. It was damn weird when I am sure something should work and it is not and is hanging like hell. I restarted Max at least 100 times last two days… Thanks for the advice, I know it is stupid, but sometimes needs another pair of eyes to see the elephant in the room:)