Community
3ds Max Programming
Welcome to Autodesk’s 3ds Max Forums. Share your knowledge, ask questions, and explore popular 3ds Max SDK, Maxscript and Python topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Code not working inside of loop (works outside of loop)

2 REPLIES 2
Reply
Message 1 of 3
Anonymous
915 Views, 2 Replies

Code not working inside of loop (works outside of loop)

I have just started working with maxscript and I am trying to write a script that will loop through a set of .max files and calculate the longest dimension of the objects in the file.  I have gotten code that will work correctly on a single file, but when I run it inside of a loop, the calculation of the longest dimension breaks.

 

Here is the code that works:

outputPath = "C:\Work\Data\ProductImages3DS"

file = (createFile "C://tmp.txt")

 

f = "C:\path\to\file.max"

inputFile = f
filename = getFilenameFile f
f = pathConfig.removePathLeaf f
styleNumber = getFilenameFile f

outputSubfolder = pathConfig.appendPath outputPath styleNumber
outputFile = pathConfig.appendPath outputSubfolder filename+"_.png"

print inputFile
loadMaxFile inputFile

max select all
group selection name:"tigger"
select $tigger
foo = maxOps.cloneNodes (selection as array) cloneType:#instance newNodes:&nnl #nodialog
select nnl
nnl[1].pos = [0, 0, 0]
select $tigger
max delete

bb = (length ($tigger001.max-$tigger001.min))/2
print bb

bb = bb as string

newline = "\r\n"
format bb to:file
format ", " to:file
format inputFile to:file
format newline to:file

 

Here is the code that doesn't work:

fn getFilesRecursive root pattern =
(
dir_array = GetDirectories (root+"/*")
for d in dir_array do
join dir_array (GetDirectories (d+"/*"))
my_files = #()
for f in dir_array do
join my_files (getFiles (f + pattern))
my_files
)
--get all .ms files from the folder c:/temp
--and all its subfolders:
modelPath = "C:\Work\Data\ProductModelsFiltered"
outputPath = "C:\Work\Data\ProductImages3DS"

file = (createFile "C://tmp.txt")

SetQuietMode true
for f in getFilesRecursive modelPath "*.max" do
(
inputFile = f
filename = getFilenameFile f
f = pathConfig.removePathLeaf f
styleNumber = getFilenameFile f

outputSubfolder = pathConfig.appendPath outputPath styleNumber
outputFile = pathConfig.appendPath outputSubfolder filename+"_.png"

-- Begin Rendering
print inputFile
loadMaxFile inputFile

max select all
group selection name:"tigger"
select $tigger
foo = maxOps.cloneNodes (selection as array) cloneType:#instance newNodes:&nnl #nodialog
select nnl
nnl[1].pos = [0, 0, 0]
select $tigger
max delete

bb = (length ($tigger001.max-$tigger001.min))/2
print bb
--bb = (ceil (bb/25))*25
--bb = bb as integer
bb = bb as string

newline = "\r\n"
format bb to:file
format ", " to:file
format inputFile to:file
format newline to:file
)

close file

 

Here is the resulting text file from the code that doesn't work:

0.0173205, C:\Work\Data\ProductModelsFiltered\172957\172957A2.max
0.0173205, C:\Work\Data\ProductModelsFiltered\172957\172957A3.max
0.0173205, C:\Work\Data\ProductModelsFiltered\172957\172957A4.max
0.0173205, C:\Work\Data\ProductModelsFiltered\172957\172957A5.max
0.0173205, C:\Work\Data\ProductModelsFiltered\172957\172957A6.max
0.0173205, C:\Work\Data\ProductModelsFiltered\172957\172957A7.max
0.0173205, C:\Work\Data\ProductModelsFiltered\172957\172957A8.max

 

The file names are updating but the dimension (first number) is way to small and doesn't change.

 

Any help would be appreciated

2 REPLIES 2
Message 2 of 3
malcomarmstrong
in reply to: Anonymous

Please repost this using the correct formatting option for code.

 

