FileObj.Read how to get imported objects or ids?

Hi @dale,

i’ve used Rhino.FileObj.Read to import an obj file. So far the meshes come in but how do i get the objects or object ids after reading ? The method just returns a boolean. I’ve tried rs.LastCreatedObjects() but this returned an empty list. Any hints ?

_
c.

Hi @clement,

The rs.LastCreatedObjects method only works when scripting Rhino commands using rs.Command.

Perhaps you can post some code that isn’t working the way you want and we can help?

– Dale

Hi @dale,

please see the example file below. The file_path needs to be adjusted to test:

Example.py (812 Bytes)

For now i get all obj ids before the import and compare with the ids after import. Is there maybe a less expensive way to find the imported objects ?

_
c.

Hi @clement,

How about this?

import Rhino
import scriptcontext as sc

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(rh_obj.Id)
            new_ids.append(rh_obj.Id)
    return new_ids

file_path = r"C:\Users\Dale\Desktop\test.obj"
ImportObj(file_path)

– Dale

1 Like

Hi @dale, thank you. This is effective.
_
c.

hi,
I am now confusing about how to read a .stl file and reduce the meth and finally output it, I use the FileObj.Read(), but I don’t know how to reduce the mesh after reading.
I also use the CommonObject.FromBase64String but it keeps telling me that CommonObject.FromBase64String, even if I just use Convert.ToBase64String() to convert the file

Hi @李汉祥,

there is a Mesh.Reduce method in Rhino common which you can use:

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

_
c.

Hi @dale .
How to get the rhino object when a file is imported by RunScript()?
I can’t get the mesh object (ON_Mesh*) by code below in importing a wavefront .obj file.

    ON_wString script;
	script.Format(L"_-Import \"%ls\" _Enter", static_cast<const wchar_t*>(tempFilename));
	RhinoApp().RunScript(RhinoApp().ActiveDoc()->RuntimeSerialNumber(), static_cast<const wchar_t*>(script), 0);

Is LastCreatedObjects() has a C++ version?

Or could you please post a C++ version of your Python code in importing a wavefront OBJ file?

Hi @Liu2,

See the attached.

cmdTestImportObj.cpp (3.4 KB)

– Dale

Thank you very much, It helps a lot.