Home Technical Talk

share: maxscript > store camera resolution per camera

sublime tool
Offline / Send Message
renderhjs sublime tool
what:
simple maxscript that stores and reads per camera stored rendering resolution settings (width and height). You simply click on a camera of that list (list of your cameras in the scene) and it will change the aspect ratio of the safe frame and change your render width and height settings.
Its quite specific but perhaps its to some use to some people here.

I wrote this script yesterday from scratch for a current project in which I need consistent resolution density renderings for a high resolution comic (~ A1 in size).
here are a few screens:
mxs_camera_resolution_switcher.gif

some notes:
- early dirty code, works but could be refined and tweaked
- if you pick a camera from the list your current active view port will be set to that view with safeframe but not as camera but as a user perspective instead so you cant change the view by accident. The camera itself however will be selected as object
- the "...of A2" or "... of A4" are compared by the width of that size, not by the square size.
- "calculate" only fills in the pixel units for width and height in the textfields, it does not actually change the values of the camera.

the script (no macroscript, just straight rollout floater script):
(
	cam_nodes = #();
	cam_names = #();
	camRollout;
	do_select;
	do_set;
	get_list_label;
	init;
	function do_set nr w h =(
		if (cam_nodes.count > 0)then(
			if (nr > cam_nodes.count)then(
				nr = 1;--make it the first
			)else if(nr <= 0)then(
				nr = cam_nodes.count;
			)
			local cam = cam_nodes[nr];
			setUserProp cam "cam_width" (w)as String;
			setUserProp cam "cam_height" (h)as String;
			
			camRollout.lbx1.selection = nr;
			do_select nr;
		)
	)
	function get_list_label nr = (
		local txt = "#"+nr as string;
		if (cam_nodes.count > 0)then(
			if (nr > cam_nodes.count)then(
				nr = 1;--make it the first
			)else if(nr <= 0)then(
				nr = cam_nodes.count;
			)
			local cam = cam_nodes[nr];
			local w = getUserProp cam "cam_width"
			local h = getUserProp cam "cam_height"
			
			if (w != undefined and h != undefined)then(
				local ppi = (camRollout.txt_ppi.text as float);
				local w_mm = floor (w as float / ppi * 2.54*10) as integer;
				local h_mm = floor (h as float / ppi * 2.54*10) as integer;
				
				txt = cam.name+"  |  "+w as string+"x"+h as string+"  |  "+cam.fov as string+"°  |  "+w_mm as string+" x "+h_mm as string+" mm";
			)
		)
		return txt;
	)
	function do_select nr =(
		
		if (cam_nodes.count > 0)then(
			if (nr > cam_nodes.count)then(
				nr = 1;--make it the first
			)else if(nr <= 0)then(
				nr = cam_nodes.count;
			)
			camRollout.lbx1.selection = nr;
			print("select "+nr as string);
			
			
			
			local cam = cam_nodes[nr];
			local w = getUserProp cam "cam_width"
			local h = getUserProp cam "cam_height"
			
			if (w != undefined and h != undefined)then(
				camRollout.txt_w.text = w as string;
				camRollout.txt_h.text = h as string;
				cam_names[nr] = get_list_label nr;--nr as string+".."+cam.name+"...."+w as string+"x"+h as string+"....."+cam.fov as string;
				camRollout.lbx1.items = cam_names;
				
				renderWidth = w as integer;
				renderHeight = h as integer;
				
				local ppi = (camRollout.txt_ppi.text as float);
				local mm_w = (w as float) / ppi * 2.54 *10;
				local mm_h = (h as float) / ppi * 2.54 *10;
				local out_of_A2 = (mm_w*mm_h) / (420.0 * 594.0) *100;
				local out_of_A4 = (mm_w*mm_h) / (210.0 * 297.0) *100;
				
				local shld_w_A1 = floor((594.0 / 2.54)*ppi) as integer;
				local shld_w_A2 = floor((420.0 / 2.54)*ppi) as integer;
				--420 × 594
				--local out = "ratio: "+((w as float / h as float) )as string;
				local out = ""+floor mm_w as string+" x "+floor mm_h as string+" mm";
				out+="n"+floor out_of_A2 as string +" % of A2";
				out+="n"+floor out_of_A4 as string +" % of A4";
				out+="n@A1 w: "+shld_w_A1 as string +" pix";
				out+="n@A2 w: "+shld_w_A2 as string +" pix";
				
				
				camRollout.txt_out.text = out;
				
				camRollout.txt_mm_w.text = mm_w as string;
				camRollout.txt_mm_h.text = mm_h as string;
				
			)else(
				print("value is not yet defined ."+w as string);
				
				setUserProp cam "cam_width" (renderWidth)as String;--use current reder resolution
				setUserProp cam "cam_height" (renderHeight)as String;
			)
			--
			select cam_nodes[nr];
			actionMan.executeAction 0 "40068"  -- Views: Camera View
			actionMan.executeAction 0 "40182"  -- Views: Perspective User View, back to perspective
			if (displaySafeFrames != true)then(
				max safeframe toggle;--
			)
		)
	)
	function do_calc_size mm_w mm_h=(
		local ppi = (camRollout.txt_ppi.text as float);
		camRollout.txt_w.text = (ceil ((mm_w/10) as float / 2.54 * ppi) as integer) as string;
		camRollout.txt_h.text = (ceil ((mm_h/10) as float / 2.54 * ppi) as integer) as string;
			
	)
	rollout camRollout "Camera Switcher" width:277 height:293
	(
		listbox lbx1 "cameras in the scene" pos:[2,2] width:271 height:12
		button btn_prev "<<" pos:[1,182] width:24 height:32
		button btn_nxt ">>" pos:[25,182] width:24 height:32
		button btn_set "set" pos:[104,182] width:29 height:32
		
		edittext txt_w "w" pos:[59,181] width:44 height:16
		edittext txt_h "h" pos:[61,198] width:42 height:16
		edittext txt_out "" pos:[138,183] width:135 height:80
		
		edittext txt_ppi "ppi" pos:[53,215] width:50 height:16
		button btn_init "initialize camera list" pos:[162,0] width:111 height:18
		edittext txt_mm_w "w" pos:[59,231] width:61 height:16
		edittext txt_mm_h "h" pos:[61,247] width:59 height:16
		label lbl1 "mm" pos:[121,232] width:19 height:15
		label lbl2 "mm" pos:[121,248] width:19 height:15
		button btn_calc "calculate" pos:[2,231] width:52 height:32
		
		
		on lbx1 selected _nr do
		(
			do_select _nr;
		)
		on btn_prev pressed do
		(
			do_select (lbx1.selection-1);
		)
		on btn_nxt pressed do
		(
			do_select (lbx1.selection+1);
		)
		on btn_set pressed do
		(
			do_set lbx1.selection (txt_w.text as integer) (txt_h.text as integer);
		)
		on btn_init pressed do
		(
			init();
		)
		on btn_calc pressed do
		(
			do_calc_size	(txt_mm_w.text as float) (txt_mm_h.text as float)
		)
	)
	
	
	function init =(
		actionMan.executeAction 0 "40021"  -- Selection: Select All;
		--local all = selection;
		cam_nodes = #();
		cam_names = #();
		local count = 1;
		for o in selection do(
			if ((classOf o) == Targetcamera or (classOf o) == Freecamera)then(
				cam_nodes[cam_nodes.count+1] = o;
				cam_names[cam_names.count+1] = get_list_label count;
				count+=1;
			)
		)
		print("count. "+count as string);
		clearSelection();
		
		local list = camRollout.lbx1;
		list.items = cam_names;
		if (cam_nodes.count > 1)then(
			list.selection = 1;
		)
		
	)
	
	rf = newRolloutFloater "Cam switch" 286 306
	addRollout camRollout rf;
	camRollout.txt_ppi.text = 300 as string;
	init();
)
Sign In or Register to comment.