Select ONLY visible polygons (eg what camera can see)

I am after a script that will allow you to make a Selection when in Polygon sub-object mode, and it will ONLY select the visible polygons. There does not seem to be any function, or script, ever made for 3DSMax to do that.

Below is a script that can select ALL visible polygons only. I assume it could be edited to take a Selection as the range to select from?
It would also be good if it could select either forward facing polygons, or backward facing ones - maybe two scripts at worst?
And there would likely be numerous other sub-function selection modes that would be very useful too.

I wonder why this has never been done before??? Some 3D programs have it as standard.

I do not know enough about scripting to alter this to work, but maybe someone could help out with it?
Thanks.

========
--Get the viewport TMatrix, invert to get the equivalent of a camera transformation
theTM = inverse (viewport.getTM())
--Loop through all geometry objects that have EPoly Base Object
for theObj in Geometry where classof theObj.baseobject == Editable_Poly do
(
theFacesToSelect = #{} --ini. a bitArray to collect faces to select
numFaces = polyOp.getNumFaces theObj --get the number of polygons in the EPoly
--loop from 1 to the number of polygons and set the corresponding bit in the bitArray
--to true if the normal of the polygon as seen in camera space has a positive Z,
--and false if it is negative or zero (facing away from the camera)
for f = 1 to numFaces do
theFacesToSelect[f] = (in coordsys theTM polyOp.getFaceNormal theObj f).z > 0
--finally, set the selection in the object
polyOp.setFaceSelection theObj theFacesToSelect
)
--when done with all, redraw the views - if a Poly SO level is selected,
--the selection will be updated in the viewport...
redrawViews()

================

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
PeterVq's picture

No, there is no such

No, there is no such script.
No one has made one to only select VISIBLE polygons, and NOT occluded ones - whether forward or back facing.
This has been asked for by dozens of people for over ten years and never done by anyone....

Your mentioned search criteria just bring up those usual requests that are never fulfilled.

The script I listed SHOULD work, if it had the extra criteria of using the Selection Area instead of how it is now with the ALL polygons selection. It starts with that selection (all) and then works out which are NOT occluded and then selects those. It works. But needs that addition of Selection Area ONLY.

Who knows why no one has ever done it..... it is a tool in many 3D editors, because it IS a useful tool to have available. But Autodesk in 3DSmax have never added it. Nor have any script writers.

miauu's picture

.

This is modification of the Bobo's script(the sript you posted). It works with all polygons of the object or with the selected polygons only.

(
	local poGetNumFaces = polyOp.getNumFaces
	local poGetFaceSelection = polyOp.getFaceSelection
	local poGetFaceNormal = polyOp.getFaceNormal
	local poSetFaceSelection = polyOp.setFaceSelection 
	function SelectVisiblePolys polysToUse: selVisiblePolys: =
	(
		--Get the viewport TMatrix, invert to get the equivalent of a camera transformation
		theTM = inverse (viewport.getTM())
 
		selObjsArr = selection as array	
		--Loop through all geometry objects that have EPoly Base Object
		for theObj in selObjsArr where classof theObj.baseobject == Editable_Poly do
		(
			theFacesToSelect = #{} --ini. a bitArray to collect faces to select
			numFaces = if polysToUse == #all then
				(
					#{1..(poGetNumFaces theObj)}
				)
				else
				(
					poGetFaceSelection theObj
				)
			--loop from 1 to the number of polygons and set the corresponding bit in the bitArray
			--to true if the normal of the polygon as seen in camera space has a positive Z,
			--and false if it is negative or zero (facing away from the camera)
			if selVisiblePolys == #visible then
			(
				for f in numFaces do
				(
					theFacesToSelect[f] = (in coordsys theTM poGetFaceNormal theObj f).z > 0
				)
			)
			else
			(
				for f in numFaces do
				(
					theFacesToSelect[f] = (in coordsys theTM poGetFaceNormal theObj f).z < 0
				)
			)
			--finally, set the selection in the object
			poSetFaceSelection theObj theFacesToSelect
		)
		--when done with all, redraw the views - if a Poly SO level is selected,
		--the selection will be updated in the viewport...
		redrawViews() 
	)
	--	"use all polys of the object and select the visible ones"
	SelectVisiblePolys polysToUse:#all selVisiblePolys:#visible
 
-- 	--	"use all polys of the object and select the hidden ones"
-- 	SelectVisiblePolys polysToUse:#all selVisiblePolys:#invisible
 
	--	"use selected polys of the object and select the visible ones"
-- 	SelectVisiblePolys polysToUse:#selected selVisiblePolys:#visible
 
	--	"use all selected of the object and select the hidden ones"
-- 	SelectVisiblePolys polysToUse:#selected selVisiblePolys:#invisible
)

But jahman is right - what is your criteria for visible or occluded polygon?

jfmonod's picture

Select all exterior polygons

Hi!
I have a question regarding your script for selecting visible faces. I'm trying to select only exterior faces of a building model to create a mobile Augmented Reality asset. I would need this script, but not from a single camera viewpoint, rather from every viewpoint on a hemispherical skydome. I need to select every face visible on an object held in the palm of my hand. So no undersides or interiors, but anything that the light can touch. Does that make sense ?

Thanks,

JF

jfmonod's picture

Select all exterior polygons

Hi!
I have a question regarding your script for selecting visible faces. I'm trying to select only exterior faces of a building model to create a mobile Augmented Reality asset. I would need this script, but not from a single camera viewpoint, rather from every viewpoint on a hemispherical skydome. I need to select every face visible on an object held in the palm of my hand. So no undersides or interiors, but anything that the light can touch. Does that make sense ?

Thanks,

JF

jahman's picture

.

There're lots of ways to treat polygon as visible or occluded. What's you criteria?

Here's my quick try. Some false-positives are still present in face selection.
The idea is simple.
Rays are fired towards the camera starting from the polygon verts. If ray hits any face on its way to camera then this poly is occluded.

miauu's picture

.

I don't want to disappoint you but there are such a scripts. Jsut search the net with this "maxscript select only visible polygons" and check the results. Also, there is a built in 3ds max command that can select only visible polygons.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.