Jump to content

Very basic Py code in Switch SOP


Kardonn

Recommended Posts

This feels like a dumb question, but it's 4am, I'm pretty new to Python and I'm sure I'm just missing something stupid here. I'm trying to change a Switch SOP in my scene based on whether or not a certain node exists in /obj/. This is my shelf button to test it out with, which works just fine:

 

root = hou.node('/obj')

nodes = root.children()
 
exist = 0
 
for child in nodes:
    if (child.name().startswith("oven_mitt")):
        exist = 1
        break
 
if exist == 0:
    print 'Nope!'
else:
    print 'Yes!'
 
Then on the Switch SOP, I'm just changing the "print 'Nope!'" to 0 and "print 'Yes!'" to 1...but it doesn't seem to work anymore. If I leave the print statements in place though and hit apply in the Python expression window, the console properly outputs Nope! and Yes!
 
I'm guessing I'm missing some kind of parm.eval() code here or something like that.
Link to comment
Share on other sites

Python expression used in parameters are implicitly functions, so in your case you would have to "return 0/1" instead of "print"

(...)
if exist == 0:
   return 0
else:
   return 1

A couple of random thoughts about that.

- I kind of dislike solving such problem wtih python script embeded into parameters. Not sure if this is prejudice or rational concerns. First, at least for 15.0.322 you will have problem with refreshing, which I'am not sure how to solve. Putting hou.frame() into the code, makes it time dependent, so it will refresh on time change, but not force recook in place. Second, this is non-procedural, hardcoded solution...

 

- Oldschool way would be to use ObjectMergeSOP with /obj/startwith_key* parm, and in switchsop use expression like bellow to check whether it has imported anything. This obviously doesn't work for many cases though...

if(npoints("../objectmerge") > 0, 1, 0)

not to mention it also has problem with refreshing once import errer occurs (which is new bahaviour, as I am pretty sure in undefined-old-version-of-houdini, object_merge Sops were nicely recover after error)...

 

- I think my main concern is that is seems to be ineletant to drive SOP level parameters with OBJ level queries about nodes' names. Sops should worry about points/prims/attributes, not random objects' names. Perhaps I'm too pedantic, but such setup usually indicates there is some design flaw in a scene.

  • Like 3
Link to comment
Share on other sites

I just ran into the python refresh issue Symek mentions. I consider this a Houdini bug. If I write a python script and place it at the root level, it should run everytime the frame changes. This does not seem to be the case, however. Leaving me the impression that python, in it's typical usage, is an after thought in Houdini.

 

Here is the code that will not run every frame.

 

cf = hou.frame()
root = hou.node('/obj')
nodes = root.children()

exist = 0
 
for child in nodes:
    print child.name()
    if (child.name().startswith("oven_mitt")):
        exist = 1
        break

target_node = hou.node('/obj/geo1/switch1')
if exist == 0:
    target_node.parm("input").set(0)
    print 0
else:
    target_node.parm("input").set(1)
    print 1
I should see a print every time the frame changes let alone having the switch change if I rename the node.

The python node only seems to refresh if you select it in the network view. If another node has focus, the code does not work.

ap_python_wont_refresh.hipnc

Edited by Atom
Link to comment
Share on other sites

Thanks Symek!

 

I like the Object Merge approach too actually, I think both work pretty well.

 

The main purpose for this is that I've got characters in shots, but who are now getting new props and costume elements added to them. In this case, the mom character is getting an oven mitt. Issue is that her hand clips the mitt and isn't designed to be rendered alongside it...so I just need a foolproof way for everyone lighting on the project to never have to think about whether or not the mom has the right blast nodes turned on, etc.

 

Edit: After some testing, the Py expression was more stable than the Object Merge using "/obj/oven_mitt*/*" was. If I changed the /obj/oven_mitt_rig name to something else and nudge the play head a frame, the Py expression would evaluate properly again (still doesn't update live unfortunately). The Object Merge would break as expected, but then not unbreak when I renamed it back to oven_mitt.

 

Maybe I'm not setting up the Obj Merge in the right way though.

Edited by Kardonn
Link to comment
Share on other sites

If your characters are participating in a crowd sim I think you can swap items using stylesheets.

 

Not an issue of swapping items as much as needing geometry like hands and feet blasted off the character meshes in order for there to be no penetration issues. So if a certain characters' prop is in the scene (like the oven mitt for example) then the character it's associated with needs to have their hand geometry deleted.

Link to comment
Share on other sites

So I saw this post while on holidays but didn't get a chance to respond.

 

Looking over the examples here I'm pretty sure things are behaving as expected in terms of how Houdini cooks and calculates dependencies (at least my understanding of them.) 

 

This is all as expected.  Querying the child nodes of /obj isn't going to build any sort of dependency between that switch input parm and the nodes in /obj.  Without a proper dependency Houdini doesn't know that when the any nodes in /obj change that it needs to reevaluate the switch input parameter.  As Symek mentions the easiest way to kind of make it work is to make the expression time dependent but that will only force it to update on frame change, not node addition.  Also, this now introduces time dependency to your network which may drastically reduce performance.

 

In regards to Atom's file, it seems to behave as I'd expect.  The issue is that you are using a Python Object that is not guaranteed to cook every frame, even if you have hou.frame() in there.  Your file will work as long as the node has reason to actually cook (ie. it is displayed or in a transform hierarchy that needs to be displayed.)

 

Object merges are also tricky when you're using them with wildcards.  Updates are guaranteed since as mentioned about Houdini is all about dependencies.  If you set the object path to something that contains a wild card and it matches, that's great!  But once you start renaming things and losing any dependencies Houdini might have to those objects you're gonna get into a pile of trouble.

 

I have always wished Houdini contained some better debugging  tools to essentially step through the cook system to see what is cooking and why.

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