Itoo Software Forum

Author Topic: Problem setting distmap by script  (Read 715 times)

monomon

  • Newbie
  • *
  • Posts: 3
Problem setting distmap by script
« on: February 01, 2018, 08:12:31 AM »
Hi, I am trying to set a ForestPack instance's distribution map with a python script in 3ds max and I am having problems with it.

First, I create a MaxPlus.Bitmap in which I load an existing file from disk. Then, a MaxPlus.BitmapTex and set my bitmap on it. Finally, I set this BitmapTex on the "distmap" property of Forestpack.

When the script completes, the forest appears empty (though the distmap button in the sidebar indicates my distmap).
If I now manually click to reselect the map from the material editor it works!  ???
I am not sure what changes when I do this, like some weird conversions or setting missing properties... or do I just need to trigger some kind of update?

Here's example code:
Code: [Select]
BMM_GRAY_8 = 3  # 8-bit grayscale bitmap.
# Bitmap open mode flags
BMM_NOT_OPEN = 0
BMM_OPEN_R = 1
BMM_OPEN_W = 2

node_name = "forest_flowers"
conf = {
    "width": 32,
    "height": 32,
    "name": "bla" + str(random.randint(1, 15000))
}


def create_bitmaptex(bm, conf):
    bt = MaxPlus.Factory.CreateDefaultBitmapTex()
    bt.SetBitmap(bm)
    bt.SetFilterType(0)
    # wtf, isUIAction has to be true for Display to work...
    bt.SetMapName(bm.GetStorage().GetBitmapInfo().GetName(), True)
    return bt


def load_bitmap(bmpath, conf):
    width = conf["width"]
    height = conf["height"]

    storage = MaxPlus.Factory.CreateStorage(BMM_GRAY_8)

    info = storage.GetBitmapInfo()
    info.SetWidth(width)
    info.SetHeight(height)
    info.SetName(conf["name"] )
    info.SetPath(bmpath)
    info.SetGamma(2.2)
    info.SetBitmapType(BMM_GRAY_8)

    storage.Allocate(info, BMM_OPEN_R)
    bm = MaxPlus.BitmapManager.Load(info)
    bm.SetStorage(storage)
    return bm


def add_to_current_library(mat):
    MaxPlus.MaterialLibrary.GetCurrentLibrary().Add(mat)


try:
    forest_pack = MaxPlus.INode.GetINodeByName(node_name)

    # use one of the forestpack distmaps for example
    bmpath = os.path.join(
        "C:/",
        "Program Files (x86)",
        "Itoo Software",
        "Forest Pack Pro",
        "distmaps",
        "images",
        "spread2.bmp"
    )

    print("bitmap path", bmpath)
    bm = load_bitmap(bmpath, conf)
    print("bitmap", bm)
    bm.Display()

    bt = create_bitmaptex(bm, conf)
    print("bitmaptex", bt)
    add_to_current_library(bt)

    forest_pack.Object.GetParameterBlock()\
                      .GetParamByName("distmap")\
                      .SetValue(bt)

    MaxPlus.ViewportManager.ForceCompleteRedraw()

    # The value of distmap is now of type MaxPlus.Texmap
    print("distmap value", forest_pack.Object.GetParameterBlock()\
          .GetParamByName("distmap")\
          .Value)

except Exception as e:
    traceback.print_exc()

As an aside, initially I wanted to make this work with procedurally generated bitmaps, but that failed worse - manual reselection did not fix it, (I am guessing) because the bitmap's in-memory storage doesn't persist.

iToo

  • Administrator
  • Hero Member
  • *****
  • Posts: 3359
    • iToo Software
Re: Problem setting distmap by script
« Reply #1 on: February 02, 2018, 09:56:41 AM »
Hi, unfortunately i cannot help you with Python scripts, because i'm not familiar with this language (we use exclusively C++ and a little of Maxscript).

But changing a texture map from MaxScript seems to work fine. I did this:

- I create a FP object, and assign a texture map of type "Bitmap" to Distribution->Map.
- Then i change the bitmap, setting $Forest001.distmap.filename with the route of the bitmap, and FP is updated correctly.

Please note i use a texture map (Distribution->Map), not a bitmap directly (Distribution->Bitmap). The bitmap slot was not designed to be changed programatically.

I don't know if your Python script is correct, or if it's necessary some specific process to update the bitmap.
Carlos Quintero
iToo Software

monomon

  • Newbie
  • *
  • Posts: 3
Re: Problem setting distmap by script
« Reply #2 on: February 02, 2018, 12:10:40 PM »
Thank you for the response!

The Python api seems to work a bit differently. There is no filename property of the distmap, it can only reference either a TexMap object or a Bitmap (via SetMap and SetBitmap).

I just solved my issue, so I'll post the below for the benefit of others:

After some digging, it turned out a BitmapTex has a SetMapName method, which actually expects a filename, making it a bit of a misnomer.
Alternatively, it also worked if I first created an Asset with the bitmap path and set that on the texture using SetMap.

It seems that SetBitmap is useless, also as reported by https://forums.autodesk.com/t5/3ds-max-programming/applying-a-bitmap-image-to-a-texture/m-p/4229576/highlight/true#M9736.
(that's using the c++ api)
Pity, because it seemed to match my use case better.
Therefore, I suspect there is no chance of applying generated in-memory bitmaps...

This part of the max api is confusing, and the documentation is somewhat scant.

Thank you again.

A bit of example code that works:
Code: [Select]
import os

bmpath = os.path.join(
    "C:/",
    "Program Files (x86)",
    "Itoo Software",
    "Forest Pack Pro",
    "distmaps",
    "images",
    "dense.bmp"
)

tex = MaxPlus.Factory.CreateDefaultBitmapTex()
tex.SetMapName(bmpath)

node = MaxPlus.INode.GetINodeByName("forest_flowers")
node.Object.GetParameterBlock().GetParamByName("distmap").SetValue(tex)

iToo

  • Administrator
  • Hero Member
  • *****
  • Posts: 3359
    • iToo Software
Re: Problem setting distmap by script
« Reply #3 on: February 02, 2018, 03:23:24 PM »
Thanks for the information ! It may be useful for other users.

Carlos Quintero
iToo Software