Created
September 27, 2023 09:29
-
-
Save BigRoy/9dbd2921680f49078176b7af6da4ebb9 to your computer and use it in GitHub Desktop.
Houdini set node image thumbnail (NetworkView background image) and attach / link it to the node through Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import hou | |
import contextlib | |
@contextlib.contextmanager | |
def editor_at_node(node, pane_tab_type=hou.paneTabType.NetworkEditor): | |
editor = hou.ui.paneTabOfType(pane_tab_type) | |
original_pwd = editor.pwd() | |
try: | |
editor.setPwd(node.parent()) | |
yield editor | |
finally: | |
editor.setPwd(original_pwd) | |
def set_node_thumbnail(node, image_path): | |
with editor_at_node(node) as editor: | |
images = editor.backgroundImages() | |
images = list(images) | |
node_path = node.path() | |
# Find existing image attached to node | |
image = next( | |
( | |
image for image in images if | |
image.relativeToPath() == node_path | |
), | |
None | |
) | |
rect = hou.BoundingRect(-1, 0.2, 2, 2) | |
if image is None: | |
# Create the image | |
image = hou.NetworkImage(path, rect) | |
image.setRelativeToPath(node.path()) | |
images.append(image) | |
else: | |
# Update existing image | |
image.setRect(rect) | |
image.setPath(path) | |
editor.setBackgroundImages(images) | |
# Example usage | |
node = hou.node("/obj/geo1") | |
path = "/path/to/thumbnail.png" | |
set_node_thumbnail(node, path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment