3dsmax中使用python创建PBR材质并挂接贴图

前言

笔者处理模型时下载到一个pbr材质库贴图包,手动每次创建材质过于麻烦,因此计划使用自动化脚本根据贴图名自动创建材质。

3dsmax的原本脚本使用的是maxscript,语法有点奇怪懒得学,发现也支持使用python编写脚本,但是python脚本的开发资料太少,官方文档的举例使用的是标准(standard)材质,pbr材质很多接口没有找到,试了很久发现名称应该是与英文3dsmax一致,这里简单记录一下。

一、思路

  1. 根据文件夹名确定材质名称

  2. 根据文件夹下贴图的名称确定各输入贴图

  3. 将贴图赋予pbr材质的各位置

二、代码

python 复制代码
import os
import pymxs

def get_files_in_folder(folder_path):
    file_paths = []
    for root,dirs,files in os.walk(folder_path):
        for file in files:
            file_path = os.path.join(root,file)
            file_paths.append(file_path)
    return file_paths
    
if __name__ == '__main__':
    rt = pymxs.runtime
    folder_path = "XXXX"  # 文件夹路径
    
    subdirectories = [d for d in os.listdir(folder_path) if os.path.isdir(os.path.join(folder_path, d))]

    for subdir in subdirectories:
        physical_material = rt.PhysicalMaterial() # 创建一个物理材质
        physical_material.name = subdir
        t = rt.sphere() # 创建赋予材质的物体
        t.material = physical_material

        mat_path = folder_path + "\\" + subdir
        texture_paths = get_files_in_folder(mat_path)

        for path in texture_paths:
            textureName = path.split("\\")[-1]
            if "diffuse" in textureName: # 漫反射贴图------>基础色贴图
                bitmap_texture = rt.BitmapTexture()
                bitmap_texture.filename = folder_path + "\\" + subdir + "\\" + textureName            
                physical_material.BaseColorMap = bitmap_texture
            elif "glossiness" in textureName: # 光泽度贴图------>粗糙度贴图
                bitmap_texture = rt.BitmapTexture()
                bitmap_texture.filename = folder_path + "\\" + subdir + "\\" + textureName
                physical_material.RoughnessMap = bitmap_texture
            elif "normal" in textureName: # 法线贴图------>凹凸贴图
                bitmap_texture = rt.BitmapTexture()
                bitmap_texture.filename = folder_path + "\\" + subdir + "\\" + textureName
                physical_material.BumpMap = bitmap_texture
            elif "reflection" in textureName: # 反射贴图------>反射贴图
                bitmap_texture = rt.BitmapTexture()
                bitmap_texture.filename = folder_path + "\\" + subdir + "\\" + textureName
                physical_material.ReflColorMap = bitmap_texture
			elif "height" in textureName: # 高度贴图------>置换贴图
                bitmap_texture = rt.BitmapTexture()
                bitmap_texture.filename = folder_path + "\\" + subdir + "\\" + textureName
                physical_material.DisplacementMap = bitmap_texture

三、说明

physical_material后的属性参照上图,去除空格保留大小写

在3dsmax中选择脚本------运行脚本,选择python脚本运行即可

相关推荐
蓝婷儿几秒前
Python 机器学习核心入门与实战进阶 Day 2 - KNN(K-近邻算法)分类实战与调参
python·机器学习·近邻算法
之歆38 分钟前
Python-封装和解构-set及操作-字典及操作-解析式生成器-内建函数迭代器-学习笔记
笔记·python·学习
天天爱吃肉82181 小时前
ZigBee通信技术全解析:从协议栈到底层实现,全方位解读物联网核心无线技术
python·嵌入式硬件·物联网·servlet
Allen_LVyingbo2 小时前
Python常用医疗AI库以及案例解析(2025年版、上)
开发语言·人工智能·python·学习·健康医疗
智能砖头2 小时前
LangChain 与 LlamaIndex 深度对比与选型指南
人工智能·python
风逸hhh3 小时前
python打卡day58@浙大疏锦行
开发语言·python
烛阴4 小时前
一文搞懂 Python 闭包:让你的代码瞬间“高级”起来!
前端·python
JosieBook4 小时前
【Java编程动手学】Java中的数组与集合
java·开发语言·python
Gyoku Mint5 小时前
深度学习×第4卷:Pytorch实战——她第一次用张量去拟合你的轨迹
人工智能·pytorch·python·深度学习·神经网络·算法·聚类
郭庆汝11 小时前
pytorch、torchvision与python版本对应关系
人工智能·pytorch·python