forked from WonkyGrub/Blender_AutoFakeDataBlocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoFakeDataBlocks_0_3.py
315 lines (272 loc) · 11.4 KB
/
AutoFakeDataBlocks_0_3.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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
bl_info = {
"name": "Advanced Save Unused Data Blocks",
"author": "WonkyG",
"version": (0, 3),
"blender": (4, 2, 0),
"location": "File Menu > Save Unused Data Blocks Settings",
"description": "Automatically or manually enable fake user for unused data blocks based on category selection",
"warning": "",
"category": "System"
}
import bpy
from bpy.props import BoolProperty
from bpy.app.handlers import persistent
from itertools import chain
def set_fake_users_in_data(datablock, action=True):
for data in datablock:
data.use_fake_user = action
# Handler function
@persistent
def auto_enable_fakeuser(dummy):
print("Attempting to auto-enable fake users before save...")
prefs = bpy.context.preferences.addons[__name__].preferences
if not prefs.auto_save_enable:
print("Auto-save feature is disabled in the preferences.")
return
actions = {
"set_brush": bpy.data.brushes,
"set_camera": bpy.data.cameras,
"set_curve": bpy.data.curves,
"set_font": bpy.data.fonts,
"set_image": bpy.data.images,
"set_light": bpy.data.lights,
"set_lattice": bpy.data.lattices,
"set_mask": bpy.data.masks,
"set_material": bpy.data.materials,
"set_mesh": bpy.data.meshes,
"set_metaball": bpy.data.metaballs,
"set_movieclip": bpy.data.movieclips,
"set_node_group": bpy.data.node_groups,
"set_object": bpy.data.objects,
"set_particle": bpy.data.particles,
"set_texture": bpy.data.textures,
"set_world": bpy.data.worlds,
# Add other data blocks here following the same pattern.
}
for attr, block in actions.items():
if getattr(prefs, attr):
print(f"Setting fake users for: {attr}")
set_fake_users_in_data(block)
class FakeUserPreferences(bpy.types.AddonPreferences):
bl_idname = __name__
# Definition of the auto_save_enable BooleanProperty
auto_save_enable: BoolProperty(
name="Enable Auto Save Fake User",
description="Automatically adds a fake user to selected data types before saving",
default=True
)
set_brush: BoolProperty(
name="Brushes",
description="Sets fake users for all brush data",
default=True
)
set_camera: BoolProperty(
name="Cameras",
description="Sets fake users for all camera data",
default=True
)
set_curve: BoolProperty(
name="Curves",
description="Sets fake users for all curve data",
default=True
)
set_font: BoolProperty(
name="Fonts",
description="Sets fake users for all font data",
default=True
)
set_image: BoolProperty(
name="Images",
description="Sets fake users for all image data",
default=True
)
set_light: BoolProperty(
name="Lights",
description="Sets fake users for all light data",
default=True
)
set_lattice: BoolProperty(
name="Lattices",
description="Sets fake users for all lattice data",
default=True
)
set_mask: BoolProperty(
name="Masks",
description="Sets fake users for all mask data",
default=True
)
set_material: BoolProperty(
name="Materials",
description="Sets fake users for all material data",
default=True
)
set_mesh: BoolProperty(
name="Meshes",
description="Sets fake users for all mesh data",
default=True
)
set_metaball: BoolProperty(
name="Metaballs",
description="Sets fake users for all metaball data",
default=True
)
set_movieclip: BoolProperty(
name="Movieclips",
description="Sets fake users for all movieclip data",
default=True
)
set_node_group: BoolProperty(
name="Node Groups",
description="Sets fake users for all node group data",
default=True
)
set_object: BoolProperty(
name="Objects",
description="Sets fake users for all object data",
default=True
)
set_particle: BoolProperty(
name="Particles",
description="Sets fake users for all particle data",
default=True
)
set_texture: BoolProperty(
name="Textures",
description="Sets fake users for all texture data",
default=True
)
set_world: BoolProperty(
name="Worlds",
description="Sets fake users for all world data",
default=True
)
def draw(self, context):
layout = self.layout
layout.prop(self, "auto_save_enable")
# Create a closed dropdown panel for data type selections
box = layout.box()
col = box.column()
col.label(text="Data Types to Auto-Save:", icon='MODIFIER')
col.prop(self, "set_brush", text="Brushes", toggle=True)
col.prop(self, "set_camera", text="Cameras", toggle=True)
col.prop(self, "set_curve", text="Curves", toggle=True)
col.prop(self, "set_font", text="Fonts", toggle=True)
col.prop(self, "set_image", text="Images", toggle=True)
col.prop(self, "set_light", text="Lights", toggle=True)
col.prop(self, "set_lattice", text="Lattices", toggle=True)
col.prop(self, "set_mask", text="Masks", toggle=True)
col.prop(self, "set_material", text="Materials", toggle=True)
col.prop(self, "set_mesh", text="Meshes", toggle=True)
col.prop(self, "set_metaball", text="Metaballs", toggle=True)
col.prop(self, "set_movieclip", text="Movieclips", toggle=True)
col.prop(self, "set_node_group", text="Node Groups", toggle=True)
col.prop(self, "set_object", text="Objects", toggle=True)
col.prop(self, "set_particle", text="Particles", toggle=True)
col.prop(self, "set_texture", text="Textures", toggle=True)
col.prop(self, "set_world", text="Worlds", toggle=True)
# Draw other properties
class RAFU_OT_ApplyFakeUsersFromPreferences(bpy.types.Operator):
"""Apply fake users according to saved addon preferences"""
bl_idname = "system.apply_fake_users_from_preferences"
bl_label = "Apply Fake Users From Preferences"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
print("Applying fake users according to preferences...")
prefs = context.preferences.addons[__name__].preferences
actions = {
"set_brush": bpy.data.brushes,
"set_camera": bpy.data.cameras,
"set_curve": bpy.data.curves,
"set_font": bpy.data.fonts,
"set_image": bpy.data.images,
"set_light": bpy.data.lights,
"set_lattice": bpy.data.lattices,
"set_mask": bpy.data.masks,
"set_material": bpy.data.materials,
"set_mesh": bpy.data.meshes,
"set_metaball": bpy.data.metaballs,
"set_movieclip": bpy.data.movieclips,
"set_node_group": bpy.data.node_groups,
"set_object": bpy.data.objects,
"set_particle": bpy.data.particles,
"set_texture": bpy.data.textures,
"set_world": bpy.data.worlds,
# Add other data blocks here following the same pattern.
}
for attr, block in actions.items():
if getattr(prefs, attr):
print(f"Manually setting fake users for: {attr}")
set_fake_users_in_data(block)
return {'FINISHED'}
class RAFU_OT_SetFakeUsersInteractive(bpy.types.Operator):
"""Interactively select data blocks to apply fake users"""
bl_idname = "system.set_fake_users_interactive"
bl_label = "Set Fake Users Interactively"
bl_options = {'REGISTER', 'UNDO'}
# Temporary properties, mirroring the ones in FakeUserPreferences but used for this operation only.
set_brush: BoolProperty(name="Brushes", default=True)
set_camera: BoolProperty(name="Cameras", default=True)
set_curve: BoolProperty(name="Curves", default=True)
set_font: BoolProperty(name="Fonts", default=True)
set_image: BoolProperty(name="Images", default=True)
set_light: BoolProperty(name="Lights", default=True)
set_lattice: BoolProperty(name="Lattices", default=True)
set_mask: BoolProperty(name="Masks", default=True)
set_material: BoolProperty(name="Materials", default=True)
set_mesh: BoolProperty(name="Meshes", default=True)
set_metaball: BoolProperty(name="Metaballs", default=True)
set_movieclip: BoolProperty(name="Movieclips", default=True)
set_node_group: BoolProperty(name="Node Groups", default=True)
set_object: BoolProperty(name="Objects", default=True)
set_particle: BoolProperty(name="Particles", default=True)
set_texture: BoolProperty(name="Textures", default=True)
set_world: BoolProperty(name="Worlds", default=True)
# Continue for eac data type...
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self)
def execute(self, context):
# Logic to apply fake users based on the selections made in the dialog.
actions = {
"set_brush": bpy.data.brushes,
"set_camera": bpy.data.cameras,
"set_curve": bpy.data.curves,
"set_font": bpy.data.fonts,
"set_image": bpy.data.images,
"set_light": bpy.data.lights,
"set_lattice": bpy.data.lattices,
"set_mask": bpy.data.masks,
"set_material": bpy.data.materials,
"set_mesh": bpy.data.meshes,
"set_metaball": bpy.data.metaballs,
"set_movieclip": bpy.data.movieclips,
"set_node_group": bpy.data.node_groups,
"set_object": bpy.data.objects,
"set_particle": bpy.data.particles,
"set_texture": bpy.data.textures,
"set_world": bpy.data.worlds,
# Continue for each data type...
}
for attr, block in actions.items():
if getattr(self, attr):
print(f"Interactively setting fake users for: {attr}")
set_fake_users_in_data(block)
return {'FINISHED'}
def menu_func(self, context):
self.layout.operator(RAFU_OT_ApplyFakeUsersFromPreferences.bl_idname)
self.layout.operator(RAFU_OT_SetFakeUsersInteractive.bl_idname)
def register():
print("Registering Advanced Save Unused Data Blocks addon...")
bpy.utils.register_class(FakeUserPreferences)
bpy.utils.register_class(RAFU_OT_ApplyFakeUsersFromPreferences) # Updated name
bpy.utils.register_class(RAFU_OT_SetFakeUsersInteractive) # New class
bpy.types.TOPBAR_MT_file_external_data.append(menu_func)
bpy.app.handlers.save_pre.append(auto_enable_fakeuser)
def unregister():
print("Unregistering Advanced Save Unused Data Blocks addon...")
bpy.types.TOPBAR_MT_file_external_data.remove(menu_func)
bpy.utils.unregister_class(RAFU_OT_ApplyFakeUsersFromPreferences) # Updated name
bpy.utils.unregister_class(RAFU_OT_SetFakeUsersInteractive) # New class
bpy.utils.unregister_class(FakeUserPreferences)
bpy.app.handlers.save_pre.remove(auto_enable_fakeuser)
if __name__ == "__main__":
register()