API导出问题

时间:2014-10-27 10:39:11

标签: python api maya

我需要有人对我的出口问题有所了解。

工作原理:选择相机(动画与否是可选的)>>文件>>导出选择>>文件类型:.chan(需要将此脚本作为插件加载)

这是问题的起点。它可以创建.text文件,但是,它不是“导出”或将内容写入文本文件,文件大小为零字节。

我正在使用它已编码的当前API,修改代码以添加到某些maya cmds中

有人可以帮助我吗?

import math, sys, string, os

import maya.OpenMaya as OpenMaya
import maya.OpenMayaMPx as OpenMayaMPx
import maya.OpenMayaAnim as OpenMayaAnim

import maya.cmds as cmds
import maya.mel as mel

kPluginTranslatorTypeName = "chan Export/Import"
kVersionNumber = "0.5a"

camSel = []
win_name = "chan_window"

class CustomNodeTranslator(OpenMayaMPx.MPxFileTranslator):   
    def __init__(self):
        OpenMayaMPx.MPxFileTranslator.__init__(self)       
    def haveWriteMethod(self):
        return True
    def haveReadMethod(self):
        return True
    def filter(self):
        return " .chan"
    def defaultExtension(self):
        return "chan"
    def writer( self, fileObject, optionString, accessMode ):
        try:

            fullName = fileObject.fullName()
            fileHandle = open(fullName,"w")

            selectList = OpenMaya.MSelectionList()

            OpenMaya.MGlobal.getActiveSelectionList(selectList)
            node = OpenMaya.MObject()
            depFn = OpenMaya.MFnDependencyNode()
            path = OpenMaya.MDagPath()
            iterator = OpenMaya.MItSelectionList(selectList)

            animationTime = OpenMayaAnim.MAnimControl()

            maxTime = int(animationTime.maxTime().value())
            minTime = int(animationTime.minTime().value())

            while (iterator.isDone() == 0):

                iterator.getDependNode(node)

                depFn.setObject(node)

                iterator.getDagPath(path, node)

                cameraObject = OpenMaya.MFnCamera(path)

                transform = OpenMaya.MFnTransform(path)

                chanMe = fileExporter(transform, minTime, maxTime, cameraObject)

                for all in chanMe():
                    fileHandle.write(all)

                iterator.next()

            fileHandle.close()

        except:
            sys.stderr.write( "Failed to write file information\n")
            raise

    def processLine( self, lineStr ):

        self.importTheChan.writeFrameData(lineStr)


class fileExporter():
    """ module for exporting chan files from application. arguments: object, startFrame, endFrame """

    def __init__(self, transform, startAnimation, endAnimation, cameraObj):
        self.fileExport = []
        self.transform = transform
        self.cameraObj = cameraObj
        self.start = startAnimation
        self.end = endAnimation
        self.exportWin()

    def exportWin(self):
        self.expWindow = cmds.window(w=150, h=100, title = "Export Selection" )
        cmds.columnLayout( adjustableColumn=True )
        form = cmds.formLayout(numberOfDivisions=100)
        cmds.radioCollection()
        self.chk1 = cmds.radioButton( label='option1', onc = self.opt1On, ofc = self.opt1Off )
        self.chk2 = cmds.radioButton( label='option2', onc = self.opt2On, ofc = self.opt2Off )
        self.okayBtn = cmds.button(label='okay!', command=self.runSel, width=150, height=35)

        cmds.formLayout(form, edit=True, attachForm=[\
        (self.chk1, 'top', 15),\
        (self.chk1, 'left', 15),\
        (self.chk2, 'top', 30),\
        (self.chk2, 'left', 15),\
        (self.okayBtn, 'top', 50),\
        (self.okayBtn, 'left', 15)])

        cmds.showWindow( self.expWindow )

    def opt1On(self,  args):
        print "User checked option1"

        startAnimation = cmds.playbackOptions(query=True, minTime=True)
        endAnimation = cmds.playbackOptions(query=True, maxTime=True)

        self.start = startAnimation
        self.end = endAnimation

    def opt1Off(self,  args):
        print "User un-checked option1"
        cmds.radioButton(self.chk2, edit = True, enable = True)
        self.start = ""
        self.end = ""

    def opt2On(self,  args):
        print "User checked option2"
        startAnimation = cmds.findKeyframe(which='first')
        endAnimation = cmds.findKeyframe(which='last')

        self.start = startAnimation
        self.end = endAnimation

        #self.start.append(int(startAnimation))
        #self.end.append(int(endAnimation))

    def opt2Off(self,  args):
        print "User un-checked option2"
        self.start = ""
        self.end = ""

    def runSel(self,  args):
        chkVal1 = cmds.radioButton(self.chk1, query=True, sl=1)
        chkVal2 = cmds.radioButton(self.chk2, query=True, sl=1)

        if chkVal1 == 1:
            print "opt1 Pressed!"      
            print self.start
            print self.end
            self.test()
            self.closeWindow()

        elif chkVal2 == 1:
            print "opt2 Pressed!"
            print self.start
            print self.end
            self.test()
            self.closeWindow()

        else:
            cmds.warning("Check an option")

    def closeWindow(self):
        cmds.deleteUI(self.expWindow, window=True)

    def test(self):
        self.actualExp(self.transform, self.start, self.end, self.cameraObj)


    def actualExp(self, transform, startAnimation, endAnimation, cameraObj):
        mayaGlobal = OpenMaya.MGlobal()
        mayaGlobal.viewFrame(OpenMaya.MTime(1))

        # Converts the float arguement into integer
        for i in range(int(startAnimation), int(endAnimation + 1)):

            focalLength = cameraObj.focalLength()

            vFilmApp = cameraObj.verticalFilmAperture()

            focalOut = 2  math.degrees(math.atan(vFilmApp   25.4/ (2  focalLength)))

            myEuler = OpenMaya.MEulerRotation()
            spc = OpenMaya.MSpace.kWorld

            trans = transform.getTranslation(spc)

            rotation = transform.getRotation(myEuler)
            rotVector = OpenMaya.MVector(myEuler.asVector())

            self.fileExport.append((str(i) + '\t' + str(trans[0]) + "\t" + str(trans[1]) + "\t" + str(trans[2]) + "\t" + str(math.degrees(rotVector[0])) + "\t" + str(math.degrees(rotVector[1])) + "\t" + str(math.degrees(rotVector[2])) + "\t" + str(focalOut) + "\n"))

            mayaGlobal.viewFrame(OpenMaya.MTime(i+1))

    def __call__(self,  args):
        return self.fileExport

    def radianToDegree(self, radians):
        outDegrees = 0.0
        outDegrees = (float(radians) / (math.pi)) 180
        return outDegrees


# creator
def translatorCreator():
    return OpenMayaMPx.asMPxPtr( CustomNodeTranslator() )

# initialize the script plug-in
def initializePlugin(mobject):
    mplugin = OpenMayaMPx.MFnPlugin(mobject)

    try:
        mplugin.registerFileTranslator(kPluginTranslatorTypeName, None, translatorCreator)
    except:
        sys.stderr.write( "Failed to register translator: %s" % kPluginTranslatorTypeName )
        raise

# uninitialize the script plug-in
def uninitializePlugin(mobject):
    mplugin = OpenMayaMPx.MFnPlugin(mobject)
    try:
        mplugin.deregisterFileTranslator( kPluginTranslatorTypeName )
    except:
        sys.stderr.write( "Failed to deregister translator: %s" % kPluginTranslatorTypeName )
        raise

2 个答案:

答案 0 :(得分:1)

__call__方法应该提供文件的内容。它返回self.fileExport,这是一个未填充的空列表。

答案 1 :(得分:0)

这里的问题是插件的编写器方法不会等待你的exportWin UI在你调用时返回用户输入

chanMe = fileExporter(transform, minTime, maxTime, cameraObject)

当用户输入输入时,后面的语句已经执行:

for all in chanMe():
    fileHandle.write(all)

    iterator.next()

    fileHandle.close()

这就是为什么像这样的基于插件的文件导出器将其选项UI隐藏在选项框中。这些选项将在调用插件的writer()之前传递。

您需要在另一个脚本文件中使用MEL导出选项UI代码(以某种特定格式),并在registerFileTranslator调用的optionsScriptName参数中指定该文件的名称。此选项UI与编写器插件本身之间需要遵循通信协议进行通信。 RobTheBloke's awesome post说明了这一过程。

理想情况下,writer()方法应该具有计算和导出所需的所有细节,而无需等待用户输入。

或者,如果您希望拥有自己的UI窗口并更多地控制事物流,则可以将导出器编写为插件,而不是简单的MEL / Python模块。您仍然可以使用API​​的强大功能。

希望这有帮助!