Hi!
I am trying to make grasshopper components using ghpython.
In the script, I need to change data type from GUID to point3d.
Can anyone tell how to convert data type by python script?
Thank you
Hi!
I am trying to make grasshopper components using ghpython.
In the script, I need to change data type from GUID to point3d.
Can anyone tell how to convert data type by python script?
Thank you
I think you should be able to use
pt = rs.coerce3dpoint(guid)
this looks up the associated point object with the specified Guid in the active document, and returns its location in the variable pt
How about the opposite? I am creating a point3D, but trying to delete it later on in the script after it is no longer needed as a visual prompt.
rs.DeleteObjects(âŚ) of course needs the GUIDs to be abe to delete them, but I canât find what routine to run to find an existing pointâs GUID.
EDIT - I got it. When i referenced my point through another variable, it returned the point co-ordinates. referencing the original AddPoint meant I could get the GUID.
@menno - I am having issues with choosing a point from the active doc and returning a point3d object with this method as the method returns a None when I enter a the guid. Thank you in advance.
import Rhino as rg
import rhinoscriptsyntax as rs
guid = rs.GetObjects('pick please')
pt = rs.coerce3dpoint(guid)
print pt
GetObjects() returns a list, even if you just pick a single object. coerce3dpoint does not accept lists as input, so you need to either:
guid=rs.GetObject("Pick a point", 1)
#can use rs.filter.point instead of 1 above
or
guids=rs.GetObjects("Pick some points", 1)
pts=[rs.coerce3dpoint(guid) for guid in guids]
or
guids=rs.GetObjects("Pick some points", 1)
pt_list=rs.coerce3dpointlist(guids)
Note that a 3dpointlist is not quite the same as a python list of Rhino point3d objects thoughâŚ
@Helvetosaur - Thanks and sorry for the delay, I didnât notice this. (should have looked at the docs more closely). Makes sense with the for loop.
Nevertheless, it still doesnât seem to work:
import Rhino.Geometry as rg
import rhinoscriptsyntax as rsobj = rs.GetObject(âselect objectâ)
rg_obj = rs.coerce3dpoint(obj)
plane = rg.Plane.WorldXYcircle = rg.Circle(rg, 50)
print = circle
This prints: âRhino.Geometry.Circleâ
Furthermore, I have been trying to recreate ârs.getobjectâ and ârs.getobjectsâ with rhino common using:
Rhino.Custom.Input as well as the 'GetOneObject but am not getting any workflow unless I use more lines of code.
Here is my attempt and it is not workingâŚ
import Rhino
from Rhino import *
from scriptcontext import docfilter = Rhino.DocObjects.ObjectType.Point
rc, obj_ref = Rhino.Input.RhinoGet.GetOneObject(âSelect pointâ, False, filter)point = obj_ref.Point()
point3 = doc.Objects.AddPoint(point3d)
I donât understand a few things:
Thank you!
I think you want this instead:
circle = rg.Circle(rg_obj, 50)
It simply returns True of False if the function succeeded. So if rc is True, your obj_ref should actually contain something. If it is False, the function failed for some reason.
import Rhino
from Rhino import *
from scriptcontext import doc
filter = Rhino.DocObjects.ObjectType.Point
rc, obj_ref = Rhino.Input.RhinoGet.GetOneObject("Select point", False, filter)
point = obj_ref.Point()
point3 = doc.Objects.AddPoint(point.Location)
obj_ref.Point() returns a âPointâ object, not a Point3d object⌠Itâs a funny construct. point.Location gets you the actual Point3d object.
Unfortunately, donât have time for much more this morning - gotta go.
@Helvetosaur - Thanks for the info. This is not a fairly straightforward implementation. I understand now why the rhinoscriptsyntax function is easier to work with.
As such, I have decided to throw out the RhinoCommon input methods for the moment and just âcoerceâ whatever geometry with rhinoscriptsyntax.
Anyway, I am using both together; however, when performing multiple operations, I wouldnât think that I would need to draw the object in the active doc with
doc.Objects.AddCircleâŚetcâŚ
As you can in this script, I cannot figure out how to delete the points used to move the circles:
import Rhino as rg
import rhinoscriptsyntax as rs
from scriptcontext import doc
def CreateCircle():
obj = rs.GetObject(âselect pointâ)
pt = rs.coerce3dpoint(obj)for i in range(0, 10, 2): xform = rg.Geometry.Transform.Translation(i, 0, 0) point_ref = doc.Objects.Transform(obj, xform, False) pts = rs.coerce3dpoint(point_ref) circle = rg.Geometry.Circle(pts, 5) obj_ref = doc.Objects.AddCircle(circle) doc.Views.Redraw()
CreateCircle()
When I set the boolean of the Translation method to âTrueâ the circles are created staggered. When false, they are equally spaced.
Thanks for all the help!
Well, again, I donât have much time this morning - have a class and I still need to prepare some stuff. But I think you are confusing the point object in the document - which you see on the screen - with a RhinoCommon point3d object which is just a virtual point. The point object you see in the document is not needed to place the circles, so it shouldnât be necessary to delete itâŚ
Also, the reason why the circles are staggered is that transforms of RhinoCommon objects are generally âin placeâ, that is to say they donât make copies, but transform the original object. So each time the original object is getting translated by i * the number of loops, so the translation distance growsâŚ
So maybe you want something like this?
import Rhino
import rhinoscriptsyntax as rs
import scriptcontext as sc
def CreateCircles():
obj = rs.GetObject('select a point')
pt = rs.coerce3dpoint(obj)
dist=2
for i in range(10):
xform = Rhino.Geometry.Transform.Translation(dist,0,0)
pt.Transform(xform)
circle = Rhino.Geometry.Circle(pt, 5)
obj_ref = sc.doc.Objects.AddCircle(circle)
sc.doc.Views.Redraw()
CreateCircles()
Sorry, I canât do much more todayâŚ
Edit - I was somewhat sleepy this morning and didnât have more than a few minutes⌠Class over now, so I also wanted to clarify a couple more thingsâŚ
First, it seems you are picking a point object just to get its point coordinates. Just in case, you can also pick a point directly on the screen - with or without osnaps - by using GetPoint()
instead of GetObject (point_filter)
Second, you really donât actually need transform the origin point in this case either - you could also just build your center points directly in the loopâŚ
import Rhino
import rhinoscriptsyntax as rs
import scriptcontext as sc
def CreateCircles():
pick_pt = rs.GetPoint("Pick start point")
if pick_pt:
dist=2
circle_ids=[]
for i in range(10):
pt=Rhino.Geometry.Point3d(pick_pt.X+i*dist,pick_pt.Y,pick_pt.Z)
circle = Rhino.Geometry.Circle(pt, 5)
circle_ids.append(sc.doc.Objects.AddCircle(circle))
sc.doc.Views.Redraw()
CreateCircles()
Thanks Helvetosaur,
This makes sense to me now. I appreciate all the help!
Hey Jonathan,
I have met the same question. But how can I reference the point through another variable? I am a little confused about your answer. can you do a little more explanation?
Thank you so much!
Hi @zmaomao981,
I donât understand your question. Do you have some sample code that isnât working for you?
â Dale