Switch if prim exists

   1943   7   0
User Avatar
Member
104 posts
Joined: Nov. 2007
Offline
I'm trying to to get a switch in lops to change based on whether a prim exists or not. The aim is to automatically create a beauty rendervar and product if one has not be defined. I can't work out how to set the switch lop up to achieve this though. Basically if "/Render/Products/beauty" exists, 0, else, 1.

Attachments:
Screenshot_5.jpg (22.7 KB)

User Avatar
Member
166 posts
Joined: Nov. 2013
Offline
This is a messy way, but I put a Python lop above the switch with this code in it. Sadly I have to right-click the node and Re-cook to get it to update.

node = hou.pwd()
stage = node.editableStage()
path = '/Render/Products/Vars/Beauty'
blah =  stage.GetPrimAtPath(path)
print blah

if blah:
    hou.parm('/stage/switch1/input').set(0)
else:
    hou.parm('/stage/switch1/input').set(1)
User Avatar
Member
104 posts
Joined: Nov. 2007
Offline
Thanks. I had a similar thought but you got further with the code than I managed! I'll try this out. I was hoping there would be a simpler way though. Perhaps not.
User Avatar
Member
166 posts
Joined: Nov. 2013
Offline
Oh there definitely is a simpler/more elegant solution, but I've been on vacation for a month and tried to forget everything about work. Someone will pipe up with a better solution I'm sure!
User Avatar
Staff
4432 posts
Joined: July 2005
Offline
I'm not quite sure why the straightforward approach wouldn't work... Can't you just set the "Switch Index" to an expression that looks at `pwd().input(0).stage()` to check for the prim, and returns 0 or 1 depending on whether the prim exists or not?

But I'll also point out that you can skip the Switch LOP entirely, and instead use an "activation parameter" on the LOP nodes that you want to operate conditionally. RMB on a LOP node, and under Parameters or LOP Actions, depending on the Houdini version, will be a menu item to "create activation parameter". When this parameter is 0, the LOP node acts like it is bypassed. So you can set this to a python expression that checks for the existence of the prim.
User Avatar
Member
104 posts
Joined: Nov. 2007
Offline
Yes. That works nicely. Code for the test:

if pwd().input(0).stage().GetPrimAtPath("/Render/Products").IsValid():
    return 0
return 1

I'm on Houdini 19.0.383 currently so can't do the activation parameter thing but that looks cool and I'll keep it in mind for when we upgrade our Houdini version.
User Avatar
Staff
4432 posts
Joined: July 2005
Offline
I can't say with complete certainty, but the activation parameter feature is quite old (though not well known). In 19.0.526 it shows up under "Parameters and Channels" -> "Create Activation Parameter". Future versions of Houdini just moved it under "LOP Actions" to make it a little easier to discover.
User Avatar
Member
166 posts
Joined: Nov. 2013
Offline
I would personally advise against the Activation parm method as it becomes less clear to people diving into the network why something is active/inactive, unless they look at every parm (and some people can or will hide the activation parm). A switch is much more self-explanatory and easy to debug.
  • Quick Links