Use RhinoCommon(python) to select all objects in Rhino file

I’m sure this is incredibly simple but I haven’t been able to get it to work…

I have a Rhino file with several objects (points, breps, lines, etc.). I’d like to be able to access these objects in a python component using RhinoCommon, so that I can use them as if they had been referenced in Grasshopper (i.e. I can Transform them, visualize them, or call any of the usual methods associated with their relevant class).

I’ve tried things like Rhino.DocObjects.BrepObject.Geometry, but that just returns a IronPython.Runtime.Types.ReflectedProperty, not the geometry. I’ve also tried Rhino.DocObjects.ObjRef() and entered the guid as the argument, but I can’t seem to format the string as a guid… Any help would be greatly appreciated.

Thanks!

Hi @jason.danforth,
All objects in the document are stored in the ObjectTable (Rhino.DocObjects.Tables.ObjectTable). If you just want to select them, you can loop through them and use ObjectTable.Select() method

import Rhino

for rObj in Rhino.RhinoDoc.ActiveDoc.Objects:
    if rObj.IsNormal:
        Rhino.RhinoDoc.ActiveDoc.Objects.Select(rObj.Id,True)

rObj.IsNormal condition is used assuming that you want to acces objects that are not hidden or locked.

you can acess the underlying geometry of objects from RhinoObject.Geometry property. if you want to do something to the geometry, then

import Rhino

for rObj in Rhino.RhinoDoc.ActiveDoc.Objects:
    if rObj.IsNormal:
        if(rObj.ObjectType == Rhino.DocObjects.ObjectType.Brep):
            brep= rObj.Geometry
            ##do something for breps
        elif(rObj.ObjectType == Rhino.DocObjects.ObjectType.Curve):
            curve = rObj.Geometry
            ##Do somthing for curves
2 Likes

@Darryl_Menezes,

Thanks so much. This works great. I have two follow-up questions to further my understanding in general:

  1. I’m a little confused about the documentation. Rhino.RhinoDoc.ActiveDoc is listed as a property, without a description of the Objects method. How would I have figured out your suggestion on my own from reading the docs?

  2. What’s the best way to access individual objects by their GUID, assuming I want to operate on the object and not just select it?

Thanks for you help!

  1. I’m not sure what exactly you want to know. If you are totally new to RhinoCommon, then it would take some time to go through the API. You just have to experiment. RhinoDoc.ActiveDoc is a static property of RhinoDoc, which is an instance of the current active Rhino document you are working with. RhinoDoc.Objects is not a method, it is a property of the RhinoDoc instance

  2. You must loop through objects in ObjectTable

import Rhino
for rObj in Rhino.RhinoDoc.ActiveDoc.Objects:
    id = rObj.Id

Thanks so much @Darryl_Menezes. I didn’t realize RhinoDoc.ActiveDoc was an instance of RhinoDoc, so I read RhinoDoc.ActiveDoc.Objects as a property of a property (which was confusing). It’s fairly obvious now…

Re: question 2, I found what I was missing in this thread. I’ll leave my code below for the next noob trying to access a specific geometry object by GUID:

import Rhino
import System

guid_string = 'b64fa0bd-28f0-4894-baa7-ae1937d3e124'
guid = System.Guid.Parse(guid_string)

for rObj in Rhino.RhinoDoc.ActiveDoc.Objects:
    if rObj.Id == guid:
        my_brep = rObj.Geometry

a = my_brep

Thanks for getting me sorted out!

Hi, Do we have anything similar to this for C# scripting through RhinoCommon?
Or any leads?

I have this mess with me that I am trying to figure out.

            ObjRef obj_ref;
            var rc = RhinoGet.GetOneObject("Select object", false, ObjectType.AnyObject, out obj_ref);
            if (rc != Result.Success)
                return rc;
            if (obj_ref == null)
                return Result.Nothing;

            var guid = obj_ref.ObjectId;      

            RhinoObject rObj = RhinoDoc.ActiveDoc.Objects;

            if (rObj.Id == guid)
            { GeometryBase my_brep = rObj.Geometry;

                var a = my_brep;
             }

@raghav.pathak

See if this helps. You can also use the FindId object route vs looping the entire object table.

  var guid_string = "b64fa0bd-28f0-4894-baa7-ae1937d3e124";
  var is_guid = Guid.TryParse(guid_string,out var guid);
  if (!is_guid) return Result.Cancel;
  
  var rhino_object = doc.Objects.FindId(guid);
  if (null == rhino_object) return Result.Cancel;

  var geometry = rhino_object.Geometry;
  // do stuff with geometry;

Hi

Thanks for your reply.

I tried with this and get the error "Error CS1061 ‘object’ does not contain a definition for ‘TryParse’ and no accessible extension method ‘TryParse’ accepting a first argument of type ‘object’ could be found (are you missing a using directive or an assembly reference?) "

Apart from that, I also want to know if you could give some leads for what I am trying to do. I intend to get a bounding box for the object in the geometry window, it could be a line, surface, or brep. I want to select it automatically and get the bounding box coordinates for further operations.

Any leads will be appreciated.
Thanks again

If you’re new to .net and c# development you may find these examples helpful to get you started.

Hi
Thanks for your reply.
I was using the Samples link that you sent, particularly SampleCsBoundingBox.cs file.
I am not quite sure how to extract the values of the coordinates of this bounding box. Right now, the code assigns the values using true/false to the box_corners array. But, what I want to do is know the coordinates of bounding box and then scale them to make the box bigger or smaller.
Can you give some idea about extracting the numeric values from this boolean type system?
Thanks
Raghav

You can find all of the available properties and methods for bounding boxes in the RhinoCommon help. You may find GetCorners helpful as well as Inflate in the link below.

https://developer.rhino3d.com/api/RhinoCommon/html/T_Rhino_Geometry_BoundingBox.htm