Decreasing the grid through python

Hello,

I made a little script that allows me to increase grid, as well as another one to decrease the grid size. By it self it works well. However, i am trying to figure out how to convert into an addon. So i was wondering if someone could look at it and tell me what im doing wrong.

A few things im not sure about is, what class i should be using. Is obviously not a mesh tool, but is also not an operator, is it?

Here is the stand alone script
curGridSize = bpy.context.scene.unit_settings.scale_length #get the current grid size
maxGridSize = 1 # This is the default grid size
minGridSize = 64 # This is the minimun grid size

#Increase the grid
if (curGridSize == minGridSize):
print(“minimun grid size reached”)
else:
bpy.context.scene.unit_settings.scale_length = (curGridSize*2)


and here it is as an addon.

bl_info ={
“name”: “ODN_GridDecrease”,
“author”: “Raul Aparicio [email protected]”,
“version”: (0, 1),
“blender”: (2, 80, 0),
“location”: “”,
“description”: “GridOps, will decrease the grid”,
“warning”: “This is experimental, use at your own risk”,
“wiki_url”: “”,
“category”: “View3DOverlay”,
}
import bpy
############################################
##ODN_GridDecrease
##This operator decrease the size of the grid by half.
##bpy.context.space_data.overlay.grid_scale = 1.5

############################################
class OVERLAY_ODN_GridDecrease(bpy.types.View3DOverlay):
“”“Connect Stuff”""
bl_idname = “overlay.gridIncrease”
bl_label = “ODN_GridIncrease”

def execute(self,context):
##Code goes here
curGridSize = bpy.context.scene.unit_settings.scale_length #get the current grid size
maxGridSize = 1 # This is the default grid size
minGridSize = 64 # This is the minimun grid size

#Increase the grid
if (curGridSize == minGridSize):
print(“minimun grid size reached”)
else:
bpy.context.scene.unit_settings.scale_length = (curGridSize*2)

return{‘FINISHED’}

def register():
bpy.utils.register_class(View3DOverlay_ODN_GridDecrease)
def unregister():
bpy.utils.unregister_class(View3DOverlay_ODN_GridDecrease)
############################################
####END OF ODN Collapse
############################################

If you just want to convert your script into an add-on, then yes you should use an operator.

Anyway I had a look at the add-on code you posted, and I found a couple of mistakes:

  1. You define a class called OVERLAY_ODN_GridDecrease, but in the register (and unregister) function you try to register View3DOverlay_ODN_GridDecrease. They should both be the same.
  2. If you want an operator (which I think you do) then your class should be bpy.types.Operator, not bpy.types.View3DOverlay
  3. bl_idname should be only lower-case. So instead of "overlay.gridIncrease" use something like "overlay.grid_increase"
  4. You shouldn’t use bpy.context inside an operator, just use context instead. For example:
curGridSize = context.scene.unit_settings.scale_length

If you fix those things your code should work fine. Once you’ve installed your add-on you will be able to find your operator from the operator search.