1
$\begingroup$

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?

enter image description here

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()
$\endgroup$
  • $\begingroup$ bpy.types.INFO_MT_add.remove(add_it) in the unregister()? Nope: But you can always use F8 to reload all scripts. $\endgroup$ – florianfelix Jul 6 '16 at 23:05
  • $\begingroup$ F8 does work. Do you know what button there is to click that's the same as F8? Also, do you know where the "remove" function is in the documentation? Even though it didn't work, I'd like to look it up, but I can't find it. $\endgroup$ – DragonautX Jul 6 '16 at 23:15
  • $\begingroup$ I can't find it either in the documentation (well hidden). F8 doesn't have a button. $\endgroup$ – florianfelix Jul 6 '16 at 23:21
  • $\begingroup$ Oh ok. I wish they had it. And alright. I've noticed I can always type Reload Scripts after pressing spacebar. $\endgroup$ – DragonautX Jul 6 '16 at 23:30
  • $\begingroup$ Actually, I found that remove() (and append(), just to add it on) can be found in older versions of the blender documentation, like here, in Blender 2.61.4. It's apparently a Menu() method. bpy.types.Menu.append, and then "Autocomplete" in the console can give you a description. $\endgroup$ – DragonautX Jul 6 '16 at 23:53
1
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ Oh ok, so I just have to find a way to access unregister(), and testing it from the addons folder is one way to do it. I can also make an operator and access unregister() with execute() then. Thanks! @florianfelix looks like you were actually right haha. $\endgroup$ – DragonautX Jul 7 '16 at 5:56
  • $\begingroup$ You can add the unregister() call to your script, before the call to register() so you can run your addon script inside blender, here is an example $\endgroup$ – sambler Jul 7 '16 at 7:13
  • $\begingroup$ It doesn't work for me. It keeps adding on another operator instance. I also tried it "manually". I put unregister() only in the if statement first, Run Script, and all the instances disappear. When I put register() only and Run Script, all the instances appear again plus one more. $\endgroup$ – DragonautX Jul 7 '16 at 7:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.