How to duplicate instance by python

Hi guys,
I’d like to ask how to duplicate a defined instance?

for example:
I created a mesh instance with python script, and successfully display it in Rhino viewport. But when I run script again to generate another cube instance, it generate nothing in the viewport. I thought it was the reason of block index conflicting and then move to try to duplicate the instance.

But the duplicate method for normal geometry is not working, I’m able to duplicate the instance reference geometry, but it was not displayed in the viewport.

How could I duplicate instance properly?

Here’s a demonstration on who to duplicate instances, though I’m not entirely certain if duplicating it is really what you should be doing :

import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino
def create_instance():
    box_id = rs.AddBox([(0,0,0), (1,0,0), (1,1,0), (0,1,0), (0,0,1), (1,0,1), (1,1,1), (0,1,1)])
    box = rs.coercegeometry(box_id)
    instance_definition_name = "BoxInstance"
    instance_definition = sc.doc.InstanceDefinitions.Find(instance_definition_name)
    # If the block definition does not exist, create a new one
    if instance_definition is None:
        instance_definition = sc.doc.InstanceDefinitions.Add(instance_definition_name, "A box instance definition", Rhino.Geometry.Point3d.Origin, [box])
    # Create an instance of the instance definition
    instance = rs.InsertBlock(instance_definition_name, (0,0,0))
    return instance
def duplicate_instance(instance):
    instance_definition_name = rs.BlockInstanceName(instance)
    new_instance = rs.InsertBlock(instance_definition_name, (0,0,0))
    return new_instance
instance = create_instance()

Hi, Farouk,

Thanks for the quick reply, I’ll dig into this code.

In my case, I’ll need hundreds of the same mesh objects array on model, I think in this condition, using instance is the efficient way.

Hi Fmyl
“You can make a copy by using the GUID. Like this.”

new_id = Rhino.RhinoDoc.ActiveDoc.Objects.Duplicate(instance);
new_object = Rhino.RhinoDoc.ActiveDoc.Objects.Transform(new_id, Rhino.Geometry.Transform.ZeroTransformation,False)