Python Import Named Objects from an External Rhino File

Hi All,

Wondering if it is at all possible to import a given named object(s) from an external file (always the same file path) into a current file with python?

I haven’t used much outside of rhinoscriptsyntax, though see sc. methods being used and imagine I would require to use these methods?

Any help would be greatly appreciated

Edit: Have used import before, albeit through the rs.command(import) - which doesn’t give me much flexibility

Hi Tom,

I made an example that uses Rhino.FileIo to read the 3dm file , find the objects with desired name and adds them to the current document.

Let me know if it is not clear or if you have further questions.
-Willem

import_by_name.py (1.0 KB)

import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino
import os


#for testing get file next to this file named test_import.3dm
import_path = os.path.join(rs.DocumentPath(), 'test_import.3dm')

#read the 3dm 
rhino_file = Rhino.FileIO.File3dm().Read(import_path)

#get table of objects in the 3dm file
rh_objects = rhino_file.Objects

#find objects in de 3dm with the name you need eg 'TestName'
name_to_find = 'TestName'
named_objects = []
for rh_object in rh_objects:
    if rh_object.Name == name_to_find:
        named_objects.append(rh_object)

#add the found objects to the document
imported_ids = []
for name_object in named_objects:
    #NOTE: passing the attributes here will not take care of possible file specific attributes
    #such as layers, linetypes etc... these will have to be created separately and set to the newly created object
    new_id = sc.doc.Objects.Add(name_object.Geometry , name_object.Attributes)
    imported_ids.append(new_id)
        
rs.SelectObjects(imported_ids)
1 Like

Hi Willem

Many thanks for this. It is appreciated. Though if I were to specify a specific folder, where abouts would I add the path?

Furthermore, is there any resource on the Rhino or scriptcontextt methods? I use rhinoscriptsyntax all the time but seldom the other two.

Thank you once again

Hi Tom,

the path can also be set as just a string: ( notice the double backslashes this is because the backslash is an escape character)

import_path = 'C:\\Users\\Willem\\Desktop\\new_file.3dm'

I’m not sure where to find good documentation on the scriptcontext module
Maybe @stevebaer has a good resource for scriptcontext documentation?

In short it is a module providing access to the context the script is running in.
Most notably access to the current document and all objects and data in it.
For instance all the objects, layers etc… but also settings like for instance tolerance settings.

to get the current file tolerance you can:

To find my way in RhinoCommon I use the online documentation:
https://developer.rhino3d.com/api/RhinoCommon/html/N_Rhino.htm

For example the RhinoDoc:
https://developer.rhino3d.com/api/RhinoCommon/html/T_Rhino_RhinoDoc.htm

Does this help?

-Willem

1 Like

Willem’s writeup on scriptcontext is pretty darn good. A he pointed out, it is mostly used just to get access to the document that the script should be working with.

Steve, Willem, thank you ever so much.

This is very helpful of you pair