Skip to content

Commit e74e42b

Browse files
authored
Merge pull request #1024 from compas-dev/new-rpc-component
Grasshopper component for RPC calls
2 parents 54b02bd + d200dcc commit e74e42b

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
* Added `Polyline.extend`, `Polyline.extended`, `Polyline.shorten`, `Polyline.shortened`.
1414
* Added `Data.sha256` for computing a hash value of data objects, for example for comparisons during version control.
1515
* Added optional `path` parameter to `compas.rpc.Proxy` to allow for non-package calls.
16+
* Added Grasshopper component to call RPC functions.
1617

1718
### Changed
1819

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
Remote Procedure Call: to invoke Python functions outside of Rhino, in the context of the CPython interpreter.
3+
"""
4+
from ghpythonlib.componentbase import executingcomponent as component
5+
6+
from compas.rpc import Proxy
7+
8+
9+
class CompasRpcCall(component):
10+
def RunScript(self, module, function, parameters, path, restart):
11+
if not (module and function):
12+
return
13+
14+
if restart:
15+
proxy = Proxy(max_conn_attempts=1)
16+
proxy.stop_server()
17+
18+
proxy = Proxy(module, path=path)
19+
fn = getattr(proxy, function)
20+
parameters = parameters or []
21+
22+
try:
23+
result = fn(*parameters)
24+
except Exception:
25+
self.Message = "Error! Check details in stacktrace"
26+
raise
27+
28+
self.Message = ""
29+
30+
return result
1.28 KB
Loading
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"name": "RPC Call",
3+
"nickname": "RPC",
4+
"category": "COMPAS",
5+
"subcategory": "RPC",
6+
"description": "Remote Procedure Call: to invoke Python functions outside of Rhino, in the context of the CPython interpreter.",
7+
"exposure": 2,
8+
9+
"ghpython": {
10+
"isAdvancedMode": true,
11+
"iconDisplay": 2,
12+
"inputParameters": [
13+
{
14+
"name": "module",
15+
"description": "Module name to import.",
16+
"typeHintID": "str"
17+
},
18+
{
19+
"name": "function",
20+
"description": "Function name to call.",
21+
"typeHintID": "str"
22+
},
23+
{
24+
"name": "parameters",
25+
"description": "List of parameters to pass to the function.",
26+
"scriptParamAccess": "list"
27+
},
28+
{
29+
"name": "path",
30+
"description": "Optional. Path where the module can be found (if not a pip installed module).",
31+
"typeHintID": "str"
32+
},
33+
{
34+
"name": "restart",
35+
"description": "If True, try to restart the RPC server before calling it.",
36+
"typeHintID": "bool"
37+
}
38+
],
39+
"outputParameters": [
40+
{
41+
"name": "result",
42+
"description": "Return value of the function call."
43+
}
44+
]
45+
}
46+
}

0 commit comments

Comments
 (0)