3rd Dimentia
02-10-2008, 03:16 AM
I've been writing a script to turn an editable poly object into an executable maxscript. This is mostly for maxscript practice but it will make it easier to take objects from later versions of max into earlier ones with things intact like ALL UV channels etc. Still early stages at the moment but it seems to work great unless I try to make a script out of a high poly object. I've been exporting a 30K poly object which seems to export OK, but when I run the script to remake the poly object, I get an unknown system exception and sometimes it gives me an out of memory error and max will crash and dissapear. Is there a way to clear the memory as I'm building the object? Currently In the script that's created, I build the vert array then make all the verts of the object, then I build the poly array and build the polys etc etc. As far as other data, I've only got the smooth groups and the material IDs up and running so far.
When I wrote the script to start with I didn't have the "with redraw off" or "undo off" and the 30k poly object worked without a crash but it took 28 mins to rebuild. A friend suggested I add redraw off and undo off and it definately improved the speed of the script a lot on smaller objects, but now I can't get it to work on larger objects without erroring.
On closer inspection, it seems that it runs out of memory on the polyop.setFaceSmoothGroup loop and I still can't figure out why.
Here is the script that will export a poly to an executable maxscript.
macroScript ExportPolyToMS
category:"CgRay"
(
try (destroyDialog ExportPolyToMS) catch()
rollout ExportPolyToMS "Export Poly to MS"
(
group "Elements to export"
( checkbox chk_SmGroups "Smooth Groups" checked:true align:#left
checkbox chk_Materials "Material" checked:true align:#left enabled:false
checkbox chk_MatId "Material ID's" checked:true align:#left
checkbox chk_UVs "UV channels" checked:true align:#left enabled:false
checkbox chk_VertCol "Vertex Colors" checked:true align:#left enabled:false
)
button btn_Export "Export Selected"offset:[0,5]
on btn_Export pressed do
( clearlistener()
if (selection.count == 1) and (superclassof $ == geometryclass) and (classof $ != TargetObject) then
(
objectName = $.name
-- set output file
outName = ("F:\\MaxStuff\\scripts\\CgScripts\\exporter\\PolyExport_"+objectName+".ms")
outFile = createfile outName
-- make snapshot of object and turn it to a poly
tempObj = snapshot $
convertTo tempObj PolyMeshObject
-- sets the vertcount and polycount variables
vertCount = polyop.getnumVerts tempObj
polyCount = polyop.getNumFaces tempObj
--start writing the script
format "with redraw off\n" to:outFile
format "(\n" to:outFile
format "clearlistener()\n" to:outFile
format "startTime = timestamp()\n" to:outFile
-- writes script to make an empty editable mesh and convert it to a poly
format "newPoly = editable_mesh()\n" to:outFile
format "convertTo newPoly PolyMeshObject\n" to:outFile
-- writes script to set the vertcount and polycount variables
format "vertCount = %\n" vertCount to:outFile
format "polyCount = %\n" polyCount to:outFile
fn ConstructArrayThenBuild arrayName loopLength tempObj outfile =
( --builds up the relevant array
format "% = #(" arrayName to:outFile
for i = 1 to looplength do
( case arrayName of
(
"vertArray": ( tempData = polyop.getvert tempObj i)
"polyArray": ( tempData = polyop.getFaceVerts tempObj i)
"smoothGroupArray": ( tempData = polyop.getFaceSmoothGroup tempObj i)
"matIdArray": ( tempData = polyop.getFaceMatID tempObj i)
)
format "%" tempData to:outFile
if i != looplength then
(
format "," to:outFile
)
)
format ")\n"to:outFile
-- writes script that will build the relevant element
format "undo off\n" looplength to:outFile
format "(\n" to:outFile
format " for i = 1 to % do\n" looplength to:outFile
format " (\n" to:outFile
case arrayName of
(
"vertArray": ( format " polyop.createVert newPoly vertArray[i]\n" to:outFile)
"polyArray": ( format " polyop.createPolygon newPoly polyArray[i]\n" to:outFile)
"smoothGroupArray": ( format " polyop.setFaceSmoothGroup newPoly i smoothGroupArray[i]\n" to:outFile)
"matIdArray": ( format " polyop.setFaceMatID newPoly i matIdArray[i]\n" to:outFile)
)
format " )\n" to:outFile
format ")\n" to:outFile
)
-- sends vert related info to the ConstructArrayThenBuild function
arrayName = "vertArray"
looplength = vertCount
polyOpType = "poVert"
ConstructArrayThenBuild arrayName loopLength tempObj outfile
-- sends poly related info to the ConstructArrayThenBuild function
arrayName = "polyArray"
looplength = polyCount
polyOpType = "poPoly"
ConstructArrayThenBuild arrayName loopLength tempObj outfile
if chk_SmGroups.state then
(
-- sends smoothing group related info to the ConstructArrayThenBuild function
arrayName = "smoothGroupArray"
looplength = polyCount
polyOpType = "poFaceSmoothGroup"
ConstructArrayThenBuild arrayName loopLength tempObj outfile
)
if chk_MatId.state then
(
-- sends material ID related info to the ConstructArrayThenBuild function
arrayName = "matIdArray"
looplength = polyCount
polyOpType = "poFaceMatID"
ConstructArrayThenBuild arrayName loopLength tempObj outfile
)
format "update newPoly\n" to:outFile
-- writes script that calculates total time spent building polyobject and prints to listener
format "endTime = timestamp()\n"to:outFile
format "duration = (endTime - startTime)/1000\n" to:outFile
format "print duration \n" to:outFile
format ")\n" to:outFile
close outFile
delete tempObj
)
else
(
messagebox "Select one geometry object to export" title:"Selection Error"
)
)
)
createdialog ExportPolyToMS
)
When I wrote the script to start with I didn't have the "with redraw off" or "undo off" and the 30k poly object worked without a crash but it took 28 mins to rebuild. A friend suggested I add redraw off and undo off and it definately improved the speed of the script a lot on smaller objects, but now I can't get it to work on larger objects without erroring.
On closer inspection, it seems that it runs out of memory on the polyop.setFaceSmoothGroup loop and I still can't figure out why.
Here is the script that will export a poly to an executable maxscript.
macroScript ExportPolyToMS
category:"CgRay"
(
try (destroyDialog ExportPolyToMS) catch()
rollout ExportPolyToMS "Export Poly to MS"
(
group "Elements to export"
( checkbox chk_SmGroups "Smooth Groups" checked:true align:#left
checkbox chk_Materials "Material" checked:true align:#left enabled:false
checkbox chk_MatId "Material ID's" checked:true align:#left
checkbox chk_UVs "UV channels" checked:true align:#left enabled:false
checkbox chk_VertCol "Vertex Colors" checked:true align:#left enabled:false
)
button btn_Export "Export Selected"offset:[0,5]
on btn_Export pressed do
( clearlistener()
if (selection.count == 1) and (superclassof $ == geometryclass) and (classof $ != TargetObject) then
(
objectName = $.name
-- set output file
outName = ("F:\\MaxStuff\\scripts\\CgScripts\\exporter\\PolyExport_"+objectName+".ms")
outFile = createfile outName
-- make snapshot of object and turn it to a poly
tempObj = snapshot $
convertTo tempObj PolyMeshObject
-- sets the vertcount and polycount variables
vertCount = polyop.getnumVerts tempObj
polyCount = polyop.getNumFaces tempObj
--start writing the script
format "with redraw off\n" to:outFile
format "(\n" to:outFile
format "clearlistener()\n" to:outFile
format "startTime = timestamp()\n" to:outFile
-- writes script to make an empty editable mesh and convert it to a poly
format "newPoly = editable_mesh()\n" to:outFile
format "convertTo newPoly PolyMeshObject\n" to:outFile
-- writes script to set the vertcount and polycount variables
format "vertCount = %\n" vertCount to:outFile
format "polyCount = %\n" polyCount to:outFile
fn ConstructArrayThenBuild arrayName loopLength tempObj outfile =
( --builds up the relevant array
format "% = #(" arrayName to:outFile
for i = 1 to looplength do
( case arrayName of
(
"vertArray": ( tempData = polyop.getvert tempObj i)
"polyArray": ( tempData = polyop.getFaceVerts tempObj i)
"smoothGroupArray": ( tempData = polyop.getFaceSmoothGroup tempObj i)
"matIdArray": ( tempData = polyop.getFaceMatID tempObj i)
)
format "%" tempData to:outFile
if i != looplength then
(
format "," to:outFile
)
)
format ")\n"to:outFile
-- writes script that will build the relevant element
format "undo off\n" looplength to:outFile
format "(\n" to:outFile
format " for i = 1 to % do\n" looplength to:outFile
format " (\n" to:outFile
case arrayName of
(
"vertArray": ( format " polyop.createVert newPoly vertArray[i]\n" to:outFile)
"polyArray": ( format " polyop.createPolygon newPoly polyArray[i]\n" to:outFile)
"smoothGroupArray": ( format " polyop.setFaceSmoothGroup newPoly i smoothGroupArray[i]\n" to:outFile)
"matIdArray": ( format " polyop.setFaceMatID newPoly i matIdArray[i]\n" to:outFile)
)
format " )\n" to:outFile
format ")\n" to:outFile
)
-- sends vert related info to the ConstructArrayThenBuild function
arrayName = "vertArray"
looplength = vertCount
polyOpType = "poVert"
ConstructArrayThenBuild arrayName loopLength tempObj outfile
-- sends poly related info to the ConstructArrayThenBuild function
arrayName = "polyArray"
looplength = polyCount
polyOpType = "poPoly"
ConstructArrayThenBuild arrayName loopLength tempObj outfile
if chk_SmGroups.state then
(
-- sends smoothing group related info to the ConstructArrayThenBuild function
arrayName = "smoothGroupArray"
looplength = polyCount
polyOpType = "poFaceSmoothGroup"
ConstructArrayThenBuild arrayName loopLength tempObj outfile
)
if chk_MatId.state then
(
-- sends material ID related info to the ConstructArrayThenBuild function
arrayName = "matIdArray"
looplength = polyCount
polyOpType = "poFaceMatID"
ConstructArrayThenBuild arrayName loopLength tempObj outfile
)
format "update newPoly\n" to:outFile
-- writes script that calculates total time spent building polyobject and prints to listener
format "endTime = timestamp()\n"to:outFile
format "duration = (endTime - startTime)/1000\n" to:outFile
format "print duration \n" to:outFile
format ")\n" to:outFile
close outFile
delete tempObj
)
else
(
messagebox "Select one geometry object to export" title:"Selection Error"
)
)
)
createdialog ExportPolyToMS
)
