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
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)
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.
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.