[Closed] Maxscript strange behavior without print statement
I have a maxscript I’m using to import CAD models, convert to Editable Mesh, and decimate that is exhibiting some very strange behavior. I’ve
had the script working for a while, and was tinkering with the function
that does the actual decimation yesterday when something went wrong and
the script would run without errors, but the decimation (through
ProOptimizer) wouldn’t work, it would have the same number of polys
before and after. The function is
fn decimateLo obj file_name = (
– Model type 1
if file_name[1] == “S” and file_name[2] == “W” then (
count1 = (vol_ratio_lo_sw * sqrt(meshVolume(obj))) as Integer
count2 = (sa_ratio_lo_sw * sqrt(meshSurfaceArea(obj))) as Integer
)
— Default
else (
count1 = (vol_ratio_lo * sqrt(meshVolume(obj))) as Integer
count2 = (sa_ratio_lo * sqrt(meshSurfaceArea(obj))) as Integer
)
select obj
modPanel.addModToSelection (ProOptimizer ()) ui:on
obj.modifiers[#ProOptimizer].Calculate = on
local count = amin(#(count1, count2)) as integer
obj.modifiers[#ProOptimizer].VertexCount = count
obj.modifiers[#ProOptimizer].Calculate = off
obj.modifiers[#ProOptimizer].LockMat = on
obj.modifiers[#ProOptimizer].KeepVC = on
obj.modifiers[#ProOptimizer].OptimizationMode = 1
obj.modifiers[#ProOptimizer].KeepNormals = on
obj.modifiers[#ProOptimizer].PreventFlip = on
obj.modifiers[#ProOptimizer].MergePoints = on
obj.modifiers[#ProOptimizer].MergePointsThreshold = 0.1
obj.modifiers[#ProOptimizer].MergeFacesAngle = 5.0
obj.modifiers[#ProOptimizer].Calculate = on
)
I was trying to debug this and found that adding a single print statement before assigning the vertex count made it work as intended (possibly noteoworthy, the line “print count” did not make it work, while the line “print obj.numVerts did). What could possibly cause this behavior? Apologies for the unformatted code in the post, I’m new to the forum and couldn’t figure out how to do it.
fn decimateLo obj file_name = (
– Model type 1
if file_name[1] == “S” and file_name[2] == “W” then (
count1 = (vol_ratio_lo_sw * sqrt(meshVolume(obj))) as Integer
count2 = (sa_ratio_lo_sw * sqrt(meshSurfaceArea(obj))) as Integer
)
— Default
else (
count1 = (vol_ratio_lo * sqrt(meshVolume(obj))) as Integer
count2 = (sa_ratio_lo * sqrt(meshSurfaceArea(obj))) as Integer
)
select obj
modPanel.addModToSelection (ProOptimizer ()) ui:on
obj.modifiers[#ProOptimizer].Calculate = on
local count = amin(#(count1, count2)) as integer
print obj.numVerts -- Adding this line fixes the script
obj.modifiers[#ProOptimizer].VertexCount = count
obj.modifiers[#ProOptimizer].Calculate = off
obj.modifiers[#ProOptimizer].LockMat = on
obj.modifiers[#ProOptimizer].KeepVC = on
obj.modifiers[#ProOptimizer].OptimizationMode = 1
obj.modifiers[#ProOptimizer].KeepNormals = on
obj.modifiers[#ProOptimizer].PreventFlip = on
obj.modifiers[#ProOptimizer].MergePoints = on
obj.modifiers[#ProOptimizer].MergePointsThreshold = 0.1
obj.modifiers[#ProOptimizer].MergeFacesAngle = 5.0
obj.modifiers[#ProOptimizer].Calculate = on
)
As you’ve found out, it’s not the print statement, it’s accessing the .numVerts which triggers an update because it needs the result after the whole modifier stack. In the same way, a call to classOf obj can be used. Forcing it to update like this is necessary for certain modifiers and operations.
Is there any rhyme or reason as to when that update is needed? This was working without doing that for a while and I haven’t been able to track down what change broke it.
Depends on the modifier, there are modifiers like this one which need modify panel to be active to work. To answer why it did work before, you did something else that triggered the update. You only need to set the vertex count after that:
(
local modOptimize = ProOptimizer optimizationMode:1 \
lockMat:on keepVC:on keepNormals:on preventFlip:on \
mergePoints:on mergePointsThreshold:0.1
addModifier obj modOptimize
setCommandPanelTaskMode #modify
modOptimize.Calculate = on
classOf obj
modOptimize.VertexCount = amin count1 count2
)