Hello, I am writing a mesh exporter and in some cases face instancing is used to instantiate children objects but I cannot find a way to get a list of these instances.

(This is what I mean by face instancing.)

This is how the hierarchy looks like, I would like to get a list of instances of Plane.001 so that I can export them as proper meshes with all necessary transforms set.
You might get some clues from the OBJ exporter, as I get correct results with that with face instancing. It’s scripts/addons/io_scene_obj/export_obj.py
.
Indeed, I think I found some code that could help me, I am going to try it and see if it works.
Something like this, I guess:
Cube: 7 objects
Suzanne: 1 objects
Edit: might want to filter out the instancer itself
Yes, this is the code snippet I had in mind. I did something similar to suit my needs, the logic is mostly identical:
dependency_graph = bpy.context.evaluated_depsgraph_get()
object_instances = dependency_graph.object_instances
objects_to_export = []
# Copy and prepare the objects we want to export (meshes only). START
for object in bpy.context.scene.objects:
if object.type != 'MESH': continue
if object.hide_viewport : continue
if object.is_instancer:
#####################################################################################################
# NOTE: @ for now we assume that the instanciation is 1 level deep and that all instances are meshes.
#####################################################################################################
for instance in object_instances:
parent = instance.parent
if parent and parent.original == object:
instance_object = instance.instance_object.original
new_object = instance_object.copy()
new_object.data = instance_object.data.copy()
new_object.matrix_world = instance.matrix_world.copy() # @ Is taking a copy necessary as we do not modify the matrix?
new_object.modifiers.new(name="triangulate_modifier", type='TRIANGULATE')
objects_to_export.append(new_object)
else:
new_object = object.copy()
new_object.data = object.data.copy()
new_object.modifiers.new(name="triangulate_modifier", type='TRIANGULATE')
objects_to_export.append(new_object)
# Copy and prepare the objects we want to export (meshes only). END
1 Like