前言
????????筆者處理模型時下載到一個pbr材質庫貼圖包,手動每次創建材質過于麻煩,因此計劃使用自動化腳本根據貼圖名自動創建材質。
????????3dsmax的原本腳本使用的是maxscript,語法有點奇怪懶得學,發現也支持使用python編寫腳本,但是python腳本的開發資料太少,官方文檔的舉例使用的是標準(standard)材質,pbr材質很多接口沒有找到,試了很久發現名稱應該是與英文3dsmax一致,這里簡單記錄一下。
一、思路
1. 根據文件夾名確定材質名稱
2. 根據文件夾下貼圖的名稱確定各輸入貼圖
3. 將貼圖賦予pbr材質的各位置
二、代碼
import os
import pymxsdef 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_pathsif __name__ == '__main__':rt = pymxs.runtimefolder_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 = subdirt = rt.sphere() # 創建賦予材質的物體t.material = physical_materialmat_path = folder_path + "\\" + subdirtexture_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_textureelif "glossiness" in textureName: # 光澤度貼圖——>粗糙度貼圖bitmap_texture = rt.BitmapTexture()bitmap_texture.filename = folder_path + "\\" + subdir + "\\" + textureNamephysical_material.RoughnessMap = bitmap_textureelif "normal" in textureName: # 法線貼圖——>凹凸貼圖bitmap_texture = rt.BitmapTexture()bitmap_texture.filename = folder_path + "\\" + subdir + "\\" + textureNamephysical_material.BumpMap = bitmap_textureelif "reflection" in textureName: # 反射貼圖——>反射貼圖bitmap_texture = rt.BitmapTexture()bitmap_texture.filename = folder_path + "\\" + subdir + "\\" + textureNamephysical_material.ReflColorMap = bitmap_textureelif "height" in textureName: # 高度貼圖——>置換貼圖bitmap_texture = rt.BitmapTexture()bitmap_texture.filename = folder_path + "\\" + subdir + "\\" + textureNamephysical_material.DisplacementMap = bitmap_texture
三、說明
physical_material后的屬性參照上圖,去除空格保留大小寫
在3dsmax中選擇腳本——運行腳本,選擇python腳本運行即可