Maya Python 脚本从控制台工作,不能从文件工作 [英] Maya Python Script works from console, doesnt work from file

查看:105
本文介绍了Maya Python 脚本从控制台工作,不能从文件工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import maya.cmds as cmds

#Function
def printTxtField(fieldID):
    print cmds.textField(fieldID, query=True, text=True)

#define ID string for Window
winID = 'myWindow'

if cmds.window(winID, exists=True):
    cmds.deleteUI(winID)

cmds.window(winID)
cmds.columnLayout()
whatUSay = cmds.textField()
cmds.button(label='Click me', command='printTxtField(whatUSay)')
cmds.showWindow()

当我从 Maya 中的控制台执行它时,这工作正常.但是当我将其另存为文件并执行时,我收到此错误消息:

this works fine when I execute it from the console in maya. But when I save it as a file and execute I get this error message:

# Error: Object 'myWindow|columnLayout30|textField29' not found.
# Traceback (most recent call last):
#   File "<maya console>", line 1, in <module>
#   File "<maya console>", line 5, in printTxtField
# RuntimeError: Object 'myWindow|columnLayout30|textField29' not found. #

我注意到 columnLayout30|textField29 中的数字增加,每次我从控制台执行脚本,然后再次从文件执行,我觉得很困惑,因为旧窗口每次都被删除.

I noticed that the numbers in columnLayout30|textField29 rise, each time I execute the script from the console, and then from the file again, which I find confusing, because the old window gets deleted each time.

我发现了这个问题:超级函数在 Maya python 模块中不起作用
但我不确定这是否是同一个问题.
我使用的是 IDLE 3.6.4 和 Maya 2016 SP6

I have found this question: super function doesn't work inside a maya python module
but I am not sure if it is the same issue.
I am using IDLE 3.6.4 and Maya 2016 SP6

(我是一个孤独的艺术家,试图进入脚本.(python/maya 和 C#/unity)我想说的是,学习编码部分真的没有那么难.有时可能很乏味,但是你几乎可以通过谷歌搜索任何东西.但它的设置"部分几乎总是让我失望.安装 IDE 和库以及连接"的东西.在这里我总是遇到错误.所以任何关于我的一般帮助此处遗漏,将不胜感激)

(I am a lonely artist, trying to get into scripting. (python/maya and C#/unity) What I want to say is, that learning the coding part really isnt that hard. It can be tedious at times, but you can google your way through almost anything. But its the "setting up" part that almost always throws me. Installing IDEs and libraries and "connecting" things. Here I am getting errors all the time. So any general help on what I am missing here, would be very much appreciated)

推荐答案

我不喜欢使用基于文本的命令.相反,您可以分配回调.尝试的一种选择是使用 lambda(因为您想传递自己的参数):

I prefer not to use text-based commands. Instead, you can assign callbacks. One option to try is using lambda (since you want to pass your own argument):

cmds.button(label='Click me', command=lambda x:printTxtField(whatUSay))

这篇关于Maya Python 脚本从控制台工作,不能从文件工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