Jump to content

Python: Deleted Node Check


galagast

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):
Link to comment
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
Link to comment
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.

Link to comment
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?

Link to comment
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
Link to comment
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
Link to comment
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.

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...