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 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'} layout_dict = JSONExporter.get_nodes_structure([n for n in ng.nodes if n.select]) 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'), sort_keys=True, indent=2) msg = 'exported to: ' + destination_path self.report({"INFO"}, msg) info(msg) return {'FINISHED'}
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()
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 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 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 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))