Search code examples
pythonmayaautodesk

code works in script editor, but not when run from command line


I'm trying to pipe an expression result into a maya text object for use as a heads up display. My script works from the script editor, but not when called from the command line or from my display expression. How can I change this to make it work from the command line?

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

Solution

  • From the error in you're comments, it looks like you're trying to run the module as a function.

    You probably need to save this in the file:

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

    and then in the command line you'd want

    import textToolHUD as th; th.text_to_hud()
    

    You could also keep the file as is and just do

    import textToolHUD
    

    which would run once, but it's bad practice to rely on code that runs on import.