How to convert GUID to Point3D by python script

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

3 Likes

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.

2 Likes

@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
1 Like

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…

4 Likes

@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 rs

obj = rs.GetObject(‘select object’)

rg_obj = rs.coerce3dpoint(obj)
plane = rg.Plane.WorldXY

circle = 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 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(point3d)

I don’t understand a few things:

  1. what is the variable ‘rc’?
  2. how can I convert the tuple that is returned into a point3d?

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()
2 Likes

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