import math import bpy from bpy.props import EnumProperty, IntProperty HU_TO_METERS_RATIO = 0.025 # 1hu = 2.5cm GRID_SUBDIVISIONS = 8 class VIEW3D_OT_ResetGrid(bpy.types.Operator): """Reset grid settings to defaults""" bl_idname = "screen.reset_grid" bl_label = "Reset Grid" bl_options = {'UNDO'} def execute(self, context): context.space_data.grid_lines = 16 context.space_data.grid_scale = 1.0 context.space_data.grid_subdivisions = 10 return {'FINISHED'} class VIEW3D_PT_grid_level(bpy.types.Panel): bl_space_type = 'VIEW_3D' bl_region_type = 'UI' bl_label = "Grid Level" def draw(self, context): layout = self.layout row = layout.row() row.prop(context.scene, "grid_type", expand=True) col = layout.column() if context.scene.grid_type == 'HAMMER': col.prop(context.scene, "grid_hu") grid_size = pow(2, context.scene.grid_hu) split = col.split(align=True) col_l = split.column() col_l.alignment = 'RIGHT' col_l.label("Hammer Units:") col_l.label("Meters:") col_r = split.column() col_r.label("%i hu" % grid_size) col_r.label("%.3f m" % (grid_size * HU_TO_METERS_RATIO)) elif context.scene.grid_type == 'POWEROF2': col.prop(context.scene, "grid_po2") layout.operator(VIEW3D_OT_ResetGrid.bl_idname) def update_grid_hu(self, context) -> None: grid_size = pow(2, self.grid_hu) context.space_data.grid_lines = max(3, math.ceil(1024 / grid_size)) context.space_data.grid_scale = grid_size * HU_TO_METERS_RATIO context.space_data.grid_subdivisions = GRID_SUBDIVISIONS def update_grid_po2(self, context) -> None: context.space_data.grid_lines = max(3, pow(2, self.grid_po2 + 5)) context.space_data.grid_scale = 1 / pow(2, self.grid_po2) context.space_data.grid_subdivisions = GRID_SUBDIVISIONS def update_type(self, context) -> None: if self.grid_type == 'HAMMER': update_grid_hu(self, context) elif self.grid_type == 'POWEROF2': update_grid_po2(self, context) def register(): bpy.types.Scene.grid_type = EnumProperty( items=( ('HAMMER', "Hammer Units", "Main basic settings"), ('POWEROF2', "Power Of 2", "Armature-related settings"), ), name="Grid type", description="", update=update_type, ) bpy.types.Scene.grid_hu = IntProperty( name="Grid level", description="Hammer Units grid level", min=0, max=9, default=6, update=update_grid_hu, ) bpy.types.Scene.grid_po2 = IntProperty( name="Grid divisions", description="Grid divisions in power of 2", min=-4, max=4, default=0, update=update_grid_po2, ) bpy.utils.register_class(VIEW3D_OT_ResetGrid) bpy.utils.register_class(VIEW3D_PT_grid_level) def unregister(): bpy.utils.unregister_class(VIEW3D_PT_grid_level) bpy.utils.register_class(VIEW3D_OT_ResetGrid) del bpy.types.Scene.grid_po2 del bpy.types.Scene.grid_hu del bpy.types.Scene.grid_type register()