Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用另一个类中的另一个方法调用一个方法(用类中的方法调用B,用类中的方法调用a)_Python_Python 2.7_Maya - Fatal编程技术网

Python 用另一个类中的另一个方法调用一个方法(用类中的方法调用B,用类中的方法调用a)

Python 用另一个类中的另一个方法调用一个方法(用类中的方法调用B,用类中的方法调用a),python,python-2.7,maya,Python,Python 2.7,Maya,我是Maya用户,目前正在编写自动装备。 我为该工具的每个主要任务创建了不同的类。(例如:UI类、手臂类、钻机类等) 我的问题是,我不能用我的“Class_UI”从“Class_Joints”(将生成所有需要的关节的类)调用方法 代码如下: 首先是课堂 import sys sys.path.append('G:\\3D2\\Script\\Auto_Rig') import Class_Joints import Class_Arms import maya.cmds as mc cla

我是Maya用户,目前正在编写自动装备。 我为该工具的每个主要任务创建了不同的类。(例如:UI类、手臂类、钻机类等)

我的问题是,我不能用我的“Class_UI”从“Class_Joints”(将生成所有需要的关节的类)调用方法

代码如下:

首先是课堂

import sys
sys.path.append('G:\\3D2\\Script\\Auto_Rig')

import Class_Joints
import Class_Arms

import maya.cmds as mc

class Window_UI(object):

# Initializing global variables
def __init__(self):
    
    # Getting acces to the different modules
    self.Arms = Class_Arms.Arms_Rig()
    self.Joints = Class_Joints.Gen_Joints()

    # Create Ui
    self.create_UI()

# Creating the UI
def create_UI(self):
    # Create window
    self.UI = mc.window(title='Auto-Rig Tool', w=(300), h=(350))

    # Main layout
    self.mainLayout = mc.menuBarLayout()

    ### Joints Option ###
    # Create Joints Button
    self.createJointsButton = mc.button(label='Create Joints', command=self.Joints.gen_arms_joints)
    
Window_UI()
mc.showWindow()
然后,类_接头:

import maya.cmds as mc

class Gen_Joints:

# Creating arm Jnts and the list of it
def gen_arms_joints(self):
    
    self.shoulderJnt = mc.joint(absolute=True, position=[5,8,0], n='L_Shoulder_Jnt')
    self.elbowJnt = mc.joint(absolute=True, position=[10,8,-1.5], n='L_Elbow_Jnt')
    self.wristJnt = mc.joint(absolute=True, position=[15,8,0], n='L_Wrist_Jnt')
    self.handcupJnt = mc.joint(absolute=True, position=[18,8,0], n='L_HandCup_Jnt')

    self.jntList = mc.ls(self.shoulderJnt, self.elbowJnt, self.wristJnt, self.handcupJnt)
当我运行
Class\u UI
code时,UI中的按钮应该运行
Class\u关节中的
gen\u arms\u joints
方法

但是我得到了这个错误消息:
#错误:gen_arms_joints()正好接受1个参数(给定2个)#

我知道self在这里是一个隐含的论点,但我不知道如何避免这个错误

谢谢大家抽出时间。 :D


诚恳地说,Luca。

问题在于,在
类UI
初始化调用中,您为实际函数
gen\u arm\u joints(self)
定义了错误的类调用,它应该是:
self.joints=gen\u joints()
,似乎您有不同的
导入类名
,但在代码中您将该类称为
Gen\u Joints
。您不能传入两个
self
类引用,即错误回溯


您必须将
import
类修改为
import Gen_joint

