Python: Reference changes with name change

   3835   13   2
User Avatar
Member
6 posts
Joined: Nov. 2022
Offline
I want references in my python module of a HDA to be correct when I change the name of the node.
For example:

switch = hou.node("/obj/DA/Brick_Wall_DA/switch3")

If I now change the name from Brick_Wall_DA to something else the reference won't work.
It feels like it should be easy to fix but I cannot work it out.

Thanks in advance.
User Avatar
Member
654 posts
Joined: Aug. 2014
Offline
Instead of hardcoding an operator path, create an "Operator Path" parameter and use it in your switchvariable. You will need to eval()this parameter and then create a hou.nodereference from it.

Then reference an existing operator from that parameter. Whenever this referenced operator's name changes, Houdini will update the parameter and the script will pull a new path from it.

Or, just use relative operator paths.
Edited by ajz3d - Nov. 9, 2022 12:36:22
User Avatar
Member
6 posts
Joined: Nov. 2022
Offline
ajz3d
Instead of hardcoding an operator path, create an "Operator Path" parameter and use it in your switchvariable. You will need to eval()this parameter and then create a hou.nodereference from it.

Then reference an existing operator from that parameter. Whenever this referenced operator's name changes, Houdini will update the parameter and the script will pull a new path from it.

Or, just use relative operator paths.

Hi, thanks for responding.

Could you explain what you mean by create an Operator path parameter, or just how I would use relative operator path?

Sorry if these seem like stupid questions. I'm very new to houdini
User Avatar
Member
313 posts
Joined: Oct. 2016
Offline
Hi, I’m on the move. Could you check the Python part in the docs. I wish this forum could focus on rather the next level. This I write with all respect, however, someone got paid to write the Puthon intro for the hou module.

If you do not find this answer there I will give it to you.

P.S. If doing Houdini Python is for you there will be a huge need to search docs from wherever you can. However, I suggest we all do our little homework”, if we are not under too much pressure, before asking here. Just referring to the most basic things. (And also I do teach programming fundamentals at high school, and know the drill)


Edit: https://www.sidefx.com/docs/houdini/hom/index.html [www.sidefx.com]

Edit2: For myself I spent years to understand programming, done courses, teach basics, and still have to do effort to get anything done with Houdini. It is because one has to get a grip of the API as well as understanding Python fundamentals.
Edited by SWest - Nov. 10, 2022 02:26:56
Interested in character concepts, modeling, rigging, and animation. Related tool dev with Py and VEX.
User Avatar
Member
6 posts
Joined: Nov. 2022
Offline
SWest
Hi, I’m on the move. Could you check the Python part in the docs. I wish this forum could focus on rather the next level. This I write with all respect, however, someone got paid to write the Puthon intro for the hou module.

If you do not find this answer there I will give it to you.

P.S. If doing Houdini Python is for you there will be a huge need to search docs from wherever you can. However, I suggest we all do our little homework”, if we are not under too much pressure, before asking here. Just referring to the most basic things. (And also I do teach programming fundamentals at high school, and know the drill)


Edit: https://www.sidefx.com/docs/houdini/hom/index.html [www.sidefx.com]

Edit2: For myself I spent years to understand programming, done courses, teach basics, and still have to do effort to get anything done with Houdini. It is because one has to get a grip of the API as well as understanding Python fundamentals.

Thank you for responding, I do understand all of this. At uni most of what we do is teaching ourselves and that’s what I’ve had to do for this project aswell. And trust me, I’ve spent the last week looking at houdini documentation for this project and it’s left me with something I’m very happy with but I couldn’t work out this 1 seemingly very simple thing and it was driving me insane. I wouldn’t want to ask otherwise. However clearly it is best for me to work it out myself, thanks.
User Avatar
Member
313 posts
Joined: Oct. 2016
Offline
Sure, there are many very helpful people here who share similar interests. Just learning to dig into technical things can be fun, as well as sharing knowledge.

Here you go:

# this node (relative)
this_node = hou.node('./')
print(this_node)
print(this_node.path())

# some parm
hou_parm_path = this_node.path()+'/b_int'
hou_parm = hou.parm(hou_parm_path)
print(hou_parm.eval())

Try that.
Edited by SWest - Nov. 10, 2022 06:54:00

Attachments:
this_node.png (274.4 KB)
swest_screen_capture_20221110_1248.mp4 (6.2 MB)

Interested in character concepts, modeling, rigging, and animation. Related tool dev with Py and VEX.
User Avatar
Member
6 posts
Joined: Nov. 2022
Offline
SWest
Sure, there are many very helpful people here who share similar interests. Just learning to dig into technical things can be fun, as well as sharing knowledge.

Here you go:

# this node (relative)
this_node = hou.node('./')
print(this_node)
print(this_node.path())

# some parm
hou_parm_path = this_node.path()+'/b_int'
hou_parm = hou.parm(hou_parm_path)
print(hou_parm.eval())

Try that.

