Example #1
0
    def node_repr(self):
        """Format to visualize the Graph."""
        canvas_ = canvas.Canvas()
        x = 0

        evaluation_matrix = self.evaluation_matrix

        for row in evaluation_matrix:
            y = 0
            x_diff = 0
            for node in row:
                item_ = item.Item(str(node), [x, y])
                node.item = item_
                x_diff = (item_.bbox[2] - item_.bbox[0] + 4 if
                          item_.bbox[2] - item_.bbox[0] + 4 > x_diff else x_diff)
                y += item_.bbox[3] - item_.bbox[1]
                canvas_.add_item(item_)
            x += x_diff

        for node in self.all_nodes:
            for i, plug in enumerate(node._sort_plugs(node.all_outputs())):
                for connection in node._sort_plugs(
                        node.all_outputs())[plug].connections:
                    dnode = connection.node
                    start = [node.item.position[0] + node.item.bbox[2],
                             node.item.position[1] + 3 + len(node.all_inputs()) + i]
                    end = [dnode.item.position[0],
                           dnode.item.position[1] + 3 +
                           list(dnode._sort_plugs(
                                dnode.all_inputs()).values()).index(
                                    connection)]
                    canvas_.add_item(item.Line(start, end), 0)
        return canvas_.render()
Example #2
0
    def node_repr(self):
        """Format to visualize the Graph."""
        canvas_ = canvas.Canvas()
        x = 0

        evaluation_matrix = self.evaluation_matrix

        for row in evaluation_matrix:
            y = 0
            x_diff = 0
            for node in row:
                item_ = item.Item(str(node), [x, y])
                node.item = item_
                x_diff = (item_.bbox[2] - item_.bbox[0] +
                          4 if item_.bbox[2] - item_.bbox[0] + 4 > x_diff else
                          x_diff)
                y += item_.bbox[3] - item_.bbox[1]
                canvas_.add_item(item_)
            x += x_diff

        # Include the input groups if any have been set
        y_off = 2
        locked_items = []
        if self.input_groups:
            for input_group in self.input_groups.values():
                y_off += 1
                i = item.Item("o {0}".format(input_group.name), [0, y_off])
                canvas_.add_item(i)
                locked_items.append(i)
                for p in input_group.plugs:
                    y_off += 1
                    i = item.Item("`-{0}.{1}".format(p.node.name, p.name),
                                  [2, y_off])
                    canvas_.add_item(i)
                    locked_items.append(i)

        # Move all items down by Y
        for i in canvas_.items:
            if i not in locked_items:
                i.position[0] += 2
                i.position[1] += y_off + 1 + int(bool(self.input_groups))

        canvas_.add_item(item.Rectangle(x, canvas_.bbox[3] + 1, [0, 0]), 0)

        # Crop the name of the graph if it is too long
        name = self.name
        if len(name) > x - 2:
            name = name[:x - 2]
        canvas_.add_item(
            item.Item("{name:^{x}}".format(name=name, x=x), [0, 1]), 0)
        canvas_.add_item(item.Rectangle(x, 3, [0, 0]), 0)

        if self.input_groups:
            canvas_.add_item(item.Rectangle(x, y_off + 2, [0, 0]), 0)

        for node in self.all_nodes:
            for i, plug in enumerate(node._sort_plugs(node.all_outputs())):
                for connection in node._sort_plugs(
                        node.all_outputs())[plug].connections:
                    dnode = connection.node
                    start = [
                        node.item.position[0] + node.item.bbox[2],
                        node.item.position[1] + 3 + len(node.all_inputs()) + i,
                    ]
                    end = [
                        dnode.item.position[0],
                        dnode.item.position[1] + 3 +
                        list(dnode._sort_plugs(
                            dnode.all_inputs()).values()).index(connection),
                    ]
                    canvas_.add_item(item.Line(start, end), 0)

        return canvas_.render()