This script adds two numbers. It shows how to add GUI elements to Blender using Python. Useful link:
https://stackoverflow.com/a/15610283/4159530bl_info = {
"name": "Calculator to add two numbers",
"category": "3D View"
}
import bpy
class MyPanel(bpy.types.Panel):
bl_label = "Calculator"
bl_space_type = 'VIEW_3D'
bl_region_type = 'TOOLS'
def draw(self, context):
layout = self.layout
col = layout.column(align = True)
col.prop(context.scene, "a_string_prop")
col.prop(context.scene, "b_string_prop")
col.prop(context.scene, "r_string_prop")
row = layout.row()
row.operator("calculator.add", text="Add two numbers")
class MyOperator(bpy.types.Operator):
bl_idname = "calculator.add"
bl_label = "Simple operator"
bl_description = "My Operator"
def execute(self, context):
a = context.scene.a_string_prop
b = context.scene.b_string_prop
r = float(a) + float(b)
context.scene.r_string_prop = str(r)
return {'FINISHED'}
def register():
bpy.utils.register_class(MyPanel)
bpy.utils.register_class(MyOperator)
bpy.types.Scene.a_string_prop = bpy.props.StringProperty \
(
name = "a",
description = "First operand"
)
bpy.types.Scene.b_string_prop = bpy.props.StringProperty \
(
name = "b",
description = "Second operand"
)
bpy.types.Scene.r_string_prop = bpy.props.StringProperty \
(
name = "result",
description = "Result operand"
)
def unregister():
bpy.utils.unregister_class(MyPanel)
bpy.utils.unregister_class(MyOperator)
del bpy.types.Scene.a_string_prop
del bpy.types.Scene.b_string_prop
del bpy.types.Scene.r_string_prop
if __name__ == "__main__":
register()