4
$\begingroup$

I have an operator, attached to a button. The button creates a tower of cubes depending on 3 input dimensions. What I would like to do is dynamically change the text on the button so that it tells me how many cubes you're about to create, before you create them.

When I run the script, it initialises the button to the correct number, but the number doesn't change when I change the input conditions. The only way to do that at the moment is to re-run the script. I would appreciate any help finding a better solution, here's a code snippet so you can understand what I mean a bit better:

# operator for pressing the button
class Button(Operator):

button_text = bpy.context.scene.my_tool.cube_number 

bl_idname = "object.simple_operator"
bl_label = str(button_text)
$\endgroup$

1 Answer 1

2
$\begingroup$

please check this code:

enter image description here

'''
BEGIN GPL LICENSE BLOCK
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.    See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
END GPL LICENCE BLOCK
'''

bl_info = {  
 "name": "test properties",  
 "author": "Diego Quevedo ( http://doshape.com/ )",  
 "version": (1, 0),  
 "blender": (2, 7 , 3),  
 "location": "View3D > EditMode > ToolShelf",  
 "description": "test properties ",  
 "warning": "",  
 "wiki_url": "",  
 "tracker_url": "",  
 "category": "Mesh"} 

import bpy, sys, math
from bpy.props import (StringProperty,
                       BoolProperty,
                       IntProperty,
                       FloatProperty,
                       FloatVectorProperty,
                       EnumProperty,
                       PointerProperty,
                       )
from bpy.types import (Panel,
                       Operator,
                       AddonPreferences,
                       PropertyGroup,
                       )



class testOperador(bpy.types.Operator):

    bl_idname = 'ops.test'
    bl_label = 'test'
    bl_description  = ""
    bl_options = {'REGISTER', 'UNDO'}

    test1 = bpy.props.BoolProperty(
        name="test_properties after execution",
        default= False
    )

    def main(self, context):  

        print("hello")


    @classmethod
    def poll(self, context):
        obj = context.active_object
        return all([obj is not None])

    def execute(self, context):

        self.main(context)
        return {'FINISHED'}




class MySettings(PropertyGroup):

    x_size = FloatProperty(
        name = "x size",
        description = "A float property",
        default = 23.7,
        min = 0.01,
        max = sys.float_info.max
        )

    test = bpy.props.BoolProperty(
        name="test",
        default= False
    )

    vertices = bpy.props.BoolProperty(
        name="vertices",
        default= True
    )

class UV_OT_my_panel(Panel):

    bl_space_type = "VIEW_3D"
    bl_region_type = "TOOLS"
    bl_label = "test"


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


        split = layout.split()
        col = split.column()

        a= bpy.context.scene.my_tool.x_size

        layout.prop(mytool, "x_size", text="x-size")                    
        col.operator(testOperador.bl_idname, text=str(a), icon="COPYDOWN") 

        ############################

        split = layout.split()
        col = split.column()

        b = bpy.context.scene.my_tool.test

        row = col.row(align=True)
        row.prop(mytool, "test")

        if b:
            col.operator(testOperador.bl_idname, text="test1", icon="COPYDOWN") 
        else: 
            col.operator(testOperador.bl_idname, text="test2", icon="COPYDOWN") 


def register():
    bpy.utils.register_module(__name__)
    bpy.types.Scene.my_tool = PointerProperty(type=MySettings)


def unregister():
    bpy.utils.unregister_module(__name__)
    del bpy.types.Scene.my_tool



if __name__ == "__main__":
    register()
$\endgroup$
3
  • 5
    $\begingroup$ The key point there is using text='xx' when adding the operator to the panel draw code. $\endgroup$
    – sambler
    Jun 17, 2017 at 8:04
  • 3
    $\begingroup$ Please add some explanation of the key parts of the code. $\endgroup$ Jun 17, 2017 at 10:51
  • 2
    $\begingroup$ I tried to do it by assigning bl_label (within the operator) to a variable, but changing that variable didn't work. However, what does work is overriding that so bl_label isn't used at all. When you draw the button as part of the "Panel", adding an extra argument "text = str(variable_name)" fixes the problem, as that seems to refresh it when the variable changes. Hope that makes sense, and thank you yhoyo for the answer $\endgroup$ Jun 17, 2017 at 11:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .