I'm trying to create an Operator and I was wondering what code to use to specifically toggle the Operator? I'm working on with a checkbox (BoolProperty).
Asked
Active
Viewed 1,550 times
-1
Robert Gützkow
- 25,622
- 3
- 47
- 78
Scottie Doria
- 51
- 9
1 Answers
3
You can access the values of all properties (almost). The following Operator prints the current state (True, False) of a custom scene property called my_bool which is displayed at the top of the panel:
import bpy
from bpy.props import (BoolProperty,
PointerProperty,
)
from bpy.types import (Panel,
Operator,
PropertyGroup,
)
# ------------------------------------------------------------------------
# Scene Properties
# ------------------------------------------------------------------------
class MyProperties(PropertyGroup):
my_bool: BoolProperty(
name="Enable or Disable",
description="A bool property",
default = False
)
# ------------------------------------------------------------------------
# Operators
# ------------------------------------------------------------------------
class SimpleOperator(bpy.types.Operator):
"""Tooltip"""
bl_idname = "object.simple_operator"
bl_label = "Simple Object Operator"
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
scene = context.scene
mytool = scene.my_tool
if mytool.my_bool:
print ("Bool Property enabled")
else:
print ("Bool Property disabled")
return {'FINISHED'}
# ------------------------------------------------------------------------
# Panel in Object Mode
# ------------------------------------------------------------------------
class OBJECT_PT_CustomPanel(Panel):
bl_label = "My Panel"
bl_idname = "OBJECT_PT_custom_panel"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "Tools"
bl_context = "objectmode"
@classmethod
def poll(self,context):
return context.object is not None
def draw(self, context):
layout = self.layout
scene = context.scene
mytool = scene.my_tool
layout.prop(mytool, "my_bool")
layout.operator(SimpleOperator.bl_idname)
state_txt = "enabled" if mytool.my_bool else "disabled"
layout.label(text="Bool Property {}".format(state_txt))
layout.separator()
# ------------------------------------------------------------------------
# Registration
# ------------------------------------------------------------------------
classes = (
MyProperties,
SimpleOperator,
OBJECT_PT_CustomPanel
)
def register():
from bpy.utils import register_class
for cls in classes:
register_class(cls)
bpy.types.Scene.my_tool = PointerProperty(type=MyProperties)
def unregister():
from bpy.utils import unregister_class
for cls in reversed(classes):
unregister_class(cls)
del bpy.types.Scene.my_tool
if __name__ == "__main__":
register()
Layout code ripped from: How to create a custom UI?
brockmann
- 12,613
- 4
- 50
- 93
layout.prop(mytool, "my_bool", toggle=True). In addition to the post rjg linked to, have a look into: Quickest way to create panel buttons with quick functionality – brockmann Sep 03 '19 at 14:24