2014-09-02 28 views
0

编辑:我设法解决了我以前遇到的问题,但不是我创建另一个新问题,我遇到的这个问题与我猜的几乎相似?如何从另一个函数中选择导入对象名称

由于我修改了一些这个脚本,我目前doingn的内容,它会启动这个UI,每当用户进口的.chan对象

目前,我试图编辑摄像机名称,这样当用户选择相机,它会将导入相机的名称继承到其名称空间。 虽然我不是很确定,但我认为customNodeTranslator类中的reader函数是读取导入相机的函数吗?

这是错误messgae:

# Error: global name 'cameraName' is not defined 
# Traceback (most recent call last): 
# File "/user_data/scripts/test/maya/plugins/chan.py", line 210, in readFileIn 
#  self.importTheChan = ChanFileImporter(chanRotationOrder) 
# File "/user_data/scripts/test/maya/plugins/chan.py", line 286, in __init__ 
#  self.cameraName = cameraName 
# NameError: global name 'cameraName' is not defined # 

这是原来的代码:

class customNodeTranslator(OpenMayaMPx.MPxFileTranslator): 
    ...  
    ... 
    ... 

    def reader(self, fileObject, optionString, accessMode): 
     self.initialWindow() 

     try: 
      fullName = fileObject.fullName() 
      print ">>> full Name is %s" %fullName 

      #self.fileHandle = open(fullName,"r") 
      camHandle = open(fullName,"r") 
      camPath = os.path.basename(camHandle.name) 
      camName = os.path.splitext(camPath)[0] 
      print ">>> This is the name: %s" % camName 

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

     return camName 

class chanImport(): 
    """ importing chan camera from nuke """ 
    def __init__(self, rotation): 
     self.rotation = rotationOrder 
     # create the camera 
     self.cameraName = cmds.camera(n=str(camName)) 
     self.cameraShape = self.cameraName[1] 

     cmds.select(self.cameraShape) 
     cmds.scale(0.5, 0.5, 0.5) 

下面的代码是实际的代码本身我修改前:

class customNodeTranslator(OpenMayaMPx.MPxFileTranslator): 
    ...  
    ... 
    ... 
    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() 
    class ChanFileImporter(): 
     def __init__(self, rotationOrder): 
      self.rotationOrder = rotationOrder 
      # create the camera 
      self.cameraName = cmds.camera() 
      self.cameraShape = self.cameraName[1] 
      cmds.select(self.cameraShape) 
      cmds.scale(0.5, 0.5, 0.5) 
+0

你能分享充分和准确的错误信息你越来越?您可能需要取出异常处理,以确保不会掩盖该错误。 – mhlester 2014-09-02 14:08:49

+0

@mhlester对不起,回复迟了。我刚刚在错误消息中添加了如下内容。看起来全局变量可能不是一个好主意。有没有什么方法可以尝试修改它以获取相机的名称? – dissidia 2014-09-16 09:12:38

回答

0

你没有将camName传递给导入器类。事实上,在上面的例子中你根本没有调用进口商类。

如果修改chanImport所以它需要的名字,你想:

class chanImport(object): 
    """ importing chan camera from nuke """ 
    def __init__(self, camName): 
     self.desiredCameraName = camName 
     self.cameraName = None 
     self.cameraShape = None 

    def create_camera(self): 
     self.cameraName, self.cameraShape = cmds.camera(n=str(camName)) 
     cmds.select(self.cameraShape) 
     cmds.scale(0.5, 0.5, 0.5) 
     return self.cameraName 

你应该能够调用它你的读者函数中:

def reader(self, fileObject, optionString, accessMode): 
    self.initialWindow() 

    try: 
     fullName = fileObject.fullName() 
     print ">>> full Name is %s" %fullName 
     camHandle = open(fullName,"r") 
     camPath = os.path.basename(camHandle.name) 
     camName = os.path.splitext(camPath)[0] 
     print ">>> This is the name: %s" % camName 
     importer = chanImport(camName) 
     actual_cam_name = importer.create_camera() 
     print ">>> created " + actual_cam_name 
     return actual_cam_name 
    except: 
     sys.stderr.write("Failed to read file information\n") 
     raise 
+0

我尝试使用你的代码,并且当我做我的导入时,我得到以下错误:'TypeError:__init __()应该返回None,而不是'unicode'在行 - 'importer = ChanFileImporter(camName)'中。但即便如此,因为我仍然需要'rotationOrder'的争论。尝试添加到__init__ functiion,我得到错误'#TypeError:__init __()需要正好3个参数(2给出)//' – dissidia 2014-09-05 02:31:23

+0

我其实有2个问题。首先,请原谅我,但我没有看到读者功能与ChanImport之间的“联系”。 'cameraName'在'reader'中返回,但是我想我会在ChanImport类中看到相同的变量,不是吗?其次,如果我将'__init__'函数设置为保持原样(在rotationOrder中读取),而createCamera类似于'def create_camera(self,camName):',它会是一个好主意吗?我问道,因为我意识到还有另外一条线正在寻找旋转值 – dissidia 2014-09-05 03:20:15

+0

您可以将旋转参数添加到chanImport的__init__并存储它,就像我对camName所做的那样。然后,您可以将其应用于create_camera(); '完全采用....'意味着你将一个参数传递给定义中不存在的函数。 您可以通过不从chanImport创建一个类来简化此操作:因为只有通过直接将功能折叠到阅读器中才能使您失去功能,它才会被使用。 – theodox 2014-09-05 03:38:11

相关问题