[Closed] Selecting and deleting elements
I have thousands of leaves on a tree – I would like to be able to cull half (or a third) of these via a maxscript but there seems to be only vert edge and face functions. The leaf element is basically a single square poly triangulated to two tris.
Any ideas?
–magilla
Here’s a very simple script. I’ve assumed you use epoly and no modifiers on top of the object:
obj = selection[1]
-- set all faces and then get them in an array
polyOp.setFaceSelection obj #all
poly_array = polyOp.getFaceSelection obj
used_polys = #()
elem_array = #()
-- get all element groups in an array
for poly in poly_array do
(
-- get all other polygons in the element
-- the conversion is to make sure the array
-- count is correct
elem_polys = (polyop.getElementsUsingFace obj poly as bitarray) as array
-- if the poly is not used before then append
-- the group to elem_array
if findItem used_polys poly == 0 do
(
-- array reversal
elem_polys_rev = #()
for idx = elem_polys.count to 1 by -1 do
(
append elem_polys_rev elem_polys[idx]
)
append elem_array elem_polys_rev
)
-- append the polys to used_polys
for ep in elem_polys do
(
if findItem used_polys ep == 0 do
(
append used_polys ep
)
)
)
-- now you can go ahead and delete some groups, below
-- is an example. "by -3" gives you 1/3 of the elements.
-- it counts backwards to avoid poly renumbering
for idx = elem_array.count to 1 by -3 do
(
for poly in elem_array[idx] do
(
polyOp.deleteFaces obj poly delIsoVerts:true
)
)
update obj
Good luck,
- Rens
excellent – thank you.
It crashes Max when I run it on an object with a large numbers of elements, but I can break it up easily enough.
I can follow most of this script, however I don’t understand the reason for appending the reversed array, can you elaborate on this please?
–magilla
Sure, I’ve done that for the same reason as counting backwards in elem_array. If you delete the first poly or index the number above that will be reassing to the first number; if you delete poly one then poly two will become poly one. Counting backwards makes sure poly two stays poly two and doesn’t mess up the script.
Note that I could also have used the following instead of reversing the array beforehand, this is the last bit of code:
[color=White]
for[/color] idx = elem_array.count to 1 by -3 do
(
for idx2 = elem_array[idx].count to 1 by[color=White] -1 [/color]do
(
polyOp.deleteFaces obj elem_array[idx][idx2] delIsoVerts:true
)
)
update obj
That would’ve been shorter.
Basically the above lines will cycle through everything in elem_arrays in reverse by 3. elem_arrays contains other arrays.
Then it cycles through everything in those arrays in reverse and deletes the polygon in question.
An example of what the arrays could look like:
elem_array: #(#(1,2,3,4,5,6),#(7,8,9,10,11,12),#(13,14,15,16,17))
elem_array[2]: #(7,8,9,10,11,12)
elem_array[2][3]: 9
elem_array[2][4]: 10
Arrays in arrays.
Hope this helps,
- Rens