-
Notifications
You must be signed in to change notification settings - Fork 49
/
archipack_manipulator.py
2802 lines (2400 loc) · 102 KB
/
archipack_manipulator.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
# -*- 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 logging
logger = logging.getLogger("manipulator")
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,
CruxHandle, PlusHandle,
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 allready running
# ones, and prevent select mode to start.
#
# TODO:
# Other manipulators should use same technique to take
# precedence over allready 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.
"""
@TODO:
Last modal running wins.
Manipulateurs without snap and thus not running own modal,
may loose events events caught by select mode of last
manipulable enabled
"""
# 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
# @TODO:
# store data path
# and use this stack to manipulate
# manipulable objects must update this stack
# through archipack_manipulator class
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
self.datablock = None
@property
def dirty(self):
"""
Check for manipulable validity
to disable modal when required
"""
return (
self.manipulable is None or
bpy.data.objects.find(self.object_name) < 0
)
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:
# always retrieve fresh datablock instance
# as manipulable might loose proper reference
o = bpy.data.objects.get(self.object_name)
d = self.datablock(o)
if d:
d.manipulate_mode = False
self.manipulable = None
self.datablock = 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 allready 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
# class method for fast datablock access
manips[key].datablock = manipulable.__class__.datablock
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'
# must hold those data here
self.o = o
self.datablock = datablock
self.manipulator = manipulator
self.snap_callback = snap_callback
self.origin = self.o.matrix_world.translation.copy()
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, DONT EVEN TRY TO OVERRIDE
"""
if self._handle is not None:
logger.debug("draw_handler_remove")
bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
# Prevent race condition with redraw and the above call
self._handle = None
logger.debug("draw_handler_remove done")
else:
logger.debug("Manipulator.exit() handle not found %s", (type(self).__name__))
# Mouse event handlers, MUST be overriden
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 overriden
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 realy
# realy realy 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
return boolean
where True means the event was handled here so stack return RUNNING_MODAL
and False means let the event bubble on stack
"""
# 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()
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:
# switch context so unselected object are manipulable too
old = context.active_object
state = self.o.select
self.o.select = True
context.scene.objects.active = self.o
if index > -1:
getattr(data, attr)[index] = value
else:
setattr(data, attr, value)
self.o.select = state
old.select = True
context.scene.objects.active = old
except:
pass
def preTranslate(self, tM, vec):
"""
return a preTranslated Matrix
tM Matrix source
vec Vector translation
"""
return tM * Matrix([
[1, 0, 0, vec.x],
[0, 1, 0, vec.y],
[0, 0, 1, vec.z],
[0, 0, 0, 1]])
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 = True
context.scene.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 = True
context.scene.objects.active = old
def move(self, context, axis, value):
"""
Move an object along local axis
"""
self._move(self.o, axis, value)
# OUT OF ORDER
class SnapPointManipulator(Manipulator):
"""
np_station based snap manipulator
dosent update anything by itself.
NOTE : currently out of order
and disabled in __init__
"""
def __init__(self, context, o, datablock, manipulator, handle_size, snap_callback=None):
raise NotImplementedError
self.handle = SquareHandle(handle_size, 1.2 * arrow_size, draggable=True)
Manipulator.__init__(self, context, o, datablock, manipulator, snap_callback)
def check_hover(self):
self.handle.check_hover(self.mouse_pos)
def mouse_press(self, context, event):
if self.handle.hover:
self.handle.hover = False
self.handle.active = True
self.o.select = True
# takeloc = self.o.matrix_world * self.manipulator.p0
# print("Invoke sp_point_move %s" % (takeloc))
# @TODO:
# implement and add draw and callbacks
# snap_point(takeloc, draw, callback)
return True
return False
def mouse_release(self, context, event):
self.check_hover()
self.handle.active = False
# False to callback manipulable_release
return False
def update(self, context, event):
# NOTE:
# dosent set anything internally
return
def mouse_move(self, context, event):
"""
"""
self.mouse_position(event)
if self.handle.active:
# self.handle.active = np_snap.is_running
# self.update(context)
# True here to callback manipulable_manipulate
return True
else:
self.check_hover()
return False
def draw_callback(self, _self, context, render=False):
logger.debug("SnapPointManipulator.draw_callback")
left, right, side, normal = self.manipulator.get_pts(self.o.matrix_world)
self.handle.set_pos(context, left, Vector((1, 0, 0)), normal=normal)
self.handle.draw(context, render)
# 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 = 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':
self.o.select = True
# apply changes to wall
d = self.datablock
d.auto_update = False
g = d.get_generator()
# rotation relative to object
rM = self.o.matrix_world.inverted().to_3x3()
delta = (rM * sp.delta).to_2d()
# 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
# 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
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.straight(1, 0).angle
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.straight(1, 0).angle
# move object when point 0
self.o.location += sp.delta
self.o.matrix_world.translation += 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)
d.auto_update = True
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.
"""
logger.debug("CounterManipulator.draw_callback")
# 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)
logger.debug("CounterManipulator.draw_callback done")
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.
"""
logger.debug("DumbStringManipulator.draw_callback")
# won't render string