Skip to content

Commit

Permalink
bump version to blender 4.0, added new text offset settings
Browse files Browse the repository at this point in the history
  • Loading branch information
MehmetHY committed Mar 18, 2024
1 parent 8d8f920 commit 3eedefc
Showing 1 changed file with 165 additions and 31 deletions.
196 changes: 165 additions & 31 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import bpy
import bgl
import blf

bl_info = {
"name": "Easy Grid Resizer",
"author": "MehmetHY",
"version": (1, 0),
"blender": (2, 93, 0),
"version": (1, 6),
"blender": (4, 0, 0),
"location": "3D View -> N Panel -> View -> Easy Grid Resizer",
"description": "Set, add, subtruct, multiply or divide the grid size with a single click.",
"warning": "",
Expand All @@ -16,20 +15,76 @@
}


font_info = {"handler": None, "active": False}
font_info = {"font_id": 0, "handler": None, "show_text": False}


class GridProperties(bpy.types.PropertyGroup):
set_grid_size: bpy.props.FloatProperty(name="Set Grid Size", default=1.0, min=0.0, description="Set Grid Size")
add_subtruct_step: bpy.props.FloatProperty(name="Add/Subtruct Step", default=1.0, min=0.0, description="Add/Subtract Value")
set_grid_size: bpy.props.FloatProperty(
name="Set Grid Size", default=1.0,
min=0.0,
description="Set Grid Size"
) # type: ignore

add_subtruct_step: bpy.props.FloatProperty(
name="Add/Subtruct Step",
default=1.0,
min=0.0,
description="Add/Subtract Value"
) # type: ignore

multiply_divide_step: bpy.props.FloatProperty(
name="Multiply/Divide Step", default=2.0, min=1.0, description="Multply/Divide Value")
name="Multiply/Divide Step",
default=2.0,
min=1.0,
description="Multply/Divide Value"
) # type: ignore

overlay_font_size: bpy.props.IntProperty(
"Overlay Size", default=24, description="Overlay Size", min=18, max=64)
overlay_color: bpy.props.FloatVectorProperty("Overlay Color", subtype="COLOR", size=4, default=(
1.0, 1.0, 1.0, 1.0), min=0.0, max=1.0, description="Overlay Color")
name="Overlay Size",
default=24,
description="Overlay Size",
min=8,
max=256,
step=1
) # type: ignore

overlay_color: bpy.props.FloatVectorProperty(
name="Overlay Color",
subtype="COLOR",
size=4,
default=(1.0, 1.0, 1.0, 1.0),
min=0.0,
max=1.0,
description="Overlay Color"
) # type: ignore

overlay_direction: bpy.props.EnumProperty(
items=[("Right", "Right", ""), ("Left", "Left", "")], name="overlay_direction", default="Left", description="Overlay Location")
items=[
("Bottom Left", "Bottom Left", ""),
("Bottom Right", "Bottom Right", ""),
("Top Left", "Top Left", ""),
("Top Right", "Top Right", "")
],
name="overlay_direction",
default="Bottom Left",
description="Overlay Location"
) # type: ignore

overlay_vertical_offset: bpy.props.FloatProperty(
name="Vertical Offset",
default=48.0,
description="Overlay text vertical offset",
min=0,
step=1
) # type: ignore

overlay_horizontal_offset: bpy.props.FloatProperty(
name="Horizontal Offset",
default=48.0,
description="Overlay text horizontal offset",
min=0,
step=1
) # type: ignore


class DrawGridSizeOverlay(bpy.types.Operator):
Expand All @@ -39,29 +94,81 @@ class DrawGridSizeOverlay(bpy.types.Operator):
bl_options = {'REGISTER'}

