asdf
This commit is contained in:
@ -2,10 +2,9 @@
|
||||
"""
|
||||
Convert to Percent Node
|
||||
|
||||
This node takes an input string formatted as "numerator/denom" (e.g., "50/50"),
|
||||
splits it into two numbers, computes (numerator / denom) * 100, and outputs the result
|
||||
as a float formatted to 4 decimal places. If an error occurs, an error message is stored.
|
||||
The node's title is always "Convert to Percent".
|
||||
This node takes two numerical inputs (A and B), computes (A / B) * 100,
|
||||
and outputs the result as a float formatted to 4 decimal places. If an error
|
||||
occurs, an error message is stored. The node's title is always "Convert to Percent".
|
||||
"""
|
||||
|
||||
from NodeGraphQt import BaseNode
|
||||
@ -17,8 +16,9 @@ class ConvertToPercentNode(BaseNode):
|
||||
|
||||
def __init__(self):
|
||||
super(ConvertToPercentNode, self).__init__()
|
||||
# Add one input port (expects a string in "numerator/denom" format).
|
||||
self.add_input("in")
|
||||
# Add two input ports for separate numerator and denominator.
|
||||
self.add_input("Numerator")
|
||||
self.add_input("Denominator")
|
||||
# Add one output port.
|
||||
self.add_output("Percent")
|
||||
# Initialize internal value.
|
||||
@ -29,34 +29,37 @@ class ConvertToPercentNode(BaseNode):
|
||||
self.values = {}
|
||||
|
||||
def process_input(self):
|
||||
input_port = self.input(0)
|
||||
connected_ports = input_port.connected_ports() if input_port is not None else []
|
||||
if connected_ports:
|
||||
connected_output = connected_ports[0]
|
||||
parent_node = connected_output.node()
|
||||
port_name = connected_output.name()
|
||||
# Use parent's values dictionary if available, else use its value attribute.
|
||||
if hasattr(parent_node, 'values') and isinstance(parent_node.values, dict):
|
||||
input_value = parent_node.values.get(port_name, "")
|
||||
else:
|
||||
input_value = getattr(parent_node, 'value', "")
|
||||
input_str = str(input_value).strip()
|
||||
try:
|
||||
parts = input_str.split('/')
|
||||
if len(parts) != 2:
|
||||
raise ValueError("Input must be in the format 'num/denom'")
|
||||
numerator = float(parts[0].strip())
|
||||
denominator = float(parts[1].strip())
|
||||
if denominator == 0:
|
||||
raise ZeroDivisionError("Division by zero")
|
||||
percent = (numerator / denominator) * 100
|
||||
formatted_percent = f"{percent:.4f}"
|
||||
self.value = formatted_percent
|
||||
except Exception as e:
|
||||
self.value = f"Error: {e}"
|
||||
else:
|
||||
self.value = "No Input"
|
||||
numerator_input = self.input(0)
|
||||
denominator_input = self.input(1)
|
||||
|
||||
numerator = self.get_connected_value(numerator_input)
|
||||
denominator = self.get_connected_value(denominator_input)
|
||||
|
||||
try:
|
||||
numerator = float(numerator)
|
||||
denominator = float(denominator)
|
||||
if denominator == 0:
|
||||
raise ZeroDivisionError("Division by zero")
|
||||
percent = (numerator / denominator) * 100
|
||||
formatted_percent = f"{percent:.4f}"
|
||||
self.value = formatted_percent
|
||||
except Exception as e:
|
||||
self.value = f"Error: {e}"
|
||||
|
||||
# Always keep the title static.
|
||||
self.set_name(self.NODE_NAME)
|
||||
# Store the computed value in the values dictionary under the output port key.
|
||||
self.values["Percent"] = self.value
|
||||
|
||||
def get_connected_value(self, input_port):
|
||||
"""
|
||||
Helper function to retrieve the value from a connected port.
|
||||
"""
|
||||
if input_port and input_port.connected_ports():
|
||||
connected_output = input_port.connected_ports()[0]
|
||||
parent_node = connected_output.node()
|
||||
port_name = connected_output.name()
|
||||
if hasattr(parent_node, 'values') and isinstance(parent_node.values, dict):
|
||||
return parent_node.values.get(port_name, "0")
|
||||
return getattr(parent_node, 'value', "0")
|
||||
return "0"
|
||||
|
Reference in New Issue
Block a user