Help getting started with Rhino.inside

I am trying rhino.inside in a Python program and having a problem getting started. Not surprisingly, the Rhino Common documentation is not intended for Python and that is going to be a constant challenge. My first task is to load a mesh that I can work with.

Running the Python program from a terminal was painful because of the long time to load Rhino every time. I decided that running it from a Rhino Script Editor would let me work out the logic and figure out Rhino Common much faster. I think that is correct, right? The code should be the same except the import?

The following code is what I have so far. The error is when I try to create a Rhino.FileIO.FileObj object. My code looks just like what the documentation says the constructor should look like. I think…

Any suggestions?

As soon as this works, I am going to need an object that represents this mesh. A Rhino.Geometry.Mesh object. Not sure how to get that reference.

Where are the examples of using rhino.inside with Python? I only found one very simple example.

import System
import Rhino

# Remember that this is in the script editor until it works properly

# document = Rhino.RhinoDoc()
document = Rhino.RhinoDoc.CreateHeadless('')

# Set the options that will be used to import an OBJ file
fileReadOptions = Rhino.FileIO.FileReadOptions()
fileReadOptions.BatchMode = True
fileReadOptions.ImportMode = True
fileReadOptions.ScaleGeometry = False
fileReadOptions.UseScaleGeometry = False

objReadOptions = Rhino.FileIO.FileObjReadOptions(fileReadOptions)
objReadOptions.DisplayColorFromObjMaterial = False
objReadOptions.IgnoreTextures = True
objReadOptions.MapYtoZ = False
objReadOptions.ReverseGroupOrder = False
objReadOptions.Split32BitTextures = False

objReadOptions.UseObjGroupsAs  = Rhino.FileIO.FileObjReadOptions.UseObjGsAs.ObjGroupsAsObjects

objReadOptions.UseObjObjectsAs = Rhino.FileIO.FileObjReadOptions.UseObjOsAs.IgnoreObjObjects

# Read the obj file in
filePath = "C:/Users/…../Rhino Inside Python/Test Mesh.obj"

objFile = Rhino.FileIO.FileObj()
result = objFile.Read(filePath, document, objReadOptions )
print(f'Result of reading the OBJ file is {result}')

Things to try inside Rhino first.py (1.1 KB)

If anyone stumbles on this post, there is an example attached that shows how to load an OBJ file (and get basic information) using Python and rhino.inside

I feel like I am the only person on the planet using rhino.inside without Revit.

Please make sure to edit the path to the OBJ file or Rhino will present you with a screen full of error messages.

RhinoInside Import Obj Example.py (2.9 KB)

There are some different posts on this:

Scott - please don’t take this personally but getting information about Rhino.Inside is very frustrating.

You linked to a single post that has 138 comments over six years. It is a tree with no branches! It is mostly about people having difficulties getting it to run.

When I click on the Rhino.Inside button at the top of this post I thought that I would see posts and discussion about Rhino.Inside. Logical, right? Nope. The first fifty posts are all about Revit - except two. It turns out that McNeel has “Rhino.Inside” that might be used with Python and “Rhino.Inside.Revit” which is specifically for another application. Two development efforts that are named almost the same… and that makes it about impossible to search for anything useful.

In your own post you say to visit https://www.rhino3d.com/inside to get information about Rhino.Inside but it links to Rhino.Inside.Revit instead.

This is why it is difficult and frustrating.

Oh, sorry I misread your original message.

So you are in Rhino and want to use Python. The steps you are looking for are to:

  1. read in an OBJ file
  2. Then see what meshes it contains.
  3. And then be able to manipulate that mesh.

Does this code work better for you as a start?

import Rhino
import scriptcontext as sc
import rhinoscriptsyntax as rs

def ImportObj(file_path_name):
    read_options = Rhino.FileIO.FileReadOptions()
    read_options.BatchMode = True
    read_options.ImportMode = True
    obj_options = Rhino.FileIO.FileObjReadOptions(read_options)
    obj_options.MapYtoZ = False
    
    sn_start = Rhino.DocObjects.RhinoObject.NextRuntimeSerialNumber
    
    rc = Rhino.FileIO.FileObj.Read(file_path_name, sc.doc, obj_options)
    if not rc: 
        print "Failed to import file"
        return
    sc.doc.Views.Redraw()
    
    sn_end = Rhino.DocObjects.RhinoObject.NextRuntimeSerialNumber
    
    new_ids = []
    for sn in range(sn_start, sn_end):
        rh_obj = sc.doc.Objects.Find(sn)
        if rh_obj:
            print(get_object_type_by_id(rh_obj))
            new_ids.append(rh_obj.Id)
    return new_ids

def get_object_type_by_id(object_id):
    if object_id:
        object_type_value = rs.ObjectType(object_id)
        if object_type_value is not None:
            print("Object type value: {object_type_value}")
            # You can also use the built-in constants for comparison
            if object_type_value == rs.filter.point:
                print("Object is a point")
            elif object_type_value == rs.filter.curve:
                print("Object is a curve")
            elif object_type_value == rs.filter.mesh:
                print("Object is a mesh")
            # ... and so on for other types
        else:
            print("Could not determine object type.")
    else:
        print("Invalid object ID.")

file_path = r"F:\Users\scottd\Documents\content cache\DXFS\1.obj"
ImportObj(file_path)

Scott -

I think it is more like using Rhino in Python than Python in Rhino :slight_smile:

Your example code is similar to the code I posted just above yours. Either one would be a good starting point if someone needs to do something similar.

BTW, I referred to your post this morning regarding Code-Driven File IO. Thank you for creating that, it was very helpful.

I have spent a lot of time looking for information on using Rhino.Inside with Python. I suppose that is mostly using RhinoCommon inside Python. The best resource has been the “source code” of the RhinoScriptSyntax functions. The rs functions are pretty well documented and contain reasonable examples - all written from a Python point of view. I used this for years before starting with RhinoCommon. For example, if want to change an objects layer in RhinoCommon I can look for what rs.ObjectLayer() is doing behind the curtain. This has really helped.

Lastly, Google directed me towards this page titled “Using RhinoCommon from Python”. This seemed perfect! Except that it was only one page long. And when you followed the link back to “Rhino.Python Guides” there was no link to the page from there. It appears to be a web page just floating around your server. On the bright side, someone saw the need for providing a guide for customers to use RhinoCommon from Python.

Thanks again for your help.