Grab 3dsmax viewport with python – Part 2

posted in: Blog | 0

I finally managed to get the viewport grabbing to work with python only, no hacks using MaxPlus.Core.EvalMAXscript(). More or less 🙂 It’s working for the standard viewport.getViewportDib() equivalent in Maxscript, I still can’t figure out how to grab it directly from the GraphicsWindow (gw.getViewportDib()) to get a clean viewport snapshot without any overlays like gizmos, etc. This method works only in 2015.

Long story short, I’ve updated the YCDIVFX MaxPlus Packages in github and even added a new package called maxhelpers where I will put code that will be reused across all other packages.

DOWNLOAD

Here’s how the code looks:

def ActiveViewport(self, filename=(MaxPlus.PathManager.GetRenderOutputDir()
                                   + r'default.jpg')):
        """Grabs viewport to a file on the hard-drive using default viewport size.

        :param str filename: a valid path to an image file

        :rtype:  MaxPlus.Bitmap
        """
        # Create storage
        storage = MaxPlus.Factory.CreateStorage(BitmapTypes.BMM_TRUE_64)

        # Create BitmapInfo
        bmi = storage.GetBitmapInfo()

        # Set filename
        bmi.SetName(filename)

        # Create bitmap to hold the dib
        bmp = MaxPlus.Factory.CreateBitmap()

        # Viewport Manager
        vm = MaxPlus.ViewportManager
        # Get active viewport
        av = vm.GetActiveViewport()
        # Grab the viewport dib into the bitmap
        av.GetDIB(bmi, bmp)

        # Open bitmap for writing
        bmp.OpenOutput(bmi)
        bmp.Write(bmi)
        bmp.Close(bmi)

        return bmp

To grab and display the bitmap in max you just need to do this:

bitmap = grabActiveViewport()
bitmap.Display()

Hope this is useful!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.