用另一个类中的另一个方法调用一个方法(在class_B中调用method_B,在class_a中调用method _a)



我是Maya用户,目前正在编写"自动装配"。我为工具的每个主要任务创建了不同的类。(例如:Class_UI、Class_Arms_Rig等。(

我遇到的问题是,我不能从";Class_Joints;(将生成每一个所需关节的类(;Class_UI";

以下是代码:

首先是Class_UI

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()

然后Class_Joints:

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_UI代码时,UI中的按钮应该在Class_Joints中运行gen_arms_joints方法

但我收到了这个错误消息:# Error: gen_arms_joints() takes exactly 1 argument (2 given) #

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

谢谢大家抽出时间。:D

亲爱的,卢卡。

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

  1. 当涉及到按钮时,我使用的每个GUI都是第一个参数是对self的引用,然后通常会再传入1到2个参数。有些将引用传递给按钮本身,而另一些则传递包含事件详细信息的参数。我猜这就是正在发生的事情。当你点击按钮时;事件";对象,其中包含有关所单击内容的详细信息和其他详细信息。

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

def gen_arms_joints(self, mystery_second_arg):
print(type(mystery_second_arg), mystery_second_arg)
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_UI__init__()调用中,您为实际函数gen_arm_joints(self)定义了错误的类调用,应该是:self.Joints = Gen_Joints(),看起来您有不同的import classNames,但在代码中,您将该类调用为Gen_Joints。不能传入两个self类引用,即错误回溯。

您必须将import类修复为import Gen_Joints

@FishingCode

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

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()

这正是我在Visual Studio Code 上看到的

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代码告诉我Undefined variable 'Gen_Joints'在玛雅我得到# Error: NameError: file <maya console> line 16: global name 'Gen_Joints' is not defined #

mc.button将一个额外的boolean值传递给该方法,因此由于gen_arms_joints中没有定义该值,它会抛出该错误,并表示参数数量错误。

所以,只需添加一个额外的参数,它就会修复它:def gen_arms_joints(self, extra_param):

或者最好使用*args,使其更通用:def gen_arms_joints(self, *args):

老实说,这有点狡猾,因为文档中并没有真正说明,但你可以在页面底部的示例中看到它的使用情况。

最新更新