Maya Python Script works from console, doesnt work from file

喜夏-厌秋 提交于 2021-01-28 17:54:32

问题


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

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. #

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.

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

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


回答1:


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



回答2:


Lambda should work but if you want something more clear, use partial :

import maya.cmds as cmds
from functools import partial

#Function
# Don't forgot the *args because maya is putting one default last arg in -c flags
def printTxtField(fieldID, *args):
    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()
# syntax :: partial(function, argument1, argument2, ...etc)
cmds.button(label='Click me', command=partial(printTxtField, whatUSay))
cmds.showWindow()


来源:https://stackoverflow.com/questions/48570602/maya-python-script-works-from-console-doesnt-work-from-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!