#!/usr/bin/env python # -*- coding: utf-8 -*- import os import sys import maya.cmds as cmds import maya.mel as mel from config import data # 根据Maya和Python版本获取正确的DNA模块路径 MAYA_VERSION = cmds.about(version=True) SYSTEM_OS = "win64" if cmds.about(os=True) == "Windows" else "linux" PYTHON_VERSION = sys.version_info def load_dna_plugin(): """安全加载插件""" plugins = [ ("dnacalib.py", "dnacalib"), ("embeddedRL4.mll", "embeddedRL4"), ("MayaUERBFPlugin.mll", "MayaUERBFPlugin") ] for file_name, plugin_name in plugins: try: plugin_path = os.path.join(data.PLUGIN_PATH, file_name) if not os.path.exists(plugin_path): cmds.warning(f"⚠️ 插件文件缺失: {file_name}") continue if cmds.pluginInfo(plugin_name, q=True, loaded=True): print(f"✅ 已加载: {plugin_name}") continue cmds.loadPlugin(plugin_path) print(f"✔️ 成功加载: {plugin_name}") except Exception as e: cmds.warning(f"❌ 加载失败 {plugin_name}: {str(e)}") # 不要退出,继续尝试加载其他插件 def setup_dna_path(): """增强路径设置""" try: # 添加二进制路径到系统PATH bin_path = os.path.join(data.PYDNA_PATH, "bin") os.environ['PATH'] = f"{bin_path}{os.pathsep}{os.environ['PATH']}" print(f"已添加二进制路径: {bin_path}") # 确保Python能搜索到必要路径 required_paths = [ data.PLUGIN_PATH, data.PYDNA_PATH, os.path.join(data.PYDNA_PATH, "bin") ] for path in required_paths: if path not in sys.path: sys.path.insert(0, path) print(f"已添加路径: {path}") except Exception as e: cmds.warning(f"路径设置失败: {str(e)}") # 设置路径并加载插件 setup_dna_path() plugin_loaded = load_dna_plugin() # 全局变量 dna = None dnacalib = None try: # 在导入前添加二进制模块路径 PYBIN_PATH = os.path.join(data.PYDNA_PATH, "bin") if PYBIN_PATH not in sys.path: sys.path.insert(0, PYBIN_PATH) # 然后导入 DNA 模块 from dna import * import dnacalib except ImportError as e: cmds.warning(f"无法导入DNA模块: {str(e)}") cmds.warning(f"Python路径: {sys.path}") cmds.warning(f"插件路径: {data.PLUGIN_PATH}") cmds.warning(f"PyDNA路径: {data.PYDNA_PATH}") class DNAManager: def __init__(self): self.current_dna = None self.reader = None self.writer = None def is_ready(self): """检查DNA模块是否可用""" return dna is not None and dnacalib is not None def load_dna(self, file_path): """载入DNA文件""" if not self.is_ready(): cmds.warning("DNA模块未正确加载") return False try: # 创建DNA读取器 self.reader = dna.DNAReader() self.reader.read(file_path) self.current_dna = self.reader.get_dna() return True except Exception as e: cmds.warning(f"DNA文件加载失败: {str(e)}") return False def save_dna(self, file_path): """保存DNA文件""" try: if not self.current_dna: raise ValueError("没有DNA数据可保存") # 创建DNA写入器 self.writer = DNAWriter() self.writer.set_dna(self.current_dna) self.writer.write(file_path) return True except Exception as e: cmds.warning(f"DNA文件保存失败: {str(e)}") return False def calibrate_joints(self): """校准骨骼位置""" try: if not self.current_dna: raise ValueError("请先加载DNA文件") # 获取骨骼定义 joints = self.current_dna.get_joints() # 校准每个骨骼 for joint in joints: # 获取骨骼名称和位置 joint_name = joint.get_name() neutral_position = joint.get_neutral_position() # 如果Maya场景中存在该骨骼 if cmds.objExists(joint_name): # 设置骨骼位置 cmds.xform(joint_name, worldSpace=True, translation=neutral_position) return True except Exception as e: cmds.warning(f"骨骼校准失败: {str(e)}") return False def export_fbx(self, file_path): """导出FBX文件""" try: # 选择所有需要导出的对象 all_objects = self.get_export_objects() cmds.select(all_objects) # 设置FBX导出选项 mel.eval('FBXExportInputConnections -v true') mel.eval('FBXExportIncludeChildren -v true') # 导出FBX cmds.file(file_path, force=True, options="v=0", type="FBX export", preserveReferences=True, exportSelected=True) return True except Exception as e: cmds.warning(f"FBX导出失败: {str(e)}") return False def get_export_objects(self): """获取需要导出的对象列表""" # 获取模型、骨骼等对象 objects = [] if self.current_dna: # 添加骨骼 joints = self.current_dna.get_joints() for joint in joints: joint_name = joint.get_name() if cmds.objExists(joint_name): objects.append(joint_name) # 添加蒙皮模型 meshes = self.current_dna.get_meshes() for mesh in meshes: mesh_name = mesh.get_name() if cmds.objExists(mesh_name): objects.append(mesh_name) return objects def import_settings(self, file_path): """导入 DNA 设置""" try: if not self.is_ready(): raise ImportError("DNA模块未正确加载") # TODO: 实现导入设置逻辑 return True except Exception as e: cmds.warning(f"导入设置失败: {str(e)}") return False def export_settings(self, file_path): """导出 DNA 设置""" try: if not self.is_ready(): raise ImportError("DNA模块未正确加载") # TODO: 实现导出设置逻辑 return True except Exception as e: cmds.warning(f"导出设置失败: {str(e)}") return False