Created
September 23, 2014 18:49
-
-
Save zeffii/0758af1191138455419d to your computer and use it in GitHub Desktop.
import / export node_group for sverchok
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
import json | |
import pprint | |
import re | |
from bpy.types import EnumProperty | |
def find_enumerators(node): | |
ignored_enums = ['bl_icon', 'bl_static_type', 'type'] | |
node_props = node.bl_rna.properties[:] | |
f = filter(lambda p: isinstance(p, EnumProperty), node_props) | |
return [p.identifier for p in f if not p.identifier in ignored_enums] | |
def compile_socket(link): | |
return (link.from_node.name, | |
link.from_socket.name, | |
link.to_node.name, | |
link.to_socket.name) | |
ng = bpy.data.node_groups['NodeTree'] | |
nodes = ng.nodes | |
layout_dict = {} | |
nodes_dict = {} | |
for node in nodes: | |
node_dict = {} | |
node_items = {} | |
node_enums = find_enumerators(node) | |
for k, v in node.items(): | |
if isinstance(v, (float, int, str)): | |
node_items[k] = v | |
else: | |
node_items[k] = v[:] | |
# post step for enum, will overwrite an int value | |
if k in node_enums: | |
v = getattr(node, k) | |
node_items[k] = v | |
node_dict['params'] = node_items | |
node_dict['location'] = node.location[:] | |
node_dict['bl_idname'] = node.bl_idname | |
node_dict['height'] = node.height | |
node_dict['width'] = node.width | |
node_dict['label'] = node.label | |
node_dict['hide'] = node.hide | |
custom_color_state = False | |
if hasattr(node, 'use_custom_color'): | |
custom_color_state = node.use_custom_color | |
node_dict['custom_color'] = custom_color_state | |
node_dict['color'] = node.color[:] | |
nodes_dict[node.name] = node_dict | |
layout_dict['nodes'] = nodes_dict | |
links = (compile_socket(l) for l in ng.links) | |
connections_dict = {idx: link for idx, link in enumerate(links)} | |
print(connections_dict) | |
layout_dict['connections'] = connections_dict | |
m = json.dumps(layout_dict, sort_keys=True, indent=2) | |
# optional post processing step | |
post_processing = False | |
if post_processing: | |
flatten = lambda match: r' {}'.format(match.group(1), m) | |
m = re.sub(r'\s\s+(\d+)', flatten , m) | |
with open('node_tree.json', 'w') as node_tree: | |
node_tree.writelines(m) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
import json | |
import os | |
import ast | |
def resolve_socket(from_node, from_socket, to_node, to_socket): | |
return (ng.nodes[from_node].outputs[from_socket], | |
ng.nodes[to_node].inputs[to_socket]) | |
filename = 'node_tree.json' | |
location = 'C:\\blender_trunk' | |
fullpath = os.path.join(location, filename) | |
with open(fullpath) as fp: | |
nodes_json = json.load(fp) | |
ng_params = {'name': '44', 'type': 'SverchCustomTreeType'} | |
ng = bpy.data.node_groups.new(**ng_params) | |
nodes = ng.nodes | |
''' first import all. ''' | |
nodes_to_import = nodes_json['nodes'] | |
for n in sorted(nodes_to_import): | |
node_ref = nodes_to_import[n] | |
print('adding {0}'.format(n)) | |
bl_idname = node_ref['bl_idname'] | |
node = nodes.new(bl_idname) | |
if not node.name == n: | |
node.name = n | |
node.location = node_ref['location'] | |
params = node_ref['params'] | |
for p in params: | |
# val = ast.literal_eval(params[p]) | |
val = params[p] | |
setattr(node, p, val) | |
node.height = node_ref['height'] | |
node.width = node_ref['width'] | |
node.label = node_ref['label'] | |
node.hide = node_ref['hide'] | |
#if node_ref['use_custom_color']: | |
# node['use_custom_color'] = True | |
node.color = node_ref['color'] | |
'''now connect them ''' | |
connections = nodes_json['connections'] | |
for idx, link in connections.items(): | |
ng.links.new(*resolve_socket(*link)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment