Generate a bitmap texture from script

Generate a bitmap texture from script

Anonymous
Not applicable
1,282 Views
1 Reply
Message 1 of 2

Generate a bitmap texture from script

Anonymous
Not applicable

I am writing a Python script that, among other things, generates bitmaps derived from some data. It then sets them as distribution maps on ForestPack. See the example code below.

The bitmap is successfully created and Display()ed properly (See attachment Bitmap_Display.png)

However, when I create a BitmapTex and use SetBitmap to set the previously created bitmap, the texture appears blank (black), both in the material editor and when applying it (as a distribution map for ForestPack), see attachment BitmapTex_Material_Library.png.

Any clues? The only remotely related post I found was this one, and it says that SetBitmap is "no use".

The only relevant documentation example just creates a bitmap, but it does nothing with BitmapTex. However, ForestPack expects a Texmap or subclass as input for its density map.

 

Edit: is it at all expected that setting a bitmap from memory works? Is it required to reside in a file? It is not an absolute requirement for me to use a bitmap in memory, but it is preferable.

 

BMM_GRAY_8 = 3 # 8-bit grayscale bitmap.
BMM_FLOAT_GRAY_32 = 18 # 32-bit floating-point (non-compressed), monochrome/grayscale
BMM_OPEN_W = 2

width = 256
height = 256

data = [[random.random() for i in range(width)] for j in range(height)]
bm_name = "python_noise"

storage = MaxPlus.Factory.CreateStorage(BMM_GRAY_8)
info = storage.GetBitmapInfo()
info.SetWidth(width)
info.SetHeight(height)
info.SetName(bm_name)
info.SetGamma(2.2)
info.SetBitmapType(BMM_GRAY_8)

storage.Allocate(info, BMM_OPEN_W)

# now set the data on the bitmap storage
for x in range(width):
	for y in range(height):
		storage.Put16GrayFloat(x, y, data[x][y])

bm = MaxPlus.Factory.CreateBitmap(info)
bm.SetStorage(storage)
bm.Display(bm_name) # this works

bt = MaxPlus.Factory.CreateDefaultBitmapTex()
bt.SetMapName(bm_name, True) # wtf, isUIAction has to be true for Display to work...
bt.SetBitmap(bm)

# add it to material library
MaxPlus.MaterialLibrary.GetCurrentLibrary().Add(bt)

# now the bitmap texture is blank

 

0 Likes
1,283 Views
1 Reply
Reply (1)
Message 2 of 2

drew_avis
Autodesk
Autodesk

Hi there, when I tried this in MAXScript, I notice that you get an error when trying to create a BitmapTexture based on an in-memory bitmap (to the effect that the bitmap needs to be saved first).  So I tried updating your script based on this idea, and it works for me.  Here's the updated script with the changed/added lines highlighted:

import random

BMM_GRAY_8 = 3 # 8-bit grayscale bitmap.
BMM_FLOAT_GRAY_32 = 18 # 32-bit floating-point (non-compressed), monochrome/grayscale
BMM_OPEN_W = 2

width = 256
height = 256

data = [[random.random() for i in range(width)] for j in range(height)]
# bm_name = "python_noise"
bm_name = MaxPlus.PathManager.GetImageDir() + r'\python_noise.bmp' # must give the full path

storage = MaxPlus.Factory.CreateStorage(BMM_GRAY_8)
info = storage.GetBitmapInfo()
info.SetWidth(width)
info.SetHeight(height)
# set name w/ path
info.SetName(bm_name)
info.SetGamma(2.2)
info.SetBitmapType(BMM_GRAY_8)
info.SetCustomFlag(0)

storage.Allocate(info, BMM_OPEN_W)

# now set the data on the bitmap storage
for x in range(width):
	for y in range(height):
		storage.Put16GrayFloat(x, y, data[x][y])

bm = MaxPlus.Factory.CreateBitmap(info)
bm.SetStorage(storage)
bm.Display(bm_name) # this works

# save as a file <- this is new
bm.OpenOutput(info)
bm.Write(info)
bm.Close(info)

bt = MaxPlus.Factory.CreateDefaultBitmapTex()
bt.SetMapName(bm_name, True) # wtf, isUIAction has to be true for Display to work...
bt.SetBitmap(bm)
bt.ReloadBitmapAndUpdate() # <- I also needed this to update the BitmapInfo w/ file information it requires

# add it to material library
MaxPlus.MaterialLibrary.GetCurrentLibrary().Add(bt)

# should be usable


Drew Avis
Content Experience Designer
0 Likes