How to make Python Wait for input?

I am using python in Maya, a 3D animation package. I would loved to run a definition (A) but within that definition I want another definiton (B) that requires a valid object selection. The script will kept going until one is made (in def B) and I want to continue with my script (def A) with a returned value from def B. How can I tell def A to wait until a valid returned value is retreived from def B?

So short question: How can I make python wait for a valid returned value to be received?

I hope that make sense and thank you in advance for your time.

C

example:

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

I need some thing in ### line for the function to wait until a desired return is received, then continue on with the rest. At the moment the function just run everything regardless.

Thank you


If the scope of commandB() is limited to commandA() you may consider using closures (what is a closure?)

or simply nested python functions (http://effbot.org/zone/closure.htm , http://www.devshed.com/c/a/Python/Nested-Functions-in-Python/)

in any part of your code considering "result = commandB()" statement,

the interpretter should wait until something is returned from commandB() and assigned to result, before proceeding to the next line to be executed.


You could just use a while loop:

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

I noticed that

selection in your code isn't actually being given a value (at least not in the code you've given us), is it not supposed to be:

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


Also, is there a reason why you're calling commandB recursively? This could end up using unnecessary resources, especially if someone repeatedly makes the wrong selection. How about this?

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 
链接地址: http://www.djcxy.com/p/4888.html

上一篇: python当函数被调用时会发生什么?

下一篇: 如何让Python等待输入?