Skip to content

Commit

Permalink
Add grasshopper client support
Browse files Browse the repository at this point in the history
  • Loading branch information
sbaer committed Oct 10, 2019
1 parent 12163b7 commit daf4615
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 11 deletions.
51 changes: 51 additions & 0 deletions compute_rhino3d/Grasshopper.py
@@ -0,0 +1,51 @@
from . import Util
import base64
import os


class DataTree:
def __init__(self, name):
self.data = {'ParamName': name, 'InnerTree': {}}

def Append(self, path, items):
"""
Append a path to this tree
Args:
path (iter): a list of integers defining a path
items (list): list of data to add to the tree
"""
key = ''
for item in path:
if len(key) == 0:
key = '{}'.format(item)
continue
key = '{};{}'.format(key, item)

self.data['InnerTree'][key] = [{'data': item} for item in items]


def EvaluateDefinition(definition, trees):
"""
Evaluate a grasshopper definition on the compute server.
Args:
definition (str): path to a grasshopper defition
trees (iter): list of DataTree instances
Returns:
"""
url = "grasshopper"
args = {'algo': None, 'pointer': None, 'values': None}
if definition.startswith('http:') or definition.startswith('https:'):
args['pointer'] = definition
else:
if os.path.isfile(definition):
with open(definition, 'r') as content_file:
definition = content_file.read()
encoded = base64.b64encode(definition.encode('utf-8'))
args['algo'] = str(encoded, 'utf-8')

args['values'] = [tree.data for tree in trees]
response = Util.ComputeFetch(url, args)
return response

27 changes: 16 additions & 11 deletions compute_rhino3d/Util.py
Expand Up @@ -2,10 +2,10 @@
import json
import requests

__version__ = '0.8.0'
__version__ = '0.9.0'

url = "https://compute.rhino3d.com/"
authToken = None
authToken = ''
stopat = 0


Expand All @@ -23,7 +23,7 @@ def default(self, o):
if(posturl.find('?')>0): posturl += '&stopat='
else: posturl += '?stopat='
posturl += str(stopat)
postdata = json.dumps(arglist, cls = __Rhino3dmEncoder)
postdata = json.dumps(arglist, cls=__Rhino3dmEncoder)
headers = {
'Authorization': 'Bearer ' + authToken,
'User-Agent': 'compute.rhino3d.py/' + __version__
Expand All @@ -32,23 +32,28 @@ def default(self, o):
return r.json()


def PythonEvaluate(script, input_, output_names):
def PythonEvaluate(script, inputs, output_names):
"""
Evaluate a python script on the compute server. The script can reference an
`input` parameter which is passed as a dictionary. The script also has access
to an 'output' parameter which is returned from the server.
`input` parameter which is passed as a dictionary. The script also has
access to an 'output' parameter which is returned from the server.
Args:
script (str): the python script to evaluate
input_ (dict): dictionary of data passed to the server for use by the script as an input variable
output_names (list): list of strings defining which variables in the script to return
inputs (dict): dictionary of data passed to the server for use by the
script as an input variable
output_names (list): list of strings defining which variables in the
script to return
Returns:
dict: The script has access to an output dict variable that it can fill with values.
This information is returned from the server to the client.
dict: The script has access to an output dict variable that it can
fill with values. This information is returned from the server to
the client.
"""
encodedInput = rhino3dm.ArchivableDictionary.EncodeDict(input_)
encodedInput = rhino3dm.ArchivableDictionary.EncodeDict(inputs)
url = "rhino/python/evaluate"
args = [script, json.dumps(encodedInput), output_names]
response = ComputeFetch(url, args)
output = rhino3dm.ArchivableDictionary.DecodeDict(json.loads(response))
return output


0 comments on commit daf4615

Please sign in to comment.