Last active
August 29, 2015 14:06
-
-
Save zeffii/3bf0414c2ba895658abc to your computer and use it in GitHub Desktop.
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 | |
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