3
$\begingroup$

How can the text, icon, and a button state (toggle high or low) be changed when a button is clicked? Just for understanding the mechanics, I wanted to know the most verbose and least verbose way to change text on a button, text on a label, an icon, and a button state (toggle up/ down) from a single button click from a button that contains the said text, icon, toggle state, and visually associated label.

            import bpy
            from bpy.props import *

            def initSceneProperties(scn):
                stin = bpy.types.Scene

                stin.MyString1 = BoolProperty(
                    name = "String")
                scn['MyString1'] = "Test 1"

                stin.MyString2 = BoolProperty(
                    name = "String")
                scn['MyString2'] = "Test 2"
                return

            initSceneProperties(bpy.context.scene)

            #   Layout panel
            class LayoutPanel(bpy.types.Panel):
                bl_label = "Hide Tools"
                bl_space_type = "VIEW_3D"
                bl_region_type = "TOOL_PROPS"

                def draw(self, context):
                    layout = self.layout
                    scn = context.scene

                    layout.label("TEST")
            # First String   
                    layout.prop(scn, 'MyString1', text="")
                    row = layout.row(align=True)
                    row.operator("my.button", text="1", icon="CANCEL", toggle=True)

            # Second String        
                    layout.prop(scn, 'MyString2', text="")
                    row = layout.row(align=True)
                    row.operator("my.button", text="2", icon="CANCEL", toggle=True)

            #   Button
            class OBJECT_OT_Button(bpy.types.Operator):
                bl_idname = "my.button"
                bl_label = "Button"

                def execute(self, context):
                    scn = context.scene
                    layout = self.layout
                    icon = LayoutPanel.draw

                    if scn.MyString1 == "MyString2" | row.operator(toggle=True):
                        label = "TEST2"
                        icon.row.operator[icon] == "ERROR"
                        icon.row.operator[text] == "A"
                        icon.row.operator[toggle] = True
                    else:    
                        scn.MyString1 == "MyString1"
                        label = "TEST1"
                        icon.row.operator[icon] == "CANCEL"
                        icon.row.operator[text] == "B"
                        icon.row.operator[toggle] = False
                    return{'FINISHED'} 

            bpy.utils.register_module(__name__)
$\endgroup$
5
  • $\begingroup$ Related blender.stackexchange.com/questions/40948/… this only hides UI, but same principle can change text, icon UILayout $\endgroup$ – batFINGER Jan 15 '17 at 6:27
  • $\begingroup$ Is this blender-id specific or is it related to Blender as a whole, and python scripting? Including relevant tags and plenty of information helps us understand the context of your question. $\endgroup$ – VRM Jan 15 '17 at 13:52
  • $\begingroup$ Updated: Thanks batFINGER, I'll look that over. $\endgroup$ – ACopeLan Jan 15 '17 at 15:22
  • 1
    $\begingroup$ If you have some BoolProperty then layout.prop(obj, "someboolprop", toggle=True, icon='MONKEY' if obj.someboolprop else 'NONE', text="Something" if obj.someboolprop else "Something Else") will give you a switch toggle, rather than a checkbox. $\endgroup$ – batFINGER Jan 15 '17 at 15:46
  • $\begingroup$ I've been trying to get that running. I must be missing something. Can you illustrate? $\endgroup$ – ACopeLan Feb 9 '17 at 22:18
2
$\begingroup$

I got it batFINGER

Thanks! Mine was verbose indeed, but alas not close.

import bpy
from bpy.props import *

def initSceneProperties(scn):
    bpy.types.Scene.MyBool = BoolProperty(
        name = "Boolean", 
        description = "None")
    scn['MyBool'] = True
    return

initSceneProperties(bpy.context.scene)

class UIPanel(bpy.types.Panel):
    bl_label = "Property panel"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"

    def draw(self, context):
        layout = self.layout
        scn = context.scene

        if scn.MyBool:
            icon = 'X_VEC'
            txt = 'Cancel'
        else:
            icon = "PAUSE"
            txt = 'Pause'

        layout.prop(scn, 'MyBool', text=txt, icon=icon, toggle = True)
        layout.label(text= txt)

bpy.utils.register_module(__name__)
$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.