Skip to content

Instantly share code, notes, and snippets.

@notionparallax
Created May 9, 2017 02:32
Show Gist options
  • Save notionparallax/8824b58799bd066eee79d909a978c5b4 to your computer and use it in GitHub Desktop.
Save notionparallax/8824b58799bd066eee79d909a978c5b4 to your computer and use it in GitHub Desktop.
Describes the topology of a grasshopper file
"""Describe the topology of the current document."""
ghdoc = ghenv.Component.OnPingDocument()
o = ghdoc.Objects
nodes = ""
connections = ""
lookup = {}
EDGE = ' {} -> {};\n'
NODE = ' {name}_{id} [shape=box,label="{name}"];\n'
def describe_thing(t):
"""Build a list of nodes for the graph AND make a lookup table."""
global nodes
global connections
if t.NickName == "":
t.NickName = t.Name
lookup[str(t.InstanceGuid)] = t.NickName
thing_id = str(t.InstanceGuid).replace("-", "")
nodes += NODE.format(name=t.NickName,
id=thing_id)
def join_up(t):
global connections
print "\n----", t.Name
try:
print "Params.input"
for in_param in t.Params.Input:
# print in_param.Sources[0].InstanceGuid
if len(in_param.Sources) > 0:
for source in in_param.Sources:
if t.NickName == "":
t.NickName = t.Name
a = '{name}_{id}'.format(name=t.NickName,
id=nice_id(t.InstanceGuid))
that_name = lookup[str(source.InstanceGuid)]
b = '{name}_{id}'.format(name=that_name,
id=nice_id(source.InstanceGuid))
c = EDGE.format(b, a)
print "c:", c
connections += c
else:
print "root node"
print "Params.Output"
for out in t.Params.Output:
# print "\t", out
# print "\tRecipients", out.Recipients
if len(out.Recipients) > 0:
for recipient in out.Recipients:
if t.NickName == "":
t.NickName = t.Name
a = '{name}_{id}'.format(name=t.NickName,
id=nice_id(t.InstanceGuid))
that_name = lookup[str(recipient.InstanceGuid)]
b = '{name}_{id}'.format(name=that_name,
id=nice_id(recipient.InstanceGuid))
c = EDGE.format(a, b)
print "c:", c
connections += c
else:
print "terminal node"
except Exception as e:
print e
def nice_id(not_nice_id):
return str(not_nice_id).replace("-", "")
print "start describing the doc"
for node in o:
describe_thing(node)
print "lookup", lookup
for node in o:
join_up(node)
dot_graph = """
digraph R {{
rankdir=LR
{n}
{c}
}}
""".format(n=nodes, c=connections)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment