3ds Max 提供了强大的 Python 开发接口,使得用户可以扩展软件功能、自动化重复任务,甚至直接处理图像数据。本教程将带领你一步步实现一个常见需求:在 3ds Max 中使用 Python 加载外部图像文件,并读取指定位置的像素值。我们将以 EXR 格式的 HDR 图像为例,详细讲解从导入模块、创建必要对象,到最终显示图像和访问像素数据的完整流程。无论你是 3ds Max 的脚本初学者,还是希望深入掌握 MaxPlus 图像处理功能的开发者,本教程都将为你提供清晰、可运行的示例和实用的技术说明。

本教程演示如何使用 Python for 3ds Max 加载并显示图像文件,并读取图像中指定位置的像素值。

示例:加载并显示 EXR 图像

以下示例假设 EXR 图像文件与 3ds Max 文件位于同一目录中。

from MaxPlus import BitmapManager
image_file_path = r'BG_park_A.exr'
bmp_storage = MaxPlus.Factory.CreateStorage(17)
bmp_info = bmp_storage.GetBitmapInfo()
bmp_info.SetName(image_file_path)
bmp = BitmapManager.Load(bmp_info)
bmp.Display()

代码逐行说明

  1. 导入 BitmapManager 类
    该类用于加载图像文件。

  2. 设置图像文件路径变量
    将图像文件的完整路径赋值给变量 image_file_path

  3. 创建 BitmapStorage 对象
    使用 MaxPlus.Factory.CreateStorage(17) 方法创建 BitmapStorage 对象。

    • 为什么这样做?
      因为 BitmapInfo 类没有构造函数,无法直接创建。这里通过创建 BitmapStorage 对象,再获取其内部的 BitmapInfo 对象,是一种间接但有效的实现方式。

    • 参数 17 的含义
      表示存储格式为 32 位浮点 RGBA 格式(不含 Alpha 通道)。该参数可根据需要选择其他颜色格式(见下方格式列表)。

    • 注意:某些格式(如 8)不可写,但本示例仅通过 BitmapStorage 获取 BitmapInfo,实际格式不影响最终加载结果。

  4. 获取 BitmapInfo 对象的引用
    通过 bmp_storage.GetBitmapInfo() 获取 BitmapStorage 内部的 BitmapInfo 对象。

  5. 设置图像文件路径
    使用 bmp_info.SetName(image_file_path) 将文件路径传递给 BitmapInfo 对象。

  6. 加载图像
    调用 BitmapManager.Load(bmp_info) 加载图像。

  7. 显示图像
    使用 bmp.Display() 在 3ds Max 的图像查看器窗口中显示图像。

附录:BitmapStorage 颜色格式常量类示例

以下类定义了常用的颜色格式常量,可替代整数参数(如 17),提高代码可读性。

class BitmapTypes(object):
     BMM_NO_TYPE = 0 # Not allocated yet
     BMM_LINE_ART = 1 # 1-bit monochrome image
     BMM_PALETTED = 2 # 8-bit paletted image. Each pixel value is an index into the color table.
     BMM_GRAY_8 = 3 # 8-bit grayscale bitmap.
     BMM_GRAY_16 = 4 # 16-bit grayscale bitmap.
     BMM_TRUE_16 = 5 # 16-bit true color image.
     BMM_TRUE_32 = 6 # 32-bit color: 8 bits each for Red, Green, Blue, and Alpha.
     BMM_TRUE_64 = 7 # 64-bit color: 16 bits each for Red, Green, Blue, and Alpha.
     BMM_TRUE_24 = 8 # 24-bit color: 8 bits each for Red, Green, and Blue. Cannot be written to.
     BMM_TRUE_48 = 9 # 48-bit color: 16 bits each for Red, Green, and Blue. Cannot be written to.
     BMM_YUV_422 = 10 # This is the YUV format - CCIR 601. Cannot be written to.
     BMM_BMP_4 = 11 # Windows BMP 16-bit color bitmap. Cannot be written to.
     BMM_PAD_24 = 12 # Padded 24-bit (in a 32 bit register). Cannot be written to.
     BMM_LOGLUV_32 = 13 BMM_LOGLUV_24 = 14
     BMM_LOGLUV_24A = 15 BMM_REALPIX_32 = 16 # The 'Real Pixel' format.
     BMM_FLOAT_RGBA_32 = 17 # 32-bit floating-point per component (non-compressed),
     RGB with or without alpha
     BMM_FLOAT_GRAY_32 = 18 # 32-bit floating-point (non-compressed), monochrome/grayscale
     BMM_FLOAT_RGB_32 = 19
     BMM_FLOAT_A_32 = 20

示例:读取图像中的像素值

加载图像后,可以读取指定坐标的像素值(例如 HDR 格式的浮点值):

bmp_storage = bmp.GetStorage()
hdr_pixel = bmp_storage.GetHDRPixel(3000,200)
print(hdr_pixel)

代码说明

  1. 从已加载的 Bitmap 对象获取 BitmapStorage 对象(会覆盖之前创建的临时 BitmapStorage)。

  2. 使用 GetHDRPixel(x, y) 读取坐标为 (3000, 200) 的像素值并打印。

注意:复制粘贴本教程中的代码时,请注意缩进可能需要手动调整。

通过本教程,你已经学会了如何在 3ds Max 中使用 Python 加载图像文件(特别是 EXR 格式),并读取 HDR 像素值。虽然创建 BitmapStorage 对象以间接获取 BitmapInfo 的方式看起来有些绕弯,但这正是 3ds Max MaxPlus 模块目前提供的可行方法之一。如果你发现了更简洁或更直接的方式,欢迎在评论区分享,让大家共同受益。

接下来,你可以尝试:

  • 修改图像文件路径,加载其他支持的格式(如 JPG、PNG、TGA 等);

  • 遍历整张图像的所有像素,进行图像分析或处理;

  • 将读取到的像素值应用到材质、贴图或渲染输出中。

希望本教程对你的 3ds Max 开发工作有所帮助。祝你编码愉快,创作顺利!

更多推荐