我建议您做两件事。我不使用Maya,但我已经用多个不同的GUI构建了应用程序

  • 当涉及到按钮时,我使用的每个GUI都是第一个参数是对self的引用,然后通常会传入一个或两个以上的参数。一些传递对按钮本身的引用,而另一些传递保存事件详细信息的参数。我猜这就是正在发生的事情。当您单击按钮时,它将传入一个“事件”对象,该对象包含有关单击内容和其他详细信息的详细信息

  • 要真正了解传递的内容,请将函数签名更改为此,并查看记录的内容

  • @钓鱼代码

    根据你告诉我的,看看我尝试了什么:

    import sys
    sys.path.append('G:\\3D2\\Script\\Auto_Rig')
    
    import Class_Joints
    import Class_Arms
    
    import maya.cmds as mc
    
    class Window_UI(object):
    
        # Initializing global variables
        def __init__(self):
        
            # Getting acces to the different modules
            self.Arms = Class_Arms.Arms_Rig()
            self.Joints = Gen_Joints()
    
            # Create Ui
            self.create_UI()
    
        # Creating the UI
        def create_UI(self):
            # Create window
            self.UI = mc.window(title='Auto-Rig Tool', w=(300), h=(350))
    
            # Main layout
            self.mainLayout = mc.menuBarLayout()
    
            # Create Joints Button
            self.createJointsButton = mc.button(label='Create Joints', command=self.Joints.gen_arms_joints)
    
    #show window
    Window_UI()
    mc.showWindow()
    
    这正是我在VisualStudio代码中看到的

        import maya.cmds as mc
    
    class Gen_Joints:
    
        # Creating arm Jnts and the list of it
        def gen_arms_joints(self):
            
            self.shoulderJnt = mc.joint(absolute=True, position=[5,8,0], n='L_Shoulder_Jnt')
            self.elbowJnt = mc.joint(absolute=True, position=[10,8,-1.5], n='L_Elbow_Jnt')
            self.wristJnt = mc.joint(absolute=True, position=[15,8,0], n='L_Wrist_Jnt')
            self.handcupJnt = mc.joint(absolute=True, position=[18,8,0], n='L_HandCup_Jnt')
    
            self.jntList = mc.ls(self.shoulderJnt, self.elbowJnt, self.wristJnt, self.handcupJnt) 
    
    VS代码告诉我未定义变量“Gen\u Joints”
    在maya中,我得到了
    #错误:name错误:文件第16行:全局名称“Gen_Joints”未定义#
    mc。按钮
    将一个额外的
    布尔值
    传递给方法,因此,由于在
    Gen_arms_Joints
    中未定义该值,因此它会抛出该错误,并表示参数数目错误

    因此,只需添加一个额外的参数,它就会修复它:
    def gen\u arms\u关节(自身、额外参数):

    或者更好地使用
    *args
    来更通用一些:
    def发电机臂关节(自身,*args):


    老实说,这有点鬼鬼祟祟,因为文档中没有真正说明,但您可以看到它在中使用。

    对不起,我不明白。我将解释我想用这些“导入东西”做什么,首先我导入名为“Class_Joints”的模块,然后在uu init_uuuuuu()中,我关联>self.Joints=Class_Joints.Gen_Joints(),它们对应于self.Joints=ModuleName.ClassName(),我从您的回答中了解到我需要导入模块“Class_Joints”然后在uuu init_uuuuuuuu()部分中,编写self.joints=Gen_joints(),但这无法完成,Gen_joints类不会找到,对吗?这是我尝试过的,但没有成功编辑应该能够找到
    self.Joints=Gen_Joints()
    。我不确定代码是否缺少了更多的组件来解决这个问题,但似乎应该这样做。函数调用中的args调用:
    self.createJointsButton=(..,command=self.Joints.gen\u arms\u Joints)
    。我试图添加这个“神秘的第二个arg”,但与之前没有什么不同,我仍然收到相同的错误消息
    。\35error:gen\u arms\u Joints()只接受1个参数(给定2个)#
    这正是
    mc.button
    所做的,以及它导致错误的原因。既然您要导入模块,请不要忘记先重新加载它,以便它运行更新的代码。您为什么要导入类关节?我这样做是为了访问此模块中的方法
    gen\u arms\u joints
    。Class_Joints是包含
    Gen_Joints
    类的模块,包含
    Gen_arms_Joints
    方法,我需要在第二个模块Class_UI中按下“创建关节”按钮时调用该方法。明白了。该模块在任何地方都不可见,因此我发表了评论。坚持下去,你可能会接近。
        import maya.cmds as mc
    
    class Gen_Joints:
    
        # Creating arm Jnts and the list of it
        def gen_arms_joints(self):
            
            self.shoulderJnt = mc.joint(absolute=True, position=[5,8,0], n='L_Shoulder_Jnt')
            self.elbowJnt = mc.joint(absolute=True, position=[10,8,-1.5], n='L_Elbow_Jnt')
            self.wristJnt = mc.joint(absolute=True, position=[15,8,0], n='L_Wrist_Jnt')
            self.handcupJnt = mc.joint(absolute=True, position=[18,8,0], n='L_HandCup_Jnt')
    
            self.jntList = mc.ls(self.shoulderJnt, self.elbowJnt, self.wristJnt, self.handcupJnt)