clement
November 12, 2019, 2:24am
1
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.
dale
(Dale Fugier)
November 12, 2019, 7:00pm
2
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
clement
November 12, 2019, 7:35pm
3
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.
dale
(Dale Fugier)
November 12, 2019, 7:45pm
4
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
clement
November 12, 2019, 8:00pm
5
Hi @dale , thank you. This is effective.
_
c.
李汉祥
(李汉祥)
June 30, 2022, 7:22am
6
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
Liu2
(Liu)
January 11, 2024, 8:25am
8
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?
Dale Fugier:
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
(Dale Fugier)
January 11, 2024, 5:01pm
9
Hi @Liu2 ,
See the attached.
cmdTestImportObj.cpp (3.4 KB)
– Dale
Liu2
(Liu)
January 12, 2024, 5:09am
10
Thank you very much, It helps a lot.