-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreload_addon.py
72 lines (51 loc) · 1.87 KB
/
reload_addon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
bl_info = {
"name": "Reload Addon",
"blender": (2, 83, 0),
"category": "Development",
}
import bpy
from bpy.props import StringProperty
import time
import os
class ReloadOperator(bpy.types.Operator):
bl_idname = "wm.reload_addon"
bl_label = "Reload Addon"
def execute(self, context):
return {'FINISHED'}
def invoke(self, context, event):
prefs = context.preferences.addons[__name__].preferences
if prefs.filepath == "" or prefs.name == "":
print("Cannot reload addon, filepath or name not set.")
return {'CANCELLED'}
now = int(time.time())
os.utime(bpy.path.abspath(prefs.filepath), (now, now))
bpy.ops.preferences.addon_disable(module=prefs.name)
bpy.ops.preferences.addon_enable(module=prefs.name)
return {'RUNNING_MODAL'}
class ReloadPreferences(bpy.types.AddonPreferences):
bl_idname = __name__
name = StringProperty(name="Addon name")
filepath = StringProperty(
name="__init__.py",
subtype='FILE_PATH',
)
def draw(self, context):
layout = self.layout
layout.prop(self, "name")
layout.prop(self, "filepath")
@bpy.app.handlers.persistent
def load_handler(dummy):
wm = bpy.context.window_manager
km = wm.keyconfigs.active.keymaps["Window"]
km.keymap_items.new('wm.reload_addon', 'F5', 'PRESS', repeat=False)
def register():
bpy.utils.register_class(ReloadOperator)
bpy.utils.register_class(ReloadPreferences)
wm = bpy.context.window_manager
km = wm.keyconfigs.active.keymaps["Window"]
km.keymap_items.new('wm.reload_addon', 'F5', 'PRESS', repeat=False)
bpy.app.handlers.load_post.append(load_handler)
def unregister():
bpy.utils.unregister_class(ReloadOperator)
bpy.utils.unregister_class(ReloadPreferences)
bpy.app.handlers.load_post.remove(load_handler)