#!/usr/bin/env python # -*- coding: utf-8 -*- import maya.cmds as cmds import re from Core import GetMeshes, GetBlendShapes, ProgressBar, SetBlendShapes def batch_add_blend_shape(lod, meshes): """ 批量为指定LOD级别的模型添加混合变形目标 参数: lod (int): LOD级别 meshes (list): 要处理的网格体名称列表 """ # 获取指定LOD级别的所有网格体索引 mesh_indices = GetMeshes(lod=lod) # 开始进度条 ProgressBar(sp=True) for mesh_index in mesh_indices: mesh = GetMeshes(m=mesh_index) if cmds.objExists(mesh): lod_mesh = GetMeshes(i=mesh_index) # 使用正则表达式匹配第一个下划线前的内容 head = re.match(r'[^_]+', lod_mesh).group(0) if head in meshes: blend_shape = f"{lod_mesh}_blendShapes" if not cmds.objExists(blend_shape): # 创建混合变形节点 cmds.blendShape(mesh, automatic=True, name=blend_shape) # 根据不同的模型类型处理不同的目标数量 target_count_map = { "head": 0, "teeth": 1, "saliva": 1, "eyeLeft": 3, "eyeRight": 4, "eyeshell": 8, "eyelashes": 8, "eyeEdge": 8, "cartilage": 8 } if head in target_count_map: target_count = target_count_map[head] count = GetBlendShapes(tc=target_count) # 设置进度条 ProgressBar(max_value=count) ProgressBar(t=f"[{blend_shape}] Creating Target Mesh...") # 创建混合变形目标 for index in range(count): ProgressBar(apr=1) bs_name = GetBlendShapes(bsn=target_count, index=index) # 复制网格体作为目标 cmds.duplicate(mesh, returnRootsOnly=True, name=bs_name) # 对LOD0级别的特定模型设置混合变形目标 if lod == 0 and (head in ["head", "teeth", "cartilage"]): SetBlendShapes(ct=mesh_index, index=index, target=bs_name) # 添加混合变形目标 cmds.blendShape( blend_shape, edit=True, tangentSpace=True, target=(mesh, index, bs_name, 1.0), weight=(index, 0) ) # 删除临时目标模型 cmds.delete(bs_name) # 结束进度条 ProgressBar(ep=True)