How can I reload a UI operator instead of creating a copy of it when I run a script? I set up an operator script where I move the active object 5 units in the x axis every time I click on the operator. However, every time I click "Run Script", a new operator is made to be clicked on instead of updating the first one. What code can I write to only have one operator copy that I can update over time?



Here's my code:

import bpy
from bpy.types import Operator 
from bpy.props import FloatVectorProperty, FloatProperty

class TestClass(Operator):
    '''Test Class'''
    bl_description = "test description"
    bl_idname = "object.test"
    bl_label = "Test Label"
    bl_options = {"REGISTER", "UNDO"}

    def execute(self,context):
        context.active_object.location.x += 5
        return {"FINISHED"}

    @classmethod  
    def poll(cls, context):  
        ob = context.active_object  
        return ob is not None and ob.mode == 'OBJECT'  

def add_it(self, context):
    self.layout.operator(
    operator = TestClass.bl_idname,
    text = TestClass.__doc__,
    icon = "QUESTION")

def register():
    bpy.utils.register_module(__name__)
    bpy.types.INFO_MT_add.append(add_it)

def unregister():
    bpy.utils.unregister_module(__name__)

if __name__ == "__main__":
    register()
When you add or alter something in your adddons register() function you also need to undo that change in unregister(). For your menu item that means adding bpy.types.INFO_MT_add.remove(add_it)

The other issue you have is running the script in blender's text editor. When you do this the code after if __name__ == "__main__": will be run, if you want to test your addon like this then add an unregister() call before the register() call. You will want to put the unregister in a try block to catch unregistering before the first register.

Blender's addons are sometimes easier to test if you save the file in your addons folder and disable/enable the addon from the preferences.