Python 代码在脚本编辑器中工作,但在从命令行运行时不工作

Python 代码在脚本编辑器中工作,但在从命令行运行时不工作,python,maya,autodesk,Python,Maya,Autodesk,我正在尝试将表达式结果通过管道传输到maya文本对象中,以用作平视显示。我的脚本在脚本编辑器中工作,但在从命令行或显示表达式调用时不能工作。如何通过命令行更改它以使其工作 import maya.cmds as cmds # convert a string to hex values, separated by a space typeNode = cmds.ls( selection=True ) type3d = cmds.listConnections(t='type') typeV

我正在尝试将表达式结果通过管道传输到maya文本对象中,以用作平视显示。我的脚本在脚本编辑器中工作,但在从命令行或显示表达式调用时不能工作。如何通过命令行更改它以使其工作

import maya.cmds as cmds

# convert a string to hex values, separated by a space

typeNode = cmds.ls( selection=True )
type3d = cmds.listConnections(t='type')
typeValue = cmds.getAttr( 'tower_CON.Spin' )

valueList=list(str('{:3.2f}'.format(typeValue)))

hexVersion=""

for x in valueList:
    hexValue=x.encode("hex")
    hexVersion=hexVersion + hexValue + " " 

cmds.setAttr(str(type3d[0]) + ".textInput", hexVersion.rstrip(), type="string")

从您的注释中的错误来看,您似乎试图将模块作为函数运行

您可能需要将其保存在文件中:

import maya.cmds as cmds

def text_to_hud():
    # convert a string to hex values, separated by a space

    typeNode = cmds.ls( selection=True )
    type3d = cmds.listConnections(t='type')
    typeValue = cmds.getAttr( 'tower_CON.Spin' )

    valueList=list(str('{:3.2f}'.format(typeValue)))

    hexVersion=""

    for x in valueList:
        hexValue=x.encode("hex")
        hexVersion=hexVersion + hexValue + " " 

    cmds.setAttr(str(type3d[0]) + ".textInput", hexVersion.rstrip(), type="string")
然后在你想要的命令行中

import textToolHUD as th; th.text_to_hud()
你也可以按原样保存文件,就这样做

import textToolHUD

它将运行一次,但依赖于在导入时运行的代码是不好的做法。

您在命令行上使用的是什么命令?预期的行为是什么?实际的行为是什么?您是否收到任何错误?当您尝试从命令行运行它时会发生什么?发布您正在运行的命令以及cli返回的内容。从python命令行运行它,如:textToolHUD()结果是:textToolHUD()#错误:TypeError:文件行1:“module”对象不可调用#我已将其作为textToolHUD.py保存在脚本目录中谢谢!我正在使用您的第一个建议,并且它正在发挥作用。您应该将其标记为已接受,以供将来的读者阅读:)