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.