forked from Jack-Cooke/blign
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blign.py
1279 lines (1121 loc) · 44.7 KB
/
blign.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
import math
from mathutils import Vector
import bpy
import numpy as np
bl_info = {
"name": "Blign",
"author": "Wilmer Lab Group",
"version": (1, 0),
"blender": (2, 80, 0),
"location": "3D View Sidebar > Geometry tab",
"description": "Align and distribute objects about an axis or between objects",
"tracker_url": "",
"category": "Geometry"
}
def count_blign_objects():
"""
Counts the number of selected Blign objects.
Arguments
---------
Returns
-------
len.... : int
Number of Blign objects.
"""
return len([obj for obj in list(bpy.data.objects) if obj.blign == True])
def find_alignment_points(direction, vertex_sign):
"""
Finds the points on the Blign objects that makes up the alignment line.
Arguments
---------
direction : str
Direction in 3D space ['x', 'y', 'z'].
vertex_sign : str
Sign of the vertex ['+', '-'].
Returns
-------
p1 : numpy array
Desired vertex on the first Blign object.
p2 : numpy array
Desired vertex on the second Blign object.
"""
if count_blign_objects() == 2:
blign1, blign2 = [np.array([obj.matrix_world @ Vector(
c) for c in obj.bound_box]) for obj in bpy.data.objects if obj.blign]
drx_idx = {'x': 0, 'y': 1, 'z': 2}[direction]
vertex_idx = {'-': 0, '+': -1}[vertex_sign]
p1, p2 = blign1[np.argsort(blign1[:, drx_idx])[vertex_idx]], blign2[np.argsort(
blign2[:, drx_idx])[vertex_idx]]
else:
raise ValueError('There should be 2 Blign objects selected!')
return p1, p2
def find_vertex(obj, direction, vertex_sign):
"""
Finds the 3d coordinates for a specified vertex on a given object.
Arguments
---------
obj : Blender object
Object to find the vertex
direction : str
Direction in 3D space ['x', 'y', 'z'].
vertex_sign : str
Sign of the vertex ['+', '-'].
Returns
-------
p : numpy array
Desired vertex on an object.
"""
drx_idx = {'x': 0, 'y': 1, 'z': 2}[direction]
vertex_idx = {'-': 0, '+': -1}[vertex_sign]
vertices = np.array([obj.matrix_world @ Vector(c) for c in obj.bound_box])
p = vertices[np.argsort(vertices[:, drx_idx])[vertex_idx]]
return p
def transform_object(obj, v):
"""
Transforms the object to the desired position.
Arguments
---------
obj : Blender object
Object to find the vertex.
v : float (or vector?)
Vector along which the object needs to be transformed ['x', 'y', 'z'].
Returns
-------
obj : Blender object
Object to find the vertex.
"""
obj.location.x += v[0]
obj.location.y += v[1]
obj.location.z += v[2]
return obj
def find_default_spacing(axis):
"""
Function finds the default distance between that objects are being distributed from their centers.
Arguments
---------
axis : str
The axis that objects get aligned to.
Returns
-------
default_spacing : float
Distance between objects' centers when distributed.
obj_idx : list
An indexed numpy list of all object locations.
"""
oblist = bpy.context.selected_objects
if axis == 'x':
pos_list = [o.location.x for o in oblist]
elif axis == 'y':
pos_list = [o.location.y for o in oblist]
elif axis == 'z':
pos_list = [o.location.z for o in oblist]
obj_idx = np.argsort(pos_list)
distance = max(pos_list) - min(pos_list)
default_spacing = distance / (len(pos_list) - 1)
return default_spacing, obj_idx
def find_d(obj_idx, direction):
"""
Function that defines the distance between objects' edges.
Arguments
---------
obj_idx : list
An indexed numpy list of all object locations.
direction : str
Either x y or z.
Returns
-------
d : float
distance between the edges of one object and the next.
"""
drx_idx = {'x': 0, 'y': 1, 'z': 2}[direction]
oblist = bpy.context.selected_objects
obj_space = 0
for i, idx in enumerate(obj_idx):
vertices = np.array([oblist[obj_idx[i]].matrix_world @ Vector(c)
for c in oblist[obj_idx[i]].bound_box])
p1 = vertices[np.argsort(vertices[:, drx_idx])[0]]
p2 = vertices[np.argsort(vertices[:, drx_idx])[-1]]
if i == 0:
start = p1[drx_idx]
elif i == max(obj_idx):
end = p2[drx_idx]
obj_space += p2[drx_idx] - p1[drx_idx]
distance = end - start
empty_space = distance - obj_space
d = empty_space / (len(oblist) - 1)
return d
def find_c_to_v(obj_idx, direction):
"""
Function finds the default distance between that objects are being distributed from their centers.
Arguments
---------
obj_idx : list
An indexed numpy list of all object locations.
direction : str
Either x y or z.
Returns
-------
c_to_v1 : list
A list of the distances from an object's most positive edge to its center.
c_to_v1 : list
A list of the distances from an object's most negative edge to its center.
"""
drx_idx = {'x': 0, 'y': 1, 'z': 2}[direction]
oblist = bpy.context.selected_objects
c_to_v1 = []
c_to_v2 = []
for i, idx in enumerate(obj_idx):
vertices = np.array([oblist[obj_idx[i]].matrix_world @ Vector(c)
for c in oblist[obj_idx[i]].bound_box])
p1 = vertices[np.argsort(vertices[:, drx_idx])[-1]]
p2 = vertices[np.argsort(vertices[:, drx_idx])[0]]
if direction == 'x':
loc = oblist[idx].location.x
elif direction == 'y':
loc = oblist[idx].location.y
elif direction == 'z':
loc = oblist[idx].location.z
dist1 = p1[drx_idx] - loc
dist2 = loc - p2[drx_idx]
c_to_v1.append(dist1)
c_to_v2.append(dist2)
return c_to_v1, c_to_v2
def align_axis_0():
"""
Aligns the object on the principal, function called in Blign_Align_Button0.
Arguments
---------
Returns
-------
"""
axis = bpy.context.scene.object_settings.Axis0
oblist = bpy.context.selected_objects
directionx = bpy.context.scene.object_settings.x_selected0
directiony = bpy.context.scene.object_settings.y_selected0
directionz = bpy.context.scene.object_settings.z_selected0
if axis == 'x':
if directionx == 'center':
for obj in oblist:
obj.location.y = 0
obj.location.z = 0
else:
for obj in oblist:
v = find_vertex(obj, directionx[1], directionx[0])
obj = transform_object(obj, [0, -v[1], -v[2]])
elif axis == 'y':
if directiony == 'center':
for obj in oblist:
obj.location.x = 0
obj.location.z = 0
else:
for obj in oblist:
v = find_vertex(obj, directiony[1], directiony[0])
obj = transform_object(obj, [-v[0], 0, -v[2]])
elif axis == 'z':
if directionz == 'center':
for obj in oblist:
obj.location.x = 0
obj.location.y = 0
else:
for obj in oblist:
v = find_vertex(obj, directionz[1], directionz[0])
obj = transform_object(obj, [-v[0], -v[1], 0])
def align_plane_0():
"""
Aligns the object to the same plane as an added Blign object, function called in Blign_Align_Button1.
Arguments
---------
Returns
-------
"""
plane = bpy.context.scene.object_settings.Plane0
oblist = bpy.context.selected_objects
directionyz = bpy.context.scene.object_settings.yz_selected0
directionxz = bpy.context.scene.object_settings.xz_selected0
directionxy = bpy.context.scene.object_settings.xy_selected0
if plane == 'y-z':
if directionyz == 'center':
for obj in oblist:
obj.location.x = 0
else:
for obj in oblist:
v = find_vertex(obj, directionyz[1], directionyz[0])
obj = transform_object(obj, [-v[0], 0, 0])
elif plane == 'x-z':
if directionxz == 'center':
for obj in oblist:
obj.location.y = 0
else:
for obj in oblist:
v = find_vertex(obj, directionxz[1], directionxz[0])
obj = transform_object(obj, [0, -v[1], 0])
elif plane == 'x-y':
if directionxy == 'center':
for obj in oblist:
obj.location.z = 0
else:
for obj in oblist:
v = find_vertex(obj, directionxy[1], directionxy[0])
obj = transform_object(obj, [0, 0, -v[2]])
def align_axis_1():
"""
Aligns the object to an added Blign object, function called in Blign_Align_Button1.
Arguments
---------
Returns
-------
"""
axis = bpy.context.scene.object_settings.Axis1
oblist = bpy.context.selected_objects
directionx = bpy.context.scene.object_settings.x_selected1
directiony = bpy.context.scene.object_settings.y_selected1
directionz = bpy.context.scene.object_settings.z_selected1
if axis == 'x':
if directionx == 'center':
for obj in list(bpy.data.objects):
if obj.blign == True:
locy = obj.location.y
locz = obj.location.z
for obj in oblist:
obj.location.y = locy
obj.location.z = locz
else:
for obj in list(bpy.data.objects):
if obj.blign == True:
blign_vertex = find_vertex(
obj, directionx[1], directionx[0])
for obj in oblist:
v = find_vertex(obj, directionx[1], directionx[0])
delta = blign_vertex - v
obj = transform_object(obj, [0, delta[1], delta[2]])
elif axis == 'y':
if directiony == 'center':
for obj in list(bpy.data.objects):
if obj.blign == True:
locx = obj.location.x
locz = obj.location.z
for obj in oblist:
obj.location.x = locx
obj.location.z = locz
else:
for obj in list(bpy.data.objects):
if obj.blign == True:
blign_vertex = find_vertex(
obj, directiony[1], directiony[0])
for obj in oblist:
v = find_vertex(obj, directiony[1], directiony[0])
delta = blign_vertex - v
obj = transform_object(obj, [delta[0], 0, delta[2]])
elif axis == 'z':
if directionz == 'center':
for obj in list(bpy.data.objects):
if obj.blign == True:
locx = obj.location.x
locy = obj.location.y
for obj in oblist:
obj.location.x = locx
obj.location.y = locy
else:
for obj in list(bpy.data.objects):
if obj.blign == True:
blign_vertex = find_vertex(
obj, directionz[1], directionz[0])
for obj in oblist:
v = find_vertex(obj, directionz[1], directionz[0])
delta = blign_vertex - v
obj = transform_object(obj, [delta[0], delta[1], 0])
def align_plane_1():
"""
Aligns the object to the same plane as the an added Blign object, function called in Blign_Align_Button1.
Arguments
---------
Returns
-------
"""
plane = bpy.context.scene.object_settings.Plane1
oblist = bpy.context.selected_objects
directionyz = bpy.context.scene.object_settings.yz_selected1
directionxz = bpy.context.scene.object_settings.xz_selected1
directionxy = bpy.context.scene.object_settings.xy_selected1
if plane == 'y-z':
if directionyz == 'center':
for obj in list(bpy.data.objects):
if obj.blign == True:
locx = obj.location.x
for obj in oblist:
obj.location.x = locx
else:
for obj in list(bpy.data.objects):
if obj.blign == True:
blign_vertex = find_vertex(
obj, directionyz[1], directionyz[0])
for obj in oblist:
v = find_vertex(obj, directionyz[1], directionyz[0])
delta = blign_vertex - v
obj = transform_object(obj, [delta[0], 0, 0])
elif plane == 'x-z':
if directionxz == 'center':
for obj in list(bpy.data.objects):
if obj.blign == True:
locy = obj.location.y
for obj in oblist:
obj.location.y = locy
else:
for obj in list(bpy.data.objects):
if obj.blign == True:
blign_vertex = find_vertex(
obj, directionxz[1], directionxz[0])
for obj in oblist:
v = find_vertex(obj, directionxz[1], directionxz[0])
delta = blign_vertex - v
obj = transform_object(obj, [0, delta[1], 0])
elif plane == 'x-y':
if directionxy == 'center':
for obj in list(bpy.data.objects):
if obj.blign == True:
locz = obj.location.z
for obj in oblist:
obj.location.z = locz
else:
for obj in list(bpy.data.objects):
if obj.blign == True:
blign_vertex = find_vertex(
obj, directionxy[1], directionxy[0])
for obj in oblist:
v = find_vertex(obj, directionxy[1], directionxy[0])
delta = blign_vertex - v
obj = transform_object(obj, [0, 0, delta[2]])
def align_2():
"""
Aligns the object to two added Blign objects, function called in Blign_Align_Button2.
Arguments
---------
Returns
-------
"""
align = bpy.context.scene.object_settings.align_to_2_ops
oblist = bpy.context.selected_objects
if align == 'center':
p1, p2 = [np.array(o.location) for o in bpy.data.objects if o.blign]
else:
p1, p2 = find_alignment_points(align[1], align[0])
u = p2 - p1
a = np.array([[(u ** 2).sum()]])
for obj in oblist:
if obj.blign == False:
if align == 'center':
p = np.array([obj.location.x, obj.location.y, obj.location.z])
else:
p = find_vertex(obj, align[1], align[0])
b = np.array([[(u * (p - p2)).sum()]])
t = np.linalg.solve(a, b)
v = u * t[0][0] + (p2 - p)
obj = transform_object(obj, v)
def distribute_0_or_1(indicate, axis, dist_type, spacing):
"""
Distributes objects from their centers or edges when 0 or 1 blign objects are added.
Arguments
---------
indicate : bool
If True, user can set spacing. If False, Blign finds default spacing.
axis: str
Either x y or z.
dist_type : str
The user's choice to distribute from either center or edge.
spacing : int
number of units between objects (specified by user).
Returns
-------
"""
oblist = bpy.context.selected_objects
if dist_type == 'center':
if len(oblist) > 1:
if not indicate:
default_spacing, obj_idx = find_default_spacing(axis)
if axis == 'x':
for i, idx in enumerate(obj_idx):
oblist[idx].location.x = oblist[obj_idx[0]
].location.x + default_spacing * i
elif axis == 'y':
for i, idx in enumerate(obj_idx):
oblist[idx].location.y = oblist[obj_idx[0]
].location.y + default_spacing * i
elif axis == 'z':
for i, idx in enumerate(obj_idx):
oblist[idx].location.z = oblist[obj_idx[0]
].location.z + default_spacing * i
else:
spacing = bpy.context.scene.object_settings.Spacing0
obj_idx = find_default_spacing(axis)[1]
if axis == 'x':
for i, idx in enumerate(obj_idx):
oblist[idx].location.x = oblist[obj_idx[0]
].location.x + spacing * i
elif axis == 'y':
for i, idx in enumerate(obj_idx):
oblist[idx].location.y = oblist[obj_idx[0]
].location.y + spacing * i
elif axis == 'z':
for i, idx in enumerate(obj_idx):
oblist[idx].location.z = oblist[obj_idx[0]
].location.z + spacing * i
elif dist_type == 'edge':
if len(oblist) > 1:
obj_idx = find_default_spacing(axis)[1]
if not indicate:
d = find_d(obj_idx, axis)
c_to_v1, c_to_v2 = find_c_to_v(obj_idx, axis)
if axis == 'x':
for i, idx in enumerate(obj_idx):
if i < max(obj_idx):
oblist[obj_idx[i + 1]].location.x = oblist[idx].location.x + \
c_to_v1[i] + d + c_to_v2[i + 1]
elif axis == 'y':
for i, idx in enumerate(obj_idx):
if i < max(obj_idx):
oblist[obj_idx[i + 1]].location.y = oblist[idx].location.y + \
c_to_v1[i] + d + c_to_v2[i + 1]
elif axis == 'z':
for i, idx in enumerate(obj_idx):
if i < max(obj_idx):
oblist[obj_idx[i + 1]].location.z = oblist[idx].location.z + \
c_to_v1[i] + d + c_to_v2[i + 1]
else:
c_to_v1, c_to_v2 = find_c_to_v(obj_idx, axis)
if axis == 'x':
for i, idx in enumerate(obj_idx):
if i < max(obj_idx):
oblist[obj_idx[i + 1]].location.x = oblist[idx].location.x + \
c_to_v1[i] + spacing + c_to_v2[i + 1]
elif axis == 'y':
for i, idx in enumerate(obj_idx):
if i < max(obj_idx):
oblist[obj_idx[i + 1]].location.y = oblist[idx].location.y + \
c_to_v1[i] + spacing + c_to_v2[i + 1]
elif axis == 'z':
for i, idx in enumerate(obj_idx):
if i < max(obj_idx):
oblist[obj_idx[i + 1]].location.z = oblist[idx].location.z + \
c_to_v1[i] + spacing + c_to_v2[i + 1]
def distribute_2():
"""
Distributes objects from their centers or edges when 2 blign objects are added.
Arguments
---------
Returns
-------
"""
indicate = bpy.context.scene.object_settings.indicate_spacing2
oblist = bpy.context.selected_objects
dist_type = bpy.context.scene.object_settings.distribute_ops2
if dist_type == 'center':
if len(oblist) > 1:
if not indicate:
default_spacing_x, obj_idx_x = find_default_spacing('x')
for i, idx in enumerate(obj_idx_x):
oblist[idx].location.x = oblist[obj_idx_x[0]].location.x + \
default_spacing_x * i
default_spacing_y, obj_idx_y = find_default_spacing('y')
for i, idx in enumerate(obj_idx_y):
oblist[idx].location.y = oblist[obj_idx_y[0]].location.y + \
default_spacing_y * i
default_spacing_z, obj_idx_z = find_default_spacing('z')
for i, idx in enumerate(obj_idx_z):
oblist[idx].location.z = oblist[obj_idx_z[0]].location.z + \
default_spacing_z * i
else:
spacing = bpy.context.scene.object_settings.Spacing2
p1, p2 = [np.array(o.location)
for o in bpy.data.objects if o.blign]
v = p2 - p1
u = v / np.linalg.norm(v)
i = 0
for obj in bpy.context.selected_objects:
obj.location.x = p1[0] + u[0] * spacing * i
obj.location.y = p1[1] + u[1] * spacing * i
obj.location.z = p1[2] + u[2] * spacing * i
i += 1
class BLIGN_OT_Add_Object(bpy.types.Operator):
"""Class that defines the Add Object button."""
bl_idname = "rigidbody.blign_add_object"
bl_label = "Add Object"
bl_description = "Set selected object as a blign object"
def execute(self, context):
"""Sets the object as object.blign."""
if len(context.selected_objects) == 1:
for object in context.selected_objects:
if not object.blign:
context.view_layer.objects.active = object
object.blign = True
else:
pass
return {'FINISHED'}
class BLIGN_OT_Remove_Object(bpy.types.Operator):
"""Class that defines the Remove Object button."""
bl_idname = "rigidbody.blign_remove_object"
bl_label = "Remove Object"
bl_description = "Remove object from as a blign object"
@classmethod
def poll(cls, context):
if context.object:
return context.object.blign
def execute(self, context):
"""Unsets object as object.blign."""
for object in context.selected_objects:
if object.blign:
context.view_layer.objects.active = object
context.object.blign = False
return {'FINISHED'}
class Blign_OT_Clear_Objects(bpy.types.Operator):
"""CLass that defines the Clear Objects button."""
bl_idname = "rigidbody.blign_clear_objects"
bl_label = "Clear Objects"
bl_description = "Removes all Blign Objects"
def execute(self, context):
for object in list(bpy.data.objects):
if object.blign:
context.view_layer.objects.active = object
context.object.blign = False
return {'FINISHED'}
class BLIGN_OT_Align_Button0(bpy.types.Operator):
"""Defines the Align button."""
bl_idname = "rigidbody.blign_align_button0"
bl_label = "Align"
bl_description = "Align selected objects"
def execute(self, context):
"""Iterates through all objects, counts number of blign objects.
If number of blign objects = 0, aligns selected objects to the selected axis or plane.
"""
i = count_blign_objects()
plane = bpy.context.scene.object_settings.check_plane0
if i == 0:
if plane == False:
align_axis_0()
else:
align_plane_0()
else:
pass
return {'FINISHED'}
class BLIGN_OT_Align_Button1(bpy.types.Operator):
"""Defines the Align button."""
bl_idname = "rigidbody.blign_align_button1"
bl_label = "Align"
bl_description = "Align selected objects"
def execute(self, context):
"""Iterates through all objects, counts number of blign objects.
If number of blign objects = 1, aligns selected objects to that one object.
"""
i = count_blign_objects()
plane = bpy.context.scene.object_settings.check_plane1
if i == 1:
if plane == False:
align_axis_1()
else:
align_plane_1()
else:
pass
return {'FINISHED'}
class BLIGN_OT_Align_Button2(bpy.types.Operator):
"""Defines the Align button."""
bl_idname = "rigidbody.blign_align_button2"
bl_label = "Align"
bl_description = "Align selected objects"
def execute(self, context):
"""Iterates through all objects, counts number of blign objects.
If number of blign objects = 2, aligns selected objects along the line between the 2 blign objects.
"""
if count_blign_objects() == 2:
align_2()
else:
pass
return {'FINISHED'}
class BLIGN_OT_Distribute_Button0(bpy.types.Operator):
"""Defines the Distribute button."""
bl_idname = "rigidbody.blign_distribute_button0"
bl_label = "Distribute"
bl_description = "Distribute objects"
def execute(self, context):
indicate = bpy.context.scene.object_settings.indicate_spacing0
axis = bpy.context.scene.object_settings.Axis0
dist_type = bpy.context.scene.object_settings.distribute_ops0
spacing = bpy.context.scene.object_settings.Spacing0
if count_blign_objects() != 2:
distribute_0_or_1(indicate, axis, dist_type, spacing)
else:
pass
return {'FINISHED'}
class BLIGN_OT_Distribute_Button1(bpy.types.Operator):
"""Defines the Distribute button."""
bl_idname = "rigidbody.blign_distribute_button1"
bl_label = "Distribute"
bl_description = "Distribute objects"
def execute(self, context):
"""Distributes objects between first and last object.
Indicate = the indicate spacing button. If unchecked, evenly distributes shapes.
If checked, distributes objects 'spacing' units apart.
"""
indicate = bpy.context.scene.object_settings.indicate_spacing1
axis = bpy.context.scene.object_settings.Axis1
dist_type = bpy.context.scene.object_settings.distribute_ops1
spacing = bpy.context.scene.object_settings.Spacing1
if count_blign_objects() != 2:
distribute_0_or_1(indicate, axis, dist_type, spacing)
else:
pass
return {'FINISHED'}
class BLIGN_OT_Distribute_Button2(bpy.types.Operator):
"""Defines the Distribute button."""
bl_idname = "rigidbody.blign_distribute_button2"
bl_label = "Distribute"
bl_description = "Distribute objects"
def execute(self, context):
"""Distributes objects between first and last object.
Indicate = the indicate spacing button. If unchecked, evenly distributes shapes.
If checked, distributes objects 'spacing' units apart.
"""
if count_blign_objects() == 2:
distribute_2()
else:
pass
return {'FINISHED'}
class BLIGN_PT_Blign(bpy.types.Panel):
"""Parent tab, all other tabs are within this one."""
bl_label = "Blign"
bl_category = "Geometry"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
def draw(self, context):
"""Outlines the Add and Remove buttons in the Blign tab.
Finds the number of objects added.
If one or more objects are added, shows remove button.
Does not allow user to add more than 2 objects as blign objects.
Shows which objects have been added as blign objects below the add/remove button.
"""
layout = self.layout
layout.use_property_split = True
blobs = []
i = 0
for object in list(bpy.data.objects):
if object.blign == True:
blobs.append(object.name)
i += 1
try:
if (bpy.context.object.blign == True):
row = layout.row()
row.operator('rigidbody.blign_remove_object')
elif (bpy.context.object.blign == False):
if (i < 2):
row = layout.row()
row.operator('rigidbody.blign_add_object')
except AttributeError:
pass
if i > 0:
row = layout.row()
row.operator('rigidbody.blign_clear_objects')
if i == 1:
row = layout.row()
row.label(text="Object 1: {}".format(str(blobs[0])))
if i == 2:
row = layout.row()
row.label(text="Object 1: {}".format(str(blobs[0])))
row = layout.row()
row.label(text="Object 2: {}".format(str(blobs[1])))
class BlignSettings(bpy.types.PropertyGroup):
"""All buttons used in the add-on are defined in this class."""
Axis0: bpy.props.EnumProperty(
name="Axis",
items=[("x", "x", "Align objects in the x direction"),
("y", "y", "Align objects in the y direction"),
("z", "z", "Align objects in the z direction")],
default='x',
options={'HIDDEN'},
)
check_plane0: bpy.props.BoolProperty(
name="Align to Plane",
description="Choose whether or not to align objects to a plane",
options={'HIDDEN'},
default=False
)
Plane0: bpy.props.EnumProperty(
name="Plane",
items=[("y-z", "y-z", "Align objects to the y-z plane"),
("x-z", "x-z", "Align objects to the x-z plane"),
("x-y", "x-y", "Align objects to the x-y plane")],
default='y-z',
options={'HIDDEN'},
)
Axis1: bpy.props.EnumProperty(
name="Axis",
items=[("x", "x", "Align objects in the x direction"),
("y", "y", "Align objects in the y direction"),
("z", "z", "Align objects in the z direction")],
default='x',
options={'HIDDEN'},
)
check_plane1: bpy.props.BoolProperty(
name="Align to Plane",
description="Choose whether or not to align objects to a plane",
options={'HIDDEN'},
default=False
)
Plane1: bpy.props.EnumProperty(
name="Plane",
items=[("y-z", "y-z", "Align objects to the y-z plane"),
("x-z", "x-z", "Align objects to the x-z plane"),
("x-y", "x-y", "Align objects to the x-y plane")],
default='y-z',
options={'HIDDEN'},
)
indicate_spacing0: bpy.props.BoolProperty(
name="",
description="Choose whether or not to indicate spacing betweeen objects",
options={'HIDDEN'},
default=False
)
indicate_spacing1: bpy.props.BoolProperty(
name="",
description="Choose whether or not to indicate spacing betweeen objects",
options={'HIDDEN'},
default=False
)
indicate_spacing2: bpy.props.BoolProperty(
name="",
description="Choose whether or not to indicate spacing betweeen objects",
options={'HIDDEN'},
default=False
)
Spacing0: bpy.props.IntProperty(
name="Spacing",
description="Set distribution value between objects",
default=1,
options={'HIDDEN'},
)
Spacing1: bpy.props.IntProperty(
name="Spacing",
description="Set distribution value between objects",
default=1,
options={'HIDDEN'},
)
Spacing2: bpy.props.IntProperty(
name="Spacing",
description="Set distribution value between objects",
default=1,
options={'HIDDEN'},
)
x_selected0: bpy.props.EnumProperty(
name="Align to",
items=[("center", "Center", "Align to center of object"),
("+y", "+y", "Align objects to their most positive point in the y direction"),
("-y", "-y", "Align objects to their most negative point in the y direction"),
("+z", "+z", "Align objects to their most positive point in the z direction"),
("-z", "-z", "Align objects to their most negative point in the z direction")],
default='center',
options={'HIDDEN'},
)
y_selected0: bpy.props.EnumProperty(
name="Align to",
items=[("center", "Center", "Align to center of object"),
("+x", "+x", "Align objects to their most positive point in the x direction"),
("-x", "-x", "Align objects to their most negative point in the x direction"),
("+z", "+z", "Align objects to their most positive point in the z direction"),
("-z", "-z", "Align objects to their most negative point in the z direction")],
default='center',
options={'HIDDEN'},
)
z_selected0: bpy.props.EnumProperty(
name="Align to",
items=[("center", "Center", "Align to center of object"),
("+x", "+x", "Align objects to their most positive point in the x direction"),
("-x", "-x", "Align objects to their most negative point in the x direction"),
("+y", "+y", "Align objects to their most positive point in the y direction"),
("-y", "-y", "Align objects to their most negative point in the y direction")],
default='center',
options={'HIDDEN'},
)
yz_selected0: bpy.props.EnumProperty(
name="Align to",
items=[("center", "Center", "Align to center of object"),
("+x", "+x", "Align objects to their most positive point in the y direction"),
("-x", "-x", "Align objects to their most negative point in the y direction")],
default='center',
options={'HIDDEN'},
)
xz_selected0: bpy.props.EnumProperty(
name="Align to",
items=[("center", "Center", "Align to center of object"),
("+y", "+y", "Align objects to their most positive point in the y direction"),
("-y", "-y", "Align objects to their most negative point in the y direction")],
default='center',
options={'HIDDEN'},
)
xy_selected0: bpy.props.EnumProperty(
name="Align to",
items=[("center", "Center", "Align to center of object"),
("+z", "+z", "Align objects to their most positive point in the y direction"),
("-z", "-z", "Align objects to their most negative point in the y direction")],
default='center',
options={'HIDDEN'},
)
x_selected1: bpy.props.EnumProperty(
name="Align to",
items=[("center", "Center", "Align to center of object"),
("+y", "+y", "Align objects to their most positive point in the y direction"),
("-y", "-y", "Align objects to their most negative point in the y direction"),
("+z", "+z", "Align objects to their most positive point in the z direction"),
("-z", "-z", "Align objects to their most negative point in the z direction")],
default='center',
options={'HIDDEN'},
)
y_selected1: bpy.props.EnumProperty(
name="Align to",
items=[("center", "Center", "Align to center of object"),
("+x", "+x", "Align objects to their most positive point in the x direction"),
("-x", "-x", "Align objects to their most negative point in the x direction"),
("+z", "+z", "Align objects to their most positive point in the z direction"),
("-z", "-z", "Align objects to their most negative point in the z direction")],
default='center',
options={'HIDDEN'},
)
z_selected1: bpy.props.EnumProperty(
name="Align to",
items=[("center", "Center", "Align to center of object"),
("+x", "+x", "Align objects to their most positive point in the x direction"),
("-x", "-x", "Align objects to their most negative point in the x direction"),
("+y", "+y", "Align objects to their most positive point in the y direction"),
("-y", "-y", "Align objects to their most negative point in the y direction")],
default='center',
options={'HIDDEN'},