Random Face Deletion / Selection with Maxscript

For sometime now, some of my friends asked me to write a script for random face selection for poly objects. Here is a simple code for doing exactly what it’s asked for:

sel = selection as array
count = 1

undo on
(
	if sel.count > 0 then
	(
		while count <= sel.count do
		(
			select sel[count]
			count += 1
			
			facenumber = polyop.getNumFaces $
			randomset = #()
			for i=1 to facenumber do
			(
				rand = random 0 100
				if rand < 50 do append randomset i
			)
			
			polyop.deleteFaces $ randomset delIsoVerts:true
		)
	)
)
select sel

It is not hard to write a simple random face select/delete script. This script will go through every selected poly object, and randomly delete some of the faces. I did not add any error checking in this, so if you select any object that’s not a poly, you’ll get an error. You can also disable the viewport update and enable after the faces get deleted. This will make the script run faster on high-poly count objects.

If you are experiencing errors before script deletes any faces, change the random range’s 0 to 1. For more randomness use a big number for the second part of the random statement. 100 seems OK. If you do not want to delete isolated vertices –vertices that do not belong to any faces-, just change delIsoVerts to false. For keeping the original object, just add an Edit Poly modifier and then run the script. By this way, removing the modifier might save you some production time.

Requested addition #1: And, this script will only randomly select faces, but not delete them :

sel = selection as array
count = 1

undo on
(
	if sel.count > 0 then
	(
		while count <= sel.count do
		(
			select sel[count]
			count += 1
			
			facenumber = polyop.getNumFaces $
			randomset = #()
			for i=1 to facenumber do
			(
				rand = random 0 100
				if rand < 50 do append randomset i
			)
			
			subobjectlevel = 4
			polyop.setFaceSelection $ randomset
		)
	)
)
select sel

Requested Addition #2: This one will assign a new material id that is not used on the mesh to the randomly selected polys.

sel = selection as array
count = 1
 
undo on
(
	if sel.count > 0 then
	(
		while count <= sel.count do
		(
			select sel[count]
			count += 1

			facenumber = polyop.getNumFaces $
			randomset = #()
			newID = 0
				for i=1 to facenumber do
				(
					if (polyop.getFaceMatID $ facenumber) > newID do newID = (polyop.getFaceMatID $ facenumber)+1
					rand = random 0 100
					if rand < 50 do append randomset i
				)

			polyop.setFaceMatID $ randomset newID
		)
	)
)
select sel

This slideshow requires JavaScript.

Detach Using Material IDs

This script detaches faces as new objects, using Material ID property of the faces. Most of the cases, matIDs are used to assign multiple materials on one object. Sometimes you might want to detach these as new objects.

Download Detach Using Material IDs Maxscript

It is possible to detach every matID as object, or simply a range, or just one with Min ID and Max ID ranges. If you keep the default settings, every matID becomes a new poly object. The remainder will be a faceless poly, script deletes this poly if this is the case. Naming will be done by adding the ID number at the end of the original name of the selected object (originalname_ID_#). Wirecolor will also be randomly changed. The pivots of the detached objects will be the same as the original object.

Before using the script please keep these in mind :

  • Script only works with polys. If you have a mesh or patch, you’ll need to convert it to poly mesh, or add an “edit poly” modifier on the stack.
  • Do not detach instanced objects, as this will cause deletion of the faces on the instances.
  • For objects with high poly count, it may take some time to process all the faces. I tried the script with a million poly object, it took 30 seconds or something close to detach every matID available. It might be better to detach some of the matID’s by hand, and then using the script.
  • Why is it slow with high poly objects ? There is no method in 3dsmax for detecting the matID count directly. Script goes through every face and count unique matID channels. Then detaches these if desired.

This slideshow requires JavaScript.