示例#1
0
    def execute(self, context):
        # if triggered from a non-initialized tree, we first make a tree
        tree = context.space_data.node_tree
        if tree is None:
            tree = bpy.data.node_groups.new(basename(self.filepath),
                                            'SverchCustomTreeType')
            context.space_data.node_tree = tree  # pass this tree to the active node view

        # Deselect everything, so as a result only imported nodes will be selected
        bpy.ops.node.select_all(action='DESELECT')
        JSONImporter.init_from_path(self.filepath).import_into_tree(tree)
        return {'FINISHED'}
示例#2
0
    def execute(self, context):
        if len(context.space_data.path) > 1:
            self.report({"WARNING"},
                        "Import is not supported inside node groups")
            return {'CANCELLED'}

        if not self.id_tree:
            ng_name = self.new_nodetree_name
            ng_params = {
                'name': ng_name or 'unnamed_tree',
                'type': 'SverchCustomTreeType'
            }
            ng = bpy.data.node_groups.new(**ng_params)
        else:
            ng = bpy.data.node_groups[self.id_tree]

        if self.gist_id == 'clipboard':
            self.gist_id = context.window_manager.clipboard

        nodes_json = load_json_from_gist(self.gist_id.strip(), self)
        if not nodes_json:
            return {'CANCELLED'}

        # import tree and set new node tree to active
        JSONImporter(nodes_json).import_into_tree(ng)
        context.space_data.node_tree = ng
        return {'FINISHED'}
示例#3
0
    def test_grouping_nodes(self):

        examples_path = Path(sverchok.__file__).parent / 'json_examples'

        with self.temporary_node_tree("ImportedTree") as new_tree:
            bpy.context.area.ui_type = 'SverchCustomTreeType'
            bpy.context.space_data.node_tree = new_tree
            with self.assert_logs_no_errors(
            ):  # just in case if the operators will get logger someday
                new_tree.sv_process = False
                importer = JSONImporter.init_from_path(
                    str(examples_path / "Architecture" /
                        "Power_transmission_tower.json"))
                importer.import_into_tree(new_tree, print_log=False)
                [setattr(n, 'select', False) for n in new_tree.nodes]
                [
                    setattr(new_tree.nodes[n], 'select', True) for n in [
                        'A Number', 'Formula', 'Vector polar input',
                        'Vector polar output'
                    ]
                ]
                with self.subTest(msg="Grouping nodes"):
                    bpy.ops.node.add_group_tree_from_selected()
                with self.subTest(msg="Ungrouping nodes"):
                    bpy.context.space_data.path.pop()
                    [setattr(n, 'select', False) for n in new_tree.nodes]
                    group_node, *_ = [
                        n for n in new_tree.nodes if hasattr(n, 'node_tree')
                    ]
                    group_node.select = True
                    bpy.ops.node.ungroup_group_tree({'node': group_node})
示例#4
0
    def test_generating_tree_structure(self):

        examples_path = Path(sverchok.__file__).parent / 'json_examples'

        with self.temporary_node_tree("ImportedTree") as new_tree:
            new_tree.sv_process = False
            importer = JSONImporter.init_from_path(
                str(examples_path / "Architecture" / "ProfileBuilding.json"))
            importer.import_into_tree(new_tree, print_log=False)
            tree = ts.Tree(new_tree)
            self.are_trees_equal(new_tree, tree)

            with self.subTest(msg="Detecting changes in tree collections"):
                new_tree.nodes.remove(tree.nodes['Number Range.001'].bl_tween)
                changed_tree = ts.Tree(new_tree)
                self.assertCountEqual(tree.nodes - changed_tree.nodes,
                                      (tree.nodes['Number Range.001'], ))
                self.assertCountEqual(
                    tree.links - changed_tree.links,
                    tree.nodes['Number Range.001'].outputs[0].links)
                self.assertRaises(TypeError,
                                  lambda: tree.nodes - {'Name 1', 'Name 2'})

                tree.nodes['Note'].bl_tween.inputs.remove(
                    tree.nodes['Note'].inputs[0].get_bl_socket(new_tree))
                self.assertRaises(LookupError,
                                  tree.nodes['Note'].inputs[0].get_bl_socket,
                                  new_tree)
示例#5
0
def monad_make_unique(node):

    """
    Create a new version of the monad class (duplicate but unique)

    This will attempt to store the duplicate in a json using create_dict_of_tree (from the Gist IO).
    The upside is that this will test the pack/unpack routine continuously. 
    The downside is that this will likely expose all the shortcommings that we don't know 
    about because it wasn't being tested extensively.
    """

    node_tree = node.id_data
    nodes = node_tree.nodes

    # generate a new copy of monad group node. using ( copy? ) 
    monad_group = bpy.data.node_groups[node.monad.name]
    new_monad_group = monad_group.copy()
    new_cls_name = make_new_classname(new_monad_group) 

    # the new tree dict will contain information about 1 node only, and 
    # the node_group too (at the moment) but the node_group data can be ignored.
    layout_json = JSONExporter.get_nodes_structure([node])

    # do not restore links this way. wipe this entry and restore at a later stage.
    layout_json['update_lists'] = []

    # massage content of node_items, to correspond with the new desired name.
    node_ref = layout_json['nodes'][node.name]
    node_items = node_ref['params']
    node_items['all_props']['name'] = new_monad_group.name
    node_items['all_props']['cls_bl_idname'] = new_cls_name
    node_items['monad'] = new_monad_group.name
    node_items['cls_dict']['cls_bl_idname'] = new_cls_name

    pre_nodes = set(nodes)

    # place new empty version of the monad node
    JSONImporter(layout_json).import_into_tree(node_tree, print_log=False)

    """
    notions..:
    
        if (original instance has no connections) then 
            replace it outright.
        else
            if mode=='replace':
                store connections
                replace instance with new unique instance
                reconnect old connections
            elif mode=='dupe_translate':
                generate unique instance
                attache node to transform operator.


    """

    # return newly generated node!
    return (set(node_tree.nodes) ^ pre_nodes).pop() 
示例#6
0
    def execute(self, context):
        ntree = context.space_data.node_tree
        node = ntree.nodes[self.node_name]
        id_tree = ntree.name
        ng = bpy.data.node_groups[id_tree]

        preset = SvPreset(path=self.preset_path, category=node.bl_idname)
        JSONImporter(preset.data).import_node_settings(node)
        return {'FINISHED'}
示例#7
0
                def execute(operator, context):
                    # please not be confused: "operator" here references to
                    # SverchPresetAddOperator instance, and "self" references to
                    # SvPreset instance.  
                    ntree = context.space_data.node_tree
                    id_tree = ntree.name
                    ng = bpy.data.node_groups[id_tree]

                    center = context.space_data.cursor_location
                    # Deselect everything, so as a result only imported nodes
                    # will be selected
                    bpy.ops.node.select_all(action='DESELECT')
                    JSONImporter.init_from_path(self.path).import_into_tree(ng)
                    new_nodes = [node for node in ng.nodes if node.select]
                    new_nodes_center = reduce(lambda v1, v2: v1 + v2, [n.location for n in new_nodes]) / len(new_nodes)
                    [setattr(n, 'location', n.location + center - new_nodes_center) for n in new_nodes]
                    bpy.ops.transform.translate('INVOKE_DEFAULT')
                    return {'FINISHED'}
示例#8
0
    def test_generating_tree_structure(self):

        examples_path = Path(sverchok.__file__).parent / 'json_examples'

        with self.temporary_node_tree("ImportedTree") as new_tree:
            new_tree.sv_process = False
            importer = JSONImporter.init_from_path(
                str(examples_path / "Architecture" / "ProfileBuilding.json"))
            importer.import_into_tree(new_tree, print_log=False)
            tree = ts.Tree(new_tree)
            self.are_trees_equal(new_tree, tree)
示例#9
0
    def execute(self, context):
        ng = context.scene.io_panel_properties.import_tree
        if not ng:
            self.report(type={'WARNING'}, message="The tree was not chosen, have a look at property (N) panel")
            return {'CANCELLED'}

        importer = JSONImporter.init_from_path(self.filepath)
        importer.import_into_tree(ng)
        if importer.has_fails:
            self.report({'ERROR'}, importer.fail_massage)

        # set new node tree to active
        context.space_data.node_tree = ng
        return {'FINISHED'}
示例#10
0
    def assert_node_equals_file(self, actual_node, reference_node_name, reference_file_name, imported_tree_name=None):
        """
        Assert that actual_node equals to node named reference_node_name imported from file reference_file_name.
        This works only for simple nodes.
        """
        if imported_tree_name is None:
            imported_tree_name = "ImportedTree"

        try:
            new_tree = get_or_create_node_tree(imported_tree_name)
            importer = JSONImporter.init_from_path(self.get_reference_file_path(reference_file_name))
            importer.import_into_tree(new_tree, print_log=False)
            self.assert_nodes_are_equal(actual_node, get_node(reference_node_name, imported_tree_name))
        finally:
            remove_node_tree(imported_tree_name)
示例#11
0
def apply_default_preset(node):
    preset = get_preset(node.bl_idname, "Default")
    if preset is not None:
        JSONImporter(preset.data).import_node_settings(node)
示例#12
0
def _import_json(json_file):
    JSONImporter.init_from_path(json_file).import_into_tree(
        _create_node_tree())