Jump to content

markinglevfx

Recommended Posts

Hey Guys,

Still quite new to Python, hoping someone can help point me in the right direction on this that I've been grappling with for a while now.

I have a simple filecache .hda that I'd like to change color if it can/can't find the cache on disk. I've been able to use hou.ui.addEventLoopCallback() to continuously track and check the file parameter to see if the cache exists, and the color changing now works as expected. 

Where I'm having trouble is removing the event loop when the .hda is deleted from the node graph. An error pops up because Houdini is still trying to access the deleted node. I've been trying to look for a way to use the OnDeleted event handler, or hou.nodeEventType.BeingDeleted, but no luck yet and I feel out of my depth with my limited python experience.

The .hda is attached, and there's a preview of the code below. Any help would be appreciated!

custom_filecache.hda

####PythonModule####
import os
import hou
    
def nodeColor(kwargs):
    node = kwargs["node"]
    def colorCallback():
        filepath = node.evalParm("file")
        if(os.path.exists(filepath)):
            node.setColor(hou.Color((0.0, 0.8, 0.0)))
            node.setComment(filepath)
            node.setGenericFlag(hou.nodeFlag.DisplayComment,True)
        else:
            node.setColor(hou.Color((0.8, 0.0, 0.0)))
            node.setComment("File not found")
            node.setGenericFlag(hou.nodeFlag.DisplayComment,True)
    startCallback(colorCallback)
    #adding this code below crashes Houdini when hda is deleted
    #node.addEventCallback([hou.nodeEventType.BeingDeleted], endCallback(colorCallback))
    
def startCallback(callback):
    hou.ui.addEventLoopCallback(callback)

def endCallback(callback):
    hou.ui.removeEventLoopCallback(callback)


####OnCreated####
kwargs["node"].hdaModule().nodeColor(kwargs)

 

Edited by markinglevfx
Link to comment
Share on other sites

Have you tried removing the callback in OnDeleted event handler instead of registering it in the nodeColor function?

On a side note, are you expecting your caches to pop up and go like every N milliseconds? :) What you're trying to do looks redundant to me, and if you have a scene with a few dozens instances of you HDA , you'll likely experience a slowdown, because global event loop runs in the main Houdini thread. 

You could install your event on the node itself ( node.addEventCallback ) and see what nodeEventType works for you. Or install it on the parent container (say your hda is a SOP, install the event on your_node.parent()) so each time you dive into a geo container, it'll trigger the event. Anyway, there's a lot of options :) 

Edited by Stalkerx777
  • Thanks 1
Link to comment
Share on other sites

Thanks Alex. I needed to look at node.addEventCallback further. I was able to have the node color update after any parameter changes, node name changes, or scene frame number changes.

####OnCreated####

import os
import hou

def nodeColor(node):
    filepath = node.evalParm("file")
    if(os.path.exists(filepath)):
        node.setColor(hou.Color((0.0, 0.8, 0.0)))
        node.setComment(filepath)
        node.setGenericFlag(hou.nodeFlag.DisplayComment,True)
    else:
        node.setColor(hou.Color((0.8, 0.0, 0.0)))
        node.setComment("File not found")
        node.setGenericFlag(hou.nodeFlag.DisplayComment,True)

def parmChanged(node, event_type, parm_tuple):
    nodeColor(node)
    
def nameChanged(node, event_type):
    nodeColor(node)
    
def frameChanged(event_type, frame):
    hda = kwargs['node']
    nodeColor(hda)

hda = kwargs['node']
nodeColor(hda)

hda.addEventCallback( (hou.nodeEventType.ParmTupleChanged, ), parmChanged)
hda.addEventCallback( (hou.nodeEventType.NameChanged, ), nameChanged)
hou.playbar.addEventCallback(frameChanged)

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...