Skip to content

Instantly share code, notes, and snippets.

@JokerMartini
Created July 8, 2025 17:26
Show Gist options
  • Save JokerMartini/54ab40c23732e5ee3746f46c11f96c8b to your computer and use it in GitHub Desktop.
Save JokerMartini/54ab40c23732e5ee3746f46c11f96c8b to your computer and use it in GitHub Desktop.
3dsmax Viewport Screenshot
from pymxs import runtime as rt
from PySide2.QtGui import QPixmap
from PySide2.QtCore import QRect
def crop_image_to_square(input_path, output_path):
"""
Crop an image to a square based on its shortest side, centered.
Args:
input_path (str): Path to the input PNG image
output_path (str): Path where the cropped image will be saved
Returns:
bool: True if successful, False otherwise
"""
try:
# Load the image
pixmap = QPixmap(input_path)
if pixmap.isNull():
print(f"Error: Could not load image from {input_path}")
return False
# Get original dimensions
width = pixmap.width()
height = pixmap.height()
# Calculate the size of the square (shortest side)
square_size = min(width, height)
# Calculate crop position to center the square
x_offset = (width - square_size) // 2
y_offset = (height - square_size) // 2
# Create crop rectangle
crop_rect = QRect(x_offset, y_offset, square_size, square_size)
# Crop the image
cropped_pixmap = pixmap.copy(crop_rect)
# Save the cropped image
success = cropped_pixmap.save(output_path, "PNG")
if success:
print(f"Successfully cropped {input_path} to {output_path}")
print(f"Original size: {width}x{height}")
print(f"Cropped size: {square_size}x{square_size}")
else:
print(f"Error: Could not save cropped image to {output_path}")
return success
except Exception as e:
print(f"Error cropping image: {e}")
return False
def capture_screenshot(filepath):
rt.pngio.setType(rt.name('true24'))
rt.pngio.setAlpha(True)
rt.pngio.setInterlaced(True)
cached_view_index = rt.ViewPanelManager.GetActiveViewPanelIndex()
cached_grid_vis = rt.viewport.GetGridVisibility(cached_view_index)
cached_axis_tripod = rt.maxOps.showWorldAxis
# required: setviewport background to use env color
rt.viewport.EnableSolidBackgroundColorMode(True)
rt.InvalidateAllBackgrounds()
rt.viewport.DispBkgImage = True
rt.hideByCategory.nonrenderables = True
rt.maxOps.showWorldAxis = False
rt.viewport.setGridVisibility(cached_view_index, False)
rt.execute('''
iGlobal = (dotNetClass "Autodesk.Max.GlobalInterface").Instance
iViewportButtonManagerID = iGlobal.Interface_ID.Create 0x47ab2195 0x22e8126e
vpButtonManager = iGlobal.GetCOREInterface iViewportButtonManagerID
vpButtonManager.EnableButtons off
''')
rt.forceCompleteRedraw()
# img = rt.gw.getViewportDib(captureAlpha=True, gammaCorrect=True)
img = rt.viewport.getViewportDib(captureAlpha=True, gammaCorrect=True)
img.filename = filepath
rt.save(img)
# rt.display(img)
# restore
rt.viewport.setGridVisibility(cached_view_index, cached_grid_vis)
rt.viewport.DispBkgImage = False
rt.viewport.EnableSolidBackgroundColorMode(True)
rt.hideByCategory.nonrenderables = False
rt.maxOps.showWorldAxis = cached_axis_tripod
rt.execute('''
iGlobal = (dotNetClass "Autodesk.Max.GlobalInterface").Instance
iViewportButtonManagerID = iGlobal.Interface_ID.Create 0x47ab2195 0x22e8126e
vpButtonManager = iGlobal.GetCOREInterface iViewportButtonManagerID
vpButtonManager.EnableButtons on
''')
rt.forceCompleteRedraw()
if __name__ == '__main__':
filepath = 'test.png'
capture_screenshot(filepath)
crop_image_to_square(filepath, filepath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment