Skip to content

Instantly share code, notes, and snippets.

@zeffii
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeffii/3bf0414c2ba895658abc to your computer and use it in GitHub Desktop.
Save zeffii/3bf0414c2ba895658abc to your computer and use it in GitHub Desktop.
import bpy
import json
import pprint
import re
ng = bpy.data.node_groups[0]
nodes = ng.nodes
# store unsorted, order is important later
layout_dict = {}
not_linked = lambda s: not hasattr(s, 'links') or not s.links
for node in nodes:
node_dict = {}
node_items = {}
for k, v in node.items():
if isinstance(v, (float, int, str)):
node_items[k] = v
else:
node_items[k] = v[:]
node_dict['params'] = node_items
# inputs to this node is limited to one per socket.
node_incoming = {}
for i in node.inputs:
if not_linked(i):
continue
link = i.links[0]
n = link.from_node.name
s = link.from_socket.name
node_incoming[i.name] = [n, s]
node_dict['incoming'] = node_incoming
node_outgoing = {}
for o in node.outputs:
if not_linked(o):
continue
connections = []
for link in o.links:
n = link.to_node.name
s = link.to_socket.name
connections.append([n, s])
node_outgoing[o.name] = connections
node_dict['outgoing'] = node_outgoing
node_dict['location'] = node.location[:]
node_dict['bl_idname'] = node.bl_idname
layout_dict[node.name] = node_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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment