def add_node(self, node, pos=None): """ Add a node into the node graph. Args: node (NodeGraphQt.BaseNode): node object. pos (list[float]): node x,y position. (optional) """ assert isinstance(node, NodeObject), 'node must be a Node instance.' wid_types = node.model.__dict__.pop('_TEMP_property_widget_types') prop_attrs = node.model.__dict__.pop('_TEMP_property_attrs') if self.model.get_node_common_properties(node.type_) is None: node_attrs = { node.type_: {n: { 'widget_type': wt } for n, wt in wid_types.items()} } for pname, pattrs in prop_attrs.items(): node_attrs[node.type_][pname].update(pattrs) self.model.set_node_common_properties(node_attrs) node._graph = self node.NODE_NAME = self.get_unique_name(node.NODE_NAME) node.model._graph_model = self.model node.model.name = node.NODE_NAME node.update() self._undo_stack.push(NodeAddedCmd(self, node, pos))
def add_node(self, node): """ Add a node into the node graph. Args: node (NodeGraphQt.Node): node object. """ assert isinstance(node, NodeObject), 'node must be a Node instance.' wid_types = node.model.__dict__.pop('_TEMP_property_widget_types') prop_attrs = node.model.__dict__.pop('_TEMP_property_attrs') graph_attrs = self.model.node_property_attrs if node.type not in graph_attrs.keys(): graph_attrs[node.type] = { n: { 'widget_type': wt } for n, wt in wid_types.items() } for pname, pattrs in prop_attrs.items(): graph_attrs[node.type][pname].update(pattrs) node._graph = self node.NODE_NAME = self.get_unique_name(node.NODE_NAME) node.model._graph_model = self.model node.model.name = node.NODE_NAME node.update() self._undo_stack.push(NodeAddedCmd(self, node))
def create_node(self, node_type, name=None, selected=True, color=None, pos=None): """ Create a new node in the node graph. (To list all node types see :meth:`NodeGraph.registered_nodes`) Args: node_type (str): node instance type. name (str): set name of the node. selected (bool): set created node to be selected. color (tuple or str): node color (255, 255, 255) or '#FFFFFF'. pos (list[int, int]): initial x, y position for the node (default: (0, 0)). Returns: NodeGraphQt.Node: the created instance of the node. """ NodeCls = self._node_factory.create_node_instance(node_type) if NodeCls: node = NodeCls() node._graph = self node.model._graph_model = self.model wid_types = node.model.__dict__.pop('_TEMP_property_widget_types') prop_attrs = node.model.__dict__.pop('_TEMP_property_attrs') if self.model.get_node_common_properties(node.type_) is None: node_attrs = {node.type_: { n: {'widget_type': wt} for n, wt in wid_types.items() }} for pname, pattrs in prop_attrs.items(): node_attrs[node.type_][pname].update(pattrs) self.model.set_node_common_properties(node_attrs) node.NODE_NAME = self.get_unique_name(name or node.NODE_NAME) node.model.name = node.NODE_NAME node.model.selected = selected if color: if isinstance(color, str): color = color[1:] if color[0] is '#' else color color = tuple(int(color[i:i + 2], 16) for i in (0, 2, 4)) node.model.color = color if pos: node.model.pos = [float(pos[0]), float(pos[1])] node.update() undo_cmd = NodeAddedCmd(self, node, node.model.pos) undo_cmd.setText('created node') self._undo_stack.push(undo_cmd) self.node_created.emit(node) return node raise Exception('\n\n>> Cannot find node:\t"{}"\n'.format(node_type))
def add_node(self, node): """ Add a node into the node graph. Args: node (NodeGraphQt.Node): node object. """ assert isinstance(node, NodeObject), 'node must be a Node instance.' node._graph = self node.NODE_NAME = self.get_unique_name(node.NODE_NAME) node.model.name = node.NODE_NAME node.update() self._undo_stack.push(NodeAddedCmd(self, node))
def create_node(self, node_type, name=None, selected=True, color=None, pos=None): """ Create a new node in the node graph. To list all node types see "NodeGraphWidget.registered_nodes()" Args: node_type (str): node instance type. name (str): set name of the node. selected (bool): set created node to be selected. color (tuple or str): node color (255, 255, 255) or '#FFFFFF'. pos (tuple): set position of the node (x, y). Returns: NodeGraphQt.Node: created instance of a node. """ NodeInstance = NodeVendor.create_node_instance(node_type) if NodeInstance: node = NodeInstance() node._graph = self node.update() self._undo_stack.beginMacro('created node') self._undo_stack.push(NodeAddedCmd(self, node, pos)) if name: node.set_name(name) else: node.set_name(node.NODE_NAME) if color: if isinstance(color, str): color = color[1:] if color[0] is '#' else color color = tuple(int(color[i:i + 2], 16) for i in (0, 2, 4)) node.set_color(*color) node.set_selected(selected) self._undo_stack.endMacro() return node raise Exception('\n\n>> Cannot find node:\t"{}"\n'.format(node_type))
def _deserialize(self, data, relative_pos=False, pos=None): """ deserialize node data. Args: data (dict): node data. relative_pos (bool): position node relative to the cursor. Returns: list[NodeGraphQt.Nodes]: list of node instances. """ nodes = {} # build the nodes. for n_id, n_data in data.get('nodes', {}).items(): identifier = n_data['type'] NodeInstance = NodeVendor.create_node_instance(identifier) if NodeInstance: node = NodeInstance() node._graph = self name = self.get_unique_name(n_data.get('name', node.NODE_NAME)) n_data['name'] = name # set properties. for prop, val in node.model.properties.items(): if prop in n_data.keys(): setattr(node.model, prop, n_data[prop]) # set custom properties. for prop, val in n_data.get('custom', {}).items(): if prop in node.model.custom_properties.keys(): node.model.custom_properties[prop] = val node.update() self._undo_stack.push( NodeAddedCmd(self, node, n_data.get('pos'))) nodes[n_id] = node # build the connections. for connection in data.get('connections', []): nid, pname = connection.get('in', ('', '')) in_node = nodes.get(nid) if not in_node: continue in_port = in_node.inputs().get(pname) if in_node else None nid, pname = connection.get('out', ('', '')) out_node = nodes.get(nid) if not out_node: continue out_port = out_node.outputs().get(pname) if out_node else None if in_port and out_port: self._undo_stack.push(PortConnectedCmd(in_port, out_port)) node_objs = list(nodes.values()) if relative_pos: self._viewer.move_nodes([n.view for n in node_objs]) [setattr(n.model, 'pos', n.view.pos) for n in node_objs] elif pos: self._viewer.move_nodes([n.view for n in node_objs], pos=pos) return node_objs