def execute(self, context):
font_info["active"] = not font_info["active"]
if bpy.context.area .type == "VIEW_3D":
if font_info["active"]:
font_info["handler"] = bpy.types.SpaceView3D.draw_handler_add(draw_callback, (self, context), 'WINDOW', 'POST_PIXEL')
if bpy.context.area.type == "VIEW_3D":
if font_info["show_text"]:
font_info["handler"] = bpy.types.SpaceView3D.draw_handler_add(
draw_callback,
(self, context),
'WINDOW',
'POST_PIXEL'
)
elif font_info["handler"] != None:
bpy.types.SpaceView3D.draw_handler_remove(font_info["handler"], 'WINDOW')
bpy.types.SpaceView3D.draw_handler_remove(
font_info["handler"],
'WINDOW'
)

bpy.context.region.tag_redraw()
return {'FINISHED'}

return {'FINISHED'}


def draw_callback(self, context):
grid_size_text = f"Grid Size: {bpy.context.space_data.overlay.grid_scale}"
overlay_width = len(grid_size_text) * bpy.context.scene.grid_property_group.overlay_font_size * 0.3
overlay_offset = bpy.context.area.width - int(overlay_width) - 60
if bpy.context.scene.grid_property_group.overlay_direction == "Right":
blf.position(0, overlay_offset, 20, 0)
text = f"# {bpy.context.space_data.overlay.grid_scale}"
props = bpy.context.scene.grid_property_group
x_offset = props.overlay_horizontal_offset
y_offset = props.overlay_vertical_offset
x_pos = 0
y_pos = 0
area_width = bpy.context.area.width
area_height = bpy.context.area.height

blf.size(
0,
bpy.context.scene.grid_property_group.overlay_font_size
)

text_width, text_height = blf.dimensions(0, text)

if props.overlay_direction == "Bottom Left":
x_pos = x_offset
y_pos = y_offset
elif props.overlay_direction == "Bottom Right":
x_pos = area_width - x_offset - text_width
y_pos = y_offset
elif props.overlay_direction == "Top Left":
x_pos = x_offset
y_pos = area_height - y_offset - text_height
else:
blf.position(0, 20, 20, 0)
blf.color(0, bpy.context.scene.grid_property_group.overlay_color[0],
bpy.context.scene.grid_property_group.overlay_color[1], bpy.context.scene.grid_property_group.overlay_color[2], bpy.context.scene.grid_property_group.overlay_color[3])
blf.size(0, bpy.context.scene.grid_property_group.overlay_font_size, bpy.context.preferences.system.dpi)
blf.draw(0, f"Grid Size: {bpy.context.space_data.overlay.grid_scale}")
x_pos = area_width - x_offset - text_width
y_pos = area_height - y_offset - text_height

blf.position(0, x_pos, y_pos, 0)

blf.color(
0,
bpy.context.scene.grid_property_group.overlay_color[0],
bpy.context.scene.grid_property_group.overlay_color[1],
bpy.context.scene.grid_property_group.overlay_color[2],
bpy.context.scene.grid_property_group.overlay_color[3]
)

blf.draw(0, text)


class ToggleText(bpy.types.Operator):
bl_idname = "view3d.toggle_grid_size_overlay"
bl_label = "Toggle Text"
bl_description = "Toggles grid size overlay text"
bl_options = {'REGISTER'}

def execute(self, context):
font_info["show_text"] = not font_info["show_text"]

if bpy.context.area.type == "VIEW_3D":
bpy.ops.view3d.draw_grid_size_overlay()

return {'FINISHED'}


class SetGridScale(bpy.types.Operator):
Expand All @@ -72,6 +179,7 @@ class SetGridScale(bpy.types.Operator):

def execute(self, context):
bpy.context.space_data.overlay.grid_scale = bpy.context.scene.grid_property_group.set_grid_size

return {'FINISHED'}


Expand All @@ -83,6 +191,7 @@ class IncreaseGridScale(bpy.types.Operator):

def execute(self, context):
bpy.context.space_data.overlay.grid_scale += bpy.context.scene.grid_property_group.add_subtruct_step

