How would I go about creating a label with a label that takes a property as the text input?
row.label(text="Hello")
I want to display the progress of a plugin instead of this Hello
.
UPDATE: Label only version
I would use a StringProperty
attached to bpy.types.Scene
. That way you could reference it from anywhere in your add-on and display it in a panel. Here's a trivial example, that also uses an IntProperty
to provide a counter:
import bpy
from bpy.types import Operator
from bpy.types import Panel
from bpy.props import StringProperty
from bpy.props import IntProperty
class TLA_OT_operator(Operator):
bl_idname = "demo.operator"
bl_label = "button"
bl_options = {"REGISTER", "UNDO"}
def execute(self, context):
context.scene.progress = "pushed"
context.scene.counter += 1
return {'FINISHED'}
class TLA_PT_sidebar(Panel):
bl_label = "demo"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "TLA"
def draw(self, context):
col = self.layout.column(align=True)
col.label(text=f"status: {context.scene.progress} {context.scene.counter}")
col.operator("demo.operator")
classes = [
TLA_OT_operator,
TLA_PT_sidebar,
]
def register():
bpy.types.Scene.progress = StringProperty(name="progress", default="None")
bpy.types.Scene.counter = IntProperty(name="progress", default=0)
for c in classes:
bpy.utils.register_class(c)
def unregister():
del bpy.types.Scene.progress
del bpy.types.Scene.counter
for c in classes:
bpy.utils.unregister_class(c)
In this example, a "TLA" tab will be added to the sidepanel (n-panel) of 3D View. When opened it will display the current value of the StringProperty
, and a button. If you push the button, the StringProperty
will be changed by the operator, resulting in the display changing. As a bonus the counter will increment and the number of button pushes will be displayed.
In an addon it's important to clean up, so you have to remember to delete the string property if your addon is disabled.
You can, of course, modify the string property from anywhere in your addon that you want to change the progress report.
In the example I attached the StringProperty
to bpy.types.Scene
but there are other places you might attach it, depending on your usage. For instance you might attach it to bpy.types.Object
if you wanted to maintain status for each object in your scene separately.