Grasshopper

algorithmic modeling for Rhino

Been puzzled with something related to ghdoc and RhinoDoc.

When I code in ghPython component, I am always targeting the ghdoc? Right? That means that if I want to add something to Rhino, I need to target the RhinoDoc?


Here is a small example of trying to add the sphere from ghpython component to RhinoDoc:

import rhinoscriptsyntax as rs
import Rhino
import scriptcontext as sc

sphere = Rhino.Geometry.Sphere(rs.coerce3dpoint([4,1,0]), 2.0)

if add:
    # target the Rhino Doc - why does this whole code work, even when I comment-out this line?
    #sc.doc = Rhino.RhinoDoc.ActiveDoc

    # add object to Rhino Doc?
    rc = sc.doc.Objects.AddSphere(sphere)



What I do not understand is why does the upper code work, even when I comment out this line:

sc.doc = Rhino.RhinoDoc.ActiveDoc

Isn't this line essential, as it quits targeting the ghdoc, and starts targeting the RhinoDoc? Or did I get all this wrong?


Thanks.

Views: 2614

Attachments:

Replies to This Discussion

I think it is because you have already set scriptcontext.doc to Rhino.RhinoDoc.ActiveDoc once, without setting it back to ghdoc afterwards. Setting scriptcontext.doc is a persistent call, which sorta makes sense since you're fiddling with scriptcontext. Hope that makes sense?

This is the reason. I would usually suggest to set back the variable to ghdoc, once you are done. This will have less possible side effects, but otherwise, just use this as you see fit!

Thanks,

Giulio

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

Thank you. Both of you.

One more thing:
I noticed the difference when adding to ghdoc and RhinoDoc:

In case of ghdoc, I could add geometry of what ever type, using sc.doc.Objects.Add():

sc.doc = ghdoc
add = sc.doc.Objects.Add(sphere_obj)

In case of RhinoDoc I can't. I have to be specific. For example, this does not work:

sc.doc = Rhino.RhinoDoc.ActiveDoc
add = sc.doc.Objects.Add(sphere_obj)   #raises the "expected GeometryBase, got Sphere" error.

But this does:

sc.doc = Rhino.RhinoDoc.ActiveDoc
add = sc.doc.Objects.AddSphere(sphere_obj)

Am I doing something wrong, or these differences truly exist?

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