I am hoping to render a hex grid in the 3d view-port. I’ve found the gpu
module, and I’ve figured out how to add controls for this functionality to the overlay panel in the gui.
I can draw the hex grid no problem using something like:
bpy.types.SpaceView3D.draw_handler_add(draw_hex_grid, (), 'WINDOW', 'POST_VIEW')
My hope is to control whether I draw the grid using a checkbox the overlay panel in the UI; however, I am stuck on how I should handle the draw handle and toggling this functionality.
My first attempt is as follows:
def draw_hex_grid(self, context):
pass
class HexGridOverlayProperties(bpy.types.PropertyGroup):
"""Settings and properties of hex grid overlay"""
draw_hex_grid: bpy.props.BoolProperty(
name = "Hex Grid",
default=False,
update=toggle_hex_grid
)
_handle_hex_grid = None
def toggle_hex_grid(self, context):
if self.get('draw_hex_grid'):
self._handle_hex_grid = bpy.types.SpaceView3D.draw_handler_add(draw_hex_grid, (self, context), 'WINDOW', 'POST_VIEW')
print(self._handle_hex_grid)
elif self._handle_hex_grid is not None:
print(self._handle_hex_grid)
bpy.types.SpaceView3D.draw_handler_remove(self._handle_hex_grid, 'WINDOW')
else:
print('No Handle', self._handle_hex_grid)
I always get No Handle
on the console when un-checking the box, which to me implies that I can’t track extra state this way in a PropertyGroup
. This makes some sense as I do feel I’ve gone a bit off the beaten track here.
Has anyone tried something like this before and have any advice on what I should do?
I want to add an operator for snapping vertices to the grid, I imagine the way to go would be using a ModalOperator
, but I don’t get the feeling that would be the appropriate way to have the grid just render all the time when toggled in UI.