Find active camera viewport

Find active camera viewport

w.haak
Contributor Contributor
1,296 Views
4 Replies
Message 1 of 5

Find active camera viewport

w.haak
Contributor
Contributor

My script needs to work in the viewport which has the only camera assigned to it in the scene, but this may be a viewport in a different viewpanel than the default opening state of the scene. 

 

Right now I'm creating an array of visible viewpanels and then loop over the viewports in each panel until I find my camera when getActiveCamera() returns 'true', I'll set that viewport to active. Seems super clunky, loops within loops. 
Is there a more direct version?  

0 Likes
Accepted solutions (1)
1,297 Views
4 Replies
Replies (4)
Message 2 of 5

wernienst
Collaborator
Collaborator

If there is only one camera in the scene you can switch the active viewport to that camera by executing

max vpt camera

If there are more cameras, the Select Camera dialog opens.

MaxScript Help

 

0 Likes
Message 3 of 5

w.haak
Contributor
Contributor

Pretty neat!  Didn't know that one. 
I was trying to find the viewport so that Background, Safe Frame etc was preserved. 
I'll keep my ugly code for now. Learned in the process that viewport.activeViewportID is a unique ID for a viewport across all panels, while viewport.activeViewport is the index of the viewport in the current panel. 
So that makes it bit easier, by cycling over the viewports until getActiveCamera() != undefined and then returning the viewport.activeViewportID to so that viewport.activeViewportID = id can then set it up correctly. 

As usual maxscript irritates with inconsistent syntax. 

 

ViewPanelManager.SetActiveViewPanel pID 

sets the Viewpanel while 
viewport.activeViewportID = fID

sets the Viewport. 

 

0 Likes
Message 4 of 5

denisT.MaxDoctor
Advisor
Advisor
Accepted solution

 

for panel = 1 to ViewPanelManager.GetViewPanelCount() do
(
	for index = 1 to viewport.numviews do
	(
		id = viewport.getid index viewPanelIndex:panel
		cam = viewport.getCamera index:index viewPanelIndex:panel
		format "panel:% viewport:% id:% camera:%\n" panel index id cam 	
	)
)

 

So, the only way is to iterate through all views in all panels. Of course, you can check if a panel is visible and the viewport is valid.

 

0 Likes
Message 5 of 5

w.haak
Contributor
Contributor

@denisT.MaxDoctor 
That's quite elegant. Learning a lot, thanks!

0 Likes