-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaddon-reloader.py
142 lines (112 loc) · 4.56 KB
/
addon-reloader.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
bl_info = {
"name": "Addon Reloader",
"description": "Save all the hassle of opening Preferences a thousand times when reloading your addon for testing.",
"author": "Amaral Krichman",
"version": (0, 0, 1),
"blender": (2, 80, 0),
"location": "Global/Text Editor",
"warning": "",
"wiki_url": "",
"category": "Development",
}
import bpy
import time
from pathlib import Path
from bpy_extras.io_utils import ImportHelper
#----------------------------------- RELOAD ADDON -----------------------------------
class ReloadAddonOperator(bpy.types.Operator):
bl_idname = "wm.addon_reload"
bl_label = "Reload Addon"
bl_description = ""
bl_options = {'REGISTER'}
def clean_path(self, context):
p = context.preferences.addons[__name__].preferences.bookmark
name = Path(p).stem
return name
def execute(self, context):
p = context.preferences.addons[__name__].preferences.bookmark
name = self.clean_path(context)
bpy.ops.preferences.addon_disable(module=name)
time.sleep(0.05)
bpy.ops.preferences.addon_remove(module=name)
time.sleep(0.05)
bpy.ops.preferences.addon_install(filepath=p)
time.sleep(0.05)
bpy.ops.preferences.addon_enable(module=name)
time.sleep(0.05)
return {'FINISHED'}
def invoke(self, context, event):
if context.preferences.addons[__name__].preferences.bookmark == "":
bpy.ops.wm.addon_reloader_select_addon_path('INVOKE_DEFAULT')
self.execute(context)
return {'FINISHED'}
else:
self.execute(context)
return {'FINISHED'}
#----------------------------------- FILEBROWSER -----------------------------------
class SelectAddonPathFileBrowser(bpy.types.Operator, ImportHelper):
bl_idname = "wm.addon_reloader_select_addon_path"
bl_label = "Select Addon Path"
bl_description = ""
filter_glob: bpy.props.StringProperty( default='*py', options={'HIDDEN'} )
def execute(self, context):
context.preferences.addons[__name__].preferences.bookmark = self.filepath
self.report({'INFO'}, "Selected file: " + self.filepath)
return {'FINISHED'}
#----------------------------------- PANEL -----------------------------------
class AddonReloaderPanel(bpy.types.Panel):
bl_label = "Addon Reloader"
bl_idname = "TEXT_PT_addon_reloader"
bl_space_type = 'TEXT_EDITOR'
bl_region_type = 'UI'
bl_category = "Dev"
def draw(self, context):
bk = context.preferences.addons[__name__].preferences.bookmark
bk_n = Path(bk).stem
layout = self.layout
row = layout.row()
if bk == "":
row.alignment = 'CENTER'
row.label(text="Select an addon!", icon='ERROR')
else:
layout.label(text="Editing: {}".format(bk_n), icon='INFO')
if bk == "":
col = layout.column()
col.operator("wm.addon_reloader_select_addon_path", text="", icon='FILEBROWSER')
col.scale_y = 2
else:
row = layout.row(align=True)
row.operator("wm.addon_reload", text="Reload Addon", icon='FILE_REFRESH')
row.operator("wm.addon_reloader_select_addon_path", text="", icon='FILEBROWSER')
row.scale_y = 2
layout.operator("screen.userpref_show",text="", icon='PREFERENCES')
#----------------------------------- MENU (templates) -----------------------------------
class AddonReloaderPreferences(bpy.types.AddonPreferences):
bl_idname = __name__
bookmark: bpy.props.StringProperty(name="Selected Addon's Path", options={'TEXTEDIT_UPDATE'}, default="")
def draw(self, context):
layout = self.layout
row = layout.row()
row.label(text="Path to addon is: ")
row = layout.row()
if self.bookmark == "":
row.label(text="No path selected!")
row = layout.row()
row.operator("wm.addon_reloader_select_addon_path", text="Select Addon's Path")
else:
row.label(text=self.bookmark)
row = layout.row()
row.operator("wm.addon_reloader_select_addon_path", text="Change Addon's Path")
#####################################################################
classes = (
ReloadAddonOperator,
SelectAddonPathFileBrowser,
AddonReloaderPanel,
AddonReloaderPreferences,
)
def register():
for c in classes:
bpy.utils.register_class(c)
def unregister():
for c in classes:
bpy.utils.unregister_class(c)