Autodesk Maya项目改变了事件(信号)

时间:2015-07-26 16:44:24

标签: pyside maya mel pymel

我正在PySide为Maya编写项目经理。它的目的是浏览项目内容以便快速访问。所以我希望自动重新扫描新设置的项目目录。据我所知,没有像" Project Changed"在maya ScriptJob命令中。任何建议将不胜感激!

1 个答案:

答案 0 :(得分:0)

你是对的,因为在Maya的scriptJob命令中没有任何“Project Changed”事件,你必须有点破解这个。 以下答案意味着修改Maya的文件。因此,如果您需要在多台计算机上部署项目经理,您还必须部署对Maya文件所做的修改。

可能有一个更简单的解决方案,但无法找到如何使用现有事件有效地做到这一点。

注意:

我正在使用Maya 2014,因此请根据您的实际版本更改提供的路径。

解决方案1 ​​ - 基于PyMel:

  • 转到C:\Program Files\Autodesk\Maya2014\Python\Lib\site-packages\pymel\core
  • 在您喜欢的编辑器中打开system.py
  • 搜索Workspace班级@547中的system.py
  • 修改chdir函数(@621):

    @classmethod
    def chdir(self, newdir):
       _OpenMaya.MUserEventMessage.postUserEvent('ProjectChanged')
       return cmds.workspace( dir=newdir )
    
  • 打开 Maya(需要重新启动Maya才能使修改生效)

  • 在脚本编辑器中运行此代码:

    import maya.OpenMaya as om
    #Register a new event (IE: Tell maya that this event really exists)
    om.MUserEventMessage.registerUserEvent('ProjectChanged')
    from pymel.all import *
    
    #This function will be called when the project will be changed
    #Do your real stuff inside
    def myCallbackFunction(data):
        print('Got a ChangedProject event!')
    
    #Tell Maya to run myCallbackFunction when ProjectCHanged occurs    
    callbackId = om.MUserEventMessage.addUserEventCallback('ProjectChanged', myCallbackFunction)
    
    #Whange your project directory:
    #   - The ProjectChanged signal will be sent
    #   - Maya will catch it and execute myCallbackFunction
    #   - This will print 'Got a ChangedProject event!'
    workspace.chdir(r"C:\Users\dhasselhoff\Documents\maya\projects\hookedonafeeling")
    
    • 如果要在完成后删除回调函数:om.MUserEventMessage.removeCallback(callbackId)

解决方案2 - 基于MEL:

  • 转到C:\Program Files\Autodesk\Maya2014\scripts\others
  • 在您喜欢的编辑器中打开setProject.mel
  • 修改setProject函数(@359):

        ...
        // Restore the current directory. The project may have changed, but the cur dir
        // need not
        workspace -dir $oldCurrentDir;
        python("import maya.OpenMaya as om\nom.MUserEventMessage.postUserEvent('ProjectChanged')");
    }
    // Try to set it directly from the name given
    else
    {
        sp_setLocalWorkspaceWithoutPopupDialog $newProject;
        python("import maya.OpenMaya as om\nom.MUserEventMessage.postUserEvent('ProjectChanged')");
    }
    
  • 打开 Maya(需要重新启动Maya才能使修改生效)

  • 在脚本编辑器中运行此代码:

    import maya.OpenMaya as om
    #Register a new event (IE: Tell maya that this event really exists)
    om.MUserEventMessage.registerUserEvent('ProjectChanged')
    
    #This function will be called when the project will be changed
    #Do your real stuff inside
    def myCallbackFunction(data):
        print('Got a ChangedProject event!')
    
    #Tell Maya to run myCallbackFunction when ProjectCHanged occurs    
    callbackId = om.MUserEventMessage.addUserEventCallback('ProjectChanged', myCallbackFunction)
    
    #Whange your project directory:
    #   - The ProjectChanged signal will be sent
    #   - Maya will catch it and execute myCallbackFunction
    #   - This will print 'Got a ChangedProject event!'
    mel.eval('setProject("C:\Users\dhasselhoff\Documents\maya\projects\hookedonafeeling");')
    
    • 如果要在完成后删除回调函数:om.MUserEventMessage.removeCallback(callbackId)