forked from millionart/MACHIN3tools_zh-Hans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.py
219 lines (149 loc) · 7.99 KB
/
handlers.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
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
import bpy
from bpy.app.handlers import persistent
from . utils.draw import remove_object_axes_drawing_handler, draw_focus_HUD, draw_surface_slide_HUD, draw_screen_cast_HUD
from . utils.registration import get_prefs, reload_msgbus, get_addon
from . utils.group import update_group_name, select_group_children
from . utils.light import adjust_lights_for_rendering, get_area_light_poll
from . utils.view import sync_light_visibility
import time
focusHUD = None
surfaceslideHUD = None
screencastHUD = None
@persistent
def update_msgbus(none):
reload_msgbus()
@persistent
def update_object_axes_drawing(none):
remove_object_axes_drawing_handler()
@persistent
def update_group(none):
context = bpy.context
if context.mode == 'OBJECT':
# avoid AttributeError: 'Context' object has no attribute 'active_object'
active = context.active_object if getattr(context, 'active_object', None) and context.active_object.M3.is_group_empty and context.active_object.select_get() else None
# AUTO SELECT
if context.scene.M3.group_select and active:
select_group_children(context.view_layer, active, recursive=context.scene.M3.group_recursive_select)
# STORE USER-SET EMPTY SIZE
if active:
# without this you can't actually set a new empty size, because it would be immediately reset to the stored value, if group_hide is enabled
if round(active.empty_display_size, 4) != 0.0001 and active.empty_display_size != active.M3.group_size:
active.M3.group_size = active.empty_display_size
# HIDE / UNHIDE
if context.scene.M3.group_hide and getattr(context, 'visible_objects', None):
selected = [obj for obj in context.visible_objects if obj.M3.is_group_empty and obj.select_get()]
unselected = [obj for obj in context.visible_objects if obj.M3.is_group_empty and not obj.select_get()]
if selected:
for group in selected:
group.show_name = True
group.empty_display_size = group.M3.group_size
if unselected:
for group in unselected:
group.show_name = False
# store existing non-zero size
if round(group.empty_display_size, 4) != 0.0001:
group.M3.group_size = group.empty_display_size
group.empty_display_size = 0.0001
@persistent
def update_asset(none):
context = bpy.context
# return
if context.mode == 'OBJECT':
# avoid AttributeError: 'Context' object has no attribute 'active_object'
active = getattr(context, 'active_object', None)
operators = context.window_manager.operators
if operators and active and active.type == 'EMPTY' and active.instance_collection and active.instance_type == 'COLLECTION':
lastop = operators[-1]
if lastop.bl_idname == 'OBJECT_OT_transform_to_mouse':
# print("inserting an asset")
# start = time.time()
for obj in context.scene.objects:
if obj.MM.isstashobj:
# print(" STASH!")
for col in obj.users_collection:
# print(f" unlinking {obj.name} from {col.name}")
col.objects.unlink(obj)
if obj.DM.isbackup:
# print(" DECAL BACKUP!")
for col in obj.users_collection:
# print(f" unlinking {obj.name} from {col.name}")
col.objects.unlink(obj)
# print(f" done, after {time.time() - start:.20f} seconds")
@persistent
def focus_HUD(scene):
global focusHUD
# if you unregister the addon, the handle will somehow stay arround as a capsule object with the following name
# despite that, the object will return True, and so we need to check for this or no new handler will be created when re-registering
if focusHUD and "RNA_HANDLE_REMOVED" in str(focusHUD):
focusHUD = None
history = scene.M3.focus_history
if history:
if not focusHUD:
focusHUD = bpy.types.SpaceView3D.draw_handler_add(draw_focus_HUD, (bpy.context, (1, 1, 1), 1, 2), 'WINDOW', 'POST_PIXEL')
elif focusHUD:
bpy.types.SpaceView3D.draw_handler_remove(focusHUD, 'WINDOW')
focusHUD = None
@persistent
def surface_slide_HUD(scene):
global surfaceslideHUD
# if you unregister the addon, the handle will somehow stay arround as a capsule object with the following name
# despite that, the object will return True, and so we need to check for this or no new handler will be created when re-registering
if surfaceslideHUD and "RNA_HANDLE_REMOVED" in str(surfaceslideHUD):
surfaceslideHUD = None
# avoid AttributeError: 'Context' object has no attribute 'active_object'
active = getattr(bpy.context, 'active_object', None)
if active:
surfaceslide = [mod for mod in active.modifiers if mod.type == 'SHRINKWRAP' and 'SurfaceSlide' in mod.name]
if surfaceslide and not surfaceslideHUD:
surfaceslideHUD = bpy.types.SpaceView3D.draw_handler_add(draw_surface_slide_HUD, (bpy.context, (0, 1, 0), 1, 2), 'WINDOW', 'POST_PIXEL')
elif surfaceslideHUD and not surfaceslide:
bpy.types.SpaceView3D.draw_handler_remove(surfaceslideHUD, 'WINDOW')
surfaceslideHUD = None
@persistent
def screencast_HUD(scene):
global screencastHUD
wm = bpy.context.window_manager
# if you unregister the addon, the handle will somehow stay arround as a capsule object with the following name
# despite that, the object will return True, and so we need to check for this or no new handler will be created when re-registering
if screencastHUD and "RNA_HANDLE_REMOVED" in str(screencastHUD):
screencastHUD = None
# if bpy.context.window_manager.operators and scene.M3.screen_cast:
if getattr(wm, 'M3_screen_cast', False):
if not screencastHUD:
screencastHUD = bpy.types.SpaceView3D.draw_handler_add(draw_screen_cast_HUD, (bpy.context, ), 'WINDOW', 'POST_PIXEL')
elif screencastHUD:
bpy.types.SpaceView3D.draw_handler_remove(screencastHUD, 'WINDOW')
screencastHUD = None
debug = False
# debug = True
@persistent
def decrease_lights_on_render_start(scene):
m3 = scene.M3
if get_prefs().activate_render and get_prefs().activate_shading_pie and get_prefs().render_adjust_lights_on_render and get_area_light_poll() and m3.adjust_lights_on_render:
if scene.render.engine == 'CYCLES':
last = m3.adjust_lights_on_render_last
divider = m3.adjust_lights_on_render_divider
# decrease on start of rendering
if last in ['NONE', 'INCREASE'] and divider > 1:
if debug:
print()
print("decreasing lights for cycles when starting render")
m3.adjust_lights_on_render_last = 'DECREASE'
m3.is_light_decreased_by_handler = True
adjust_lights_for_rendering(mode='DECREASE')
if get_prefs().activate_render and get_prefs().render_sync_light_visibility:
sync_light_visibility(scene)
@persistent
def increase_lights_on_render_end(scene):
m3 = scene.M3
if get_prefs().activate_render and get_prefs().activate_shading_pie and get_prefs().render_adjust_lights_on_render and get_area_light_poll() and m3.adjust_lights_on_render:
if scene.render.engine == 'CYCLES':
last = m3.adjust_lights_on_render_last
# increase again when finished
if last == 'DECREASE' and m3.is_light_decreased_by_handler:
if debug:
print()
print("increasing lights for cycles when finshing/aborting render")
m3.adjust_lights_on_render_last = 'INCREASE'
m3.is_light_decreased_by_handler = False
adjust_lights_for_rendering(mode='INCREASE')