Rhino7 GHPython Runtime Error: Unable to add line

I’m new to GHPython and getting the following error:

Runtime error (PythonException): Unable to add line to document

Traceback:
line 482, in AddLine, “C:\Users\Leo\AppData\Roaming\McNeel\Rhinoceros\7.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib\rhinoscript\curve.py”
line 70, in script

The problematic line of code:


path = rs.AddLine(point_last, point_current)

Any suggestions?

I’ve searched forums, but not found any solutions.

Are you able to upload the rest of your code as I don’t know the context of the variables.

Thanks for your reply Christopher. Here’s the Grasshopper file I’m working on:

g-code paths.gh (6.6 KB)

Are you sure you have the correct file as the python code runs and your line is missing.
main issue I can see is that you are in grasshopper space rather than Rhino space.
Please read the following:

1 Like

Thanks again Christopher. I’ve read through those posts but I’m not yet understanding how the ghpython context is working.

I tried to re-upload my gh file to the one that is not working.

I don’t understand how my code is breaking the simple example listed in the Developer Docs https://developer.rhino3d.com/guides/rhinopython/ghpython-question-answer/


import rhinoscriptsyntax as rs

line = rs.AddLine((1, 2, 3), (10, 11, 12))
a = line

What am I doing which stops AddLine from working?

Simply put:
you need to use script context to target Rhino before rhinoscriptsyntax can create objects in Rhino.
Afterwards, the control is returned back to grasshopper.

import Rhino
import rhinoscriptsyntax as rs
import scriptcontext as sc

sc.doc = Rhino.RhinoDoc.ActiveDoc
line = rs.AddLine((1, 2, 3), (10, 11, 12))
a = line
sc.doc = ghdoc
1 Like

idk whether this caused the problem: you may check whether two points are too close, when the line is too tiny, it will show this kind of error :slight_smile:

1 Like

Hi Leo, the two points you are trying to make a line from are both (0,0,0). Which the rhinoscriptsyntax function appears to consider invalid. You can implement Rhino.Geometry.Point3d directly if you do indeed need a line with the same start and end point:

Edit: Here’s a quick example:

201217_InvalidLineRhinoCommon_GHPython_00.gh (1.7 KB)

1 Like

I appreciate your answer Christopher, but I’m afraid I still don’t understand it.

Why are there so many simple examples of ghpython scripts which draw lines but only have

import rhinoscriptsyntax as rs

and don’t require the context switching which you’ve recommended?

Thanks very much Anders!

Your example makes very much sense to me and I will be able to clean up my code to prevent it from generating lines with zero length.

1 Like

Aha! :bulb:

Now I understand the direction you were trying to guide me in @christopher.ho

Once I got the lines drawing correctly I then realized they were being drawn directly in Rhino, whereas I just wanted to generate lines in the Grasshopper preview.

I added your

import scriptcontext as sc
sc.doc = ghdoc

and voila! My lines are just in the Grasshopper preview.

Thanks again!

If you use rhinocommon in Grasshopper, instead of rhinoscriptsyntax, you can forget about coercion issues, geometry getting falsely baked to Rhino, switching contexts, etc.
Simply look how simple and elegant @AndersDeleuran has solved this!

It really isn’t much more difficult than using rhinoscriptsyntax, and you can do exactly the same, since rhinoscriptsyntax only wraps rhinocommon in a pseudo-userfriendly fashion.
You can look at each of its functions like this, if you want to retrace how something is done, that you don’t know how to:

import rhinoscriptsyntax as rs
import inspect

print inspect.getsource(rs.AddLine)

This great functionality was pointed out by @Dancergraham a while back.
It outputs the source code of the rs.AddLine, like so:

def AddLine(start, end):
    """Adds a line curve to the current model.
    Parameters:
      start, end (point|guid) end points of the line
    Returns:
      guid: id of the new curve object
    Example:
      import rhinoscriptsyntax as rs
      start = rs.GetPoint("Start of line")
      if start:
          end = rs.GetPoint("End of line")
          if end: rs.AddLine(start, end)
    See Also:
      CurveEndPoint
      CurveStartPoint
      IsLine
    """
    start = rhutil.coerce3dpoint(start, True)
    end = rhutil.coerce3dpoint(end, True)
    rc = scriptcontext.doc.Objects.AddLine(start, end)
    if rc==System.Guid.Empty: raise Exception("Unable to add line to document")
    scriptcontext.doc.Views.Redraw()
    return rc

You can clearly see how many hoops it has to jump through to just to produce the geometry in Rhino, which you don’t even need at this moment or ever in Grasshopper (at least if you stick to the usual baking workflow). This also means a worse performance than using rhinocommon, with which you can abstract and simplify things much more.

import Rhino.Geometry as rg

pt1 = rg.Point3d(0, 1, 0)
pt2 = rg.Point3d(0, 1, 0)
line = rg.Line(pt1, pt2)

if not line.IsValid:
    raise RuntimeError("This is not a valid line")

# Output
a = rg.LineCurve(line)

The entire API is pretty well documented here.

4 Likes