forked from AlienPolygon/pribambase
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy path__init__.py
More file actions
268 lines (217 loc) · 8.39 KB
/
__init__.py
File metadata and controls
268 lines (217 loc) · 8.39 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# Copyright (c) 2021 lampysprites
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# import wheel deps
import sys
import os.path as path
from glob import glob
thirdparty = path.join(path.dirname(__file__), "thirdparty", "*.whl")
sys.path += glob(thirdparty)
from bpy.app.handlers import persistent
from .async_loop import *
from .props import *
from .sync import *
from .image import *
from .ui import *
from .object import *
from .animation import *
from .modify import *
from .util import *
from .setup import *
from .addon import addon
bl_info = {
"name": "Pribambase",
"author": "lampysprites",
"description": "Paint pixelart textures in Blender using Aseprite",
"blender": (2, 80, 0),
"version": (2, 6, 1),
"category": "Paint"
}
classes = (
# Property types
SB_State,
SB_Preferences,
SB_ObjectProperties,
SB_ImageProperties,
SB_ShaderNodeTreeProperties,
SB_ActionProperties,
# Operators
SB_OT_server_start,
SB_OT_server_stop,
SB_OT_uv_send,
SB_OT_uv_send_ui,
SB_OT_send_texture_list,
SB_OT_sprite_stub,
SB_OT_sprite_open,
SB_OT_sprite_new,
SB_OT_sprite_edit,
SB_OT_sprite_edit_copy,
SB_OT_sprite_replace,
SB_OT_sprite_make_animated,
SB_OT_sprite_purge,
SB_OT_material_add,
SB_OT_plane_add,
SB_OT_sprite_reload_all,
SB_OT_grid_set,
SB_OT_update_image,
SB_OT_update_image_layers,
SB_OT_update_spritesheet,
SB_OT_update_frame,
SB_OT_new_texture,
SB_OT_action_preview_set,
SB_OT_action_preview_clear,
SB_OT_spritesheet_rig,
SB_OT_spritesheet_unrig,
SB_OT_report,
SB_OT_setup,
SB_OT_launch,
# Panels
SB_PT_link,
SB_PT_edit,
SB_PT_uv_draw,
SB_PT_animation,
SB_PT_sprite,
SB_PT_sprite_edit,
# Menus
SB_MT_sprite
)
addon_keymaps = []
def register():
# async thread
async_loop.setup_asyncio_executor()
# types
from bpy.utils import register_class
for cls in classes:
register_class(cls)
# custom data
bpy.types.Scene.sb_state = bpy.props.PointerProperty(type=SB_State)
bpy.types.Image.sb_props = bpy.props.PointerProperty(type=SB_ImageProperties)
bpy.types.Action.sb_props = bpy.props.PointerProperty(type=SB_ActionProperties)
bpy.types.Object.sb_props = bpy.props.PointerProperty(type=SB_ObjectProperties)
bpy.types.ShaderNodeTree.sb_props = bpy.props.PointerProperty(type=SB_ShaderNodeTreeProperties)
# add menu items
try:
editor_menus = bpy.types.IMAGE_MT_editor_menus
except AttributeError:
editor_menus = bpy.types.MASK_MT_editor_menus
editor_menus.append(SB_MT_sprite.header_draw)
# hotkeys
try:
kcfg = bpy.context.window_manager.keyconfigs.addon
# register empty item
key = lambda km,idname: addon_keymaps.append((km, km.keymap_items.new(idname=idname, type='NONE', value='PRESS')))
km_screen = kcfg.keymaps.new(name="Window", space_type='EMPTY')
key(km_screen, "pribambase.server_start")
key(km_screen, "pribambase.server_stop")
key(km_screen, "pribambase.grid_set")
key(km_screen, "pribambase.action_preview_set")
key(km_screen, "pribambase.action_preview_clear")
key(km_screen, "pribambase.sprite_reload_all")
km_v3d = kcfg.keymaps.new(name="3D View", space_type='VIEW_3D')
key(km_v3d, "pribambase.plane_add")
key(km_v3d, "pribambase.material_add")
key(km_v3d, "pribambase.spritesheet_rig")
km_img = kcfg.keymaps.new(name="Image", space_type='IMAGE_EDITOR')
key(km_img, "pribambase.uv_send")
key(km_img, "pribambase.sprite_open")
key(km_img, "pribambase.sprite_new")
key(km_img, "pribambase.sprite_edit")
key(km_img, "pribambase.sprite_edit_copy")
key(km_img, "pribambase.sprite_purge")
key(km_img, "pribambase.sprite_replace")
key(km_img, "pribambase.sprite_make_animated")
except Exception as e:
# not sure when it fails (headless launch?) but keymaps don't affect functionality, let's ignore and continue
bpy.ops.pribambase.report(message_type='WARNING', message=f"Failed to register addon keymap: {str(e)}")
# execute async loop
# delay is just in case something else happens at startup
# `persistent` protects the timer if the user loads a file before it fires
bpy.app.timers.register(start, first_interval=0.5, persistent=True)
def unregister():
if addon.server_up:
addon.stop_server()
if bpy.app.timers.is_registered(start):
bpy.app.timers.unregister(start)
if sb_on_load_post in bpy.app.handlers.load_post:
bpy.app.handlers.load_post.remove(sb_on_load_post)
if sb_on_load_pre in bpy.app.handlers.load_pre:
bpy.app.handlers.load_pre.remove(sb_on_load_pre)
if sb_on_save_post in bpy.app.handlers.save_post:
bpy.app.handlers.save_post.remove(sb_on_save_post)
if sb_on_depsgraph_update_post in bpy.app.handlers.depsgraph_update_post:
bpy.app.handlers.depsgraph_update_post.remove(sb_on_depsgraph_update_post)
try:
editor_menus = bpy.types.IMAGE_MT_editor_menus
except AttributeError:
editor_menus = bpy.types.MASK_MT_editor_menus
editor_menus.remove(SB_MT_sprite.header_draw)
global addon_keymaps
for km,item in addon_keymaps:
km.keymap_items.remove(item)
addon_keymaps = []
del bpy.types.Scene.sb_state
del bpy.types.Image.sb_props
del bpy.types.Action.sb_props
del bpy.types.Object.sb_props
del bpy.types.ShaderNodeTree.sb_props
from bpy.utils import unregister_class
for cls in reversed(classes):
unregister_class(cls)
# hash for the set of image sources/names that is used to check if new images were added
_images_hv = 0
@persistent
def start():
# hasn't been called for already loaded file
sb_on_load_post(None)
if sb_on_load_post not in bpy.app.handlers.load_post:
bpy.app.handlers.load_post.append(sb_on_load_post)
if sb_on_load_pre not in bpy.app.handlers.load_pre:
bpy.app.handlers.load_pre.append(sb_on_load_pre)
if sb_on_save_post not in bpy.app.handlers.save_post:
bpy.app.handlers.save_post.append(sb_on_save_post)
if sb_on_depsgraph_update_post not in bpy.app.handlers.depsgraph_update_post:
bpy.app.handlers.depsgraph_update_post.append(sb_on_depsgraph_update_post)
@persistent
def sb_on_load_post(scene):
global _images_hv
_images_hv = hash(frozenset(img.sb_props.sync_name for img in bpy.data.images))
# these settings aren't supposed to persist but 'SKIP_SAVE' flag didn't do anyhitng so let's clear them manually if needed
if addon.state.action_preview_enabled:
bpy.context.scene.use_preview_range = False
addon.state.action_preview = None
addon.state.action_preview_enabled = False
@persistent
def sb_on_load_pre(scene):
if addon.server_up:
addon.stop_server()
@persistent
def sb_on_save_post(scene):
if addon.server_up:
bpy.ops.pribambase.send_texture_list()
@persistent
def sb_on_depsgraph_update_post(scene):
global _images_hv
dg = bpy.context.evaluated_depsgraph_get()
if dg.id_type_updated('IMAGE'):
imgs = frozenset(img.sb_props.sync_name for img in bpy.data.images)
hv = hash(imgs)
if _images_hv != hv:
_images_hv = hv
if addon.server_up:
addon.server.send(encode.texture_list(addon.state.identifier, addon.texture_list))