Notifications
Clear all

[Closed] A faster method for retrieving element count

I’ve been searching around for this for a while, but I haven’t been able to find anything faster than what this one:

-- Store elements here
elementsToStore = #()
-- Get the faces of the selected object
objFaces = selection[1].faces as bitArray
-- Collect all of the elements of the selected object
for faceIndex in objFaces where objFaces[faceIndex] == true do
(
    -- Get the current element's faces
    currentElement = polyop.getElementsUsingFace selection[1] faceIndex
    objFaces -= currentElement
    -- Append the current element
    append elementsToStore currentElement
)

-- Print the number of elements
print elementsToStore.count

Does anyone know how to improve the speed for this?

18 Replies

check out these two

fn GetPolyElementsCount poly =
(
	local g      = (dotNetClass "Autodesk.Max.GlobalInterface").Instance
	local inode  = g.COREInterface14.GetINodeByHandle poly.inode.handle asdotnetobject:true
	local iobj   = inode.evalworldstate (currenttime as integer) true asdotnetobject:true
	local mnmesh = iobj.obj.mesh
	local felem  = g.MNFaceElement.Create mnmesh
	local count  = felem.Count
	
	inode.dispose()
	iobj.dispose()
	mnmesh.dispose()
	felem.dispose()
	
	count
)

fn GetPolyElementsCountMXS obj =
(
	local counter = 0
	local faces   = #{1..(polyop.getNumFaces obj)}
	local func    = polyop.getElementsUsingFace

	for f in faces where faces[f] do
	(
		faces -= func obj f
		counter += 1		
	)

	counter
)

Time: 0.013sec. Mem: 1760L – GetPolyElementsCount
Time: 0.036sec. Mem: 23904L – GetPolyElementsCountMXS

But keep in mind that mxs version will always be faster for low elements count objects.
You can split teapot edges to make a lot of elements and you’ll see the diffirence.

Dig in the forum… if you can!
There’s a thread with a wonderful solution from PolyTools3D that is times faster than this one.
It’s one of those threads that made this forum ‘The Forum’ of maxscript.

coding is like life… it’s all about lifelong learning. I’ve double checked the performance… The using of MNFaceElement is faster then all other methods i’ve tried. (at least if you need to process almost all faces).

as I remember it was about Mesh elements. Which is different in case of Poly. Poly object stores a topology data for quick access instead of Mesh

Yes. My bad.

Yep, it is pretty convenient especially when used in unsafe manner like I do in c#.
I’ve managed to get all 6k elements-verts for 500k mesh inabout 150ms on my decade old laptop
%D0%B8%D0%B7%D0%BE%D0%B1%D1%80%D0%B0%D0%B6%D0%B5%D0%BD%D0%B8%D0%B5

could you post a sample scene setup please? so that we can play with the same data

here is for example:

delete objects

obj = undo off, redraw off 
(
	fn polyAttach nodes =
	(
		k = 1
		count = nodes.count
		attach = polyop.attach

		while nodes.count > 1 do
		(
			k += 1
			attach nodes[k-1] nodes[k]
			deleteItem nodes k
			if k >= nodes.count do k = 1
		)
		nodes[1]
	)


	--disablerefmsgs()
	bb = for k=0 to 2000 collect 
	(
		s = 10 + 0.01*k
		b = box width:s length:s height:s widthsegs:1 lengthsegs:1 heightsegs:1
		b.center = [0,0,0]
		converttopoly b 
	)


	b = polyAttach bb
	--enablerefmsgs()
	
	update b
	select b
	b
)
1 Reply
(@serejah)
Joined: 11 months ago

Posts: 0

looks like a hypercube

  1. – 0 ms Preparations complete
  2. – 0.002 ms 2001 vert elements found.
  3. – 0.002 ms Randomize elements complete
    Time: 0.027sec. Mem: 336L

ha, it’s even faster in laptop’s performance mode
7yYDdLgOwI

and that’s the scene

	delete objects
	gc()
	obj = plane lengthsegs:20 widthsegs:20 

	with redraw off 
	(
		
		convertToPoly obj

		polyop.splitEdges obj #{1..(polyop.getNumEdges obj)}
		addModifier obj (shell outerAmount:1)
		addModifier obj (relax amount:1)
		addModifier obj (TurboSmooth iterations:4 smoothResult:off sepBySmGroups:on)
		convertToPoly obj
		
	)
Page 1 / 2