【问题标题】:code works in script editor, but not when run from command line [closed]代码在脚本编辑器中有效,但从命令行运行时无效[关闭]
【发布时间】:2018-05-29 04:42:40
【问题描述】:

我正在尝试将表达式结果通过管道传输到 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")

【问题讨论】:

  • 你在命令行使用什么命令?预期的行为是什么?实际行为是什么?你有什么错误吗?
  • 当您尝试从命令行运行它时会发生什么?发布您正在运行的命令,以及 cli 为您返回的内容。
  • 像这样从 python 命令行运行它: textToolHUD() 结果是: textToolHUD() # Error: TypeError: file line 1: 'module' object is not callable #
  • 我已经把它作为 textToolHUD.py 保存在我的脚本目录中

标签: python maya autodesk


【解决方案1】:

从您是 cmets 中的错误来看,您似乎正在尝试将模块作为函数运行。

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

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

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

【讨论】:

  • 谢谢!我正在使用您的第一个建议,它正在发挥作用。
  • 你应该标记为接受未来的读者:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-14
  • 2023-03-31
  • 2021-11-13
  • 2017-03-26
  • 2013-02-07
  • 2016-08-06
相关资源
最近更新 更多