But, I see some paths have forward slashes and some have single back slashes. Try using dbl bslashes on your path calls. If you format the code properly, it will be easier to read and help solve the issue with.

 

It seems to be the data going into the for loop that may be the issue, but wont know till its formatted to read better.

 

regards

Message 3 of 3
Anonymous
in reply to: Anonymous

Re posting the code as code blocks.  I still haven't found a solution, but I have root caused the problem to $.min & $.max.  Upon initially loading a file these have the wrong values.  I can reproduce this by just opening a file and looking at a group $.min property in the script listener.  Any selection I do in the listener or in the UI fixes the values and they are correct from then on whether anything is selected or not.  The problem is that select $group doesn't fix the problem when I run the script, only in the listener.

 

Any ideas?

 

Here is the code that works:

 

outputPath = "C:\Work\Data\ProductImages3DS"
file = (createFile "C:\tmp.txt")

f = "C:\path\to\file.max"

inputFile = f
filename = getFilenameFile f
f = pathConfig.removePathLeaf f
styleNumber = getFilenameFile f

outputSubfolder = pathConfig.appendPath outputPath styleNumber
outputFile = pathConfig.appendPath outputSubfolder filename+"_.png"

print inputFile
loadMaxFile inputFile

max select all
group selection name:"tigger"
select $tigger
foo = maxOps.cloneNodes (selection as array) cloneType:#instance newNodes:&nnl #nodialog
select nnl
nnl[1].pos = [0, 0, 0]
select $tigger
max delete

bb = (length ($tigger001.max-$tigger001.min))/2
print bb

bb = bb as string

newline = "\r\n"
format bb to:file
format ", " to:file
format inputFile to:file
format newline to:file

 

Here is the code that doesn't work:

 

fn getFilesRecursive root pattern =
(
dir_array = GetDirectories (root+"/*")
for d in dir_array do
join dir_array (GetDirectories (d+"/*"))
my_files = #()
for f in dir_array do
join my_files (getFiles (f + pattern))
my_files
)
--get all .ms files from the folder c:/temp
--and all its subfolders:
modelPath = "C:\Work\Data\ProductModelsFiltered"
outputPath = "C:\Work\Data\ProductImages3DS"

file = (createFile "C://tmp.txt")

SetQuietMode true
for f in getFilesRecursive modelPath "*.max" do
(
inputFile = f
filename = getFilenameFile f
f = pathConfig.removePathLeaf f
styleNumber = getFilenameFile f

outputSubfolder = pathConfig.appendPath outputPath styleNumber
outputFile = pathConfig.appendPath outputSubfolder filename+"_.png"

-- Begin Rendering
print inputFile
loadMaxFile inputFile

max select all
group selection name:"tigger"
select $tigger
foo = maxOps.cloneNodes (selection as array) cloneType:#instance newNodes:&nnl #nodialog
select nnl
nnl[1].pos = [0, 0, 0]
select $tigger
max delete

bb = (length ($tigger001.max-$tigger001.min))/2
print bb
--bb = (ceil (bb/25))*25
--bb = bb as integer
bb = bb as string

newline = "\r\n"
format bb to:file
format ", " to:file
format inputFile to:file
format newline to:file
)

close file

 

Here is the resulting text file from the code that doesn't work:

0.0173205, C:\Work\Data\ProductModelsFiltered\172957\172957A2.max
0.0173205, C:\Work\Data\ProductModelsFiltered\172957\172957A3.max
0.0173205, C:\Work\Data\ProductModelsFiltered\172957\172957A4.max
0.0173205, C:\Work\Data\ProductModelsFiltered\172957\172957A5.max
0.0173205, C:\Work\Data\ProductModelsFiltered\172957\172957A6.max
0.0173205, C:\Work\Data\ProductModelsFiltered\172957\172957A7.max
0.0173205, C:\Work\Data\ProductModelsFiltered\172957\172957A8.max

 

The file names are updating but the dimension (first number) is way to small and doesn't change.

 

Any help would be appreciated

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report