Refactor comparison operations in geometry nodes

- Introduced a new `compare_node` function to streamline comparison operations across various geometry nodes.
- Replaced instances of mathematical comparison nodes with the new `compare_node` function in multiple files, enhancing code readability and maintainability.
- Updated related logic in cutout, depth surface, and image processing groups to utilize the new comparison method.
- Ensured consistent handling of boolean logic through the introduction of a `boolean_node` function for combining boolean inputs.
This commit is contained in:
2026-09-16 21:42:02 +08:00
parent c62ec2cd48
commit 78bbff6351
19 changed files with 282 additions and 200 deletions

View File

@@ -79,6 +79,41 @@ def math_node(nodes, operation):
return node
_COMPARE_INPUTS = {
"FLOAT": ("A", "B"),
"INT": ("A_INT", "B_INT"),
"VECTOR": ("A_VEC3", "B_VEC3"),
"RGBA": ("A_COL", "B_COL"),
}
def compare_node(group, operation, a, b, data_type="FLOAT"):
"""Compare two inputs on the given data type and return the boolean result."""
node = group.nodes.new("FunctionNodeCompare")
node.data_type, node.operation = data_type, operation
for value, identifier in zip((a, b), _COMPARE_INPUTS[data_type]):
socket = next(item for item in node.inputs if item.identifier == identifier)
if isinstance(value, (bool, int, float)):
socket.default_value = value
else:
group.links.new(value, socket)
return node.outputs["Result"]
def boolean_node(group, operation, a=None, b=None):
"""Combine boolean inputs and return the boolean result."""
node = group.nodes.new("FunctionNodeBooleanMath")
node.operation = operation
for value, index in ((a, 0), (b, 1)):
if value is None:
continue
if isinstance(value, bool):
node.inputs[index].default_value = value
else:
group.links.new(value, node.inputs[index])
return node.outputs["Boolean"]
def menu_switch(group, control, names, kind):
selector = next((node for node in group.nodes if node.bl_idname == "GeometryNodeMenuSwitch" and node.inputs["Menu"].is_linked and node.inputs["Menu"].links[0].from_socket.name == control), None)
if selector is None: