Skip to content

Python

Find and replace path in whole scene

You have to specify UI params to make this script works as part you HDA.

Path reslover

The Path Resolver will replace any part of the path if it finds matches. For example:

If your project includes absolute paths like /Volumes/FOO/Projects/MyProject_Houdini/assets/ + /assetName/geo/assetGeo.bgeo and you want to replace the first part and map it to a different location, you can specify it as follows:

  • first path: /Volumes/FOO/Projects/MyProject_Houdini/assets/
  • second path: T:/FOO/Projects/MyProject_Houdini/assets/

The script will then resolve all matches for you.

from __future__ import print_function
import os
def replace_paths_in_scene(windows_path, macos_path, hda_node, hda_node_path):
hda_name = hda_node.type().nameComponents()
if os.name == 'nt':
old_path = macos_path
new_path = windows_path
elif os.name == 'posix':
old_path = windows_path
new_path = macos_path
else:
raise OSError("Unsupported operating system")
changed_paths = []
for node in hou.node("/").allSubChildren():
if node.type().nameComponents() == hda_node.type().nameComponents():
continue
for parm in node.parms():
# Only consider parameters with a string value (likely file paths)
if parm.parmTemplate().type() == hou.parmTemplateType.String:
current_value = parm.eval()
# Check if the current value contains the old path
if old_path in current_value:
new_value = current_value.replace(old_path, new_path)
parm.set(new_value)
changed_paths.append(f"Node: {node.path()}, Parameter: {parm.name()},\nOld: {current_value},\nNew: {new_value}\n")
return changed_paths
def resolve_btn():
hda_node = hou.pwd()
windows_path = hda_node.parm("win_path").eval().strip()
macos_path = hda_node.parm("macos_path").eval().strip()
display_changes = hda_node.parm("display_changes").eval()
hda_node_path = hda_node.path()
changed_paths = replace_paths_in_scene(windows_path, macos_path, hda_node, hda_node_path)
if display_changes:
if changed_paths:
message = "The following paths were updated:\n\n" + "\n".join(changed_paths)
hou.ui.displayMessage(message, title="Path Replacement Summary", severity=hou.severityType.Message, close_choice=True, help="The paths have been replaced.", details=message)
else:
hou.ui.displayMessage("No paths were updated.", title="Path Replacement Summary", severity=hou.severityType.Message)