Notifications
Clear all

[Closed] Performant way to get all used Material IDs?

Hi,

I’m writing a scene exporter and I wondered if there is a way to get all used material IDs in the current scene without going through every single triangle? Because I tried the following and it takes ages with complex scenes:


global ALL_TRIANGLES = #()
struct triStruct (
	triNode,
	triMatID,
	triIndex)

fn getAllTriangles =
(
	start = timeStamp()
	ALL_TRIANGLES = #()
	for obj in $* where (superclassof obj == GeometryClass) do
	(
		s = (snapShotAsMesh obj)
		numFaces = meshop.getNumFaces s
		
		for i = 1 to numFaces do
		(
			newFace = triStruct triNode:obj triMatID:(getFaceMatID s i) triIndex:i
			append ALL_TRIANGLES newFace
		)
	)
	end = timeStamp()
	format "%ms
" (end-start)
)

I’ve been searching the Maxscript help for hours without any luck.
Any ideas how to improve the speed here?

kthxbye

4 Replies
3 Replies
(@denist)
Joined: 11 months ago

Posts: 0

i don’t know the other way then go through all geo nodes and their faces to collect face ids.
how complex your scene is?


(
	ids = #{}
	for node in geometry as array do
	(
		m = snapshotasmesh node
		for f=1 to m.numfaces do ids[getFaceMatID m f] = on
		delete m
	)
	ids
)

for 1,500,000 faces it takes ~3sec. not too bad.

(@canislupus)
Joined: 10 months ago

Posts: 0

Thank you, this is indeed faster than my approach. Could you explain what exactly “ids[getFaceMatID m f] = on”, especially the “= on”, does? Is there a difference to:


ids = for f=1 to m.numfaces collect (getFaceMatID m f)

?

(@light)
Joined: 11 months ago

Posts: 0

It just sets the indexed BitArray element to true. The difference is his uses a BitArray, yours creates an Array. BitArrays are more compact than Arrays, so more efficient in certain problems.

Light

hi,
Look at it the other way, if you have the material/submaterial on the objects in the scene try to select the faces on each Id and if you get a result that is greater than 0 faces its in use, that way you loop will run once per id not once per poly

Hope that helps