Thank you so much for the help. I pretty much tried what you had here but ../ was returning none type every time. Maybe it has something to do with my code being in the event handler scripts? I've ended up using kwargs instead and then .parent() to find the rest of the path. Thanks again though
User Avatar
Member
654 posts
Joined: Aug. 2014
Offline
jacobtitheridge
Could you explain what you mean by create an Operator path parameter, or just how I would use relative operator path?

Sorry, I was typing that post in a hurry.

A rule of thumb is to reference HDA's internal operators with paths relative to the HDA's main node. Meaning: "switch1" or "subnet1/box1", instead of "/obj/geo1/my_hda_module/switch1" and "/obj/geo1/my_hda_module/subnet1/box1" for instance. HDA which relies on absolute paths to call its internal nodes is not portable, because a mere change in HDA's name, or any intermediate part of the path, will break the reference.

I mentioned the "Operator Path" parameter (which can be found in "Type Properties"->"Parameters"->"Create Parameters" list) only by mistake, because I didn't read your post correctly. Somehow I thought you were trying to reference an external node, while in fact you were referencing a node inside an HDA itself. I realized that right after posting and added a line about relative paths.

SWest
# this node (relative)
this_node = hou.node('./')
print(this_node)
print(this_node.path())

# some parm
hou_parm_path = this_node.path()+'/b_int'
hou_parm = hou.parm(hou_parm_path)
print(hou_parm.eval())

It's probably better to use hou.pwd() [www.sidefx.com] instead of hou.node('./')
For example:
# PythonModule
def test():
    hda = hou.pwd()
    switch = hda.node('switch1')
    print(switch.path())
    print(switch.parm('input').eval())
User Avatar
Member
6 posts
Joined: Nov. 2022
Offline
ajz3d
O

No worries, this last part works well thanks. Cant believe I didnt think to use the HDA before .node to reference things inside. I got too stuck on why hou.pwd() was returning just "/" to think to try it. You cant reference kwargs within the python module on a hda for some reason so I will need to use this method. Thank you so much.
User Avatar
Member
313 posts
Joined: Oct. 2016
Offline
In Linux ./ is ”this” relative folder while ../ is the parent. It is not obvoius for Windows users.
Interested in character concepts, modeling, rigging, and animation. Related tool dev with Py and VEX.
User Avatar
Member
654 posts
Joined: Aug. 2014
Offline
jacobtitheridge
I got too stuck on why hou.pwd() was returning just "/" to think to try it.
Yes,hou.pwd()returns the current path (or prints "current working directory" if we were to speak in GNU/Linux terms). When you call it from within an HDA or any other node, it will return their paths. It returned a hou.Nodeat "/" because you were calling it from "/".
>>> hou.pwd()
<hou.Node at />
>>> hou.cd('/obj/geo1/box1')
>>> hou.pwd()
<hou.SopNode of type box at /obj/geo1/box1> 
User Avatar
Member
654 posts
Joined: Aug. 2014
Offline
jacobtitheridge
You cant reference kwargs within the python module on a hda for some reason so I will need to use this method. Thank you so much.
Can you elaborate? I don't see why kwargs wouldn't work.
User Avatar
Member
6 posts
Joined: Nov. 2022
Offline
ajz3d
jacobtitheridge
You cant reference kwargs within the python module on a hda for some reason so I will need to use this method. Thank you so much.
Can you elaborate? I don't see why kwargs wouldn't work.

https://www.sidefx.com/forum/topic/75182/ [www.sidefx.com]
This thread talks about it, kwargs works within the event callback scripts but not the python module for a node. Very weird.
User Avatar
Member
654 posts
Joined: Aug. 2014
Offline
It's not weird, but rather how scopes work in programming. Functions are unaware of anything that is not passed to them.
In PythonModule and in event callbacks (like OnCreate), there are still some local kwargs implicitly passed under-the-hood. In case of PythonModule kwargs by default contain only the 'type' key, while in callbacks you also have access to 'node'.

# Button's callback script:
hou.phm().no_kwargs()

# PythonModule
def no_kwargs():
    print(kwargs)

Result:
{'type': <hou.SopNodeType for Sop test::1.0>}

You can however explicitly pass kwargs to functions from PythonModule via parameter callback script, which will enable you to access 'node', 'parm' you're calling the function from, and some other related keys.

# Button's callback script:
hou.phm().test_kwargs(kwargs)

# PythonModule
def test_kwargs(kwargs):
    print(kwargs)

Result:
{'node': <hou.SopNode of type test::1.0 at /obj/geo1/test1>, 'parm': <hou.Parm kwargs_button in /obj/geo1/test1>, 'script_multiparm_index': '-1', 'script_value0': '0',
'script_value': '0', 'parm_name': 'kwargs_button', 'script_multiparm_nesting': '0', 'script_parm': 'kwargs_button'}
Edited by ajz3d - Nov. 11, 2022 07:31:31
  • Quick Links