ModelTranslator: reuse unity_assets.safe_name in bl_convert, hoist stdlib imports
This commit is contained in:
@@ -1,8 +1,18 @@
|
||||
"""Blender 内运行:FBX -> 纯模型 FBX + XPbr 基础/混合贴图。
|
||||
调用:blender -b --factory-startup --python bl_convert.py -- <src.fbx> <outdir> <max_size>
|
||||
"""
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
|
||||
import numpy as np
|
||||
|
||||
# blender --python 不会把脚本目录加入 sys.path,手动加入以复用 unity_assets(纯 stdlib)
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
from unity_assets import safe_name
|
||||
|
||||
AO_NAME_RE = re.compile(r"(ao|occlusion|ambient)", re.I)
|
||||
|
||||
|
||||
def encode_normal_rg(n):
|
||||
"""切线法线 (N,3) -> 球面编码 (N,2),范围 [0,1]。
|
||||
@@ -25,14 +35,8 @@ def decode_sphere_map(enc01):
|
||||
|
||||
# ---------------- 以下仅在 Blender 内执行 ----------------
|
||||
|
||||
AO_NAME_RE = None # 延迟编译,见 _looks_like_ao
|
||||
|
||||
|
||||
def _looks_like_ao(name):
|
||||
import re
|
||||
global AO_NAME_RE
|
||||
if AO_NAME_RE is None:
|
||||
AO_NAME_RE = re.compile(r"(ao|occlusion|ambient)", re.I)
|
||||
return bool(AO_NAME_RE.search(name))
|
||||
|
||||
|
||||
@@ -143,7 +147,6 @@ def _save_png(path, rgba, srgb_view=False):
|
||||
|
||||
|
||||
def _convert_material(mat, model_name, outdir, max_size):
|
||||
import os
|
||||
warnings = []
|
||||
maps, consts = _material_maps(mat, warnings)
|
||||
tw, th = _target_size(maps, max_size)
|
||||
@@ -189,8 +192,7 @@ def _convert_material(mat, model_name, outdir, max_size):
|
||||
else:
|
||||
mix[..., 3] = consts["roughness"]
|
||||
|
||||
import re
|
||||
sname = re.sub(r"[^0-9A-Za-z_\-]", "_", mat.name).strip("_") or "mat"
|
||||
sname = safe_name(mat.name)
|
||||
base_png = os.path.abspath(os.path.join(outdir, "%s_%s_base.png" % (model_name, sname)))
|
||||
mix_png = os.path.abspath(os.path.join(outdir, "%s_%s_mix.png" % (model_name, sname)))
|
||||
_save_png(base_png, base)
|
||||
@@ -223,7 +225,7 @@ def _strip_textures():
|
||||
|
||||
|
||||
def main():
|
||||
import bpy, json, os, sys
|
||||
import bpy, json
|
||||
argv = sys.argv[sys.argv.index("--") + 1:]
|
||||
src, outdir, max_size = argv[0], argv[1], int(argv[2])
|
||||
os.makedirs(outdir, exist_ok=True)
|
||||
|
||||
Reference in New Issue
Block a user