Jump to content
galagast

Python: Deleted Node Check

Recommended Posts

On the shell:

1. I stored a hou.node("/obj/geo1") onto a variable obj.

2. Then I deleted the geo1 object in the scene.

3. I check the obj variable, it now returns an error as expected:

>>> obj
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:/PROGRA~1/SIDEEF~1/HOUDIN~1.393/houdini/python2.7libs\hou.py", line 8015, in __repr__
    def __repr__(*args): return _hou.ObjNode___repr__(*args)
ObjectWasDeleted: Attempt to access an object that no longer exists in Houdini.

My question now is, how do I check if a node was deleted? Or if it is still valid?

I'm looking for a command similar to MaxScript's isValidNode <var>: Returns true if <var> is a node value, and the node has not been deleted. Otherwise, it returns false.
 

I'm planning to use it on an if statement like so:

if isValidNode(obj):

Share this post


Link to post
Share on other sites

Holding a reference to a node, beyond it's current scope, is not wise. Simply fetch the expected node at the top of the code. If it exists, you will have a valid node to operate upon.

There are some status functions associated with nodes, however, use print dir(obj) to review them. But if the node has been deleted those functions will not be available.

 

Otherwise create your own test with a try/except to determine if the node is still valid.

Edited by Atom

Share this post


Link to post
Share on other sites

Thank you for the response Atom. I'll use print dir(obj) to study more of the associated functions of the node. :)

 

Yes, my current workaround for now was I created a separate function with a try/except inside it to determine a node's validity.

Share this post


Link to post
Share on other sites

In relation to the question, is there a way to check if a provided path is valid?

 

Say I have an input string of:

foo = "/geo/box1"

I then would later use that for:

obj = hou.node(foo)

But before I do, is there a way to check if the foo string path is a valid path on the current session?

Share this post


Link to post
Share on other sites
import os.path
obj = ""
nodename = os.path.basename(foo)
kids = map( lambda x: x.name(), node.children() )
if nodename in kids:
   obj = hou.node(foo)

could this suffice?

im sure you can use filter() straight after map()

 

or you can just do:

raise hou.Error("no such node in object") or raise ValueError("no such node in object") somewhere in the code

 

edit: i noticed its a path

Edited by willow wafflebeard

Share this post


Link to post
Share on other sites

...before I do, is there a way to check if the foo string path is a valid path on the current session?

hou.node() will return None if the path is invalid

so you can straight away just do:

path = "/any/path"
node = hou.node(path)
if node:
    print "node %s exists and it's a %s" % (node.path(), node.type().name())
else:
    print "node %s doesn't exist" % path
Edited by anim

Share this post


Link to post
Share on other sites

Thanks guys!

@Willow: Those are new to me, I'm reading up on lambda, map and filter now. I will definitely consider its usage during my forays in python.

@Tomas, awesome, I did end up doing that after a couple of tests last night ^_^ Glad I was on the right path.

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

×