Is there a preferred way to cull the hierarchy of everything but meshes and produce a single merged mesh?
I have been converting a number of large files containing scenes comprised of thousands of separate pieces -- I've tried a couple approaches to create a single mesh from many (thousands) of separate meshes. My goal is to jettison the hierarchy/mode tree data and transformation history, creating a single mesh that performs as well as possible.
I settled on an approach that attaches even and odd meshes iteratively until only a single mesh remains, a method advocated separately by several people in different forums. The below code works, but the problem here is that some of my source files have nodes that can't be attached (they're not meshes) but aren't deleted by this 'delete everything but meshes' loop:
for i in objects where classOf i != geometryClass do delete i
My question: am I going about this wrong? Is there a preferred way to cull the hierarchy of everything but meshes and produce a single merged mesh?
Here's a summary of the two approaches I've tried:
- Collapse Stack is the obvious built-in function for discarding the complex hierarchy tree that slows 3DS down. It's a production-oriented feature to decrease the CPU load, especially when sending heavy files to rendering, when working in an asset sharing environment or exporting models to other formats or olders versions of 3DS. The problem here for me was time and memory -- the process was really slow, and ate up all 64GB of RAM on the systems I was using, so Windows was swapping quite a bit and slowing things down even further.
- Attach mesh is an alternative to collapse stack, and it can be much faster and require less memory. My simplest form of the attach script is below: it imports, ungroups, deletes non-geometry and runs the attach process by matching even and odd pairs until there's only one mesh left, then exports it.
---
undo off files = getFiles "C:\\test\*.fbx" theClasses = exporterPlugin.classes for f in files do ( resetMaxFile #noprompt ImportFile (f) #noPrompt select objects if selection.count > 0 do ( currentname = f + "-md.fbx" print currentname -- Ungroup all groups = for o in helpers where isGroupHead o collect o while groups.count > 0 do ( ungroup groups[groups.count] ; groups.count = groups.count-1 ) -- Cull everything but geometry for i in objects where classOf i != geometryClass do delete i while selection.count > 1 do ( selcount = selection.count for i = selcount to 2 by -2 do ( meshop.attach selection[i] selection[i-1] ) ) update selection[1] exportFile (currentname) #noPrompt using:theClasses[9] ) )
Comments
Import time slows dramatically with
Using @faraj's method below, I note that the time to process goes up steeply (nearly exponentially) with the model size; please see the attached image for a chart of times run on an i7 PC with 32GB RAM.
For instance, I can process about (4) ~165K files a day per machine, but > (1000) ~38K files. The timings from the attached chart:
File #1: ~1 minutes (source: 18K, output OBJ: 22K)
File #2: ~1 minutes (source: 38K, output OBJ: 54K)
File #3: ~90 minutes (source: 51K, output OBJ: 24K + MTL: 24K)
File #4: ~330 minutes (source: 165K, output OBJ: 11K)
Simply loading the initial FBX file takes much of the time (CPU @ ~24%, i.e.: one core).
Importing an ~18K file takes ~10 seconds, importing a ~51K files takes ~3 minutes and importing a ~165K file takes several hours and uses all available RAM (~32GB).
Unless there is a way to cull or flatten the node hierarchy on import via Maxscript, or use multiple cores during import, I can't see a way to avoid the near-exponential load time relation to file size.
Any ideas? All suggestions are most welcome!
Hope it help.....
first you can cut off those goupp ungroup thing because in attach you only geometry which can convert to mesh or poly ,redraw off thing first, replace select object to select geometry
try this code , just rough thing from me, hope it help
@fajar's approach works for me
This seems to be a sane way to proceed and solves the memory consumption problems with the native Max 'collapse' merge option.
Loading the 3DS files themselves now takes the most RAM, and time, of any of the steps in the script. I may need to break individual files into pieces if they are larger than the RAM available.
Thanks @faraj for your contribution!