return {'FINISHED'}


Expand All @@ -94,6 +203,7 @@ class DecreaseGridScale(bpy.types.Operator):

def execute(self, context):
bpy.context.space_data.overlay.grid_scale -= bpy.context.scene.grid_property_group.add_subtruct_step

return {'FINISHED'}


Expand All @@ -105,6 +215,7 @@ class MultiplyGridScale(bpy.types.Operator):

def execute(self, context):
bpy.context.space_data.overlay.grid_scale *= bpy.context.scene.grid_property_group.multiply_divide_step

return {'FINISHED'}


Expand All @@ -116,6 +227,7 @@ class DivideGridScale(bpy.types.Operator):

def execute(self, context):
bpy.context.space_data.overlay.grid_scale /= bpy.context.scene.grid_property_group.multiply_divide_step

return {'FINISHED'}


Expand Down Expand Up @@ -144,11 +256,14 @@ def draw(self, context):
layout = self.layout

box = layout.box()
box.operator("view3d.draw_grid_size_overlay", text="Show/Hide Text")
box.operator("view3d.toggle_grid_size_overlay", text="Toggle Text")
box.prop(prop_group, "overlay_font_size", text="Text Size")
box.prop(prop_group, "overlay_direction", text="Text Location")
box.prop(prop_group, "overlay_vertical_offset", text="Vertical Offset")
box.prop(prop_group, "overlay_horizontal_offset", text="Horizontal Offset")
box.prop(prop_group, "overlay_color", text="Text Color")


class GridSizePanel(bpy.types.Panel):
bl_idname = "EASYGRIDSIZER_PT_grid_size"
bl_label = "Grid Size"
Expand All @@ -160,6 +275,11 @@ def draw(self, context):
prop_group = bpy.context.scene.grid_property_group
layout = self.layout

box = layout.box()
row = box.row()
row.label(text="Current Size")
row.label(text=f"{bpy.context.space_data.overlay.grid_scale}")

box = layout.box()
box.label(text="Set Grid Size")
box.prop(prop_group, "set_grid_size")
Expand All @@ -182,6 +302,7 @@ def draw(self, context):
row.operator("view3d.divide_grid_size", text="Divide")
row.operator("view3d.multiply_grid_size", text="Multiply")


class GridSnapPanel(bpy.types.Panel):
bl_idname = "EASYGRIDSIZER_PT_grid_snap"
bl_label = "Snap to Grid"
Expand All @@ -207,25 +328,38 @@ class SnapAllVerticesToGrid(bpy.types.Operator):
def execute(self, context):
if bpy.context.active_object == None:
return {'FINISHED'}

pre_mode = bpy.context.object.mode
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.view3d.snap_selected_to_grid()
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.object.mode_set(mode=pre_mode)

return {'FINISHED'}

classes_to_register = (GridProperties, SetGridScale, IncreaseGridScale, DecreaseGridScale,
MultiplyGridScale, DivideGridScale, GridPanel, DrawGridSizeOverlay, SnapAllVerticesToGrid, GridOverlayPanel, GridSizePanel, GridSnapPanel)

classes_to_register = (
GridProperties, ToggleText, SetGridScale, IncreaseGridScale,
DecreaseGridScale, MultiplyGridScale, DivideGridScale, GridPanel,
DrawGridSizeOverlay, SnapAllVerticesToGrid, GridOverlayPanel, GridSizePanel,
GridSnapPanel
)


def register():
for cls in classes_to_register:
bpy.utils.register_class(cls)
bpy.types.Scene.grid_property_group = bpy.props.PointerProperty(type=GridProperties)

bpy.types.Scene.grid_property_group = bpy.props.PointerProperty(
type=GridProperties
)


def unregister():
for cls in classes_to_register:
bpy.utils.unregister_class(cls)

del bpy.types.Scene.grid_property_group


Expand Down

0 comments on commit 3eedefc

Please sign in to comment.