#!/usr/bin/env python # -*- coding: utf-8 -*- """ 版权所有: 清泉时代科技有限公司 联系方式: q.100@qq.com 创建日期: 2024/09/24 """ import maya.cmds as cmds import re def sg_batch_add_blend_shape(lod, meshes): """ 批量为指定LOD级别的模型添加混合变形目标 参数: lod (int): LOD级别 meshes (list): 要处理的网格体名称列表 """ # 获取指定LOD级别的所有网格体索引 mesh_indices = cmds.SGGetMeshes(lod=lod) # 开始进度条 cmds.SGProgressBar(sp=True) for mesh_index in mesh_indices: mesh = cmds.SGGetMeshes(m=mesh_index) if cmds.objExists(mesh): lod_mesh = cmds.SGGetMeshes(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 = cmds.SGGetBlendShapes(tc=target_count) # 设置进度条 cmds.SGProgressBar(max=count) cmds.SGProgressBar(t=f"[{blend_shape}] Creating Target Mesh...") # 创建混合变形目标 for index in range(count): cmds.SGProgressBar(apr=1) bs_name = cmds.SGGetBlendShapes(bsn=target_count, index=index) # 复制网格体作为目标 cmds.duplicate(mesh, returnRootsOnly=True, name=bs_name) # 对LOD0级别的特定模型设置混合变形目标 if lod == 0 and (head in ["head", "teeth", "cartilage"]): cmds.SGSetBlendShapes(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) # 结束进度条 cmds.SGProgressBar(ep=True)