How can I get objects in camera view?

How can I get objects in camera view?

icapathos
Advocate Advocate
1,900 Views
1 Reply
Message 1 of 2

How can I get objects in camera view?

icapathos
Advocate
Advocate

[Reference] obj in frustum 

Actually I can get the objects in view with above script.

But, recently I got in trouble in some cases.

If a big object locate in foreground, it blocks lots of objects so actually they are not seen in camera view.

I want to increase accuracy that detecting whether the object is in a view or not.

Is there any way to find that objects is blocked by another in a view?

0 Likes
1,901 Views
1 Reply
Reply (1)
Message 2 of 2

Kahylan
Advisor
Advisor

Hi!

 

Your link seems to be dead, but I'm assuming you are using OpenMaya.MGlobal.SelectFromScreen().

This function basically makes a marquee selection over the viewport. So if objects behind objects get selected depends on wether "camera-base selection" is turned on in the selection preferences.

This script does what you want for the active viewport:

import maya.OpenMaya as om
import maya.OpenMayaUI as omu
import maya.cmds as mc



def selectFromViewport():
	'''
	Select all objects visible in the current active viewport
	'''
	#set marquee selection to camerabased
	state = mc.selectPref( q= True,useDepth = True )
	mc.selectPref(useDepth = True)
	
	# Get active viewport
	viewPort = omu.M3dView.active3dView()
	
	# Select from screen
	om.MGlobal.selectFromScreen(0,0,viewPort.portWidth(),viewPort.portHeight(),om.MGlobal.kReplaceList,om.MGlobal.kSurfaceSelectMethod)
	mc.selectPref(useDepth = state)
selectFromViewport()

lines 11-13 and 20 are what you need to put into your script around the part that does the selection to get it working.

 

I hope it helps!