0
$\begingroup$

I have to show the state of a programmable logical controller (PLC). PLCs can have different states like "RUN", "STOP", "CONFIG" etc., and I want to show this state with an icon in a custom panel.

I tried it in this way, my problem is how to ask Blender to redraw the panel (or call the draw method).

If the panel is closed and opened afterwards, the correct icon is shown. Any idea?

enter image description here

import bpy, os
bpy.types.Scene.WG_Status = bpy.props.StringProperty(default="STOP")

class WgPanel:
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = "Tool"
    bl_idname = "PANEL_PT_Wg"

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

class Address():
    ID ='CV004'

preview_collections = {}
class WgPanelIcon(WgPanel, bpy.types.Panel):
    bl_idname = "ICON_PT_Wg"
    bl_label = "plc - Settings"
    import bpy.utils.previews
    pcoll = bpy.utils.previews.new()
    # load a preview thumbnail of a file and store in the previews collection
    pcoll.load("Run", "Run.png", 'IMAGE')
    pcoll.load("Stop", "Stop.png", 'IMAGE')
    preview_collections["main"] = pcoll
    def draw(self, context):
        pcoll= preview_collections["main"]    
        if context.scene.WG_Status == "STOP":
            wgIcon = pcoll["Stop"]     
        else:
            if context.scene.WG_Status == "RUN":
                wgIcon = pcoll["Run"]     
        self.layout.label(text="Address:", icon_value=wgIcon.icon_id)
        self.layout.label(text= Address.ID)

def register():
    bpy.utils.register_class(WgPanelIcon)

def unregister():
    bpy.utils.unregister_class(WgPanelIcon)

if __name__ == "__main__":
    register()
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.