首页 文章

如何让Python等待输入?

提问于
浏览
0

我在Maya中使用python,这是一个3D动画包 . 我很乐意运行定义(A),但在该定义中我想要另一个定义(B),需要有效的对象选择 . 脚本将继续运行直到一个(在def B中)并且我想继续使用来自def B的返回值的脚本(def A) . 如何判断def A等待从有效返回的值返回到def B?

这么简短的问题:如何让python等待接收到有效的返回值?

我希望有意义,并提前感谢您的时间 .

C

例:

def commandA () :
   result = commandB()
   ### Wait for a value here ###
   if result == "OMG its a valid selection" :
      do_another_commandC()

def commandB () :
   # This command is kept running until a desired type of selection is made
   maya.mel.eval("scriptjob \"making a valid selection\" -type polygon")
   if selection == "polygon" :
      return "OMG its a valid selection"
   else :
      commandB()

我需要###行中的一些东西让函数等到收到所需的返回,然后继续其余的 . 目前,该功能无论如何都能运行 .

谢谢

2 回答

  • 0

    如果commandB()的范围仅限于commandA(),您可以考虑使用闭包(what is a closure?

    或者只是嵌套的python函数(http://effbot.org/zone/closure.htm,http://www.devshed.com/c/a/Python/Nested-Functions-in-Python/

    在代码的任何部分考虑“result = commandB()”语句,

    解释器应该等到从commandB()返回一些东西并分配给result,然后再继续执行下一行 .

  • 0

    你可以使用while循环:

    def commandA () :
        result = ""
        while (result != "OMG its a valid selection")
           # perhaps put a 0.1s sleep in here
           result = commandB()
        do_another_command()
    

    我注意到了

    selection 在你的代码中没有给我们),它不应该是:

    selection = maya.mel.eval("scriptjob \"making a valid selection\" -type polygon")


    另外,是否有理由以递归方式调用commandB?这最终可能会使用不必要的资源,特别是如果有人反复做出错误的选择 . 这个怎么样?

    def commandA () :
        result = ""
        while (result != "polygon")
           # perhaps put a 0.1s sleep in here (depending on the behavior of the maya command)
           result = commandB()
        do_another_command()
    
    def commandB () :
       # This command retrieves the selection
       selection = maya.mel.eval("scriptjob \"making a valid selection\" -type polygon")
       return selection
    

相关问题