Toggle camera script problem

Toggle camera script problem

keith_brown
Advocate Advocate
2,041 Views
9 Replies
Message 1 of 10

Toggle camera script problem

keith_brown
Advocate
Advocate

I found a neat script called Camera Changer created by laky which toggles the viewport between cameras in the scene. I've binded this to a hotkey. It works great but stops when it has reached the last object/camera in the array.

 

Any ideas how to fix this?

 

-------------------------------------------------
--------------------------------------------------
-- Camera Changer 1.0 (Forward)
--
-- By laky (at) radiocityfx.com
--
-- Original idea: Sam O'Hare
--
-- 2004 Oct 22
----------------------------------------------------

global currCam
global newCam
global currCamNumber

macroScript CamChangerFwd category:"My_Macro_Category" tooltip:"Camera Changer>>"
(
	
	camera_array = cameras as array
	if camera_array[1] != undefined then 
	(
		currView = viewport.getType()

        

		if currView != #view_camera then newCam = camera_array[1] else
		(
			
			currCam = viewport.getCamera()
			currCamNumber = findItem camera_array currCam
			if currCamNumber < camera_array.count-1 then newCam = camera_array[currCamNumber+2] else
			newCam = camera_array[1]

		)

		viewport.setCamera newCam
		
		curcam = try(getActiveCamera()) catch()
		if curcam != undefined do select curcam
					

	)
	
	else messagebox "No cameras in scene"
	
	
)

 

0 Likes
Accepted solutions (1)
2,042 Views
9 Replies
Replies (9)
Message 2 of 10

denisT.MaxDoctor
Advisor
Advisor
Accepted solution

 

 

macroScript LoopCameras 
	tooltip:"Loop Cameras" 
	buttontext:"LOOP CAMS"
	category:"Camera Helps"
	autoUndoEnabled:off
(	
	local cams
	local cam 
	
	fn enabled = 
	(
		cams = for c in cameras where iskindof c Camera collect c
		cams.count > 0	
	)
	on isEnabled do (enabled())
	on isChecked do (getactivecamera() != undefined)

	on execute do if (enabled()) do undo "Loop Camera" on 
	(		
		if ((cam = getactivecamera()) == undefined) then
		(
			cam = cams[1]
		)
		else
		(
			k = finditem cams cam
			if keyboard.shiftpressed then
				cam = if k > 1 then cams[k-1] else cams[cams.count]
			else 
				cam = if k < cams.count then cams[k+1] else cams[1]
		)
		if cam != viewport.getcamera() do 
		(
			viewport.setcamera cam 
			-- select cam -- not sure it is a good feature to be default
		)
	)
)

 

 

 

here is a modified version... 

* some bug fixed

* no camera auto-selection (if you like to select uncomment -- select cam line)

* use SHIFT to loop backward

* undoable (if you don't like this feature set undo "Loop Camera" on to OFF)

0 Likes
Message 3 of 10

keith_brown
Advocate
Advocate
Awesome! The script worked fantastically! However, I encountered an issue with using the shift key to go backward when the script was assigned to a hotkey (in my case, C).

Clicking the LOOP CAMS button while holding down the shift key worked as expected.
Would it be necessary to create a separate maxscript that contains the function for going backward, which can then be assigned to another hotkey?
0 Likes
Message 4 of 10

denisT.MaxDoctor
Advisor
Advisor

you can and should assign two keys to the script... in your case C and SHIFT+C

 

I prefer to do it this way and use SHIFT for macroaction alternative option because it works for both hotkeys and direct button presses.

0 Likes
Message 5 of 10

keith_brown
Advocate
Advocate

Ohhh! So cool! Thanks a lot!! 💪

0 Likes
Message 6 of 10

keith_brown
Advocate
Advocate

I ran into a problem when starting 3ds max up again. Pressing the C key switches the selected cam but does not change the viewport to that camera for some reason. If I go into the Maxscript Editor and press CTRL+E it will kick in and change the viewport as expected.

 

Any idea why this has to be done? 

0 Likes
Message 7 of 10

denisT.MaxDoctor
Advisor
Advisor

@keith_brown wrote:

I ran into a problem when starting 3ds max up again. Pressing the C key switches the selected cam but does not change the viewport to that camera for some reason. If I go into the Maxscript Editor and press CTRL+E it will kick in and change the viewport as expected.

 

Any idea why this has to be done? 


Did you make a .MCR file and put it in an autoloading place?

Are you sure that both keys are stored well and the hotkey preset loads with MAX?

0 Likes
Message 8 of 10

keith_brown
Advocate
Advocate

The extension was .ms but I changed it to .mcr. Also placed the file in 3ds Max 2022\scripts\Startup. Drag and dropped the file into max and changed the hotkeys to use the new file/location. Still the same though. It toggles between cameras but does not change to correlating viewport. It only "kicks" in when I click the Evaluate All (ctrl+e) from the MaxscriptEditor.

0 Likes
Message 9 of 10

denisT.MaxDoctor
Advisor
Advisor
There are five ways a MacroScript can be defined:
  • The default user-interface directory and its subdirectories are automatically scanned at startup for files of type .mcr, which are parsed for MacroScript definitions.

 

 

Read the MAX Help and find what directories can be used for MCR autoloading.

 

0 Likes
Message 10 of 10

keith_brown
Advocate
Advocate

Thanks! I'll look into it!

 

0 Likes