Skip to content

Instantly share code, notes, and snippets.

Created September 23, 2014 18:49
Show Gist options
  • Save zeffii/0758af1191138455419d to your computer and use it in GitHub Desktop.
Save zeffii/0758af1191138455419d to your computer and use it in GitHub Desktop.
import / export node_group for sverchok
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 =[:]
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 (,,,
ng =['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
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_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)}
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(, m)
m = re.sub(r'\s\s+(\d+)', flatten , m)
with open('node_tree.json', 'w') as node_tree:
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],
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 =**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 =
if not == n: = 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():*resolve_socket(*link))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment