def execute(self, context): ng = context.space_data.node_tree is_tree_exportable, msg = self.can_be_exported(ng) if not is_tree_exportable: self.report({'ERROR'}, msg) return {'CANCELLED'} gist_filename = ng.name app_version = bpy.app.version_string.replace(" ", "") time_stamp = strftime("%Y.%m.%d | %H:%M", localtime()) license = 'license: CC BY-SA' gist_description = f"Sverchok.{version_and_sha} | Blender.{app_version} | {ng.name} | {time_stamp} | {license}" # layout_dict = create_dict_of_tree(ng, skip_set={}, selected=self.selected_only) if self.selected_only: layout_dict = JSONExporter.get_nodes_structure( [node for node in ng.nodes if node.select]) else: layout_dict = JSONExporter.get_tree_structure(ng) try: gist_body = json.dumps(layout_dict, sort_keys=True, indent=2) except Exception as err: if 'not JSON serializable' in repr(err): error(layout_dict) exception(err) self.report({'WARNING'}, "See terminal/Command prompt for printout of error") return {'CANCELLED'} try: gist_url = sv_gist_tools.main_upload_function(gist_filename, gist_description, gist_body, show_browser=False) if not gist_url: self.report( {'ERROR'}, "You have not specified GitHub API access token, which is " + "required to create gists from Sverchok. Please see " + TOKEN_HELP_URL + " for more information.") return {'CANCELLED'} context.window_manager.clipboard = gist_url # full destination url info(gist_url) self.report({'WARNING'}, "Copied gist URL to clipboad") sv_gist_tools.write_or_append_datafiles(gist_url, gist_filename) return {'FINISHED'} except Exception as err: exception(err) self.report({'ERROR'}, "Error 222: net connection or github login failed!") return {'CANCELLED'}
def execute(self, context): ng = bpy.data.node_groups[self.id_tree] is_tree_exportable, msg = self.can_be_exported(ng) if not is_tree_exportable: self.report({'ERROR'}, msg) return {'CANCELLED'} destination_path = self.filepath if not destination_path.lower().endswith('.json'): destination_path += '.json' # future: should check if filepath is a folder or ends in \ if self.selected_only: layout_dict = JSONExporter.get_nodes_structure( [node for node in ng.nodes if node.select]) else: layout_dict = JSONExporter.get_tree_structure(ng) if not layout_dict: msg = 'no update list found - didn\'t export' self.report({"WARNING"}, msg) warning(msg) return {'CANCELLED'} json.dump(layout_dict, open(destination_path, 'w'), sort_keys=True, indent=2) msg = 'exported to: ' + destination_path self.report({"INFO"}, msg) info(msg) if self.compress: comp_mode = zipfile.ZIP_DEFLATED # destination path = /a../b../c../somename.json base = basename(destination_path) # somename.json basedir = dirname(destination_path) # /a../b../c../ # somename.zip final_archivename = base.replace('.json', '') + '.zip' # /a../b../c../somename.zip fullpath = os.path.join(basedir, final_archivename) with zipfile.ZipFile(fullpath, 'w', compression=comp_mode) as myzip: myzip.write(destination_path, arcname=base) info('wrote:', final_archivename) return {'FINISHED'}
def test_profile_export(self): try: upgrade_nodes.upgrade_nodes(self.tree) except: traceback.print_exc() export_result = JSONExporter.get_tree_structure(self.tree) importer = JSONImporter(export_result) importer.import_into_tree(self.tree, print_log=False) if importer.has_fails: raise (ImportError(importer.fail_massage))
def execute(self, context): if len(context.space_data.path) > 1: self.report({"WARNING"}, "Export is not supported inside node groups") return {'CANCELLED'} ng = bpy.data.node_groups[self.id_tree] destination_path = self.filepath if not destination_path.lower().endswith('.json'): destination_path += '.json' # future: should check if filepath is a folder or ends in \ layout_dict = JSONExporter.get_tree_structure(ng, self.selected_only) if not layout_dict: msg = 'no update list found - didn\'t export' self.report({"WARNING"}, msg) warning(msg) return {'CANCELLED'} indent = None if self.compact else 2 json.dump(layout_dict, open(destination_path, 'w'), indent=indent) # json_struct doesn't expect sort_keys = True msg = 'exported to: ' + destination_path self.report({"INFO"}, msg) info(msg) if self.compress: comp_mode = zipfile.ZIP_DEFLATED # destination path = /a../b../c../somename.json base = basename(destination_path) # somename.json basedir = dirname(destination_path) # /a../b../c../ # somename.zip final_archivename = base.replace('.json', '') + '.zip' # /a../b../c../somename.zip fullpath = os.path.join(basedir, final_archivename) with zipfile.ZipFile(fullpath, 'w', compression=comp_mode) as myzip: myzip.write(destination_path, arcname=base) info('wrote:', final_archivename) return {'FINISHED'}
def execute(self, context): if not self.id_tree: msg = "Node tree is not specified" error(msg) self.report({'ERROR'}, msg) return {'CANCELLED'} if not self.preset_name: msg = "Preset name is not specified" error(msg) self.report({'ERROR'}, msg) return {'CANCELLED'} ng = bpy.data.node_groups[self.id_tree] nodes = list(filter(lambda n: n.select, ng.nodes)) if not len(nodes): msg = "There are no selected nodes to export" error(msg) self.report({'ERROR'}, msg) return {'CANCELLED'} # the operator can be used for both preset importing of a node and preset from a bunch of selected nodes if self.is_node_preset: layout_dict = JSONExporter.get_node_structure(nodes[0]) else: layout_dict = JSONExporter.get_tree_structure(ng, True) preset = SvPreset(name=self.preset_name, category=self.category) preset.make_add_operator() destination_path = preset.path json.dump(layout_dict, open(destination_path, 'w'), indent=2) # sort keys is not expected by the exporter msg = 'exported to: ' + destination_path self.report({"INFO"}, msg) info(msg) return {'FINISHED'}
def test_export_import_all_nodes(self): for node_class in iter_classes_from_module(sverchok.nodes, [bpy.types.Node]): if node_class.bl_idname in self.known_troubles: continue try: create_node(node_class.bl_idname, self.tree.name) except RuntimeError: # the node probably was not registered for missing dependencies pass else: tree_structure = None with self.subTest(type='EXPORT', node=node_class.bl_idname): tree_structure = JSONExporter.get_tree_structure(self.tree) if tree_structure is not None: with self.subTest(type='IMPORT', node=node_class.bl_idname): importer = JSONImporter(tree_structure) importer.import_into_tree(self.tree, print_log=False) if importer.has_fails: raise (ImportError(importer.fail_massage)) finally: # you have to clean tree by yourself self.tree.nodes.clear()
def test_list_join_node_import(self): export_result = JSONExporter.get_tree_structure(self.tree) importer = JSONImporter(export_result) importer.import_into_tree(self.tree, print_log=False) if importer.has_fails: raise (ImportError(importer.fail_massage))