View Full Version : python: wait until a string variable is returned

07 July 2012, 01:27 AM
Hi all, Can anyone help me to solve this problem please? I got this python script:

import maya.cmds as mc

def testA () :
result = ''
while result == '':
result = testB()
print result

def testB () :
sel =
if sel :
if sel[0] == 'pCube1' :
return sel[0]
else :
mc.scriptJob(runOnce=1,ct=['SomethingSelected', testB])
else :
mc.scriptJob(runOnce=1,ct=['SomethingSelected', testB])

What I need to do is for testA to wait until a valid string variable is returned, and then print it. testB will keep running itself until my first object selection is 'pCube1' and 'pCube1' is returned. I need something like a "wait" function in python.... or in maya term... a scriptjob in python.

the while statement block everything i can no longer make any selection in the scene. And if I put

result = testB()

It doesnt seem to wait until it received a return, it simply gives me this line :

# Result: <function testA at 0x0000000039167898> #

Anyone? Thanks in advance

07 July 2012, 07:21 AM
From testA execute a timer script job that each interval checks for testB. When testB is satisfied then continue testA. The implementation of this involves breaking testA to two functions - 'before' and 'after'.

A proper way to do that would be to put testA in a different thread, which would wait for a signal from testB (also executed in a timer script job). But maya in general hardly supports multitasking (see my experiment with NodeUpdateTests), and even less with python.

07 July 2012, 09:18 AM
why are you even doing any of this? just create a scriptJob that uses the selectionChanged event, and in the function it calls do a check to see if the selection is 'pCube1' (or whatever)

07 July 2012, 12:32 PM

testB is a gizmo that later will be used over and over again in different script. At the moment I got it to work by writing a custom attribute in time1 node which is read by testA via scriptJob - attributeChange on time1. I still have to put come codes to clean up the mess - such as deleteing the custom attribute on time1 that i created - in testB which I think is very ugly wy to do. So I figure if only I can execute testA and wait until testB feed a valid returned string then I can really make testB independent and re-usable in other scripts.

I am making a "mirror selected objects" script that trigger a gizmo in front of the camera to choose the direction axis of mirroring. This gizmo is created by a definition and I would love to be able to use this gizmo elegantly in other scripts.


CGTalk Moderation
07 July 2012, 12:32 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.