Skip to content

Commit

Permalink
move functions defs to lib
Browse files Browse the repository at this point in the history
  • Loading branch information
MustafaJafar committed Sep 2, 2024
1 parent 7fd74c3 commit 3818b57
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 42 deletions.
56 changes: 56 additions & 0 deletions client/ayon_houdini/api/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1430,3 +1430,59 @@ def load_node_preset(target_node, node_preset, update_locked=False, log=None):

if update_locked and parm_locked:
param.lock(True)


def read_and_apply_preset(node, update_locked=False):
"""Read and apply a node preset.
This function expects the following parameters to be present on the node:
1. filepath: Path to the preset file node.
2. target_node: The node that will receive the preset settings.
Args:
node(hou.Node): file path node
update_locked (bool, optional): if set, it will update locked parameters.
Returns:
None
"""

json_path = node.parm("filepath").eval()
target_node = node.parm("target_node").evalAsNode()
if target_node:
node_preset = {}
with open(json_path, "r") as f:
node_preset = json.load(f)

load_node_preset(target_node, node_preset, update_locked=update_locked)


def node_preset_validate_target(node):
"""Validates the type of the target node.
This function provides visual confirmation when the target node's type
aligns with the type used to create the node preset.
This function expects the following parameters to be present on the node:
1. filepath: Path to the preset file node.
2. target_node: The node that will receive the preset settings.
"""

json_path = node.parm("filepath").eval()
target_node = node.parm("target_node").evalAsNode()
node_preset = {}
with open(json_path, "r") as f:
node_preset = json.load(f)

node_type = node_preset["metadata"]["type"]

node.setColor(hou.Color(0.7, 0.8, 0.87))
node.setComment("")
node.setGenericFlag(hou.nodeFlag.DisplayComment, True)
if target_node and target_node.type().name() != node_type:
node.setColor(hou.Color(0.8, 0.45, 0.1))
node.setComment(
f"Target Node type '{target_node.type().name()}' doesn't match the loaded preset type '{node_type}'."
"Please note, Applying the preset skips parameters that doesn't exist"
)
48 changes: 6 additions & 42 deletions client/ayon_houdini/plugins/load/load_filepath.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,26 +170,8 @@ def _add_more_node_params(self, attr_folder, node):
tags= {
"oprelative" : ".",
"script_callback" : """
import json
from ayon_houdini.api.lib import load_node_preset
json_path = hou.parm("./filepath").eval()
target_node = hou.parm("./target_node").evalAsNode()
node_preset = {}
with open(json_path, "r") as f:
node_preset = json.load(f)
node_type = node_preset["metadata"]["type"]
hou.pwd().setColor(hou.Color(0.7, 0.8, 0.87))
hou.pwd().setComment("")
hou.pwd().setGenericFlag(hou.nodeFlag.DisplayComment, True)
if target_node and target_node.type().name() != node_type:
hou.pwd().setColor(hou.Color(0.8, 0.45, 0.1))
hou.pwd().setComment(
f"Target Node type '{target_node.type().name()}' doesn't match the loaded preset type '{node_type}'."
"Please note, Applying the preset skips parameters that doesn't exist"
)
from ayon_houdini.api.lib import node_preset_validate_target
node_preset_validate_target(hou.pwd())
""",
"script_callback_language" : "python",
}
Expand All @@ -200,17 +182,8 @@ def _add_more_node_params(self, attr_folder, node):
label="Apply Preset",
tags= {
"script_callback" : """
import json
from ayon_houdini.api.lib import load_node_preset
json_path = hou.parm("./filepath").eval()
target_node = hou.parm("./target_node").evalAsNode()
if target_node:
node_preset = {}
with open(json_path, "r") as f:
node_preset = json.load(f)
load_node_preset(target_node, node_preset)
from ayon_houdini.api.lib import read_and_apply_preset
read_and_apply_preset(hou.pwd())
""",
"script_callback_language" : "python",
},
Expand All @@ -223,17 +196,8 @@ def _add_more_node_params(self, attr_folder, node):
label="Force Apply Preset",
tags= {
"script_callback" : """
import json
from ayon_houdini.api.lib import load_node_preset
json_path = hou.parm("./filepath").eval()
target_node = hou.parm("./target_node").evalAsNode()
if target_node:
node_preset = {}
with open(json_path, "r") as f:
node_preset = json.load(f)
load_node_preset(target_node, node_preset, update_locked=True)
from ayon_houdini.api.lib import read_and_apply_preset
read_and_apply_preset(hou.pwd(), update_locked=True)
""",
"script_callback_language" : "python",
},
Expand Down

0 comments on commit 3818b57

Please sign in to comment.