Hello,
I’ve tried a few times to have a button press trigger a class method (usually my own class), and the button just disappears.
What’s the correct Blender/Python syntax for accomplishing this?
Thanks!
Hello,
I’ve tried a few times to have a button press trigger a class method (usually my own class), and the button just disappears.
What’s the correct Blender/Python syntax for accomplishing this?
Thanks!
Create an operator (see templates in text editor) that calls the class method in its execute() method and add it to a panel.
import bpy
class SomeClass:
@classmethod
def main(cls, context):
for ob in context.scene.objects:
print(ob)
class SimplePanel(bpy.types.Panel):
"""Creates a Panel in the Object properties window"""
bl_label = "Simple Panel"
bl_idname = "OBJECT_PT_simple"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "object"
def draw(self, context):
layout = self.layout
layout.operator(SimpleOperator.bl_idname)
class SimpleOperator(bpy.types.Operator):
"""Tooltip"""
bl_idname = "object.simple_operator"
bl_label = "Simple Object Operator"
def execute(self, context):
SomeClass.main(context)
return {'FINISHED'}
def register():
bpy.utils.register_class(SimpleOperator)
bpy.utils.register_class(SimplePanel)
def unregister():
bpy.utils.unregister_class(SimpleOperator)
bpy.utils.unregister_class(SimplePanel)
if __name__ == "__main__":
register()
# test call
bpy.ops.object.simple_operator()