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

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
KevinCain's picture

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!

AttachmentSize
image.png 18.04 KB
fajar's picture

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

 
files = getFiles "C:\\*.fbx" -- change it to your file dir
resetMaxFile #noprompt -- if you start from not empty scene you can delete this line or add -- in frint resetMaXfile
 
fn attachMesh objArr =
(
	count = objArr.count
	if count >1 then 
	(
		a=timeStamp()
 
		with undo off with redraw off
		(
			local j = 1
			local myAttach =meshop.attach,delItem = deleteItem,clsOf = classof,conMesh = converttomesh
			while objArr.count > 1 do
			(
			if clsOf objArr[j] != Editable_Mesh then conMesh objArr[j]
 
			myAttach objArr[j] objArr[j+1]
			delItem objArr (j+1)
			j += 1
			if (j + 1) > objArr.count then j = 1
			)
		)
		return objArr[1]
	)
	else
	(
		return "Selection must more than 2 objects" 
	)
)
 
 
with undo off with redraw off 
(
	local rst=resetMaxFile,imp=ImportFile, sel = select, del = delete,unName = uniqueName, \
	act = actionMan.executeAction,expr=exportFile,getFilePath = getFilenamePath, \
	getName=getFilenameFile ,getSel = getCurrentSelection,att=attachMesh
 
	local insel, curName, ext = ".obj" -- change it to whatever max eport support files
 
	for i in files do 
	(
		rst #noprompt
		imp i #noPrompt
		sel geometry 
		inSel = getSel() 
		curName = ((getFilePath i) +"\\"+(getName i) + (unName "-md_") + ext)
 
		act 0 "40044" -- Selection: Select Invert
		del selection -- delete non geometry n non helper
 
		if inSel.count != 0 do 
		(
			att inSel
                        del helpers
			expr curName #noPrompt 
		)
 
	)
)
KevinCain's picture

@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!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.