Skip to content

Instantly share code, notes, and snippets.

@CGArtPython
Created May 9, 2024 07:55
Show Gist options
  • Save CGArtPython/af1838172eded5037aecec13c1592ddd to your computer and use it in GitHub Desktop.
Save CGArtPython/af1838172eded5037aecec13c1592ddd to your computer and use it in GitHub Desktop.
A example of an addon that logs into a file
bl_info = {
"name": "File Logging Addon",
"description": "Creates a panel with three buttons that add a cube, cone, and sphere to the scene. Logs messages to a file.",
"author": "Viktor Stepanov",
"version": (0, 0, 1),
"blender": (4, 0, 0),
"location": "View3D",
"warning": "This addon is still in development.",
"wiki_url": "",
"category": "Object"
}
import bpy
import logging
import os
# Configure logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
# Create file handler
log_file = os.path.join(os.path.dirname(__file__), "addon.log")
file_handler = logging.FileHandler(log_file)
file_handler.setLevel(logging.DEBUG)
# Create formatter and add it to the file handler
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
file_handler.setFormatter(formatter)
# Add the file handler to the logger
logger.addHandler(file_handler)
# Custom operators
class Operator1(bpy.types.Operator):
bl_idname = "object.add_mesh1"
bl_label = "Add Cube"
def execute(self, context):
logger.info("Executing Operator 1")
logger.debug("Adding a cube to the scene")
bpy.ops.mesh.primitive_cube_add(size=2)
return {'FINISHED'}
class Operator2(bpy.types.Operator):
bl_idname = "object.add_mesh2"
bl_label = "Add Cone"
def execute(self, context):
logger.info("Executing Operator 2")
logger.debug("Adding a cone to the scene")
bpy.ops.mesh.primitive_cone_add(vertices=8, radius1=1, depth=2)
return {'FINISHED'}
class Operator3(bpy.types.Operator):
bl_idname = "object.add_mesh3"
bl_label = "Add Sphere"
def execute(self, context):
logger.info("Executing Operator 3")
logger.debug("Adding a sphere to the scene")
bpy.ops.mesh.primitive_uv_sphere_add(radius=1)
return {'FINISHED'}
# Panel
class CustomPanel(bpy.types.Panel):
bl_label = "Custom Panel"
bl_idname = "OBJECT_PT_custom_panel"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'Custom'
def draw(self, context):
logger.debug("Drawing the custom panel")
layout = self.layout
row = layout.row()
row.operator("object.add_mesh1")
row = layout.row()
row.operator("object.add_mesh2")
row = layout.row()
row.operator("object.add_mesh3")
# Register the operators and panel
classes = (Operator1, Operator2, Operator3, CustomPanel)
def register():
logger.info("Registering the %s addon", __name__)
for cls in classes:
logger.debug("Registering class: %s", cls)
bpy.utils.register_class(cls)
def unregister():
logger.info("Unregistering the %s addon", __name__)
for cls in classes:
logger.debug("Unregistering class: %s", cls)
bpy.utils.unregister_class(cls)
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment