mcneel / computeclient_js

javascript client library for compute.rhino3d.com
MIT License
7 stars 1 forks source link

Add grasshopper functionality to js compute client #2

Closed pearswj closed 4 years ago

pearswj commented 4 years ago

Node.js example

compute = require('./compute.rhino3d.js')
fs = require('fs')

compute.url = 'https://staging.compute.rhino3d.com/'
compute.authToken = 'Bearer' + process.env.RHINO_COMPUTE_TOKEN

var buffer = fs.readFileSync('leland.gh')
var definition = new Uint8Array(buffer) // works for both .gh and .ghx

var tree = new compute.Grasshopper.DataTree('RH_IN:106:0001')
tree.append([0], ['Lorem ipsum dolor sit amet'])

compute.Grasshopper.evaluateDefinition(definition, [tree]).then(res => {
    console.log(JSON.stringify(res))
}).catch(err => {
    throw err
})

Browser example

<html>
    <head>
        <script src="compute.rhino3d.js"></script>
        <script>
            RhinoCompute.url = 'https://staging.compute.rhino3d.com/'
            RhinoCompute.authToken = RhinoCompute.getAuthToken()

            var tree = new RhinoCompute.Grasshopper.DataTree('RH_IN:106:0001')
            tree.append([0], ['Lorem ipsum dolor sit amet'])
            var trees = [tree]

            fetch('leland.gh')

            // .gh
            .then(response => response.arrayBuffer())
            .then(buffer => new Uint8Array(buffer))

            // .ghx
            // .then(response => response.text())

            .then(definition => RhinoCompute.Grasshopper.evaluateDefinition(definition, [tree]))
            .then(res => console.log(JSON.stringify(res)))
            .catch(err => { throw err })
        </script>
    </head>
</html>

Example Grasshopper defintion – leland.gh.zip