Draw custom grid in Blender view

I'm trying to add a custom grid that will be drawn in blender's viewport, just like default grid. The problem is, i dont understand how should i continuously run the draw code, and how should i attach it to a button the right way.

Here i have the drawing code:

def create_batch(self, context):
    self.shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR')
    self.batch = batch_for_shader(self.shader, 'LINES', {'pos': [(0,0,0), (1,0,0)]})
def draw_callback(self, context):
    self.shader.uniform_float('color', context.scene.cdtrk_grid.color)

and here is the property class

class CDTRKGridProps(bpy.types.PropertyGroup):
    enabled         : bpy.props.BoolProperty (
        name        = 'enabled',
        default     = False

    color           : bpy.props.FloatVectorProperty (
        name        = 'color',
        subtype     = 'COLOR',
        default     = (0.13, 0.25, 0.03)

    def register():
        bpy.types.Scene.cdtrk_grid = bpy.props.PointerProperty(type=CDTRKGridProps)

I tried creating an operator which would run the draw code, but im not sure where should i call the operator so it works from the start, and i didn't understand how to make it run everytime and not once.

Read more here: https://stackoverflow.com/questions/64943706/draw-custom-grid-in-blender-view

Content Attribution

This content was originally published by Wurunduk at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: