Key map for an addon activate the class more than one time?

I am creating an addon I have created key map for that addon,but the problem is when I press the hotkey that I set up it activate the class more than one time,while if I press it from menu it happen only 1 time.I tried to find a solution but I couldn't find it, here is the key map portion of my addon:

so what is the problem?

• Do you have the addon in blender's text editor and click run script? Restart blender and try again. – sambler Mar 10 '15 at 8:37
• @sambler when I restart blender it do work well,but when I run the operator some more times it start to do as I said,so I want to know what is the problem – Omar Emara Mar 10 '15 at 14:56

When you have an addon listed in the preferences you click the tick box to enable it, by enabling it the addons register() function is run. When you disable the addon the unregister() function is run.

Most addon scripts will end with a section of code similar to -

if __name__ == "__main__":
register()


When you run that addon script in blenders text editor the register() function will be run, and as you run the script again the register() function will again be run. At this stage your keyboard additions have been added twice as the unregister() function has not been run to remove the first entry.

There are two ways to solve this, first is to add an unregister() function call to the end of the script, as it will throw an exception the first time it is run you will want to add in within a try block.

if __name__ == "__main__":
try:
unregister()
except:
pass
register()


Now no matter how many times you click "Run script" you should only have one keyboard entry.

This is useful as you develop using blender's text editor but I would recommend you remove it before sharing your script.

The second way to solve this issue is to save the file to disk and use an external text editor. When you save changes to the addon, disable and enable it in the preferences, this will run the unregister() function for you before the new register() function is called as you enable it.