Material Preview Not Updated from Tool Shelf?

I am trying to show the material preview in both the Tool Shelf and the Scene Property Panel. It shows up in both places, but it is not refreshed in the Tool Shelf unless a redraw is forced (by resizing the Tool Shelf, for example).

Here’s a small addon that demonstrates the problem:

bl_info = {
  "version": "0.1",
  "name": "Color Preview",
  'author': 'Bob',
  "category": "Blender Experiments"
  }


import bpy
from bpy.props import *


class ColorPreviewPropGroup(bpy.types.PropertyGroup):
    def draw_layout ( self, layout ):
        if len(bpy.data.materials) > 0:
            mat_name = bpy.data.materials[0].name
            layout.template_preview(bpy.data.materials[mat_name])
            row = layout.row()
            row.prop ( bpy.data.materials[mat_name], "diffuse_color", text="Color" )
            row = layout.row()
            row.prop ( bpy.data.materials[mat_name], "emit", text="Emit" )


class ColorPreviewToolPanel(bpy.types.Panel):
    bl_label = "Color Preview Tool"

    bl_space_type = "VIEW_3D"
    bl_region_type = "TOOLS"
    bl_category = "Color Preview"

    @classmethod
    def poll(cls, context):
        return (context.scene is not None)

    def draw(self, context):
        self.layout.row().label ( "Tool Shelf Version", icon='COLOR_RED' )
        context.scene.app.draw_layout ( self.layout )


class ColorPreviewScenePanel(bpy.types.Panel):
    bl_label = "Color Preview Scene"

    bl_space_type = "PROPERTIES"
    bl_region_type = "WINDOW"
    bl_context = "scene"

    @classmethod
    def poll(cls, context):
        return (context.scene is not None)

    def draw(self, context):
        self.layout.row().label ( "Scene Panel Version", icon='COLOR_GREEN' )
        context.scene.app.draw_layout ( self.layout )


def register():
    print ("Registering ", __name__)
    bpy.utils.register_module(__name__)
    bpy.types.Scene.app = bpy.props.PointerProperty(type=ColorPreviewPropGroup)

def unregister():
    print ("Unregistering ", __name__)
    bpy.utils.unregister_module(__name__)
    del bpy.types.Scene.app

if __name__ == "__main__":
    register()

You’ll notice that both Panels (ColorPreviewToolPanel and ColorPreviewScenePanel) call the exact same “draw_layout” function. In fact, they only differ in their names and the labels that they draw.

Here’s a picture of it running in Blender 2.76b (after a Load Factory Settings):


The color controls (diffuse and emit) can be changed from either panel and are reflected in both the 3D Cube object and the other panel … with one exception. The preview in the Tool Shelf will not update unless a redraw is forced (by resizing the panel for example). In this example, the preview still shows the previous gray color even though the color chooser and the Cube itself show the proper color.

Is there something special that needs to be done to get the Preview to refresh in the Tool Shelf … or is this a bug?

Thanks in advance.

I think this may be a bug, but I’m refreshing the topic before filing a bug report. If you can see anything that I’m doing wrong in this example, please let me know.

Thanks in advance.