Import one files objects into another

Hi,

Can anyone recommend and easy way to import one file into another? I have the following and want to import all objects from a different file into an existing open file. My code is as follows but I’m getting a ‘parameter must be a Guid’ error.

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

import_path = “insert path…”

rhino_file = Rhino.FileIO.File3dm().Read(import_path)

rh_objects = rhino_file.Objects
rs.SelectObjects(rh_objects)
sc.doc.Objects.Add(rh_objects)

Hi @happysad ,
If you are using Rhino 7, then you could use
sc.doc.Import(import_path)

or, you can run a script,
Rhino.RhinoApp.RunScript("_-Import "+import_path+" Enter",False)

this is easier than adding objects from File3dm, because then you need to take care of the “layers”, or any other attributes before adding any objects, and also block definitions.

A very easy way is just to use Grasshopper (then you don’t have to worry about your Python script running in two different contexts or storing the objects in it, e.g. as component state).

Open the Rhino file to import from.
Select all the objects in a geom param object.
Right click and internalise the data to make a copy of the objects in Grasshopper.
Open the Rhino file to import into.
Back in Grasshopper, bake the objects in the geom param.

thanks this was exactly what i was after. :slightly_smiling_face:

1 Like

Hi Darryl,
I see that sc.doc.Import(import_path) asks me a question on import so I found Rhino.RhinoDoc.ReadFile(import_path, FileReadOptions) instead and that doesn’t ask questions I don’t want.

import Rhino
import_path = rs.OpenFileName("file to import")
if import_path:
    FileReadOptions = Rhino.FileIO.FileReadOptions()
    FileReadOptions.ScaleGeometry = True
    FileData = Rhino.RhinoDoc.ReadFile(import_path, FileReadOptions)

But how do I get a list of the stuff imported? All it returns is a Bool. The objects are in the file, but I can’t find them through code. So I made a workaround, but it is the best way? I feel there should be a faster and easier way.

import Rhino
import scriptcontext as sc
import rhinoscriptsyntax as rs

objects = sc.doc.ActiveDoc.Objects
objects_before = []
for o in objects:
    objects_before.append(o.Id)

import_path = rs.OpenFileName("file to import")
if import_path:
    FileReadOptions = Rhino.FileIO.FileReadOptions()
    FileReadOptions.ScaleGeometry = True
    FileData = Rhino.RhinoDoc.ReadFile(import_path, FileReadOptions)

objects = sc.doc.ActiveDoc.Objects
newObjects = []
for o in objects:
    if not o.Id in objects_before:
        newObjects.append(o.Id)

if len(newObjects)>0:
    rs.SelectObjects(newObjects)

Hi @Holo,

if you already have some objects before importing, you could do something like below:

import scriptcontext
import rhinoscriptsyntax as rs

# get most recent object in document
obj = scriptcontext.doc.Objects.MostRecentObject()

# use ReadFile 
# TODO

# get all new rhino objects 
rh_objs = scriptcontext.doc.Objects.AllObjectsSince(obj.RuntimeSerialNumber)
if rh_objs: rs.SelectObjects(rh_objs)   

_
c.

1 Like