-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathLodify.py
1418 lines (1106 loc) · 57 KB
/
Lodify.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
###########################################################################################
bl_info = {
"name" : "Lodify",
"description" : "LOD and Proxy system for blender 2.80 & above",
"author" : "DB3D",
"version" : (0, 3),
"blender" : (2, 80, 0),
"location" : "''Propeties'' > ''Object Data'' > ''Level of Detail''",
"warning" : "",
"wiki_url" : "https://devtalk.blender.org/t/level-of-detail-addon/12840",
"tracker_url" : "https://devtalk.blender.org/t/level-of-detail-addon/12840",
"category" : "Object"
}
import bpy
###########################################################################################
doctxt="""
v0.3 - dirty fix - correct the rendered view lag issue, replacing depsgraph update handler with timer loop for checking rendered view state
_____________________________________________________________________________________________
About the addon
_____________________________________________________________________________________________
- You can choose which lod you want to display in either the active viewport, the
rendered view or the final render individually.
- The LOD system use API properties stored within your object and object-data
properties, that mean that once you created a LOD system for your object,
the LOD(s) data will stick with it once you copy or append your obj to
another file for example. Even to users who don't have Lodify installed *yet*.
- You can automatically search for lods in your file according to their names,
if you use a numerical suffix "Suzanne_LOD0","Suzanne_LOD1",...
Same goes for lods using custom suffixes such as "_high" "_low" "_proxy"
- You can Batch-generate proxies on the fly (by applying a shrinkwrap modifier automatically).
- You can Batch-change LOD properties by lod name ending. There's also simple Batch rename
operator available.
- You can use this LOD system as a data backup management system if you need to store
your mesh data while working on destructive hardsurface modeling for example.
You will find two 'backup' operators in the menu next to the LOD list.
- Note that each LODs can have their own materials, as the material data is stored per
meshes and not per objects.
- Keep in mind that pointers are considered as data-users. You might want to clean
leftovers pointers from deleted objects with the 'cleanse data-block' operator.
- If you are animating via Modifiers (Bones for ex) the LOD system will work perfectly with
your animation, assuming that the Vgroups are all assigned correctly for each level of detail
(that's why it's more easy to create your lod from the final model, as the vgroup will
automatically be assigned when you simplify/decimate the topology). Shape Keys animation
are not compatible with LOD.
- Regarding the Rendered view automatic mesh-data switching:
Lodify will try to update the mesh-data on each "blender internal update signal"
(called depsgraph update), so if you use a custom shortcut or pie menu that don't send those
"depsgraph updates" you might just want to click anywhere on the viewport to send a new one.
(the default header shading viewport buttons will work 100% of the time for sure).
- Locking the interface on render is mandatory, as cycles don't like mesh data exchange while
calculating (cause instant-crash).
- The addon was NOT made with linking/override from external blends in mind.
Don't try to use Lodify with such workflows, unfortunately it will not work, as Lodify
require a dynamic data exchange (linking/overrides don't allow this, it will freeze data
and update mesh internally).
_____________________________________________________________________________________________
About the addon python code:
_____________________________________________________________________________________________
- This addon basically act as a big mesh-data exchanging system where i exchange mesh data
according to booleans stored in ui-lists.
- As i'm drawing inside object-meshdata and i'm constantly switching the active mesh,
'lod_original' pointer is used as a constance, and is stored in object properties of
all mesh-data owners. if you need to work with lodify api, always use this constance
if not None. object.data is simply not reliable (see function 'true_mesh_data', that will
find the original mesh data of an object with LOD enabled)
- The Lod-switching is done via a fct on each depsg udpate (see 'analyse_and_exchange_data')
the code just analyse the ui-lists of all objects, if list exist and if boolean filled,
the mesh-data is exchanged or restored accordingly depending on viewport shading or
pre/post render handlers.
- Due to a severe blender crash, while in rendered view, if data is changed from a fct in a
depsgraph, it will crash blender back to desktop instantaneously. To counter that, if user
in rendered view and changing rendered boolan, the view will be toggled back and forth.
You can experience this bug for yourself if you comment 'toggle_shading' in
'analyse_and_exchange_data'. Bug was on 2.80/2.81, not sure if resolved.
"""
###########################################################################################
#
# .oooooo. .
# d8P' `Y8b .o8
# 888 888 oo.ooooo. .ooooo. oooo d8b .oooo. .o888oo .ooooo. oooo d8b .oooo.o
# 888 888 888' `88b d88' `88b `888""8P `P )88b 888 d88' `88b `888""8P d88( "8
# 888 888 888 888 888ooo888 888 .oP"888 888 888 888 888 `"Y88b.
# `88b d88' 888 888 888 .o 888 d8( 888 888 . 888 888 888 o. )88b
# `Y8bood8P' 888bod8P' `Y8bod8P' d888b `Y888""8o "888" `Y8bod8P' d888b 8""888P'
# 888
# o888o
###########################################################################################
class LODIFY_OT_list_actions(bpy.types.Operator):
"""add or remove items from list"""
bl_idname = "lodify.list_action"
bl_label = ""
bl_description = "Add and remove Level of Details"
action : bpy.props.StringProperty()
mesh_n : bpy.props.StringProperty()
def execute(self, context):
msh = bpy.data.meshes[self.mesh_n]
idx = msh.lod_list_index
if self.action == 'ADD':
item = msh.lod_list.add()
item.name = msh.name
item.ui_idx = len(msh.lod_list)
msh.lod_list_index = len(msh.lod_list)-1
# fill pointer with original data if needed
fill_original_pointer(msh)
if self.action == 'REMOVE':
msh.lod_list_index -= 1
msh.lod_list.remove(idx)
# clean pointer and restore original data if needed
clean_original_pointer()
# maybe user deleted active boolean
analyse_and_exchange_data()
return {"FINISHED"}
class LODIFY_OT_clear_list(bpy.types.Operator):
"""Clear all items of the desired mesh list"""
bl_idname = "lodify.clear_list"
bl_label = ""
bl_description = "Clear all items of the list"
mesh_n : bpy.props.StringProperty()
def execute(self, context):
if bool(bpy.data.meshes[self.mesh_n].lod_list):
bpy.data.meshes[self.mesh_n].lod_list.clear()
#clean pointer and restore original data if needed
clean_original_pointer()
return{'FINISHED'}
class LODIFY_OT_docs(bpy.types.Operator):
"""Clear all items of the desired mesh list"""
bl_idname = "lodify.docs"
bl_label = ""
bl_description = "Pop up documentations"
def execute(self, context):
def draw(self, context):
global doctxt
layout = self.layout
layout.scale_y = 0.8
for line in doctxt.splitlines():
layout.label(text=line)
bpy.context.window_manager.popup_menu(draw, title = 'Documentation', icon = 'QUESTION')
return{'FINISHED'}
class LODIFY_OT_data_refresh(bpy.types.Operator):
"""scan for mesh-data to exchange manually
usually this fct is done on each despg update
within the addon, but you might want to refresh it manually"""
bl_idname = "lodify.data_refresh"
bl_label = ""
bl_description = "scan for mesh-data to exchange according to ui-list booleans"
def execute(self, context):
analyse_and_exchange_data()
return{'FINISHED'}
class LODIFY_OT_create_backup(bpy.types.Operator):
"""create a backup of the active mesh-data used by the object, and store it as a Lod"""
bl_idname = "lodify.create_backup"
bl_label = ""
bl_description = "use the lod system to create a backup of the active mesh your object is using"
def execute(self, context):
obj = context.object
new_msh = bpy.data.meshes.new_from_object(obj)
new_msh.name = obj.data.name + '_copy'
msh = true_mesh_data(obj)
bpy.ops.lodify.list_action(action='ADD',mesh_n=msh.name)
obj.lod_original.lod_list[-1].ui_lod = new_msh
return{'FINISHED'}
class LODIFY_OT_restore_backup(bpy.types.Operator):
"""restore chosen backup data to mesh-data of active object"""
bl_idname = "lodify.restore_backup"
bl_label = ""
bl_description = "restore chosen backup data to mesh-data of active object"
act_n : bpy.props.StringProperty(description="active msh name")
bck_n : bpy.props.StringProperty(description="backup msh name")
def execute(self, context):
if (self.act_n in bpy.data.meshes) and (self.bck_n in bpy.data.meshes):
act_n = bpy.data.meshes[self.act_n]
bck_n = bpy.data.meshes[self.bck_n]
exchange_bmesh_data( act_n , bck_n )
return{'FINISHED'}
class LODIFY_MT_restore_backup(bpy.types.Menu):
"""restore backup sub-menu"""
bl_idname = "LODIFY_MT_restore_backup"
bl_label = ""
def draw(self, context):
layout = self.layout
layout.label(text='Replace active data by : ')
layout.separator(factor=1.0)
obj = context.object
msh = true_mesh_data(obj)
for lod_n in [s.ui_lod.name for s in msh.lod_list if s.ui_lod]:
t=layout.operator("lodify.restore_backup", icon="LOOP_BACK", text=lod_n)
t.act_n=obj.data.name
t.bck_n=lod_n
class LODIFY_OT_cleanse_data(bpy.types.Operator):
"""when user delete meshes that use a lod system, the deleted data
may still have used-pointer that take users data. (may)
if a mesh-data iss not used in any scenes, and if this mesh-data have
an active lod-list with active pointers, or, if not used object still
have original_mesh pointer active, all pointer need to be cleanse"""
bl_idname = "lodify.cleanse_data"
bl_label = ""
bl_description = "cleanse left over pointers from deleted/non-used data (obj/msh)"
def execute(self, context):
for msh in _all_unused_meshes():
if len(msh.lod_list) !=0:
#print(f'unused list detected,start cleaning :{msh.name}')
bpy.ops.lodify.clear_list(mesh_n=msh.name)
msh.lod_enabled = False
for obj in _all_unused_objects():
if bool(obj.lod_original):
#print(f'unused original pointer detected,start cleaning :{obj.name}')
obj.lod_original = None
return{'FINISHED'}
class LODIFY_OT_clear_by_index(bpy.types.Operator):
"""clear specific slot from ui-list by it's index, starting from 0"""
bl_idname = "lodify.clear_by_index"
bl_label = ""
bl_description = "clear this specific slot"
index : bpy.props.IntProperty()
def execute(self, context):
obj = context.object
msh = true_mesh_data(obj)
msh.lod_list_index -= 1
msh.lod_list.remove(self.index)
# clean pointer and restore original data if needed
clean_original_pointer()
# maybe user deleted active boolean
analyse_and_exchange_data()
return{'FINISHED'}
class LODIFY_MT_clear_by_index(bpy.types.Menu):
"""clear_by_index"""
bl_idname = "LODIFY_MT_clear_by_index"
bl_label = ""
def draw(self, context):
layout = self.layout
layout.label(text='Clear a Specific Slot : ')
layout.separator(factor=1.0)
obj = context.object
msh = true_mesh_data(obj)
for i,s in enumerate(msh.lod_list):
t=layout.operator("lodify.clear_by_index", icon="PANEL_CLOSE", text= str(i) + ' : ' + s.ui_lod.name if s.ui_lod else str(i) )
t.index = i
class LODIFY_OT_auto_setup_op(bpy.types.Operator):
"""automatically set up lods/proxies from a naming system"""
bl_idname = "lodify.auto_setup_op"
bl_label = ""
bl_description = "automatically set up lods/proxies from a naming system"
object_n : bpy.props.StringProperty()
basic : bpy.props.StringProperty()
suffixn : bpy.props.StringProperty(default='_LOD_')
nbr : bpy.props.IntProperty(min=1,max=3)
auto : bpy.props.BoolProperty(default=False) #auto set LOD0 to render, last LOD to display
def execute(self, context):
lod_list = find_lod(self.basic,self.suffixn,self.nbr)
obj = bpy.data.objects[self.object_n]
setup_obj_lod(obj, lod_list,self.auto)
#reset default
self.suffixn = '_LOD_'
self.auto = False
return{'FINISHED'}
class LODIFY_OT_auto_setup_dialog(bpy.types.Operator):
"""Automatically set up LOD's for the whole scene by a Simple Enumeration Suffix"""
bl_idname = "lodify.auto_setup_dialog"
bl_label = "What is the name of your Object ?"
bl_options = {'REGISTER', 'INTERNAL'}
ob_all : bpy.props.EnumProperty(items= [('scene','Whole Scene assets',''),('ob','Just this asset','')],default='ob') #just ob supported right now
basic : bpy.props.StringProperty()
suffixn : bpy.props.StringProperty(default='_LOD_')
nbr : bpy.props.EnumProperty(items= [('1','1',''),('2','2',''),('3','3','')],default = '1')
@classmethod
def poll(cls, context):
return True
def execute(self, context):
bpy.ops.lodify.auto_setup_op(object_n= context.object.name, basic= self.basic, suffixn= self.suffixn,nbr= int(self.nbr),auto= True)
return {'FINISHED'}
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self)
def draw(self, context):
layout = self.layout
if self.ob_all == 'ob':
layout.prop(self, 'basic',text="")
layout.label(text=f'What is your Suffix ?')
layout.prop(self, 'suffixn',text="")
layout.label(text=f'How many Numbers after your Suffix ?')
layout.prop(self, 'nbr',text="")
if self.ob_all == 'ob':
l = find_lod(self.basic, self.suffixn, int(self.nbr))
txt=layout.column()
txt.label(text=f'We found {len(l)} Lod(s) for the automatic set-up.')
txt.scale_y = 0.9
for m in l: txt.label(text=f' " {m.name} "')
class LODIFY_OT_auto_name_setup_dialog(bpy.types.Operator):
"""Automatically set up LOD's for the whole scene by Custom Suffix"""
bl_idname = "lodify.auto_name_setup_dialog"
bl_label = "What are your LOD meshes suffixes ?"
bl_options = {'REGISTER', 'INTERNAL'}
LOD0 : bpy.props.StringProperty(name="LOD0 ",default="_high")
LOD1 : bpy.props.StringProperty(name="LOD1 ",default="_low")
LOD2 : bpy.props.StringProperty(name="LOD2 ",default="_proxy")
@classmethod
def poll(cls, context):
return True
def execute(self, context):
for o in bpy.context.scene.objects:
if o.data.name.endswith(self.LOD0):
#if original pointer full then we got a active ui_list
if not o.lod_original:
#get name of the mesh with no suffix
msh_n = o.data.name.split(self.LOD0)[0]
#check if suffix even exist
if (msh_n+self.LOD1 in bpy.data.meshes) or (msh_n+self.LOD2 in bpy.data.meshes):
o.data.lod_enabled = True
#set up LOD0
bpy.ops.lodify.list_action(action="ADD", mesh_n=o.data.name)
o.data.lod_list[0].ui_lod = o.data
o.data.lod_list[0].ui_rdf = True
o.data.lod_list[0].ui_rdv = True
#set up LOD1
if (msh_n+self.LOD1 in bpy.data.meshes):
bpy.ops.lodify.list_action(action="ADD", mesh_n=o.data.name)
o.data.lod_list[-1].ui_lod = bpy.data.meshes[msh_n+self.LOD1]
#set up LOD2
if (msh_n+self.LOD2 in bpy.data.meshes):
bpy.ops.lodify.list_action(action="ADD", mesh_n=o.data.name)
o.data.lod_list[-1].ui_lod = bpy.data.meshes[msh_n+self.LOD2]
o.data.lod_list[-1].ui_dsp = True
o.data.lod_enabled = False
return {'FINISHED'}
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self)
def draw(self, context):
layout = self.layout
layout.prop(self, 'LOD0')
layout.prop(self, 'LOD1')
layout.prop(self, 'LOD2')
layout.label(text="Lodify will automatically set-up LOD's for LOD0 object")
layout.label(text="By searching for meshes-names suffixes (here above) ")
layout.label(text="If the LOD0 object have empty slots of course")
class LODIFY_OT_auto_lod_generation(bpy.types.Operator):
"""automatically create LOD meshes for selection by using the shrinkwrap modifier"""
bl_idname = "lodify.auto_lod_generation"
bl_label = ""
bl_description = "automatically create an LOD mesh by using a shrinkwrap modifier automatically"
def execute(self, context): #special thanks to 'Ludwig Seibt' for this shrinkwrap idea !
active = bpy.context.object
selection = bpy.context.selected_objects
mesh_selection = [o for o in bpy.context.selected_objects if o.type == 'MESH']
for o in mesh_selection:
#add proxy mesh, modifiers
bpy.ops.mesh.primitive_ico_sphere_add(radius=100,location=o.location)
proxy = bpy.context.view_layer.objects.active
proxy.name = o.name + " Proxy"
proxy.data.name = proxy.name.lower().replace(' ','_')
if len(o.material_slots):
mat = o.material_slots[0].material
proxy.data.materials.append(mat)
#add modifier
proxy.modifiers.new(type="SHRINKWRAP",name="SHRINKWRAP")
proxy.modifiers["SHRINKWRAP"].target = o
override = bpy.context.copy()
override['object'] = proxy
bpy.ops.object.modifier_apply(override,modifier="SHRINKWRAP")
#add Lodify Slot
msh = true_mesh_data(o) #make sure to get the correct data
msh.lod_enabled = True
bpy.ops.lodify.list_action(action="ADD", mesh_n=msh.name)
msh.lod_list[-1].ui_lod = o.data
msh.lod_list[-1].ui_rdf = True
msh.lod_list[-1].ui_rdv = True
bpy.ops.lodify.list_action(action="ADD", mesh_n=msh.name)
msh.lod_list[-1].ui_lod = proxy.data
msh.lod_list[-1].ui_dsp = True
#delete proxy, no longer needed, as it is now stored in pointer
bpy.ops.object.delete(override,use_global=False)
#restore selection/active
bpy.context.view_layer.objects.active = active
for o in selection: o.select_set(state=True)
bpy.ops.ed.undo_push(message="proxy generation" )
return{'FINISHED'}
class LODIFY_OT_batch_dialog(bpy.types.Operator):
"""Dialog Box Batch Actions Center"""
bl_idname = "lodify.batch_dialog"
bl_label = "Operation Influence ?"
bl_options = {'REGISTER', 'INTERNAL'}
influence : bpy.props.EnumProperty(items= [('Scene','Whole Scene',''),('Selection','Selected Objects','')],default='Scene') #just ob supported right now
name_end : bpy.props.StringProperty()
@classmethod
def poll(cls, context):
return True
def execute(self, context):
return {'FINISHED'}
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self)
def draw(self, context):
layout = self.layout
layout.prop(self, 'influence', text='')
layout.label(text='Batch Enable/Disable Level of Detail Operation:')
row = layout.row(align=True)
t = row.operator("lodify.batch_enabled", text="Enable" , icon="MESH_ICOSPHERE" )
t.status = True
t.opt = self.influence
t = row.operator("lodify.batch_enabled", text="Disable", icon="QUIT" )
t.status = False
t.opt = self.influence
layout.label(text='Batch Change Boolean Properties by suffix:')
row = layout.row(align=True)
row.prop(self,"name_end", text='')
row = row.row(align=True)
row.scale_x = 1.2
t = row.operator("lodify.batch_status",text='',icon='RESTRICT_VIEW_OFF')
t.api = 'ui_dsp'
t.opt = self.influence
t.sti = self.name_end
if bpy.context.scene.lod.p_rdv_switch:
t = row.operator("lodify.batch_status",text='',icon='SHADING_RENDERED')
t.api = 'ui_rdv'
t.opt = self.influence
t.sti = self.name_end
if bpy.context.scene.lod.p_rdf_switch:
t = row.operator("lodify.batch_status",text='',icon='RESTRICT_RENDER_OFF')
t.api = 'ui_rdf'
t.opt = self.influence
t.sti = self.name_end
layout.separator(factor=1.0)
class LODIFY_OT_batch_rename_dialog(bpy.types.Operator):
"""Dialog Box Batch Rename selection and mesh names"""
bl_idname = "lodify.batch_rename_dialog"
bl_label = "What is your suffix ?"
bl_options = {'REGISTER', 'INTERNAL'}
suffix : bpy.props.StringProperty(default=" proxy")
separate : bpy.props.BoolProperty(default=False)
@classmethod
def poll(cls, context):
return True
def execute(self, context):
sel = bpy.context.selected_objects
for o in sel:
on = o.name
if self.separate and ('.'in on):
on = on.split('.')[0]
o.name = on + self.suffix
return {'FINISHED'}
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self)
def draw(self, context):
layout = self.layout
layout.prop(self, 'suffix', text='')
layout.label(text="Would you like to get rid of '.000' suffixes?")
layout.prop(self, 'separate', text="yes, remove everythign after '.' character")
layout.separator(factor=1.0)
class LODIFY_OT_batch_mesh_name(bpy.types.Operator):
"""batch Mesh Name = mesh_name refactor"""
bl_idname = "lodify.batch_mesh_name"
bl_label = ""
bl_description = "object 'Suzanne Big 01' mesh name will be 'suzanne_big_01'"
def execute(self, context):
sel = bpy.context.selected_objects
for o in sel:
o.data.name = o.name.lower().replace(' ','_')
return{'FINISHED'}
class LODIFY_OT_batch_enabled(bpy.types.Operator):
"""batch enable/disable lod_enabled boolean"""
bl_idname = "lodify.batch_enabled"
bl_label = ""
bl_description = "batch enable/disable lod_enabled boolean"
status : bpy.props.BoolProperty()
opt : bpy.props.StringProperty()
def execute(self, context):
if self.opt == 'Scene': sel = [o for o in bpy.context.scene.objects if o.type == 'MESH']
elif self.opt == 'Selection': sel = [o for o in bpy.context.selected_objects if o.type == 'MESH']
for ob in sel:
msh = true_mesh_data(ob)
msh.lod_enabled = self.status
analyse_and_exchange_data()
return{'FINISHED'}
class LODIFY_OT_batch_status(bpy.types.Operator):
"""batch change ui lists mesh data status"""
bl_idname = "lodify.batch_status"
bl_label = ""
bl_description = "batch change ui lists mesh data status"
api : bpy.props.StringProperty()
opt : bpy.props.StringProperty()
sti : bpy.props.StringProperty()
def execute(self, context):
if self.opt == 'Scene': sel = [o for o in bpy.context.scene.objects if o.type == 'MESH']
elif self.opt == 'Selection': sel = [o for o in bpy.context.selected_objects if o.type == 'MESH']
#loop over selection
for o in sel:
#if original pointer full then we got a active ui_list
if o.lod_original:
#loop over ui_list
for s in o.lod_original.lod_list:
#if lod pointer not empty
if s.ui_lod:
#if data name ends with our string, then change values
if s.ui_lod.name.endswith(self.sti):
exec(f's.{self.api} = True')
analyse_and_exchange_data()
return{'FINISHED'}
###########################################################################################
#
# oooooooooooo . o8o
# `888' `8 .o8 `"'
# 888 oooo oooo ooo. .oo. .ooooo. .o888oo oooo .ooooo. ooo. .oo. .oooo.o
# 888oooo8 `888 `888 `888P"Y88b d88' `"Y8 888 `888 d88' `88b `888P"Y88b d88( "8
# 888 " 888 888 888 888 888 888 888 888 888 888 888 `"Y88b.
# 888 888 888 888 888 888 .o8 888 . 888 888 888 888 888 o. )88b
# o888o `V88V"V8P' o888o o888o `Y8bod8P' "888" o888o `Y8bod8P' o888o o888o 8""888P'
#
###########################################################################################
def true_mesh_data(obj):
""" because user will constantly switch data,
and gui rely on data, we'll need to find correct mesh"""
if obj.lod_original: return obj.lod_original
else: return obj.data
def only_one_prop(msh,active_idx,prop_api):
""" only one boolean props active at the time per type"""
clist = msh.lod_list
#loop over ui list slots
for p in clist:
if p.ui_idx != active_idx:
exec(f"p.{prop_api} = False")
return None
def all_handlers():
"""return a list of all handlers of the blend"""
r = []
for oh in bpy.app.handlers: #so can also remove dupplicates
try:
for h in oh:
r.append(h)
except:
pass
return r
def _all_viewports_shading_type():
"""list all shading in window_manager"""
r = []
for window in bpy.context.window_manager.windows:
for area in window.screen.areas:
if(area.type == 'VIEW_3D'):
for space in area.spaces:
if(space.type == 'VIEW_3D'):
r.append(space.shading.type)
return r
def quit_rendered_shading():
"""for all spaces,quit rendered back to solid"""
for window in bpy.context.window_manager.windows:
for area in window.screen.areas:
if(area.type == 'VIEW_3D'):
for space in area.spaces:
if(space.type == 'VIEW_3D'):
if(space.shading.type == 'RENDERED'):
space.shading.type = 'SOLID'
return None
spc = None #global prop
def toggle_shading(True_False):
"""because of a blender related bug
changind data from a handler while in rendered
view will crash to destop, forced to back down"""
global spc
if True_False == False:
for window in bpy.context.window_manager.windows:
for area in window.screen.areas:
if(area.type == 'VIEW_3D'):
for space in area.spaces:
if(space.type == 'VIEW_3D'):
if(space.shading.type == 'RENDERED'):
spc = space
space.shading.type = 'SOLID'
else:
if spc != None: spc.shading.type = 'RENDERED'
return None
def find_instances(mesh_data):
"""return all obj using this mesh data"""
r=[]
for o in bpy.data.objects:
if (o.type=='MESH'):
if true_mesh_data(o) == mesh_data:
r.append(o)
return r
def fill_original_pointer(mesh_data):
"""fill original data pointer
according to list status, to all instances"""
for ob in find_instances(mesh_data):
#if ui list not empty but pointer is -> fill pointer
if (len(ob.data.lod_list) > 0) and (not ob.lod_original): ob.lod_original = mesh_data
return None
def clean_original_pointer():
"""clean original-data-pointer (lod_original) to all obj
in data if ui list empty, and restore to original"""
for ob in bpy.data.objects:
if ob.type =='MESH':
#if ui list empty but pointer original full -> restore and clean
if (ob.lod_original) and (len(ob.lod_original.lod_list) == 0):
if ob.data != ob.lod_original: ob.data = ob.lod_original
ob.lod_original = None
return None
def c_print(txt):
"""custom print, only when boolean allows it"""
if bpy.context.scene.lod.p_dev_print: print(txt)
return None
def find_lod(basic,suf,nbr):
"""return all lod meshes from data.meshes
from a given naming system"""
r = []
for m in bpy.data.meshes:
#if suffix at right place
if m.name[-len(suf)-nbr:-nbr] == suf:
#if basic name ok
if m.name.split(suf)[0] == basic:
r.append(m)
return r
def setup_obj_lod(obj,mesh_lod_list,auto):
"""automatic set up lods"""
if mesh_lod_list:
#clean list
bpy.ops.lodify.clear_list(mesh_n=obj.data.name)
for i,msh in enumerate(mesh_lod_list):
#add new item in list
item = obj.data.lod_list.add()
item.name = obj.data.name
item.ui_idx = len(obj.data.lod_list)
#fill original pointer with data
if (i==0): fill_original_pointer(obj.data)
#define item lod
item.ui_lod = msh
#automatic boolean set up
if (auto==True) and (i==0): item.ui_rdf = item.ui_rdv = True #render and rendered true if first (highest resolution)
if (auto==True) and (i+1==len(mesh_lod_list)): item.ui_dsp = True #display true if last (lowest resolution)
#adjust index
obj.data.lod_list_index = len(obj.data.lod_list)-1
def _all_unused_meshes():
"""search for unused meshes"""
scn_obj = [ob for scene in bpy.data.scenes for ob in scene.objects if ob.type == 'MESH']
msh_scn = [ob.data for ob in scn_obj]#all scene meshes
msh_pnt = [ui.ui_lod for msh in msh_scn for ui in msh.lod_list if ui.ui_lod]#all pointers used
msh_opt = [ob.lod_original for ob in scn_obj if ob.lod_original]#all pointer from obj
msh_act = msh_scn + msh_pnt + msh_opt #all scnenes meshes and their relative active pointers
return [x for x in list(bpy.data.meshes) if x not in msh_act]
def _all_unused_objects():
"""search for unused objects"""
scn_obj = [ob for scene in bpy.data.scenes for ob in scene.objects if ob.type == 'MESH']
return [x for x in list(bpy.data.objects) if x not in scn_obj]
def exchange_bmesh_data(msh_active,msh_paste):
"""exchange bmesh data, args must be data.meshes"""
import bmesh
bm = bmesh.new()
bm.from_mesh(msh_paste)
bm.to_mesh(msh_active)
msh_active.update()
###########################################################################################
#
# oooooooooo. o8o
# `888' `Y8b `"'
# 888 888 oooo d8b .oooo. oooo oooo ooo oooo ooo. .oo. .oooooooo
# 888 888 `888""8P `P )88b `88. `88. .8' `888 `888P"Y88b 888' `88b
# 888 888 888 .oP"888 `88..]88..8' 888 888 888 888 888
# 888 d88' 888 d8( 888 `888'`888' 888 888 888 `88bod8P'
# o888bood8P' d888b `Y888""8o `8' `8' o888o o888o o888o `8oooooo.
# d" YD
# "Y88888P'
###########################################################################################
class LODIFY_UL_items(bpy.types.UIList):
"""ui list drawing"""
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
scn = context.scene
row = layout.row(align=True)
sub=row.row(align=True)
sub.scale_x = 1.75
sub.prop(item,"ui_lod",text='')
sub=row.row(align=True)
sub.scale_x = 1.1
sub.enabled = bool(item.ui_lod)
sub.prop(item,"ui_dsp",text='',icon='RESTRICT_VIEW_OFF' if item.ui_dsp else'RESTRICT_VIEW_ON')
if scn.lod.p_rdv_switch: sub.prop(item,'ui_rdv',text='',icon='SHADING_RENDERED')
if scn.lod.p_rdf_switch: sub.prop(item,"ui_rdf",text='',icon='RESTRICT_RENDER_OFF'if item.ui_rdf else'RESTRICT_RENDER_ON')
class LODIFY_PT_objectList(bpy.types.Panel):
"""panel drawing in mesh data"""
bl_idname = 'LODIFY_PT_objectList'
bl_label = "Level of Detail"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "data"
@classmethod
def poll(cls, context):
obj = context.object
return (obj!=None) and (obj.type=='MESH')
def draw_header(self, context):
layout = self.layout
obj = context.object
msh = true_mesh_data(obj)
layout.prop(msh,"lod_enabled", text="")
layout.enabled = (not obj.data.is_editmode)
def draw(self, context):
layout = self.layout
obj = context.object
scn = context.scene
msh = true_mesh_data(obj)
#disable in edit mode or if Boolean false
main = layout.column()
main.enabled = (msh.lod_enabled) and (not obj.data.is_editmode)
#dev info
if scn.lod.p_dev_info:
dev = main.box().column()
dev.box().label(text='Developpement Infos-Box',icon='INFO')
dev.separator()
dev.prop(obj,'data',text='active mesh')
dev.prop(obj,'lod_original')
dev.separator()
dev.label(text=f'actively drawing panel for : {msh.name}')
dev.separator()
#define two rows
row = main.row()
col1 = row.column()
col2 = row.column()
#draw template
template = col1.column()
template.template_list("LODIFY_UL_items", "", msh, "lod_list", msh, "lod_list_index", rows=2)
template.scale_y = 1.1
#
#original/active mesh
original = col1.column(align=False)
original.prop(obj,'lod_original',text='original')
original.prop(obj,'data',text='active')
original.enabled = False #always disabled
original.scale_y = 0.9
#draw side bar
col2.separator(factor=1.0)
#
add = col2.column(align=True)
t = add.operator("lodify.list_action", icon='ADD', text="")
t.action = 'ADD'
t.mesh_n = msh.name
#
rem = col2.column(align=True)
rem.enabled = bool(len(msh.lod_list))
t = rem.operator("lodify.list_action", icon='REMOVE', text="")
t.action = 'REMOVE'
t.mesh_n = msh.name
#
col2.separator()
col2.menu("LODIFY_MT_operators_menu", icon='DOWNARROW_HLT', text="")
#additional
if scn.lod.more_op:
more = layout.column()
more.operator("lodify.batch_rename_dialog" , icon="SORTALPHA" , text="Batch Rename Objects-names for Selection")
more.operator("lodify.batch_mesh_name" , icon="SORTALPHA" , text="Automatic Meshes-name refactor for Selection")
class LODIFY_MT_operators_menu(bpy.types.Menu):
"""operator sub menu"""
bl_idname = "LODIFY_MT_operators_menu"
bl_label = ""
def draw(self, context):
layout = self.layout
obj = context.object
msh = true_mesh_data(obj)
layout.operator("lodify.clear_list" ,icon="TRASH" ,text="Clear Whole Slot List").mesh_n = msh.name
layout.menu("LODIFY_MT_clear_by_index" ,icon="TRASH" ,text="Remove a Specific Slot")
layout.operator("lodify.cleanse_data" ,icon="TRASH" ,text="Cleanse File Data")
layout.separator()
layout.operator("lodify.auto_setup_dialog" ,icon="SMALL_CAPS" ,text="LOD Search by Numeric Suffix").basic = obj.name
layout.operator("lodify.auto_name_setup_dialog" ,icon="SMALL_CAPS" ,text="LOD Search by Alphabetical Suffix")
layout.separator()
layout.operator("lodify.batch_dialog" ,icon="SHADERFX" ,text="Batch Change LOD's properties by Suffix")
layout.operator("lodify.auto_lod_generation" ,icon="SHADERFX" ,text="Batch Auto-Generate LOD's")
layout.separator()
layout.operator("lodify.create_backup" ,icon="COPY_ID" ,text="Store Mesh-Data as Slot")
layout.menu("LODIFY_MT_restore_backup" ,icon="COPY_ID" ,text="Replace Active Mesh-Data")
layout.separator()
layout.operator("lodify.parameters_dialog" ,icon="PREFERENCES",text="Parameters")
layout.operator("lodify.docs" ,icon="QUESTION" ,text="Documentation")
layout.operator("wm.url_open" ,icon="COMMUNITY" ,text="Author Products").url = "https://www.blendermarket.com/creators/bd3d-store"
class LODIFY_OT_parameters_dialog(bpy.types.Operator):
bl_idname = "lodify.parameters_dialog"
bl_label = "Options"
bl_options = {'REGISTER', 'INTERNAL'}
@classmethod
def poll(cls, context):