Override dupligroup's object's material (Python) How can I link a dupligroup of a group from File A in to File B and override the material of an object within the dupligroup with a material from File B?

The end goal would be to have multiple dupligroups of linked objects all linked from one file, but with different materials. Having the code in one script with the dupligroups and the materials to be applied would be ideal; like this:



I got the idea from this video on Python overrides in Blender:
https://youtu.be/bOLMkw_wDGE

The video just shows how to override the attributes of materials and modifiers, but it seems like it should be possible to override a whole material of a dupligroup's child objects. For example:



source.blend has a group calling 'thing' with an object called 'box' in it with a material applied called 'red'.


dest.blend has a material called 'blue' and a dupligroup of 'thing' from source.blend. 
In dest.blend I want to override the 'red' material and put 'blue' on the 'box' that is within the linked dupligroup of 'thing.'
The final goal is being able to import multiple dupligroups of 'thing' and have different materials (from dest.blend) on all of the different dupligroups' 'box' objects.


The closest I've come is this Python line within dest.blend in the python console:

bpy.data.objects['box'].material_slots['red'].material = bpy.data.materials['blue']


This does NOT give me an error message (and it does autocomplete to that point) but it just doesn't do anything.

This is the closest I've come because all my other attempts have given me error messages. It also looked like I was on the right track because just putting in:

bpy.data.objects['box'].material_slots['red'].material

returned:

bpy.data.materials['red']

which matches the syntax of:

bpy.data.materials['blue']

But as I said it didn't seem to do anything.

What am I doing wrong here? Is it even possible to do this with a dupligroup? Any ideas would be very much appreciated. Thank you.

A few notes: I do know about the Edit Linked Libraries add-on, but that doesn't work for what I want to use this method for. As far as I can tell the add-on just lets you more easily edit the linked object's source file thereby editing the linked object, whereas with this method I'd like to be able to have multiple instances of the linked object all linked from one file, but with different materials.

I also know about this thread: Multiple Linked Objects with unique Materials / Group Materials override but not only can I not get any of those examples to work, but also they're just dealing with linked objects and not dupligroups. Dupligroups are a lot more useful in my opinion.

I am new to Blender Python scripting so specific code examples with explanations would be very much appreciated. Thank you.

(This question was asked about Blender versions 2.7*)

This is a (vastly improved) repeat of a question I asked on BlenderArtists.org: http://blenderartists.org/forum/showthread.php?385851-Override-dupligroup-s-object-s-material&amp;highlight= I hope that's okay. The thread died months ago without any solutions.
Here is the basic code how to do it with Python:
http://www.pasteall.org/66951

import bpy

#1 Link like you do now. Dupligroup object is selected
#-----------------------------------------------
#2 take copies (.copy()) of our group objects
#-----------------------------------------------
o = bpy.context.object
objs = [i.copy() for i in o.dupli_group.objects]

#3 link them to the scene
#-----------------------------------------------
bpy.ops.object.select_all(action='DESELECT')
for i in objs:
    bpy.context.scene.objects.link(i)
    i.select = True

#4 make single users (mat+tex)
#-----------------------------------------------
bpy.ops.object.make_single_user(object=False, obdata=False, material=True, texture=True, animation=False)

#5 change matslot 0 material 
#-----------------------------------------------
for i in objs:
    i.material_slots[0].material = bpy.data.materials[0]    

#6 Use original group name
#-----------------------------------------------
groupname = o.dupli_group.name

for i in objs:
    bpy.data.groups[groupname].objects.link(i)

#7 set dupligroup for empty
#-----------------------------------------------
o.dupli_group = bpy.data.groups[groupname]

#Now you have exact mesh clones with different materials and they are still linked on mesh level and you control them with empty object like before.