hi will, I had the same problem. Could you please give a hand?
The code is as follows:
import compute_rhino3d.Util
import compute_rhino3d.Grasshopper as gh
import rhino3dm
import json
compute_rhino3d.Util.authToken = ‘http://localhost:8081/ ’
pt1 = rhino3dm.Point3d(0, 0, 0)
circle = rhino3dm.Circle(pt1, 5)
angle = 20
curve = json.dumps(circle.ToNurbsCurve().Encode())
curve_tree = gh.DataTree(“RH_IN:curve”)
curve_tree.Append([0], [curve])
rotate_tree = gh.DataTree(“RH_IN:rotate”)
rotate_tree.Append([0], [angle])
trees = [curve_tree, rotate_tree]
output = gh.EvaluateDefinition(‘workshop_step5.ghx’, trees)
branch = output[‘values’][0][‘InnerTree’][‘{ 0; }’]
lines = [rhino3dm.CommonObject.Decode(json.loads(item[‘data’])) for item in branch]
filename = ‘workshop_step5.3dm’
print(‘Writing {} lines to {}’.format(len(lines), filename))
model = rhino3dm.File3dm()
for l in lines:
model.Objects.AddCurve(l) # they’re actually LineCurves…
And the error is as bellow:
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
will
(Will Pearson)
March 1, 2021, 4:09pm
3
Try replacing compute_rhino3d.Util.authToken = 'http://localhost:8081/'
with compute_rhino3d.Util.url = 'http://localhost:8081/'
.
Here’s the details of the exception, found by catching the error and setting a breakpoint.
Here’s a full working sample:
import compute_rhino3d.Util
import compute_rhino3d.Grasshopper as gh
import rhino3dm
import json
compute_rhino3d.Util.url= 'http://localhost:8081/'
pt1 = rhino3dm.Point3d(0, 0, 0)
circle = rhino3dm.Circle(pt1, 5)
angle = 20
curve = json.dumps(circle.ToNurbsCurve().Encode())
curve_tree = gh.DataTree('RH_IN:curve')
curve_tree.Append([0], [curve])
rotate_tree = gh.DataTree('RH_IN:rotate')
rotate_tree.Append([0], [angle])
trees = [curve_tree, rotate_tree]
try:
output = gh.EvaluateDefinition('workshop_step5.ghx', trees)
except Exception as err:
print(err)
branch = output['values'][0]['InnerTree']['{ 0; }']
lines = [rhino3dm.CommonObject.Decode(json.loads(item['data'])) for item in branch]
filename = 'workshop_step5.3dm'
print('Writing {} lines to {}'.format(len(lines), filename))
model = rhino3dm.File3dm()
for l in lines:
model.Objects.AddCurve(l) # they’re actually LineCurves…
1 Like
Sorry to disturb you again…
I changed the url as you suggest, while there’s another error occurred:
KeyError Traceback (most recent call last)
in
22 print(err)
23
—> 24 branch = output[‘values’][0][‘InnerTree’][‘{ 0; }’]
25 lines = [rhino3dm.CommonObject.Decode(json.loads(item[‘data’])) for item in branch]
26
KeyError: ‘values’
This is the message in the compute.geometry.exe board:
will
(Will Pearson)
March 4, 2021, 11:11am
6
Did you try saving the file as .gh instead of .ghx?
Are you sending a .ghx file? My guess is that’s the server doesn’t like the BOM at the beginning of the .ghx (XML) file. Try sending the file as a .gh (binary) instead.
Yes, I did it as you suggested:
import compute_rhino3d.Util
import compute_rhino3d.Grasshopper as gh
import rhino3dm
import json
compute_rhino3d.Util.url= 'http://localhost:8081/'
pt1 = rhino3dm.Point3d(0, 0, 0)
circle = rhino3dm.Circle(pt1, 5)
angle = 20
curve = json.dumps(circle.ToNurbsCurve().Encode())
curve_tree = gh.DataTree('RH_IN:curve')
curve_tree.Append([0], [curve])
rotate_tree = gh.DataTree('RH_IN:rotate')
rotate_tree.Append([0], [angle])
trees = [curve_tree, rotate_tree]
try:
output = gh.EvaluateDefinition('workshop_step5.gh', trees)
except Exception as err:
print(err)
print(output)
branch = output['values'][0]['InnerTree']['{ 0; }']
lines = [rhino3dm.CommonObject.Decode(json.loads(item['data'])) for item in branch]
filename = 'workshop_step5_test.3dm'
print('Writing {} lines to {}'.format(len(lines), filename))
model = rhino3dm.File3dm()
for l in lines:
model.Objects.AddCurve(l) # they’re actually LineCurves…
model.Write(filename)
if I print ‘output’, the error showed as below:
{‘statusCode’: 500, ‘message’: ‘Something went horribly, horribly wrong while servicing your request.’, ‘details’: ‘Nancy.RequestExecutionException: Oh noes! —> System.Xml.XmlException: 根级别上的数据无效。 第 1 行,位置 1。\r\n 在 System.Xml.XmlTextReaderImpl.Throw(Exception e)\r\n 在 System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()\r\n 在 System.Xml.XmlTextReaderImpl.ParseDocumentContent()\r\n 在 System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)\r\n 在 System.Xml.XmlDocument.Load(XmlReader reader)\r\n 在 System.Xml.XmlDocument.LoadXml(String xml)\r\n 在 GH_IO.Serialization.GH_Archive.Deserialize_Xml(String xmlContent)\r\n 在 compute.geometry.GrasshopperDefinition.ArchiveFromBase64String(String blob) 位置 C:\Users\SHU\Downloads\compute.rhino3d-master\src\compute.geometry\GrasshopperDefinition.cs:行号 910\r\n 在 compute.geometry.GrasshopperDefinition.FromBase64String(String data) 位置 C:\Users\SHU\Downloads\compute.rhino3d-master\src\compute.geometry\GrasshopperDefinition.cs:行号 104\r\n 在 compute.geometry.ResthopperEndpointsModule.Grasshopper(NancyContext ctx) 位置 C:\Users\SHU\Downloads\compute.rhino3d-master\src\compute.geometry\ResthopperEndpoints.cs:行号 93\r\n 在 compute.geometry.ResthopperEndpointsModule.<.ctor>b__0_0(Object _) 位置 C:\Users\SHU\Downloads\compute.rhino3d-master\src\compute.geometry\ResthopperEndpoints.cs:行号 23\r\n 在 Nancy.Routing.Route.<>c__DisplayClass4.b__3(Object parameters, CancellationToken context)\r\n — 内部异常堆栈跟踪的结尾 —\r\n 在 Nancy.NancyEngine.InvokeOnErrorHook(NancyContext context, ErrorPipeline pipeline, Exception ex)’}
KeyError Traceback (most recent call last)
<ipython-input-6-1e2b6d5bf90b> in <module>
24 print(output)
25
---> 26 branch = output['values'][0]['InnerTree']['{ 0; }']
27 lines = [rhino3dm.CommonObject.Decode(json.loads(item['data'])) for item in branch]
28
KeyError: 'values'
will
(Will Pearson)
March 4, 2021, 3:05pm
8
Hmm, same error. Did you open the file in Grasshopper and re-save it to .gh, or just rename the file?
Oh, I think I made a very silly mistake. What you said made me just realized there should be a GH file. While, I didn’t download it before. I regarded this line of code means to form a new GH file. So, could you please send me the GH file for test?
Maybe… It will be better if there’s some introduction about the compute_rhino3d.Grasshopper library in the API documentation website Welcome to compute.rhino3d.py’s documentation! — compute_rhino3d 0.12.1 documentation
will
(Will Pearson)
March 4, 2021, 7:17pm
10
I’ve added a full working sample to our developer samples repository.
2 Likes
Sorry Will, for now, the example code you provided runs well. Then, I built a new GH file myself for testing, and tried to modify its input parameters through python. However, the same error occurred. I checked many times, and could not debug. Could you helps to find where the mistake is?
I have uploaded the new testing file.Thanks for your kind help.
https://github.com/xiaohehuanshu/Rhino3dcompute_test
The error is as bellow:
KeyError Traceback (most recent call last)
<ipython-input-10-12f01296a465> in <module>
35
36 # decode results
---> 37 branch = output['values'][0]['InnerTree']['{ 0; }']
38
39
KeyError: 'values'