|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +import inspect |
| 4 | +from NodeGraphQt import BaseNode |
| 5 | + |
| 6 | + |
| 7 | +class ObjectWrapperNode(BaseNode): |
| 8 | + """ |
| 9 | + Take an object from the input port and wrappe it. |
| 10 | + """ |
| 11 | + |
| 12 | + # set a unique node identifier. |
| 13 | + __identifier__ = 'Util' |
| 14 | + |
| 15 | + # set the initial default node name. |
| 16 | + NODE_NAME = 'Object Wrapper' |
| 17 | + |
| 18 | + def __init__(self): |
| 19 | + super(ObjectWrapperNode, self).__init__() |
| 20 | + self.selfPort = self.add_input('self') |
| 21 | + self.create_property('self', None) |
| 22 | + |
| 23 | + def buildNode(self): |
| 24 | + obj = self.get_property('self') |
| 25 | + if obj: |
| 26 | + self.set_name(obj.__class__.__name__.capitalize()) |
| 27 | + else: |
| 28 | + self.set_name('Object Wrapper (None)') |
| 29 | + |
| 30 | + # switch math function type |
| 31 | + if 'methods' not in self.view.widgets: |
| 32 | + self.add_combo_menu('methods', |
| 33 | + 'Methods', |
| 34 | + items=dir(obj), |
| 35 | + tab='widgets') |
| 36 | + |
| 37 | + self.view.widgets['methods'].value_changed.connect( |
| 38 | + self.addFunction) |
| 39 | + self.view.widgets['methods'].value_changed.connect( |
| 40 | + self.update_streams) |
| 41 | + self.add_output('output') |
| 42 | + self.create_property('output', None) |
| 43 | + else: |
| 44 | + comboBox = self.view.widgets['methods'].widget |
| 45 | + comboBox.clear() |
| 46 | + comboBox.addItems(dir(obj)) |
| 47 | + |
| 48 | + def addFunction(self, prop, func): |
| 49 | + """ |
| 50 | + Create inputs based on math functions arguments. |
| 51 | + """ |
| 52 | + self.funcName = func |
| 53 | + obj = self.get_property('self') |
| 54 | + func = getattr(obj, self.funcName) |
| 55 | + dataFunc = inspect.signature(func) |
| 56 | + |
| 57 | + for arg in dataFunc.args: |
| 58 | + if not self.has_property(arg): |
| 59 | + inPort = self.add_input(arg) |
| 60 | + self.create_property(arg, None) |
| 61 | + |
| 62 | + for inPort in self._inputs: |
| 63 | + if inPort.name() in dataFunc.args: |
| 64 | + if not inPort.visible(): |
| 65 | + inPort.set_visible(True) |
| 66 | + else: |
| 67 | + inPort.set_visible(False) |
| 68 | + |
| 69 | + def getSelf(self): |
| 70 | + for from_port in self.selfPort.connected_ports(): |
| 71 | + return from_port.node().get_property(from_port.name()) |
| 72 | + |
| 73 | + def run(self): |
| 74 | + """ |
| 75 | + Evaluate all entries, pass them as arguments of the |
| 76 | + chosen mathematical function. |
| 77 | + """ |
| 78 | + obj = self.getSelf() |
| 79 | + if not obj: |
| 80 | + return |
| 81 | + |
| 82 | + for to_port in self.input_ports(): |
| 83 | + if to_port.visible() == False: |
| 84 | + continue |
| 85 | + |
| 86 | + from_ports = to_port.connected_ports() |
| 87 | + if not from_ports: |
| 88 | + raise Exception('Port %s not connected!' % to_port.name(), |
| 89 | + to_port) |
| 90 | + |
| 91 | + for from_port in from_ports: |
| 92 | + if from_port.name() == 'self': |
| 93 | + obj = from_port.node().get_property(from_port.name()) |
| 94 | + continue |
| 95 | + from_port.node().run() |
| 96 | + data = from_port.node().get_property(from_port.name()) |
| 97 | + self.set_property(to_port.name(), data) |
| 98 | + |
| 99 | + self.func = getattr(obj, self.funcName) |
| 100 | + |
| 101 | + try: |
| 102 | + # Execute math function with arguments. |
| 103 | + data = self.func(*[ |
| 104 | + self.get_property(inport.name()) for inport in self._inputs |
| 105 | + if inport.visible() and inport.name() != 'self' |
| 106 | + ]) |
| 107 | + |
| 108 | + self.set_property('output', data) |
| 109 | + except KeyError as error: |
| 110 | + print("An input is missing! %s" % str(error)) |
| 111 | + except TypeError as error: |
| 112 | + print("Error evaluating function: %s" % str(error)) |
| 113 | + |
| 114 | + def on_input_connected(self, to_port, from_port): |
| 115 | + """Override node callback method.""" |
| 116 | + from_port.node().run() |
| 117 | + outValue = from_port.node().get_property(from_port.name()) |
| 118 | + self.set_property(to_port.name(), outValue) |
| 119 | + |
| 120 | + if to_port.name() == 'self': |
| 121 | + self.buildNode() |
| 122 | + self.update_streams() |
| 123 | + |
| 124 | + def on_input_disconnected(self, to_port, from_port): |
| 125 | + """Override node callback method.""" |
| 126 | + self.set_property('self', None) |
| 127 | + self.set_property('output', None) |
| 128 | + comboBox = self.view.widgets['methods'].widget |
| 129 | + comboBox.clear() |
| 130 | + self.update_streams() |
0 commit comments