Creating a mesh object via GUID

I was wondering if someone could clear up something conceptually for me. I have been scripting in Rhino for a while via python but strictly within the rhinoscriptsyntax space. When venturing into the Rhino namespace I have a hard time getting things done. I assume its because its based off of C++ methodology and I havent studied that before. Anyways, I am trying to perform a quadremesh via Rhino.Geometry.SubD.CreateFromMesh(mesh). I can get it to work when I explicitly prompt the user to select a mesh. But lets say I have multiple meshs withing a file, but all named differently. Normally what I would do is create a list with rs.allobjects(), then loop threw and see which object has a matching name with an if statement and rs.objectname. But that gets me the GUID not the mesh object itself. How would I go about doing that ?

Thanks

You can use rs.coercemesh(mesh_id) to get the mesh that’s referenced by the GUID.
rhinoscriptsyntax is just sugar on top of RhinoCommon, meaning a Python library that’s at least meant to make life easier. It’s all functions that wrap API code.

1 Like

Hi @Derek_Comeau,

i would strongly adivice to work with Rhino objects and get this from your Id, this way you can access everything, the object, it’s properties, it’s Id and the mesh geometry eg:

import Rhino
import scriptcontext
import rhinoscriptsyntax as rs

def DoSomething():
    
    obj_id = rs.GetObject("Select a mesh", rs.filter.mesh, True, False)
    if obj_id: 
        rh_obj = rs.coercerhinoobject(obj_id, True, True)
        
        msg = "Rhino object: {}  Object Id: {}  Mesh geometry: {}"
        print msg.format(rh_obj, rh_obj.Id, rh_obj.Geometry)
        
if __name__=="__main__":
    DoSomething()

_
c.

@clement Thanks for the reply ! Well the whole point is for me not to engage with the script at all. I dont want to select a mesh manually.

Hi @Derek_Comeau,

yes that is clear, i just wanted to demonstrate how to obtain the Rhino object from the Id using rs.coercerhinoobject compared to using rs.coercemesh. Using the latter, you get a mesh (the geometry) but this has no reference to the original Id. Once you make some changes to the mesh geometry and want to replace the original Rhino object with the changed mesh, you’ll need the Id.

The Rhino object therefore is a better way to work with as it gives access to everything.

_
c.

1 Like

I see thanks for this. I am going to do some testing today. I am really excited to finally explore outside of rhinoscriptsyntax. I cant seem to find coercemesh or coercerhinoobject in the documentation. Do you know where it is located ?

There isn’t any really. The ‘coerce’ methods were created as utility functions and not originally designed to be exposed to the public in the way the other rhinoscriptsyntax methods are.

If you look in your rhinoscriptsyntax library here:

C:\Users\<username>\AppData\Roaming\McNeel\Rhinoceros\7.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib\rhinoscript

and open utility.py in a text editor, you will find all of them, and their comment sections explain a little bit about them.

Basically they can take diverse input and try to return the desired object type. For example rs.coerce3dpoint() can accept a Rhino point object’s GUID, a vector, a string or a list of 3 numbers and return a 3dPoint object (if it finds valid data that can be converted).

1 Like

Awesome this was exactly what I was interested in. Thats really handy to have. Is there a method/attribute to go from object to GUID? or would I go about it a more traditional way.

Yes. I assume that you mean you have created one or more ‘virtual’ objects via RhinoCommon - which do not exist in the document, only in memory space. So you need to add them to the document, and when you do, a unique GUID is generated - all objects in a Rhino document have a GUID.

The most commonly used ‘bridge’ between RhinoCommon and the document is via the scriptcontext module. The scriptcontext module allows you easy access to all the tables in the document that have objects in them - not only geometry objects but also layers, views, materials, etc. scriptcontext.doc is basically a shortcut for Rhino.RhinoDoc.ActiveDoc

For example if you have created some sort of curve object via RhinoCommon, you can add it to the document via:

crv_guid=scriptcontext.doc.Objects.AddCurve(your_curve_object)

If the addition of the curve is successful, the variable crv_guid will contain the curve’s GUID for future reference.

1 Like

Isn’t that strange, given that it’s a question that comes up very regularly? And such a common operation one would think (to add an object that a scripter has poured effort into to create).