Python: Apply object operator to dupli objects I wanted to apply an object operator (shade_smooth/flat) to linked/dupli objects, but can't find a way to pass the object to this operator.

import bpy

# flat or smooth
flat = False

for ob in context.scene.objects:
    if ob.dupli_group and ob.dupli_group.objects:
        for dob in ob.dupli_group.objects:
            if flat:
                bpy.ops.object.shade_flat()
            else:
                bpy.ops.object.shade_smooth()


Of course this doesn't work, because it applies on selected objects only. Was surprised to see this even works from the python terminal, without the need to override context.

Any clues?
This uses selected_editable_objects, You can find out which attributes are used by passing in a Python context, and then check the terminal for where each requested attribute is printed.

(Yes, we know this isnt an ideal way to design the API, however this is internal C code which defines which context variables are used).

import bpy
from bpy import context

# flat or smooth
flat = True

# use a 'set' to avoid duplicates, can convert into a list after.
selected_editable_objects = set()

for obj in context.scene.objects:
    if obj.dupli_type == 'GROUP':
        group = obj.dupli_group
        if group:
            for dob in obj.dupli_group.objects:
                selected_editable_objects.add(dob)

context_py = context.copy()

context_py["selected_editable_objects"] = list(selected_editable_objects)


if flat:
    bpy.ops.object.shade_flat(context_py)
else:
    bpy.ops.object.shade_smooth(context_py)




Note: this would be more efficient to de-duplicate based on the object data, since its possible many objects reference the same data. But for the example above, its simpler to just take unique objects.