Notifications
Clear all

[Closed] Grab Viewport With Alpha

I added the ability to take a screenshot that’s larger than the screen resolution.

--***************************************************************************************************
-- Created: 		29-05-2011
-- Last Updated:	29-05-2011
-- Version:			1.1
--
-- Author :  Kostadin Kotev / miau_u@yahoo.com /
-- Version:  3ds max 9 and up
--
-- Discription: 
--	Grab the active viewport and include an alpha channel. Use the spinners to expand the active viewport.
--	IMPORTANT: The script is not very fast. :)
--	
--***************************************************************************************************
-- MODIFY THIS AT YOUR OWN RISK

macroscript miauuGrabViewport
category:"miauu"
tooltip:"Grab Viewport"
buttonText:"Grab Viewport"
(
	global rol_miauuGrabViewport
	try(destroyDialog rol_miauuGrabViewport)catch()
	rollout rol_miauuGrabViewport "miauu's Grab Viewport v1.1"
	(
		local grab_viewpBMP
		local defaultVbkgColor
		local grab_alphaBMP
		local tempBMP
		local viewport_name
		local mfn
		local gct
		local bmp_name
		local winSizeX = gw.getWinSizeX() as float
		local winSizeY = gw.getWinSizeY() as float
		local ratio = (winSizeX/winSizeY)	
		
		spinner spn_height "Viewport height:" range:[1,999999,winSizeY] type:#float scale:1
		spinner spn_width "Viewport width:" range:[1,999999,winSizeX] type:#float scale:1
		button btn_grabViewport "Grab Viewport"
		
		on btn_grabViewport pressed do
		(
		--////	credits to Borislav "Bobo" Petrov (MaxScript reference)
			--	get viewport name
			viewport_name = viewport.GetType() as string
			--	cut the string
			viewport_name = substring viewport_name 6 (viewport_name.count-5)
			
			if viewport_name == "camera" then --	if the remaining string is "camera"
			( 
				gac = getActiveCamera() --	get the camera
				viewport_name = gac.name --	get the name of the camera
			)
			--	get max file name
			mfn = MaxFileName
			
			if mfn == "" then --	if there is no name
				mfn = "Untitled" --	use "Untitled"
			else
				mfn = getFileNameFile mfn --	use the file name without .MAX extension
			
			--	get current frame time
			gct = SliderTime as string 
			--	build the output file name
			bmp_name = "VPGRAB_"+ mfn +"_" +viewport_name + "_" + gct

			--	Display file save dialog
			bmp_name = getSaveFileName caption:"Save Viewport to:" filename:bmp_name \
			types:"TGA(*.tga)|*.tga|PNG(*.png)|*.png|TIFF(*.tif)|*.tif|JPG(*.jpg)|*.jpg|BMP(*.bmp)|*.bmp|"
		--////
			if bmp_name != undefined then	--	if user has confirmed / entered a valid name
			( 
				--	store the viewport height and width
				curVPheight = gw.getWinSizeY()
				curVPwidth = gw.getWinSizeX()				
				--	expand the viewport
				gw.setPos 0 0 spn_width.value spn_height.value
				forcecompleteredraw dodisabled:true
				theHold.begin()
				--	grab the viewport
				grab_viewpBMP = gw.getViewportDib()
				--	make all visible objects white
				for o in objects where not o.isHidden do
				(
					o.displayByLayer = false
					o.showVertexColors = true
				)
				--	save the default viewport color
					--	defaultVbkgColor = getVPortBGColor()		-- not worked in max2010
				defaultVbkgColor = GetUIColor 41
				--	set the viewport color to pure black
					--	setVPortBGColor  ((color 0 0 0) as Point4)		--	not worked in max2010
				SetUIColor 41 ((color 0 0 0) as Point4)
				colorMan.repaintUI #repaintAll
				--	grab the bitmap that will be used as alpha channel
				grab_alphaBMP = gw.getViewportDib()
				
				theHold.cancel()
				
					--	setVPortBGColor  defaultVbkgColor	--	not worked in max2010
				SetUIColor 41 defaultVbkgColor
				colorMan.repaintUI #repaintAll
				
			--////	credits to DaveWortley.
				--create new temporary bitmap
				tempBMP = bitmap grab_viewpBMP.width grab_viewpBMP.height

				for i = 1 to grab_viewpBMP.height do
				(
					AR_thepixels_A = Getpixels grab_viewpBMP [0,i] grab_viewpBMP.width
					AR_thepixels_B = Getpixels grab_alphaBMP [0,i] grab_alphaBMP.width
					
					for j = 1 to AR_thepixels_A.count do
					(
						alfaVal = 0.0
						if AR_thepixels_B[j] == (color 0 0 0) do
						(
							swap AR_thepixels_A[j].a alfaVal
						)
					)
					setpixels tempBMP [0,i] AR_thepixels_A
				)
				--close the original images so we can overwrite them
				close grab_viewpBMP
				close grab_alphaBMP	
			--////
				tempBMP.filename = bmp_name	--	set output name to the one entered in the save file dialog
				save tempBMP
				Close tempBMP
				grab_viewpBMP = undefined
				grab_alphaBMP = undefined
				tempBMP = undefined
			)
			freeSceneBitmaps()
			gc()
			--	restore the viewport
			gw.setPos 0 0 curVPwidth curVPheight
			forcecompleteredraw dodisabled:true
		)
		
		on spn_height changed val do
		(
			spn_width.value = spn_height.value*ratio		
		)
		on spn_width changed val do
		(
			spn_height.value = spn_width.value/ratio		
		)
	)
	createdialog rol_miauuGrabViewport style:#(#style_titlebar, #style_sysmenu, #style_toolwindow) 
)

just a short question ( as i did’nt test and install the script … )

i guess i does not work in Nitrous / Max2012 ? I see in the source that you are using gw.setPos, but that ( plus other gw related maxscript commands) is broken / do not work in nitrous …

UPDATE:
okay i tested it in Max 2012, and as expected it does’nt work, not even in Direct3D mode

in D3D mode it grabs an empty viewport, you might be able to repair this by doing the actual grabbing in a viewport redraw callback (see further docs [here (Max online Help)](here http://docs.autodesk.com/3DSMAX/14/ENU/MAXScript%20Help%202012/files/GUID-1B088FF0-6A36-420E-9F37-F0DBE9FB267-2008.htm ) )

In Nitrous display mode the grabbing works, but the grabbed size always sticks to the size of the currently active viewport. ( As i mentioned above SetPos does’nt work in nitrous)
Additionally all shadows and AO are there in the alpha channel ( because of the method how you generate it ) . I guess you would have to switch off all advanced lighting and rendering options in the viewport for you alpha pass first…

Otherwise a reall usefull script

@tassel, one guy from policuont forum will add new things to the script – go to post #10.
May be hi will add “send the image to photoshop”.

I can,t use max2012(my pc is too old). If you don’t mind – can you send me the maxscript help file from max2012, so I can see what is the new in maxscript for this 3ds max release.

ahh i see -the link is broken in that previous post of mine …

you can grab all from here (first entries)
http://usa.autodesk.com/adsk/servlet/item?siteID=123112&id=16707768

either browse online or download the maxscript help zip, you can extract it locally and browse it there ( just html files … )

Here’s the entry i wanted to link inmy previous post , but failed …
http://docs.autodesk.com/3DSMAX/14/ENU/MAXScript%20Help%202012/files/GUID-1B088FF0-6A36-420E-9F37-F0DBE9FB267-2008.htm

@spacefrog, thank you for the links.

Page 2 / 2