Jump to content

using hou.Node.destroy()


JJM

Recommended Posts

Hi!

How to use Node.destroy() method?

If we define something like this:

def delSearch(patt):
    for n in hou.node('/obj/').allSubChildren():
        if n.name().startswith(patt):
            print (n.path())
            n.destroy()

Then call it:

hou.session.delSearch("rozo")

Houdini prints one node name and removes this node. But then reports error:

>>> hou.session.delSearch("rozo")
/obj/rozok26
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "hou.session", line 3, in delSearch
  File "/opt/hfs15.0.347/houdini/python2.7libs/hou.py", line 5140, in name
    return _hou.Node_name(*args)
ObjectWasDeleted: Attempt to access an object that no longer exists in Houdini.

What can i do to delete all nodes with same name pattern?

This case is described in documentation but i can't find a way to resolve this problem.
 

 

 

 

Link to comment
Share on other sites

Maybe it is because you are modifying the list you are iterating over?

 

Instead of deleting inside the loop, consider collecting names to delete in a second pass.

def delSearch(patt):
    results = []
    for n in hou.node('/obj/').allSubChildren():
        if n.name().startswith(patt):
            print (n.path())
            results.append(n.path())
 
     if len(results):
        for node_name in results:
            hou.node(node_name).destroy()
 
hou.session.delSearch("rozo")
Edited by Atom
Link to comment
Share on other sites

it's doesn't work.

def delSearch(patt):
    results = []
    for n in hou.node('/obj/').allSubChildren():
        if n.name().startswith(patt):
            print (n.path())
            results.append(n.path())
    print (len(results))            
    if len(results):
        for node_name in results:
            hou.node(node_name).destroy()

output:

hou.session.delSearch("Arch")
/obj/Arch41_052_obj_20
.....
/obj/Arch41_052_obj_7
/obj/Arch41_052_obj_7/Arch41_052_obj_7
/obj/Arch41_052_obj_19
/obj/Arch41_052_obj_19/Arch41_052_obj_19
30
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "hou.session", line 11, in delSearch
AttributeError: 'function' object has no attribute 'destroy'
something wrong...
Edited by JJM
Link to comment
Share on other sites

if you have 

/obj/Arch41_052_obj_20
.....
/obj/Arch41_052_obj_7

 

don't use subChildren() - that will search recursively through the entire file

and find things like /obj/Arch41_052_obj_7/Arch41_052_obj_7

just use 

for child in node.children()

that will get all the nodes at the /obj/ level

/obj/Arch41_052_obj_7 etc

Link to comment
Share on other sites

Thanks for the help.
this code works fine:

def delSearch(patt):    
    results = []
    for n in hou.node('/obj/').children():
        if n.name().startswith(patt):
            print (n.path())
            results.append(n.path())
     
    print (len(results))            
    if len(results):
        for node_name in results:
            hou.node(node_name).destroy()
Edited by JJM
  • Like 1
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...