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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
|
# -*- coding:utf-8 -*-
# ##### BEGIN GPL LICENSE BLOCK #####
#
# 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.
#
# ##### END GPL LICENSE BLOCK #####
# <pep8 compliant>
# ----------------------------------------------------------
# Author: Stephen Leger (s-leger)
#
# ----------------------------------------------------------
import bpy
from math import atan2, pi
from mathutils import Vector, Matrix
from mathutils.geometry import intersect_line_plane, intersect_point_line, intersect_line_sphere
from bpy_extras import view3d_utils
from bpy.types import PropertyGroup, Operator
from bpy.props import (
FloatVectorProperty,
StringProperty,
CollectionProperty,
BoolProperty
)
from bpy.app.handlers import persistent
from .archipack_snap import snap_point
from .archipack_keymaps import Keymaps
from .archipack_gl import (
GlLine, GlArc, GlText,
GlPolyline, GlPolygon,
TriHandle, SquareHandle, EditableText,
FeedbackPanel, GlCursorArea
)
# NOTE:
# Snap aware manipulators use a dirty hack :
# draw() as a callback to update values in realtime
# as transform.translate in use to allow snap
# does catch all events.
# This however has a wanted side effect:
# the manipulator take precedence over already running
# ones, and prevent select mode to start.
#
# TODO:
# Other manipulators should use same technique to take
# precedence over already running ones when active
#
# NOTE:
# Select mode does suffer from this stack effect:
# the last running wins. The point is left mouse select mode
# requiring left drag to be RUNNING_MODAL to prevent real
# objects select and move during manipulators selection.
#
# TODO:
# First run a separate modal dedicated to select mode.
# Selecting in whole manips stack when required
# (manips[key].manipulable.manip_stack)
# Must investigate for a way to handle unselect after drag done.
import logging
logger = logging.getLogger("archipack")
"""
Change object location when moving 1 point
When False, change data.origin instead
"""
USE_MOVE_OBJECT = True
# Arrow sizes (world units)
arrow_size = 0.05
# Handle area size (pixels)
handle_size = 10
# a global manipulator stack reference
# prevent Blender "ACCESS_VIOLATION" crashes
# use a dict to prevent collisions
# between many objects being in manipulate mode
# use object names as loose keys
# NOTE : use app.drivers to reset before file load
manips = {}
class ArchipackActiveManip:
"""
Store manipulated object
- object_name: manipulated object name
- stack: array of Manipulators instances
- manipulable: Manipulable instance
"""
def __init__(self, object_name):
self.object_name = object_name
# manipulators stack for object
self.stack = []
# reference to object manipulable instance
self.manipulable = None
@property
def dirty(self):
"""
Check for manipulable validity
to disable modal when required
"""
o = bpy.data.objects.get(self.object_name)
return (
self.manipulable is None or
o is None or
not o.select_get()
)
def exit(self):
"""
Exit manipulation mode
- exit from all running manipulators
- empty manipulators stack
- set manipulable.manipulate_mode to False
- remove reference to manipulable
"""
for m in self.stack:
if m is not None:
m.exit()
if self.manipulable is not None:
self.manipulable.manipulate_mode = False
self.manipulable = None
self.object_name = ""
self.stack.clear()
def remove_manipulable(key):
"""
disable and remove a manipulable from stack
"""
global manips
# print("remove_manipulable key:%s" % (key))
if key in manips.keys():
manips[key].exit()
manips.pop(key)
def check_stack(key):
"""
check for stack item validity
use in modal to destroy invalid modals
return true when invalid / not found
false when valid
"""
global manips
if key not in manips.keys():
# print("check_stack : key not found %s" % (key))
return True
elif manips[key].dirty:
# print("check_stack : key.dirty %s" % (key))
remove_manipulable(key)
return True
return False
def empty_stack():
# print("empty_stack()")
"""
kill every manipulators in stack
and cleanup stack
"""
global manips
for key in manips.keys():
manips[key].exit()
manips.clear()
def add_manipulable(key, manipulable):
"""
add a ArchipackActiveManip into the stack
if not already present
setup reference to manipulable
return manipulators stack
"""
global manips
if key not in manips.keys():
# print("add_manipulable() key:%s not found create new" % (key))
manips[key] = ArchipackActiveManip(key)
manips[key].manipulable = manipulable
return manips[key].stack
# ------------------------------------------------------------------
# Define Manipulators
# ------------------------------------------------------------------
class Manipulator():
"""
Manipulator base class to derive other
handle keyboard and modal events
provide convenient funcs including getter and setter for datablock values
store reference of base object, datablock and manipulator
"""
keyboard_ascii = {
".", ",", "-", "+", "1", "2", "3",
"4", "5", "6", "7", "8", "9", "0",
"c", "m", "d", "k", "h", "a",
" ", "/", "*", "'", "\""
# "="
}
keyboard_type = {
'BACK_SPACE', 'DEL',
'LEFT_ARROW', 'RIGHT_ARROW'
}
def __init__(self, context, o, datablock, manipulator, snap_callback=None):
"""
o : object to manipulate
datablock : object data to manipulate
manipulator: object archipack_manipulator datablock
snap_callback: on snap enabled manipulators, will be called when drag occurs
"""
self.keymap = Keymaps(context)
self.feedback = FeedbackPanel()
self.active = False
self.selectable = False
self.selected = False
# active text input value for manipulator
self.keyboard_input_active = False
self.label_value = 0
# unit for keyboard input value
self.value_type = 'LENGTH'
self.pts_mode = 'SIZE'
self.o = o
self.datablock = datablock
self.manipulator = manipulator
self.snap_callback = snap_callback
self.origin = Vector((0, 0, 1))
self.mouse_pos = Vector((0, 0))
self.length_entered = ""
self.line_pos = 0
args = (self, context)
self._handle = bpy.types.SpaceView3D.draw_handler_add(self.draw_callback, args, 'WINDOW', 'POST_PIXEL')
@classmethod
def poll(cls, context):
"""
Allow manipulator enable/disable
in given context
handles will not show
"""
return True
def exit(self):
"""
Modal exit, DON'T EVEN TRY TO OVERRIDE
"""
if self._handle is not None:
bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
self._handle = None
else:
print("Manipulator.exit() handle not found %s" % (type(self).__name__))
# Mouse event handlers, MUST be overridden
def mouse_press(self, context, event):
"""
Manipulators must implement
mouse press event handler
return True to callback manipulable_manipulate
"""
raise NotImplementedError
def mouse_release(self, context, event):
"""
Manipulators must implement
mouse mouse_release event handler
return False to callback manipulable_release
"""
raise NotImplementedError
def mouse_move(self, context, event):
"""
Manipulators must implement
mouse move event handler
return True to callback manipulable_manipulate
"""
raise NotImplementedError
# Keyboard event handlers, MAY be overridden
def keyboard_done(self, context, event, value):
"""
Manipulators may implement
keyboard value validated event handler
value: changed by keyboard
return True to callback manipulable_manipulate
"""
return False
def keyboard_editing(self, context, event, value):
"""
Manipulators may implement
keyboard value changed event handler
value: string changed by keyboard
allow realtime update of label
return False to show edited value on window header
return True when feedback show right on screen
"""
self.label_value = value
return True
def keyboard_cancel(self, context, event):
"""
Manipulators may implement
keyboard entry cancelled
"""
return
def cancel(self, context, event):
"""
Manipulators may implement
cancelled event (ESC RIGHTCLICK)
"""
self.active = False
return
def undo(self, context, event):
"""
Manipulators may implement
undo event (CTRL+Z)
"""
return False
# Internal, do not override unless you really
# really really deeply know what you are doing
def keyboard_eval(self, context, event):
"""
evaluate keyboard entry while typing
do not override this one
"""
c = event.ascii
if c:
if c == ",":
c = "."
self.length_entered = self.length_entered[:self.line_pos] + c + self.length_entered[self.line_pos:]
self.line_pos += 1
if self.length_entered:
if event.type == 'BACK_SPACE':
self.length_entered = self.length_entered[:self.line_pos - 1] + self.length_entered[self.line_pos:]
self.line_pos -= 1
elif event.type == 'DEL':
self.length_entered = self.length_entered[:self.line_pos] + self.length_entered[self.line_pos + 1:]
elif event.type == 'LEFT_ARROW':
self.line_pos = (self.line_pos - 1) % (len(self.length_entered) + 1)
elif event.type == 'RIGHT_ARROW':
self.line_pos = (self.line_pos + 1) % (len(self.length_entered) + 1)
try:
value = bpy.utils.units.to_value(context.scene.unit_settings.system, self.value_type, self.length_entered)
draw_on_header = self.keyboard_editing(context, event, value)
except: # ValueError:
draw_on_header = True
pass
if draw_on_header:
a = ""
if self.length_entered:
pos = self.line_pos
a = self.length_entered[:pos] + '|' + self.length_entered[pos:]
context.area.header_text_set("%s" % (a))
# modal mode: do not let event bubble up
return True
def modal(self, context, event):
"""
Modal handler
handle mouse, and keyboard events
enable and disable feedback
"""
# print("Manipulator modal:%s %s" % (event.value, event.type))
if event.type == 'MOUSEMOVE':
return self.mouse_move(context, event)
elif event.value == 'PRESS':
if event.type == 'LEFTMOUSE':
active = self.mouse_press(context, event)
if active:
self.feedback.enable()
return active
elif self.keymap.check(event, self.keymap.undo):
if self.keyboard_input_active:
self.keyboard_input_active = False
self.keyboard_cancel(context, event)
self.feedback.disable()
# prevent undo CRASH
return True
elif self.keyboard_input_active and (
event.ascii in self.keyboard_ascii or
event.type in self.keyboard_type
):
# get keyboard input
return self.keyboard_eval(context, event)
elif event.type in {'ESC', 'RIGHTMOUSE'}:
self.feedback.disable()
if self.keyboard_input_active:
# allow keyboard exit without setting value
self.length_entered = ""
self.line_pos = 0
self.keyboard_input_active = False
self.keyboard_cancel(context, event)
return True
elif self.active:
self.cancel(context, event)
return True
return False
elif event.value == 'RELEASE':
if event.type == 'LEFTMOUSE':
if not self.keyboard_input_active:
self.feedback.disable()
return self.mouse_release(context, event)
elif self.keyboard_input_active and event.type in {'RET', 'NUMPAD_ENTER'}:
# validate keyboard input
if self.length_entered != "":
try:
value = bpy.utils.units.to_value(
context.scene.unit_settings.system,
self.value_type, self.length_entered)
self.length_entered = ""
ret = self.keyboard_done(context, event, value)
except: # ValueError:
ret = False
self.keyboard_cancel(context, event)
pass
context.area.header_text_set(None)
self.keyboard_input_active = False
self.feedback.disable()
return ret
return False
def mouse_position(self, event):
"""
store mouse position in a 2d Vector
"""
self.mouse_pos.x, self.mouse_pos.y = event.mouse_region_x, event.mouse_region_y
def get_pos3d(self, context):
"""
convert mouse pos to 3d point over plane defined by origin and normal
pt is in world space
"""
region = context.region
rv3d = context.region_data
rM = context.active_object.matrix_world.to_3x3()
view_vector_mouse = view3d_utils.region_2d_to_vector_3d(region, rv3d, self.mouse_pos)
ray_origin_mouse = view3d_utils.region_2d_to_origin_3d(region, rv3d, self.mouse_pos)
pt = intersect_line_plane(ray_origin_mouse, ray_origin_mouse + view_vector_mouse,
self.origin, rM @ self.manipulator.normal, False)
# fix issue with parallel plane
if pt is None:
pt = intersect_line_plane(ray_origin_mouse, ray_origin_mouse + view_vector_mouse,
self.origin, view_vector_mouse, False)
return pt
def get_value(self, data, attr, index=-1):
"""
Datablock value getter with index support
"""
try:
if index > -1:
return getattr(data, attr)[index]
else:
return getattr(data, attr)
except:
print("get_value of %s %s failed" % (data, attr))
return 0
def set_value(self, context, data, attr, value, index=-1):
"""
Datablock value setter with index support
"""
try:
if self.get_value(data, attr, index) != value:
o = self.o
# switch context so unselected object may be manipulable too
old = context.object
state = o.select_get()
o.select_set(state=True)
context.view_layer.objects.active = o
if index > -1:
getattr(data, attr)[index] = value
else:
setattr(data, attr, value)
o.select_set(state=state)
old.select_set(state=True)
context.view_layer.objects.active = old
except:
pass
def preTranslate(self, tM, vec):
"""
return a preTranslated Matrix
tM Matrix source
vec Vector translation
"""
return tM @ Matrix.Translation(vec)
def _move(self, o, axis, value):
if axis == 'x':
vec = Vector((value, 0, 0))
elif axis == 'y':
vec = Vector((0, value, 0))
else:
vec = Vector((0, 0, value))
o.matrix_world = self.preTranslate(o.matrix_world, vec)
def move_linked(self, context, axis, value):
"""
Move an object along local axis
takes care of linked too, fix issue #8
"""
old = context.active_object
bpy.ops.object.select_all(action='DESELECT')
self.o.select_set(state=True)
context.view_layer.objects.active = self.o
bpy.ops.object.select_linked(type='OBDATA')
for o in context.selected_objects:
if o != self.o:
self._move(o, axis, value)
bpy.ops.object.select_all(action='DESELECT')
old.select_set(state=True)
context.view_layer.objects.active = old
def move(self, context, axis, value):
"""
Move an object along local axis
"""
self._move(self.o, axis, value)
# Generic snap tool for line based archipack objects (fence, wall, maybe stair too)
gl_pts3d = []
class WallSnapManipulator(Manipulator):
"""
np_station snap inspired manipulator
Use prop1_name as string part index
Use prop2_name as string identifier height property for placeholders
Misnamed as it work for all line based archipack's
primitives, currently wall and fences,
but may also work with stairs (sharing same data structure)
"""
def __init__(self, context, o, datablock, manipulator, handle_size, snap_callback=None):
self.placeholder_area = GlPolygon((0.5, 0, 0, 0.2))
self.placeholder_line = GlPolyline((0.5, 0, 0, 0.8))
self.placeholder_line.closed = True
self.label = GlText()
self.line = GlLine()
self.handle = SquareHandle(handle_size, 1.2 * arrow_size, draggable=True, selectable=True)
Manipulator.__init__(self, context, o, datablock, manipulator, snap_callback)
self.selectable = True
def select(self, cursor_area):
self.selected = self.selected or cursor_area.in_area(self.handle.pos_2d)
self.handle.selected = self.selected
def deselect(self, cursor_area):
self.selected = not cursor_area.in_area(self.handle.pos_2d)
self.handle.selected = self.selected
def check_hover(self):
self.handle.check_hover(self.mouse_pos)
def mouse_press(self, context, event):
global gl_pts3d
global manips
if self.handle.hover:
self.active = True
self.handle.active = True
gl_pts3d = []
idx = int(self.manipulator.prop1_name)
# get selected manipulators idx
selection = []
for m in manips[self.o.name].stack:
if m is not None and m.selected:
selection.append(int(m.manipulator.prop1_name))
# store all points of wall
for i, part in enumerate(self.datablock.parts):
p0, p1, side, normal = part.manipulators[2].get_pts(self.o.matrix_world)
# if selected p0 will move and require placeholder
gl_pts3d.append((p0, p1, i in selection or i == idx))
self.feedback.instructions(context, "Move / Snap", "Drag to move, use keyboard to input values", [
('CTRL', 'Snap'),
('X Y', 'Constraint to axis (toggle Global Local None)'),
('SHIFT+Z', 'Constraint to xy plane'),
('MMBTN', 'Constraint to axis'),
('RIGHTCLICK or ESC', 'exit without change')
])
self.feedback.enable()
self.handle.hover = False
self.o.select_set(state=True)
takeloc, right, side, dz = self.manipulator.get_pts(self.o.matrix_world)
dx = (right - takeloc).normalized()
dy = dz.cross(dx)
takemat = Matrix([
[dx.x, dy.x, dz.x, takeloc.x],
[dx.y, dy.y, dz.y, takeloc.y],
[dx.z, dy.z, dz.z, takeloc.z],
[0, 0, 0, 1]
])
snap_point(takemat=takemat, draw=self.sp_draw, callback=self.sp_callback,
constraint_axis=(True, True, False))
# this prevent other selected to run
return True
return False
def mouse_release(self, context, event):
self.check_hover()
self.handle.active = False
self.active = False
self.feedback.disable()
# False to callback manipulable_release
return False
def sp_callback(self, context, event, state, sp):
"""
np station callback on moving, place, or cancel
"""
global gl_pts3d
logger.debug("WallSnapManipulator.sp_callback")
if state == 'SUCCESS':
o = self.o
o.select_set(state=True)
context.view_layer.objects.active = o
# apply changes to wall
d = self.datablock
g = d.get_generator()
# rotation relative to object
rM = o.matrix_world.inverted().to_3x3()
delta =rM @ sp.delta
# x_axis = (rM @ Vector((1, 0, 0))).to_2d()
# update generator
idx = 0
for p0, p1, selected in gl_pts3d:
if selected:
# new location in object space
pt = g.segs[idx].lerp(0) + delta.to_2d()
# move last point of segment before current
if idx > 0:
g.segs[idx - 1].p1 = pt
# move first point of current segment
g.segs[idx].p0 = pt
idx += 1
# update properties from generator
idx = 0
d.auto_update = False
for p0, p1, selected in gl_pts3d:
if selected:
# adjust segment before current
if idx > 0:
w = g.segs[idx - 1]
part = d.parts[idx - 1]
if idx > 1:
part.a0 = w.delta_angle(g.segs[idx - 2])
else:
part.a0 = w.a0
if "C_" in part.type:
part.radius = w.r
else:
part.length = w.length
# adjust current segment
w = g.segs[idx]
part = d.parts[idx]
if idx > 0:
part.a0 = w.delta_angle(g.segs[idx - 1])
else:
part.a0 = w.a0
# move object when point 0
if USE_MOVE_OBJECT:
d.move_object(o, o.matrix_world.translation + sp.delta)
# self.o.location += sp.delta
# self.o.matrix_world.translation += sp.delta
else:
d.origin += sp.delta
if "C_" in part.type:
part.radius = w.r
else:
part.length = w.length
# adjust next one
if idx + 1 < d.n_parts:
d.parts[idx + 1].a0 = g.segs[idx + 1].delta_angle(w)
idx += 1
self.mouse_release(context, event)
if hasattr(d, "relocate_childs"):
d.relocate_childs(context, o, g)
d.auto_update = True
d.update(context)
if state == 'CANCEL':
self.mouse_release(context, event)
logger.debug("WallSnapManipulator.sp_callback done")
return
def sp_draw(self, sp, context):
# draw wall placeholders
logger.debug("WallSnapManipulator.sp_draw")
global gl_pts3d
if self.o is None:
return
z = self.get_value(self.datablock, self.manipulator.prop2_name)
placeholders = []
for p0, p1, selected in gl_pts3d:
pt = p0.copy()
if selected:
# when selected, p0 is moving
# last one p1 should move too
# last one require a placeholder too
pt += sp.delta
if len(placeholders) > 0:
placeholders[-1][1] = pt
placeholders[-1][2] = True
placeholders.append([pt, p1, selected])
# first selected and closed -> should move last p1 too
if gl_pts3d[0][2] and self.datablock.closed:
placeholders[-1][1] = placeholders[0][0].copy()
placeholders[-1][2] = True
# last one not visible when not closed
if not self.datablock.closed:
placeholders[-1][2] = False
for p0, p1, selected in placeholders:
if selected:
self.placeholder_area.set_pos([p0, p1, Vector((p1.x, p1.y, p1.z + z)), Vector((p0.x, p0.y, p0.z + z))])
self.placeholder_line.set_pos([p0, p1, Vector((p1.x, p1.y, p1.z + z)), Vector((p0.x, p0.y, p0.z + z))])
self.placeholder_area.draw(context, render=False)
self.placeholder_line.draw(context, render=False)
p0, p1, side, normal = self.manipulator.get_pts(self.o.matrix_world)
self.line.p = p0
self.line.v = sp.delta
self.label.set_pos(context, self.line.length, self.line.lerp(0.5), self.line.v, normal=Vector((0, 0, 1)))
self.line.draw(context, render=False)
self.label.draw(context, render=False)
logger.debug("WallSnapManipulator.sp_draw done")
def mouse_move(self, context, event):
self.mouse_position(event)
if self.handle.active:
# False here to pass_through
# print("i'm able to pick up mouse move event while transform running")
return False
else:
self.check_hover()
return False
def draw_callback(self, _self, context, render=False):
left, right, side, normal = self.manipulator.get_pts(self.o.matrix_world)
self.handle.set_pos(context, left, (left - right).normalized(), normal=normal)
self.handle.draw(context, render)
self.feedback.draw(context, render)
class CounterManipulator(Manipulator):
"""
increase or decrease an integer step by step
right on click to prevent misuse
"""
def __init__(self, context, o, datablock, manipulator, handle_size, snap_callback=None):
self.handle_left = TriHandle(handle_size, arrow_size, draggable=True)
self.handle_right = TriHandle(handle_size, arrow_size, draggable=True)
self.line_0 = GlLine()
self.label = GlText()
self.label.unit_mode = 'NONE'
self.label.precision = 0
Manipulator.__init__(self, context, o, datablock, manipulator, snap_callback)
def check_hover(self):
self.handle_right.check_hover(self.mouse_pos)
self.handle_left.check_hover(self.mouse_pos)
def mouse_press(self, context, event):
if self.handle_right.hover:
value = self.get_value(self.datablock, self.manipulator.prop1_name)
self.set_value(context, self.datablock, self.manipulator.prop1_name, value + 1)
self.handle_right.active = True
return True
if self.handle_left.hover:
value = self.get_value(self.datablock, self.manipulator.prop1_name)
self.set_value(context, self.datablock, self.manipulator.prop1_name, value - 1)
self.handle_left.active = True
return True
return False
def mouse_release(self, context, event):
self.check_hover()
self.handle_right.active = False
self.handle_left.active = False
return False
def mouse_move(self, context, event):
self.mouse_position(event)
if self.handle_right.active:
return True
if self.handle_left.active:
return True
else:
self.check_hover()
return False
def draw_callback(self, _self, context, render=False):
"""
draw on screen feedback using gl.
"""
# won't render counter
if render:
return
left, right, side, normal = self.manipulator.get_pts(self.o.matrix_world)
self.origin = left
self.line_0.p = left
self.line_0.v = right - left
self.line_0.z_axis = normal
self.label.z_axis = normal
value = self.get_value(self.datablock, self.manipulator.prop1_name)
self.handle_left.set_pos(context, self.line_0.p, -self.line_0.v, normal=normal)
self.handle_right.set_pos(context, self.line_0.lerp(1), self.line_0.v, normal=normal)
self.label.set_pos(context, value, self.line_0.lerp(0.5), self.line_0.v, normal=normal)
self.label.draw(context, render)
self.handle_left.draw(context, render)
self.handle_right.draw(context, render)
class DumbStringManipulator(Manipulator):
"""
not a real manipulator, but allow to show a string
"""
def __init__(self, context, o, datablock, manipulator, handle_size, snap_callback=None):
self.label = GlText(colour=(0, 0, 0, 1))
self.label.unit_mode = 'NONE'
self.label.label = manipulator.prop1_name
Manipulator.__init__(self, context, o, datablock, manipulator, snap_callback)
def check_hover(self):
return False
def mouse_press(self, context, event):
return False
def mouse_release(self, context, event):
return False
def mouse_move(self, context, event):
return False
def draw_callback(self, _self, context, render=False):
"""
draw on screen feedback using gl.
"""
# won't render string
if render:
return
left, right, side, normal = self.manipulator.get_pts(self.o.matrix_world)
pos = left + 0.5 * (right - left)
self.label.set_pos(context, None, pos, pos, normal=normal)
self.label.draw(context, render)
class SizeManipulator(Manipulator):
def __init__(self, context, o, datablock, manipulator, handle_size, snap_callback=None):
self.handle_left = TriHandle(handle_size, arrow_size)
self.handle_right = TriHandle(handle_size, arrow_size, draggable=True)
self.line_0 = GlLine()
self.line_1 = GlLine()
self.line_2 = GlLine()
self.label = EditableText(handle_size, arrow_size, draggable=True)
# self.label.label = 'S '
Manipulator.__init__(self, context, o, datablock, manipulator, snap_callback)
def check_hover(self):
self.handle_right.check_hover(self.mouse_pos)
self.label.check_hover(self.mouse_pos)
def mouse_press(self, context, event):
global gl_pts3d
if self.handle_right.hover:
self.active = True
self.original_size = self.get_value(self.datablock, self.manipulator.prop1_name)
self.original_location = self.o.matrix_world.translation.copy()
self.feedback.instructions(context, "Size", "Drag or Keyboard to modify size", [
('CTRL', 'Snap'),
('SHIFT', 'Round'),
('RIGHTCLICK or ESC', 'cancel')
])
left, right, side, dz = self.manipulator.get_pts(self.o.matrix_world)
dx = (right - left).normalized()
dy = dz.cross(dx)
takemat = Matrix([
[dx.x, dy.x, dz.x, right.x],
[dx.y, dy.y, dz.y, right.y],
[dx.z, dy.z, dz.z, right.z],
[0, 0, 0, 1]
])
gl_pts3d = [left, right]
snap_point(takemat=takemat,
callback=self.sp_callback,
constraint_axis=(True, False, False))
self.handle_right.active = True
return True
if self.label.hover:
self.feedback.instructions(context, "Size", "Use keyboard to modify size",
[('ENTER', 'Validate'), ('RIGHTCLICK or ESC', 'cancel')])
self.label.active = True
self.keyboard_input_active = True
return True
return False
def mouse_release(self, context, event):
self.active = False
self.check_hover()
self.handle_right.active = False
if not self.keyboard_input_active:
self.feedback.disable()
return False
def mouse_move(self, context, event):
self.mouse_position(event)
if self.active:
self.update(context, event)
return True
else:
self.check_hover()
return False
def cancel(self, context, event):
if self.active:
self.mouse_release(context, event)
self.set_value(context, self.datablock, self.manipulator.prop1_name, self.original_size)
def keyboard_done(self, context, event, value):
self.set_value(context, self.datablock, self.manipulator.prop1_name, value)
self.label.active = False
return True
def keyboard_cancel(self, context, event):
self.label.active = False
return False
def update(self, context, event):
# 0 1 2
# |_____|
#
pt = self.get_pos3d(context)
pt, t = intersect_point_line(pt, self.line_0.p, self.line_2.p)
length = (self.line_0.p - pt).length
if event.alt:
length = round(length, 1)
self.set_value(context, self.datablock, self.manipulator.prop1_name, length)
def draw_callback(self, _self, context, render=False):
"""
draw on screen feedback using gl.
"""
left, right, side, normal = self.manipulator.get_pts(self.o.matrix_world)
self.origin = left
self.line_1.p = left
self.line_1.v = right - left
self.line_0.z_axis = normal
self.line_1.z_axis = normal
self.line_2.z_axis = normal
self.label.z_axis = normal
self.line_0 = self.line_1.sized_normal(0, side.x * 1.1)
self.line_2 = self.line_1.sized_normal(1, side.x * 1.1)
self.line_1.offset(side.x * 1.0)
self.handle_left.set_pos(context, self.line_1.p, -self.line_1.v, normal=normal)
self.handle_right.set_pos(context, self.line_1.lerp(1), self.line_1.v, normal=normal)
if not self.keyboard_input_active:
self.label_value = self.line_1.length
self.label.set_pos(context, self.label_value, self.line_1.lerp(0.5), self.line_1.v, normal=normal)
self.line_0.draw(context, render)
self.line_1.draw(context, render)
self.line_2.draw(context, render)
self.handle_left.draw(context, render)
self.handle_right.draw(context, render)
self.label.draw(context, render)
self.feedback.draw(context, render)
def sp_callback(self, context, event, state, sp):
logger.debug("SizeManipulator.sp_callback")
global gl_pts3d
p0 = gl_pts3d[0].copy()
p1 = gl_pts3d[1].copy()
if state != 'CANCEL':
p1 += sp.delta
length = (p0 - p1).length
if state != 'CANCEL' and event.alt:
if event.shift:
length = round(length, 2)
else:
length = round(length, 1)
self.set_value(context, self.datablock, self.manipulator.prop1_name, length)
if state != 'RUNNING':
self.mouse_release(context, event)
logger.debug("SizeManipulator.sp_callback done")
class SizeLocationManipulator(SizeManipulator):
"""
Handle resizing by any of the boundaries
of objects with centered pivots
so when size change, object should move of the
half of the change in the direction of change.
Also take care of moving linked objects too
Changing size is not necessary as link does
already handle this and childs panels are
updated by base object.
"""
def __init__(self, context, o, datablock, manipulator, handle_size, snap_callback=None):
SizeManipulator.__init__(self, context, o, datablock, manipulator, handle_size, snap_callback)
self.handle_left.draggable = True
def check_hover(self):
self.handle_right.check_hover(self.mouse_pos)
self.handle_left.check_hover(self.mouse_pos)
self.label.check_hover(self.mouse_pos)
def mouse_press(self, context, event):
if self.handle_right.hover:
self.active = True
self.original_location = self.o.matrix_world.translation.copy()
self.original_size = self.get_value(self.datablock, self.manipulator.prop1_name)
self.feedback.instructions(context, "Size", "Drag to modify size", [
('ALT', 'Round value'), ('RIGHTCLICK or ESC', 'cancel')
])
self.handle_right.active = True
return True
if self.handle_left.hover:
self.active = True
self.original_location = self.o.matrix_world.translation.copy()
self.original_size = self.get_value(self.datablock, self.manipulator.prop1_name)
self.feedback.instructions(context, "Size", "Drag to modify size", [
('ALT', 'Round value'), ('RIGHTCLICK or ESC', 'cancel')
])
self.handle_left.active = True
return True
if self.label.hover:
self.feedback.instructions(context, "Size", "Use keyboard to modify size",
[('ENTER', 'Validate'), ('RIGHTCLICK or ESC', 'cancel')])
self.label.active = True
self.keyboard_input_active = True
return True
return False
def mouse_release(self, context, event):
self.active = False
self.check_hover()
self.handle_right.active = False
self.handle_left.active = False
if not self.keyboard_input_active:
self.feedback.disable()
return False
def mouse_move(self, context, event):
self.mouse_position(event)
if self.handle_right.active or self.handle_left.active:
self.update(context, event)
return True
else:
self.check_hover()
return False
def keyboard_done(self, context, event, value):
self.set_value(context, self.datablock, self.manipulator.prop1_name, value)
# self.move_linked(context, self.manipulator.prop2_name, dl)
self.label.active = False
self.feedback.disable()
return True
def cancel(self, context, event):
if self.active:
self.mouse_release(context, event)
# must move back to original location
itM = self.o.matrix_world.inverted()
dl = self.get_value(itM @ self.original_location, self.manipulator.prop2_name)
self.move(context, self.manipulator.prop2_name, dl)
self.set_value(context, self.datablock, self.manipulator.prop1_name, self.original_size)
self.move_linked(context, self.manipulator.prop2_name, dl)
def update(self, context, event):
# 0 1 2
# |_____|
#
pt = self.get_pos3d(context)
pt, t = intersect_point_line(pt, self.line_0.p, self.line_2.p)
len_0 = (pt - self.line_0.p).length
len_1 = (pt - self.line_2.p).length
length = max(len_0, len_1)
if event.alt:
length = round(length, 1)
dl = length - self.line_1.length
if len_0 > len_1:
dl = 0.5 * dl
else:
dl = -0.5 * dl
self.move(context, self.manipulator.prop2_name, dl)
self.set_value(context, self.datablock, self.manipulator.prop1_name, length)
self.move_linked(context, self.manipulator.prop2_name, dl)
class SnapSizeLocationManipulator(SizeLocationManipulator):
"""
Snap aware extension of SizeLocationManipulator
Handle resizing by any of the boundaries
of objects with centered pivots
so when size change, object should move of the
half of the change in the direction of change.
Also take care of moving linked objects too
Changing size is not necessary as link does
already handle this and childs panels are
updated by base object.
"""
def __init__(self, context, o, datablock, manipulator, handle_size, snap_callback=None):
SizeLocationManipulator.__init__(self, context, o, datablock, manipulator, handle_size, snap_callback)
def mouse_press(self, context, event):
global gl_pts3d
if self.handle_right.hover:
self.active = True
self.original_size = self.get_value(self.datablock, self.manipulator.prop1_name)
self.original_location = self.o.matrix_world.translation.copy()
self.feedback.instructions(context, "Size", "Drag or Keyboard to modify size", [
('CTRL', 'Snap'),
('SHIFT', 'Round'),
('RIGHTCLICK or ESC', 'cancel')
])
left, right, side, dz = self.manipulator.get_pts(self.o.matrix_world)
dx = (right - left).normalized()
dy = dz.cross(dx)
takemat = Matrix([
[dx.x, dy.x, dz.x, right.x],
[dx.y, dy.y, dz.y, right.y],
[dx.z, dy.z, dz.z, right.z],
[0, 0, 0, 1]
])
gl_pts3d = [left, right]
snap_point(takemat=takemat,
callback=self.sp_callback,
constraint_axis=(True, False, False))
self.handle_right.active = True
return True
if self.handle_left.hover:
self.active = True
self.original_size = self.get_value(self.datablock, self.manipulator.prop1_name)
self.original_location = self.o.matrix_world.translation.copy()
self.feedback.instructions(context, "Size", "Drag or Keyboard to modify size", [
('CTRL', 'Snap'),
('SHIFT', 'Round'),
('RIGHTCLICK or ESC', 'cancel')
])
left, right, side, dz = self.manipulator.get_pts(self.o.matrix_world)
dx = (left - right).normalized()
dy = dz.cross(dx)
takemat = Matrix([
[dx.x, dy.x, dz.x, left.x],
[dx.y, dy.y, dz.y, left.y],
[dx.z, dy.z, dz.z, left.z],
[0, 0, 0, 1]
])
gl_pts3d = [left, right]
snap_point(takemat=takemat,
callback=self.sp_callback,
constraint_axis=(True, False, False))
self.handle_left.active = True
return True
if self.label.hover:
self.feedback.instructions(context, "Size", "Use keyboard to modify size",
[('ENTER', 'Validate'), ('RIGHTCLICK or ESC', 'cancel')])
self.label.active = True
self.keyboard_input_active = True
return True
return False
def sp_callback(self, context, event, state, sp):
logger.debug("SnapSizeLocationManipulator.sp_callback")
global gl_pts3d
p0 = gl_pts3d[0].copy()
p1 = gl_pts3d[1].copy()
if state != 'CANCEL':
if self.handle_right.active:
p1 += sp.delta
else:
p0 += sp.delta
l0 = self.get_value(self.datablock, self.manipulator.prop1_name)
length = (p0 - p1).length
if state != 'CANCEL' and event.alt:
if event.shift:
length = round(length, 2)
else:
length = round(length, 1)
dp = length - l0
if self.handle_left.active:
dp = -dp
dl = 0.5 * dp
# snap_helper = context.object
self.move(context, self.manipulator.prop2_name, dl)
self.set_value(context, self.datablock, self.manipulator.prop1_name, length)
self.move_linked(context, self.manipulator.prop2_name, dl)
# snapping child objects may require base object update
# eg manipulating windows requiring wall update
if self.snap_callback is not None:
snap_helper = context.active_object
self.snap_callback(context, o=self.o, manipulator=self)
snap_helper.select_set(state=True)
if state != 'RUNNING':
self.mouse_release(context, event)
logger.debug("SnapSizeLocationManipulator.sp_callback done")
class DeltaLocationManipulator(SizeManipulator):
"""
Move a child window or door in wall segment
not limited to this by the way
"""
def __init__(self, context, o, datablock, manipulator, handle_size, snap_callback=None):
SizeManipulator.__init__(self, context, o, datablock, manipulator, handle_size, snap_callback)
self.label.label = ''
self.feedback.instructions(context, "Move", "Drag to move", [
('CTRL', 'Snap'),
('SHIFT', 'Round value'),
('RIGHTCLICK or ESC', 'cancel')
])
def check_hover(self):
self.handle_right.check_hover(self.mouse_pos)
def mouse_press(self, context, event):
global gl_pts3d
if self.handle_right.hover:
self.original_location = self.o.matrix_world.translation.copy()
self.active = True
self.feedback.enable()
self.handle_right.active = True
left, right, side, dz = self.manipulator.get_pts(self.o.matrix_world)
dp = (right - left)
dx = dp.normalized()
dy = dz.cross(dx)
p0 = left + 0.5 * dp
takemat = Matrix([
[dx.x, dy.x, dz.x, p0.x],
[dx.y, dy.y, dz.y, p0.y],
[dx.z, dy.z, dz.z, p0.z],
[0, 0, 0, 1]
])
gl_pts3d = [p0]
snap_point(takemat=takemat,
callback=self.sp_callback,
constraint_axis=(
self.manipulator.prop1_name == 'x',
self.manipulator.prop1_name == 'y',
self.manipulator.prop1_name == 'z'))
return True
return False
def mouse_release(self, context, event):
self.check_hover()
self.feedback.disable()
self.active = False
self.handle_right.active = False
return False
def mouse_move(self, context, event):
self.mouse_position(event)
if self.handle_right.active:
# self.update(context, event)
return True
else:
self.check_hover()
return False
def sp_callback(self, context, event, state, sp):
logger.debug("DeltaLocationManipulator.sp_callback")
if state == 'CANCEL':
self.cancel(context, event)
else:
global gl_pts3d
p0 = gl_pts3d[0].copy()
p1 = p0 + sp.delta
itM = self.o.matrix_world.inverted()
dl = self.get_value(itM @ p1, self.manipulator.prop1_name)
self.move(context, self.manipulator.prop1_name, dl)
# snapping child objects may require base object update
# eg manipulating windows requiring wall update
if self.snap_callback is not None:
snap_helper = context.active_object
self.snap_callback(context, o=self.o, manipulator=self)
snap_helper.select_set(state=True)
if state == 'SUCCESS':
self.mouse_release(context, event)
logger.debug("DeltaLocationManipulator.sp_callback done")
def cancel(self, context, event):
if self.active:
self.mouse_release(context, event)
# must move back to original location
itM = self.o.matrix_world.inverted()
dl = self.get_value(itM @ self.original_location, self.manipulator.prop1_name)
self.move(context, self.manipulator.prop1_name, dl)
def draw_callback(self, _self, context, render=False):
"""
draw on screen feedback using gl.
"""
left, right, side, normal = self.manipulator.get_pts(self.o.matrix_world)
self.origin = left
self.line_1.p = left
self.line_1.v = right - left
self.line_1.z_axis = normal
self.handle_left.set_pos(context, self.line_1.lerp(0.5), -self.line_1.v, normal=normal)
self.handle_right.set_pos(context, self.line_1.lerp(0.5), self.line_1.v, normal=normal)
self.handle_left.draw(context, render)
self.handle_right.draw(context, render)
self.feedback.draw(context)
class DumbSizeManipulator(SizeManipulator):
"""
Show a size while not being editable
"""
def __init__(self, context, o, datablock, manipulator, handle_size, snap_callback=None):
SizeManipulator.__init__(self, context, o, datablock, manipulator, handle_size, snap_callback)
self.handle_right.draggable = False
self.label.draggable = False
self.label.colour_inactive = (0, 0, 0, 1)
# self.label.label = 'Dumb '
def mouse_move(self, context, event):
return False
class AngleManipulator(Manipulator):
"""
NOTE:
There is a default shortcut to +5 and -5 on angles with left/right arrows
Manipulate angle between segments
bound to [-pi, pi]
"""
def __init__(self, context, o, datablock, manipulator, handle_size, snap_callback=None):
# Angle
self.handle_right = TriHandle(handle_size, arrow_size, draggable=True)
self.handle_center = SquareHandle(handle_size, arrow_size)
self.arc = GlArc()
self.line_0 = GlLine()
self.line_1 = GlLine()
self.label_a = EditableText(handle_size, arrow_size, draggable=True)
self.label_a.unit_type = 'ANGLE'
Manipulator.__init__(self, context, o, datablock, manipulator, snap_callback)
self.pts_mode = 'RADIUS'
def check_hover(self):
self.handle_right.check_hover(self.mouse_pos)
self.label_a.check_hover(self.mouse_pos)
def mouse_press(self, context, event):
if self.handle_right.hover:
self.active = True
self.original_angle = self.get_value(self.datablock, self.manipulator.prop1_name)
self.feedback.instructions(context, "Angle", "Drag to modify angle", [
('SHIFT', 'Round value'),
('RIGHTCLICK or ESC', 'cancel')
])
self.handle_right.active = True
return True
if self.label_a.hover:
self.feedback.instructions(context, "Angle (degree)", "Use keyboard to modify angle",
[('ENTER', 'validate'),
('RIGHTCLICK or ESC', 'cancel')])
self.value_type = 'ROTATION'
self.label_a.active = True
self.label_value = self.get_value(self.datablock, self.manipulator.prop1_name)
self.keyboard_input_active = True
return True
return False
def mouse_release(self, context, event):
self.check_hover()
self.handle_right.active = False
self.active = False
return False
def mouse_move(self, context, event):
self.mouse_position(event)
if self.active:
# print("AngleManipulator.mouse_move")
self.update(context, event)
return True
else:
self.check_hover()
return False
def keyboard_done(self, context, event, value):
self.set_value(context, self.datablock, self.manipulator.prop1_name, value)
self.label_a.active = False
return True
def keyboard_cancel(self, context, event):
self.label_a.active = False
return False
def cancel(self, context, event):
if self.active:
self.mouse_release(context, event)
self.set_value(context, self.datablock, self.manipulator.prop1_name, self.original_angle)
def update(self, context, event):
pt = self.get_pos3d(context)
c = self.arc.c
v = 2 * self.arc.r * (pt - c).normalized()
v0 = c - v
v1 = c + v
p0, p1 = intersect_line_sphere(v0, v1, c, self.arc.r)
if p0 is not None and p1 is not None:
if (p1 - pt).length < (p0 - pt).length:
p0, p1 = p1, p0
v = p0 - self.arc.c
da = atan2(v.y, v.x) - self.line_0.angle
if da > pi:
da -= 2 * pi
if da < -pi:
da += 2 * pi
# from there pi > da > -pi
# print("a:%.4f da:%.4f a0:%.4f" % (atan2(v.y, v.x), da, self.line_0.angle))
if da > pi:
da = pi
if da < -pi:
da = -pi
if event.shift:
da = round(da / pi * 180, 0) / 180 * pi
self.set_value(context, self.datablock, self.manipulator.prop1_name, da)
def draw_callback(self, _self, context, render=False):
c, left, right, normal = self.manipulator.get_pts(self.o.matrix_world)
self.line_0.z_axis = normal
self.line_1.z_axis = normal
self.arc.z_axis = normal
self.label_a.z_axis = normal
self.origin = c
self.line_0.p = c
self.line_1.p = c
self.arc.c = c
self.line_0.v = left
self.line_0.v = -self.line_0.cross.normalized()
self.line_1.v = right
self.line_1.v = self.line_1.cross.normalized()
self.arc.a0 = self.line_0.angle
self.arc.da = self.get_value(self.datablock, self.manipulator.prop1_name)
self.arc.r = 1.0
self.handle_right.set_pos(context, self.line_1.lerp(1),
self.line_1.sized_normal(1, -1 if self.arc.da > 0 else 1).v)
self.handle_center.set_pos(context, self.arc.c, -self.line_0.v)
label_value = self.arc.da
if self.keyboard_input_active:
label_value = self.label_value
self.label_a.set_pos(context, label_value, self.arc.lerp(0.5), -self.line_0.v)
self.arc.draw(context, render)
self.line_0.draw(context, render)
self.line_1.draw(context, render)
self.handle_right.draw(context, render)
self.handle_center.draw(context, render)
self.label_a.draw(context, render)
self.feedback.draw(context, render)
class DumbAngleManipulator(AngleManipulator):
def __init__(self, context, o, datablock, manipulator, handle_size, snap_callback=None):
AngleManipulator.__init__(self, context, o, datablock, manipulator, handle_size, snap_callback=None)
self.handle_right.draggable = False
self.label_a.draggable = False
def draw_callback(self, _self, context, render=False):
c, left, right, normal = self.manipulator.get_pts(self.o.matrix_world)
self.line_0.z_axis = normal
self.line_1.z_axis = normal
self.arc.z_axis = normal
self.label_a.z_axis = normal
self.origin = c
self.line_0.p = c
self.line_1.p = c
self.arc.c = c
self.line_0.v = left
self.line_0.v = -self.line_0.cross.normalized()
self.line_1.v = right
self.line_1.v = self.line_1.cross.normalized()
# prevent ValueError in angle_signed
if self.line_0.length == 0 or self.line_1.length == 0:
return
self.arc.a0 = self.line_0.angle
self.arc.da = self.line_1.v.to_2d().angle_signed(self.line_0.v.to_2d())
self.arc.r = 1.0
self.handle_right.set_pos(context, self.line_1.lerp(1),
self.line_1.sized_normal(1, -1 if self.arc.da > 0 else 1).v)
self.handle_center.set_pos(context, self.arc.c, -self.line_0.v)
label_value = self.arc.da
self.label_a.set_pos(context, label_value, self.arc.lerp(0.5), -self.line_0.v)
self.arc.draw(context, render)
self.line_0.draw(context, render)
self.line_1.draw(context, render)
self.handle_right.draw(context, render)
self.handle_center.draw(context, render)
self.label_a.draw(context, render)
self.feedback.draw(context, render)
class ArcAngleManipulator(Manipulator):
"""
Manipulate angle of an arc
when angle < 0 the arc center is on the left part of the circle
when angle > 0 the arc center is on the right part of the circle
bound to [-pi, pi]
"""
def __init__(self, context, o, datablock, manipulator, handle_size, snap_callback=None):
# Fixed
self.handle_left = SquareHandle(handle_size, arrow_size)
# Angle
self.handle_right = TriHandle(handle_size, arrow_size, draggable=True)
self.handle_center = SquareHandle(handle_size, arrow_size)
self.arc = GlArc()
self.line_0 = GlLine()
self.line_1 = GlLine()
self.label_a = EditableText(handle_size, arrow_size, draggable=True)
self.label_r = EditableText(handle_size, arrow_size, draggable=False)
self.label_a.unit_type = 'ANGLE'
Manipulator.__init__(self, context, o, datablock, manipulator, snap_callback)
self.pts_mode = 'RADIUS'
def check_hover(self):
self.handle_right.check_hover(self.mouse_pos)
self.label_a.check_hover(self.mouse_pos)
def mouse_press(self, context, event):
if self.handle_right.hover:
self.active = True
self.original_angle = self.get_value(self.datablock, self.manipulator.prop1_name)
self.feedback.instructions(context, "Angle (degree)", "Drag to modify angle", [
('SHIFT', 'Round value'),
('RIGHTCLICK or ESC', 'cancel')
])
self.handle_right.active = True
return True
if self.label_a.hover:
self.feedback.instructions(context, "Angle (degree)", "Use keyboard to modify angle",
[('ENTER', 'validate'),
('RIGHTCLICK or ESC', 'cancel')])
self.value_type = 'ROTATION'
self.label_value = self.get_value(self.datablock, self.manipulator.prop1_name)
self.label_a.active = True
self.keyboard_input_active = True
return True
if self.label_r.hover:
self.feedback.instructions(context, "Radius", "Use keyboard to modify radius",
[('ENTER', 'validate'),
('RIGHTCLICK or ESC', 'cancel')])
self.value_type = 'LENGTH'
self.label_r.active = True
self.keyboard_input_active = True
return True
return False
def mouse_release(self, context, event):
self.check_hover()
self.handle_right.active = False
self.active = False
return False
def mouse_move(self, context, event):
self.mouse_position(event)
if self.handle_right.active:
self.update(context, event)
return True
else:
self.check_hover()
return False
def keyboard_done(self, context, event, value):
self.set_value(context, self.datablock, self.manipulator.prop1_name, value)
self.label_a.active = False
self.label_r.active = False
return True
def keyboard_cancel(self, context, event):
self.label_a.active = False
self.label_r.active = False
return False
def cancel(self, context, event):
if self.active:
self.mouse_release(context, event)
self.set_value(context, self.datablock, self.manipulator.prop1_name, self.original_angle)
def update(self, context, event):
pt = self.get_pos3d(context)
c = self.arc.c
v = 2 * self.arc.r * (pt - c).normalized()
v0 = c - v
v1 = c + v
p0, p1 = intersect_line_sphere(v0, v1, c, self.arc.r)
if p0 is not None and p1 is not None:
# find nearest mouse intersection point
if (p1 - pt).length < (p0 - pt).length:
p0, p1 = p1, p0
v = p0 - self.arc.c
s = self.arc.tangeant(0, 1)
res, d, t = s.point_sur_segment(pt)
if d > 0:
# right side
a = self.arc.sized_normal(0, self.arc.r).angle
else:
a = self.arc.sized_normal(0, -self.arc.r).angle
da = atan2(v.y, v.x) - a
# bottom side +- pi
if t < 0:
# right
if d > 0:
da = pi
else:
da = -pi
# top side bound to +- pi
else:
if da > pi:
da -= 2 * pi
if da < -pi:
da += 2 * pi
if event.shift:
da = round(da / pi * 180, 0) / 180 * pi
self.set_value(context, self.datablock, self.manipulator.prop1_name, da)
def draw_callback(self, _self, context, render=False):
# center : 3d points
# left : 3d vector pt-c
# right : 3d vector pt-c
c, left, right, normal = self.manipulator.get_pts(self.o.matrix_world)
self.line_0.z_axis = normal
self.line_1.z_axis = normal
self.arc.z_axis = normal
self.label_a.z_axis = normal
self.label_r.z_axis = normal
self.origin = c
self.line_0.p = c
self.line_1.p = c
self.arc.c = c
self.line_0.v = left
self.line_1.v = right
self.arc.a0 = self.line_0.angle
self.arc.da = self.get_value(self.datablock, self.manipulator.prop1_name)
self.arc.r = left.length
self.handle_left.set_pos(context, self.line_0.lerp(1), self.line_0.v)
self.handle_right.set_pos(context, self.line_1.lerp(1),
self.line_1.sized_normal(1, -1 if self.arc.da > 0 else 1).v)
self.handle_center.set_pos(context, self.arc.c, -self.line_0.v)
label_a_value = self.arc.da
label_r_value = self.arc.r
if self.keyboard_input_active:
if self.value_type == 'LENGTH':
label_r_value = self.label_value
else:
label_a_value = self.label_value
self.label_a.set_pos(context, label_a_value, self.arc.lerp(0.5), -self.line_0.v)
self.label_r.set_pos(context, label_r_value, self.line_0.lerp(0.5), self.line_0.v)
self.arc.draw(context, render)
self.line_0.draw(context, render)
self.line_1.draw(context, render)
self.handle_left.draw(context, render)
self.handle_right.draw(context, render)
self.handle_center.draw(context, render)
self.label_r.draw(context, render)
self.label_a.draw(context, render)
self.feedback.draw(context, render)
class ArcAngleRadiusManipulator(ArcAngleManipulator):
"""
Manipulate angle and radius of an arc
when angle < 0 the arc center is on the left part of the circle
when angle > 0 the arc center is on the right part of the circle
bound to [-pi, pi]
"""
def __init__(self, context, o, datablock, manipulator, handle_size, snap_callback=None):
ArcAngleManipulator.__init__(self, context, o, datablock, manipulator, handle_size, snap_callback)
self.handle_center = TriHandle(handle_size, arrow_size, draggable=True)
self.label_r.draggable = True
def check_hover(self):
self.handle_right.check_hover(self.mouse_pos)
self.handle_center.check_hover(self.mouse_pos)
self.label_a.check_hover(self.mouse_pos)
self.label_r.check_hover(self.mouse_pos)
def mouse_press(self, context, event):
if self.handle_right.hover:
self.active = True
self.original_angle = self.get_value(self.datablock, self.manipulator.prop1_name)
self.feedback.instructions(context, "Angle (degree)", "Drag to modify angle", [
('SHIFT', 'Round value'),
('RIGHTCLICK or ESC', 'cancel')
])
self.handle_right.active = True
return True
if self.handle_center.hover:
self.active = True
self.original_radius = self.get_value(self.datablock, self.manipulator.prop2_name)
self.feedback.instructions(context, "Radius", "Drag to modify radius", [
('SHIFT', 'Round value'),
('RIGHTCLICK or ESC', 'cancel')
])
self.handle_center.active = True
return True
if self.label_a.hover:
self.feedback.instructions(context, "Angle (degree)", "Use keyboard to modify angle",
[('ENTER', 'validate'),
('RIGHTCLICK or ESC', 'cancel')])
self.value_type = 'ROTATION'
self.label_value = self.get_value(self.datablock, self.manipulator.prop1_name)
self.label_a.active = True
self.keyboard_input_active = True
return True
if self.label_r.hover:
self.feedback.instructions(context, "Radius", "Use keyboard to modify radius",
[('ENTER', 'validate'),
('RIGHTCLICK or ESC', 'cancel')])
self.value_type = 'LENGTH'
self.label_r.active = True
self.keyboard_input_active = True
return True
return False
def mouse_release(self, context, event):
self.check_hover()
self.active = False
self.handle_right.active = False
self.handle_center.active = False
return False
def mouse_move(self, context, event):
self.mouse_position(event)
if self.handle_right.active:
self.update(context, event)
return True
elif self.handle_center.active:
self.update_radius(context, event)
return True
else:
self.check_hover()
return False
def keyboard_done(self, context, event, value):
if self.value_type == 'LENGTH':
self.set_value(context, self.datablock, self.manipulator.prop2_name, value)
self.label_r.active = False
else:
self.set_value(context, self.datablock, self.manipulator.prop1_name, value)
self.label_a.active = False
return True
def update_radius(self, context, event):
pt = self.get_pos3d(context)
c = self.arc.c
left = self.line_0.lerp(1)
p, t = intersect_point_line(pt, c, left)
radius = (left - p).length
if event.alt:
radius = round(radius, 1)
self.set_value(context, self.datablock, self.manipulator.prop2_name, radius)
def cancel(self, context, event):
if self.handle_right.active:
self.mouse_release(context, event)
self.set_value(context, self.datablock, self.manipulator.prop1_name, self.original_angle)
if self.handle_center.active:
self.mouse_release(context, event)
self.set_value(context, self.datablock, self.manipulator.prop2_name, self.original_radius)
# ------------------------------------------------------------------
# Define a single Manipulator Properties to store on object
# ------------------------------------------------------------------
# Allow registering manipulators classes
manipulators_class_lookup = {}
def register_manipulator(type_key, manipulator_class):
if type_key in manipulators_class_lookup.keys():
raise RuntimeError("Manipulator of type {} already exists, unable to override".format(type_key))
manipulators_class_lookup[type_key] = manipulator_class
class archipack_manipulator(PropertyGroup):
"""
A property group to add to manipulable objects
type_key: type of manipulator
prop1_name = the property name of object to modify
prop2_name = another property name of object to modify (eg: angle and radius)
p0, p1, p2 3d Vectors as base points to represent manipulators on screen
normal Vector normal of plane on with draw manipulator
"""
type_key : StringProperty(default='SIZE')
# How 3d points are stored in manipulators ?
# SIZE = 2 absolute positioned and a scaling vector
# RADIUS = 1 absolute positioned (center) and 2 relatives (sides)
# POLYGON = 2 absolute positioned and a relative vector (for rect polygons)
pts_mode : StringProperty(default='SIZE')
prop1_name : StringProperty()
prop2_name : StringProperty()
p0 : FloatVectorProperty(subtype='XYZ')
p1 : FloatVectorProperty(subtype='XYZ')
p2 : FloatVectorProperty(subtype='XYZ')
# allow orientation of manipulators by default on xy plane,
# but may be used to constrain heights on local object space
normal : FloatVectorProperty(subtype='XYZ', default=(0, 0, 1))
def set_pts(self, pts, normal=None):
"""
set 3d location of gl points (in object space)
pts: array of 3 vectors 3d
normal: optional vector 3d default to Z axis
"""
pts = [Vector(p) for p in pts]
self.p0, self.p1, self.p2 = pts
if normal is not None:
self.normal = Vector(normal)
def get_pts(self, tM):
"""
convert points from local to world absolute
to draw them at the right place
tM : object's world matrix
"""
rM = tM.to_3x3()
if self.pts_mode in ['SIZE', 'POLYGON']:
return tM @ self.p0, tM @ self.p1, self.p2, rM @ self.normal
else:
return tM @ self.p0, rM @ self.p1, rM @ self.p2, rM @ self.normal
def get_prefs(self, context):
global __name__
global arrow_size
global handle_size
try:
# retrieve addon name from imports
addon_name = __name__.split('.')[0]
prefs = context.preferences.addons[addon_name].preferences
arrow_size = prefs.arrow_size
handle_size = prefs.handle_size
except:
pass
def setup(self, context, o, datablock, snap_callback=None):
"""
Factory return a manipulator object or None
o: object
datablock: datablock to modify
snap_callback: function call y
"""
self.get_prefs(context)
global manipulators_class_lookup
if self.type_key not in manipulators_class_lookup.keys() or \
not manipulators_class_lookup[self.type_key].poll(context):
# RuntimeError is overkill but may be enabled for debug purposes
# Silently ignore allow skipping manipulators if / when deps as not meet
# manip stack will simply be filled with None objects
# raise RuntimeError("Manipulator of type {} not found".format(self.type_key))
return None
m = manipulators_class_lookup[self.type_key](context, o, datablock, self, handle_size, snap_callback)
# points storage model as described upside
self.pts_mode = m.pts_mode
return m
# ------------------------------------------------------------------
# Define Manipulable to make a PropertyGroup manipulable
# ------------------------------------------------------------------
class ARCHIPACK_OT_manipulate(Operator):
bl_idname = "archipack.manipulate"
bl_label = "Manipulate"
bl_description = "Manipulate"
bl_options = {'REGISTER', 'UNDO'}
object_name : StringProperty(default="")
@classmethod
def poll(self, context):
return context.active_object is not None
def exit_selectmode(self, context, key):
"""
Hide select area on exit
"""
global manips
if key in manips.keys():
if manips[key].manipulable is not None:
manips[key].manipulable.manipulable_exit_selectmode(context)
def modal(self, context, event):
global manips
# Exit on stack change
# handle multiple object stack
# use object_name property to find manupulated object in stack
# select and make object active
# and exit when not found
if context.area is not None:
context.area.tag_redraw()
key = self.object_name
if check_stack(key):
self.exit_selectmode(context, key)
remove_manipulable(key)
# print("modal exit by check_stack(%s)" % (key))
return {'FINISHED'}
res = manips[key].manipulable.manipulable_modal(context, event)
if 'FINISHED' in res:
self.exit_selectmode(context, key)
remove_manipulable(key)
# print("modal exit by {FINISHED}")
return res
def invoke(self, context, event):
if context.space_data is not None and context.space_data.type == 'VIEW_3D':
context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}
else:
self.report({'WARNING'}, "Active space must be a View3d")
return {'CANCELLED'}
class ARCHIPACK_OT_disable_manipulate(Operator):
bl_idname = "archipack.disable_manipulate"
bl_label = "Disable Manipulate"
bl_description = "Disable any active manipulator"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(self, context):
return True
def execute(self, context):
empty_stack()
return {'FINISHED'}
class Manipulable():
"""
A class extending PropertyGroup to setup gl manipulators
Beware : prevent crash calling manipulable_disable()
before changing manipulated data structure
"""
manipulators : CollectionProperty(
type=archipack_manipulator,
# options={'SKIP_SAVE'},
# options={'HIDDEN'},
description="store 3d points to draw gl manipulators"
)
# TODO: make simple instance vars
manipulable_refresh : BoolProperty(
default=False,
options={'SKIP_SAVE'},
description="Flag enable to rebuild manipulators when data model change"
)
manipulate_mode : BoolProperty(
default=False,
options={'SKIP_SAVE'},
description="Flag manipulation state so we are able to toggle"
)
select_mode : BoolProperty(
default=False,
options={'SKIP_SAVE'},
description="Flag select state so we are able to toggle"
)
manipulable_selectable : BoolProperty(
default=False,
options={'SKIP_SAVE'},
description="Flag make manipulators selectable"
)
keymap = None
manipulable_area = None
manipulable_start_point = None
manipulable_end_point = None
manipulable_draw_handler = None
def setup_manipulators(self):
"""
Must implement manipulators creation
TODO: call from update and manipulable_setup
"""
raise NotImplementedError
def manipulable_draw_callback(self, _self, context):
self.manipulable_area.draw(context)
def manipulable_disable(self, context):
"""
disable gl draw handlers
"""
if self.keymap is None:
self.keymap = Keymaps(context)
self.manipulable_area = GlCursorArea()
self.manipulable_start_point = Vector((0, 0))
self.manipulable_end_point = Vector((0, 0))
o = context.active_object
if o is not None:
self.manipulable_exit_selectmode(context)
remove_manipulable(o.name)
self.manip_stack = add_manipulable(o.name, self)
self.manipulate_mode = False
self.select_mode = False
def manipulable_exit_selectmode(self, context):
self.manipulable_area.disable()
self.select_mode = False
# remove select draw handler
if self.manipulable_draw_handler is not None:
bpy.types.SpaceView3D.draw_handler_remove(
self.manipulable_draw_handler,
'WINDOW')
self.manipulable_draw_handler = None
def manipulable_setup(self, context):
"""
TODO: Implement the setup part as per parent object basis
"""
self.manipulable_disable(context)
o = context.active_object
self.setup_manipulators()
for m in self.manipulators:
self.manip_stack.append(m.setup(context, o, self))
def _manipulable_invoke(self, context):
object_name = context.active_object.name
# store a reference to self for operators
add_manipulable(object_name, self)
# copy context so manipulator always use
# invoke time context
ctx = context.copy()
# take care of context switching
# when call from outside of 3d view
if context.space_data is not None and context.space_data.type != 'VIEW_3D':
for window in bpy.context.window_manager.windows:
screen = window.screen
for area in screen.areas:
if area.type == 'VIEW_3D':
ctx['area'] = area
for region in area.regions:
if region.type == 'WINDOW':
ctx['region'] = region
break
if ctx is not None:
bpy.ops.archipack.manipulate(ctx, 'INVOKE_DEFAULT', object_name=object_name)
def manipulable_invoke(self, context):
"""
call this in operator invoke()
NB:
if override don't forget to call:
_manipulable_invoke(context)
"""
# print("manipulable_invoke self.manipulate_mode:%s" % (self.manipulate_mode))
if self.manipulate_mode:
self.manipulable_disable(context)
return False
# else:
# bpy.ops.archipack.disable_manipulate('INVOKE_DEFAULT')
# self.manip_stack = []
# kills other's manipulators
# self.manipulate_mode = True
self.manipulable_setup(context)
self.manipulate_mode = True
self._manipulable_invoke(context)
return True
def manipulable_modal(self, context, event):
"""
call in operator modal()
should not be overridden
as it provide all needed
functionality out of the box
"""
# setup again when manipulators type change
if self.manipulable_refresh:
# print("manipulable_refresh")
self.manipulable_refresh = False
self.manipulable_setup(context)
self.manipulate_mode = True
if context.area is None:
self.manipulable_disable(context)
return {'FINISHED'}
context.area.tag_redraw()
if self.keymap is None:
self.keymap = Keymaps(context)
if self.keymap.check(event, self.keymap.undo):
# user feedback on undo by disabling manipulators
self.manipulable_disable(context)
return {'FINISHED'}
# clean up manipulator on delete
if self.keymap.check(event, self.keymap.delete): # {'X'}:
# @TODO:
# for doors and windows, seek and destroy holes object if any
# a dedicated delete method into those objects may be an option ?
# A type check is required any way we choose
#
# Time for a generic archipack's datablock getter / filter into utils
#
# May also be implemented into nearly hidden "reference point"
# to delete / duplicate / link duplicate / unlink of
# a complete set of wall, doors and windows at once
self.manipulable_disable(context)
if bpy.ops.object.delete.poll():
bpy.ops.object.delete('INVOKE_DEFAULT', use_global=False)
return {'FINISHED'}
"""
# handle keyborad for select mode
if self.select_mode:
if event.type in {'A'} and event.value == 'RELEASE':
return {'RUNNING_MODAL'}
"""
for manipulator in self.manip_stack:
# manipulator should return false on left mouse release
# so proper release handler is called
# and return true to call manipulate when required
# print("manipulator:%s" % manipulator)
if manipulator is not None and manipulator.modal(context, event):
self.manipulable_manipulate(context, event, manipulator)
return {'RUNNING_MODAL'}
# print("Manipulable %s %s" % (event.type, event.value))
# Manipulators are not active so check for selection
if event.type == 'LEFTMOUSE':
# either we are starting select mode
# user press on area not over maniuplator
# Prevent 3 mouse emultation to select when alt pressed
if self.manipulable_selectable and event.value == 'PRESS' and not event.alt:
self.select_mode = True
self.manipulable_area.enable()
self.manipulable_start_point = Vector((event.mouse_region_x, event.mouse_region_y))
self.manipulable_area.set_location(
context,
self.manipulable_start_point,
self.manipulable_start_point)
# add a select draw handler
args = (self, context)
self.manipulable_draw_handler = bpy.types.SpaceView3D.draw_handler_add(
self.manipulable_draw_callback,
args,
'WINDOW',
'POST_PIXEL')
# don't keep focus
# as this prevent click over ui
# return {'RUNNING_MODAL'}
elif event.value == 'RELEASE':
if self.select_mode:
# confirm selection
self.manipulable_exit_selectmode(context)
# keep focus
# return {'RUNNING_MODAL'}
else:
# allow manipulator action on release
for manipulator in self.manip_stack:
if manipulator is not None and manipulator.selectable:
manipulator.selected = False
self.manipulable_release(context)
elif self.select_mode and event.type == 'MOUSEMOVE' and event.value == 'PRESS':
# update select area size
self.manipulable_end_point = Vector((event.mouse_region_x, event.mouse_region_y))
self.manipulable_area.set_location(
context,
self.manipulable_start_point,
self.manipulable_end_point)
if event.shift:
# deselect
for i, manipulator in enumerate(self.manip_stack):
if manipulator is not None and manipulator.selectable:
manipulator.deselect(self.manipulable_area)
else:
# select / more
for i, manipulator in enumerate(self.manip_stack):
if manipulator is not None and manipulator.selectable:
manipulator.select(self.manipulable_area)
# keep focus to prevent left select mouse to actually move object
return {'RUNNING_MODAL'}
# event.alt here to prevent 3 button mouse emulation exit while zooming
if event.type in {'RIGHTMOUSE', 'ESC'} and event.value == 'PRESS' and not event.alt:
self.manipulable_disable(context)
self.manipulable_exit(context)
return {'FINISHED'}
return {'PASS_THROUGH'}
# Callbacks
def manipulable_release(self, context):
"""
Override with action to do on mouse release
eg: big update
"""
return
def manipulable_exit(self, context):
"""
Override with action to do when modal exit
"""
return
def manipulable_manipulate(self, context, event, manipulator):
"""
Override with action to do when a handle is active (pressed and mousemove)
"""
return
@persistent
def cleanup(dummy=None):
empty_stack()
def register():
# Register default manipulators
global manips
global manipulators_class_lookup
manipulators_class_lookup = {}
manips = {}
register_manipulator('SIZE', SizeManipulator)
register_manipulator('SIZE_LOC', SizeLocationManipulator)
register_manipulator('ANGLE', AngleManipulator)
register_manipulator('DUMB_ANGLE', DumbAngleManipulator)
register_manipulator('ARC_ANGLE_RADIUS', ArcAngleRadiusManipulator)
register_manipulator('COUNTER', CounterManipulator)
register_manipulator('DUMB_SIZE', DumbSizeManipulator)
register_manipulator('DELTA_LOC', DeltaLocationManipulator)
register_manipulator('DUMB_STRING', DumbStringManipulator)
# snap aware size loc
register_manipulator('SNAP_SIZE_LOC', SnapSizeLocationManipulator)
# register_manipulator('SNAP_POINT', SnapPointManipulator)
# wall's line based object snap
register_manipulator('WALL_SNAP', WallSnapManipulator)
bpy.utils.register_class(ARCHIPACK_OT_manipulate)
bpy.utils.register_class(ARCHIPACK_OT_disable_manipulate)
bpy.utils.register_class(archipack_manipulator)
bpy.app.handlers.load_pre.append(cleanup)
def unregister():
global manips
global manipulators_class_lookup
empty_stack()
del manips
manipulators_class_lookup.clear()
del manipulators_class_lookup
bpy.utils.unregister_class(ARCHIPACK_OT_manipulate)
bpy.utils.unregister_class(ARCHIPACK_OT_disable_manipulate)
bpy.utils.unregister_class(archipack_manipulator)
bpy.app.handlers.load_pre.remove(cleanup)
|