Grasshopper

algorithmic modeling for Rhino

Hi there!

I recently tried to hatch something with the help of Python in Grasshopper. I know, that I will only see the result once I baked it.

But I ask myself, if it is even possible to do so? Because GH gives me strange console-messages with e.g. this code:

import Rhino
import rhinoscriptsyntax as rs
circle = rs.AddCircle(rs.WorldXYPlane(), 10.0)
rs.AddHatch( circle, "Raster" )

The "test" output in the script editor:

Runtime error (MissingMemberException): 'GrasshopperDocument' object has no attribute 'HatchPatterns'
Traceback:
  line 39, in AddHatches, "C:\Users\***\AppData\Roaming\McNeel\Rhinoceros\5.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib\rhinoscript\hatch.py"
  line 19, in AddHatch, "C:\Users\***\AppData\Roaming\McNeel\Rhinoceros\5.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib\rhinoscript\hatch.py"
  line 6, in script

What's the Problem here?

Views: 3555

Replies to This Discussion

Looks like a bug in rhinoscriptsyntax. However, you can get around it by just using regular RhinoCommon calls in python:


import Rhino.Geometry as rg
import Rhino.RhinoDoc as RhinoDocument
circle = rg.Circle(rg.Plane.WorldXY,10)
curvelist = []
curvelist.append(rg.ArcCurve(circle))
hatch = rg.Hatch.Create(curvelist,0,0,1)
for h in hatch:
RhinoDocument.ActiveDoc.Objects.Add(h)

Hi guys,

this is not a bug in rhinoscriptsyntax. It is just a design choice in the implementation of Grasshopper. GH does not have a native Hatch datatype (Param), so it it just not possible to have a GH-compatible hatch as stand-alone object, even if invisible. I could create that Param, but it would go beyond the scope of the GhPython add-on.

It is, however, absolutely possible to create hatches in RhinoCommon as Andrew is noting.


Here is the good news part.

You can always switch to the Rhino document and use Hatches in every rhinoscript-supported way, directly. Also in GhPython. This is an example:

 

import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino

if active:
    # let's get the GH geometry and set Rhino's doc as target
    geom = rs.coercegeometry(G)
    sc.doc = Rhino.RhinoDoc.ActiveDoc

    # we have to add the geometry to Rhino
    new_id = sc.doc.Objects.Add(geom)

    rs.AddHatch(new_id, N)

    # we can delete the geometry we used
    rs.DeleteObject(new_id)

    # let's put back everything
    sc.doc = ghdoc

Giulio
--

Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

Attachments:

Thank you for clearing it up!

Hi Giulio,

Looks like you can't add a hatch pattern that hasn't been previously added to the document's HatchPattern's table (by using Rhino's command "Hatch").
An example:

pattern = ["Solid", "Hatch1", "Hatch2", "Hatch3", "HatchDash", "Grid", "Grid60", "Plus", "Squares"]
index = sc.doc.HatchPatterns.Find(pattern[4], True)


It does not matter which hatch pattern one choses, the HatchPatterns.Find method will always return -1, if the chosen hatch pattern hasn't been added previously through Rhino's "Hatch" command. It's logically to say that you can't find something that hasn't been added to a table, but still this means we can't add new hatch through RhinoCommon without that one already being added to Rhino document.
Or did I get all this wrong?

I'll check how that works. I don't know now.

Giulio
--
Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

The idea is that you can read them as existing objects from another 'master' file that will be the Hatch master definition.

rs.AddHatchPatterns() in rhinoscriptsyntax and

Rhino.DocObjects.HatchPattern.ReadFromFile() in RhinoCommon

work for these purposes.


If you need a new patter, you could create AUTOCAD-compatible .pat files from scratch and import those, as explained here: http://wiki.mcneel.com/rhino/hatchpatterns

import rhinoscriptsyntax as rs

filename = 'VASTRAP.pat'
newnames = rs.AddHatchPatterns(filename, True)
if newnames: newnames = newnames[0] #first
else: newnames = filename #already existing, or error

crv = rs.GetObject(filter=rs.filter.curve)
rs.AddHatch(crv, newnames)

EDIT:

Regarding IDs, @djordje, I think that your understanding is correct. In order to find your pattern, it needs to be in the document. Please note that running the _Hatch command and selecting anything will add the pattern to the document.

I suggest the use of rs. functions to work with patterns, as they more easily show how to deal with them. Especially,

print rs.HatchPatternNames()

...is very useful.

I hope this helps,

Giulio
--
Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

Thank you. Still if feels kinda strange, that one could not directly use 9 default hatch patterns in RhinoCommon without them being actually added to the Rhino document first (of course we could script this with Rhino.RhinoApp.RunScript("_-Hatch ...", True)).
I also noticed entering Options->Annotation->Hatch, and then closing the window (OK), automatically adds all 9 default hatch patterns to hatch pattern table.

I fully agree, Djordje. I have reported the behavior of the _Hatch command dialog as a bug, but the developer of that command will probably be able to tell more.

Thanks a lot for your help with this,

Giulio
--
Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

Ah, okay, that makes sense. Thanks Giulio!

Interesting note here: Your definition will work fine one time.

But (I tried it now several times) if I change the input via making e.g. some new circles in rhino Python will just go red alert and stop :(

The error is line 11:

Runtime error (ArgumentNullException): Der Wert darf nicht NULL sein. [[: the value can't be NULL]]
Parametername: geometry

I just don't get it.

Thank you for answering, Andrew!

Funny enough, I tried to rewrite your great "hatch by density" script in Python. (Sadly it doesn't work anymore in 0.9.0075)

Also, thanks for the good work with your "Human" Plugin. Such useful! ;)

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service