Skip to content

Instantly share code, notes, and snippets.

@cecilemuller
Created January 3, 2019 14:35
Show Gist options
  • Save cecilemuller/057d5ccdcbe8964d9babc6b339986bf5 to your computer and use it in GitHub Desktop.
Save cecilemuller/057d5ccdcbe8964d9babc6b339986bf5 to your computer and use it in GitHub Desktop.
Maxscript: get the Material Editor thumbnail for an arbitrary material
-- Reads the 88x88 thumbnail from Material Editor for an arbitrary material.
-- Note that it renders faster when the material editor is closed,
-- so you could call `MatEditor.Close()` first.
--
-- Parameters:
-- mat: a material instance (StandardMaterial, Universal_Material, etc..)
--
-- Returns:
-- a Bitmap or `undefined`
--
fn getMaterialThumbnail mat = (
local thumb = undefined
local iGlobal = (dotnetClass "Autodesk.Max.GlobalInterface").Instance
if iGlobal != undefined then (
local IntPtr = dotnetClass "System.IntPtr"
local pStampSize = (dotnetclass "Autodesk.Max.PostageStampSize").Large
local backupMat = meditMaterials[1]
meditMaterials[1] = mat
local matRef = iGlobal.CoreInterface.GetMtlSlot 0
local pStamp = matRef.CreatePStamp pStampSize true
local bytes = pStamp.Image
local size = pStamp.Width
thumb = bitmap size size gamma:0.5
local step = size * 3
for y = 1 to bytes.count by step do (
local row = for x = y to (y + step - 1) by 3 collect [bytes[x + 2], bytes[x + 1], bytes[x]]
setpixels thumb [0, size -= 1] row
)
pStamp.Dispose()
matRef.Dispose()
meditMaterials[1] = backupMat
)
return thumb
)
@andriypogribniy
Copy link

andriypogribniy commented Jun 23, 2024

Any way to generate larger bitmaps?
88x88 is kind of useless

@theacb
Copy link

theacb commented Jun 23, 2024

No, 88x88 is a hard limit imposed by this interface. Most people seem to instead set up background rendering in this situation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment