Jump to content
Milan

execute in main thread with results

Recommended Posts

Hey guys.

 

I'm trying to find equivalent to maya's and nuke's executeInMainThreadWithResult helper function.

 

The point is that I'd like to spawn a child thread using python in houdini and have it communicate back to the main thread. Ideally waiting for response. Has anyone tried that, and if so how successful were you? 

 

The best explanation of what I'll need to do is having a look at these 2 pages of nuke and maya docs, which explain it very clearly.

 

http://docs.thefoundry.co.uk/nuke/63/pythondevguide/threading.html

http://knowledge.autodesk.com/support/maya/learn-explore/caas/CloudHelp/cloudhelp/2015/ENU/Maya/files/Python-Python-and-threading-htm.html

 

I'm planning to start ingegrating pyblish (http://www.pyblish.com/) into houdini and this would make it fairly straightforward.

 

Cheers

Share this post


Link to post
Share on other sites

I think you could use this method to add callback, which will be executed in main thread.

import threading
import hou

def callback():
    print 'In callback'
    worker()
    hou.ui.removeEventLoopCallback(callback)
    print "I'm done with it"
      
def worker():
    print "I'm doing something"

class T(threading.Thread):
    def run(self):
        hou.ui.addEventLoopCallback(callback)

T().start()
Edited by Stalkerx777

Share this post


Link to post
Share on other sites

Hmm I though so too, but it seem to be running constantly (I assume on every ui update). And I'm unable to stop it. 

Share this post


Link to post
Share on other sites

I must be missing something. Why do you need to execute it in the main thread? ie. why not just spawn a python thread do do you work and then call join() on it to wait for it to finish.

Share this post


Link to post
Share on other sites

We were integrating http://www.pyblish.com into houdini. It runs outside of houdini as endpoint service. This approach was used in maya and nuke, so we were wondering if it was possible to plug it into houdini in the same fashion to keep the same structure of the integration.

 

I was trying to just use join(), however it kept on freezing my houdini (which most likely means I was doing something very wrong :) )

 

Anyways we got it working thanks to SESI support and pyblish for houdini will be out and usable as soon as the code get's a tiny bit of cleanup and when it get's integrated into the main package.

 

Turns out houdini has exactly what we were looking for 

import hdefereval

hdefereval.executeInMainThreadWithResult(houdini_command)

The way we got it working is this. 

import hou
import hdefereval
import threading

def houdini_command():
    hou.node('/obj').createNode('geo')

def worker():
    n = 0
    while n < 5:
        hdefereval.executeInMainThreadWithResult(houdini_command)
        n += 1

thread = threading.Thread(target=worker)
thread.daemon = True

thread.start()

and this was a try with join() which I gave up on.

import hou
import hdefereval
import threading

def worker():
    n = 0
    while n < 10:
        houdini_command()
        n += 1

thread = threading.Thread(target=worker)
thread.daemon = True

thread.start()
thread.join()

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×