Newer
Older
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
# ##### 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 #####
'''
DESCRIPTION:
Makes a copy (duplicate) of objects using snap points. Emulates the functionality of the standard 'copy' command in CAD applications, with vertex snapping. Optionally, using selected objects and last distance used, makes an array of selected objects.
INSTALLATION:
Unzip and place .py file to scripts / addons_contrib folder. In User Preferences / Addons tab, search with Testing filter - NP Point Copy and check the box.
Now you have the operator in your system. If you press Save User Preferences, you will have it at your disposal every time you run Blender.
SHORTCUTS:
After successful installation of the addon, the NP Point Copy operator should be registered in your system. Enter User Preferences / Input, and under that, 3DView / Object mode. At the bottom of the list click the 'Add new' button. In the operator field type object.np_point_copy_xxx (xxx being the number of the version) and assign a shortcut key of your preference. At the moment i am using 'C' for 'copy', as in standard CAD applications. I rarely use circle selection so letter 'C' is free.
USAGE:
You can run the operator with spacebar search - NP Point Copy, or shortcut key if you assigned it.
Select a point anywhere in the scene (holding CTRL enables snapping). This will be your 'take' point.
Move your mouse and click to a point anywhere in the scene with the left mouse button (LMB), in relation to the 'take' point and the operator will duplicate the selected objects at that position (again CTRL - snap enables snapping to objects around the scene). You can continue duplicating objects in the same way. When you want to finish the process, press ESC or RMB. If you want to make an array of the copied objects in relation to the last pair, press the 'ENTER' button (ENT). The command will automatically read the direction and the distance between the last pair of copied objects and present an interface to specify the number of arrayed copies. You specify the number with CTRL + mouse scroll, with the possibility to go below the amount of 2 which changes the mode of array to division. You confirm the array with RMB / ENTER / TAB key or cancel it with ESC. Pressing RMB at the end will confirm the array and keep it as a modifier in the modifier stack, ENTER will apply the modifier as a single object and remove the modifier, while TAB would apply the modifier as an array of separate individual objects and remove the modifier from the modifier stack.
If at any point you lose sight of the next point you want to snap to, you can press SPACE to go to NAVIGATION mode in which you can change the point of view. When your next point is clearly in your field of view, you return to normal mode by pressing SPACE again or LMB.
Middle mouse button (MMB) enables axis constraint during snapping, while numpad keys enable numerical input for the copy distance.
ADDON SETTINGS:
Below the addon name in the user preferences / addon tab, you can find a couple of settings that control the behavior of the addon:
Unit scale: Distance multiplier for various unit scenarios
Suffix: Unit abbreviation after the numerical distance
Custom colors: Default or custom colors for graphical elements
Mouse badge: Option to display a small cursor label
IMPORTANT PERFORMANCE NOTES:
None so far.
'''
bl_info = {
'name':'NP 020 Point Copy',
'author':'Okavango & the Blenderartists community',
'version': (0, 2, 0),
'blender': (2, 75, 0),
'location': 'View3D',
'warning': '',
'description': 'Duplicates selected objects using "take" and "place" snap points',
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
'category': '3D View'}
import bpy
import copy
import bmesh
import bgl
import blf
import mathutils
from bpy_extras import view3d_utils
from bpy.app.handlers import persistent
from mathutils import Vector, Matrix
from blf import ROTATION
from math import radians
from bpy.props import *
from .utils_geometry import *
from .utils_graphics import *
from .utils_function import *
# Defining the main class - the macro:
class NP020PointCopy(bpy.types.Macro):
bl_idname = 'object.np_020_point_copy'
bl_label = 'NP 020 Point Copy'
bl_options = {'UNDO'}
# Defining the storage class that will serve as a variable bank for exchange among the classes. Later, this bank will receive more variables with their values for safe keeping, as the program goes on:
class NP020PC:
take = None
place = None
takeloc3d = (0.0,0.0,0.0)
placeloc3d = (0.0,0.0,0.0)
dist = None
mode = 'MOVE'
flag = 'NONE'
deltavec = Vector ((0, 0, 0))
deltavec_safe = Vector ((0, 0, 0))
# Defining the scene update algorithm that will track the state of the objects during modal transforms, which is otherwise impossible:
@persistent
def NPPC_scene_update(context):
#np_print('00_SceneUpdate_START')
if bpy.data.objects.is_updated:
np_print('NPPC_update1')
mode = NP020PC.mode
flag = NP020PC.flag
#np_print(mode, flag)
take = NP020PC.take
place = NP020PC.place
if flag in ('RUNTRANSZERO', 'RUNTRANSFIRST','RUNTRANSNEXT', 'NAVTRANSZERO', 'NAVTRANSFIRST', 'NAVTRANSNEXT'):
np_print('NPPC_update2')
NP020PC.takeloc3d = take.location
NP020PC.placeloc3d = place.location
#np_print('up3')
#np_print('00_SceneUpdate_FINISHED')
# Defining the first of the classes from the macro, that will gather the current system settings set by the user. Some of the system settings will be changed during the process, and will be restored when macro has completed.
class NPPCGetContext(bpy.types.Operator):
bl_idname = 'object.np_pc_get_context'
bl_label = 'NP PC Get Context'
bl_options = {'INTERNAL'}
def execute(self, context):
if bpy.context.selected_objects == []:
self.report({'WARNING'}, "Please select objects first")
return {'CANCELLED'}
NP020PC.use_snap = copy.deepcopy(bpy.context.tool_settings.use_snap)
NP020PC.snap_element = copy.deepcopy(bpy.context.tool_settings.snap_element)
NP020PC.snap_target = copy.deepcopy(bpy.context.tool_settings.snap_target)
NP020PC.pivot_point = copy.deepcopy(bpy.context.space_data.pivot_point)
NP020PC.trans_orient = copy.deepcopy(bpy.context.space_data.transform_orientation)
NP020PC.curloc = copy.deepcopy(bpy.context.scene.cursor.location)
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
NP020PC.acob = bpy.context.active_object
if bpy.context.mode == 'OBJECT':
NP020PC.edit_mode = 'OBJECT'
elif bpy.context.mode in ('EDIT_MESH', 'EDIT_CURVE', 'EDIT_SURFACE', 'EDIT_TEXT', 'EDIT_ARMATURE', 'EDIT_METABALL', 'EDIT_LATTICE'):
NP020PC.edit_mode = 'EDIT'
elif bpy.context.mode == 'POSE':
NP020PC.edit_mode = 'POSE'
elif bpy.context.mode == 'SCULPT':
NP020PC.edit_mode = 'SCULPT'
elif bpy.context.mode == 'PAINT_WEIGHT':
NP020PC.edit_mode = 'WEIGHT_PAINT'
elif bpy.context.mode == 'PAINT_TEXTURE':
NP020PC.edit_mode = 'TEXTURE_PAINT'
elif bpy.context.mode == 'PAINT_VERTEX':
NP020PC.edit_mode = 'VERTEX_PAINT'
elif bpy.context.mode == 'PARTICLE':
NP020PC.edit_mode = 'PARTICLE_EDIT'
return {'FINISHED'}
# Changing to OBJECT mode which will be the context for the procedure:
if bpy.context.mode not in ('OBJECT'):
bpy.ops.object.mode_set(mode = 'OBJECT')
# De-selecting objects in prepare for other processes in the script:
bpy.ops.object.select_all(action = 'DESELECT')
np_print('01_ReadContext_FINISHED', ';', 'flag = ', NP020PC.flag)
return {'FINISHED'}
# Defining the operator for aquiring the list of selected objects and storing them for later re-calls:
class NPPCGetSelection(bpy.types.Operator):
bl_idname = 'object.np_pc_get_selection'
bl_label = 'NP PC Get Selection'
bl_options = {'INTERNAL'}
def execute(self, context):
# Reading and storing the selection:
NP020PC.selob = bpy.context.selected_objects
return {'FINISHED'}
# Defining the operator that will read the mouse position in 3D when the command is activated and store it as a location for placing the 'take' and 'place' points under the mouse:
class NPPCGetMouseloc(bpy.types.Operator):
bl_idname = 'object.np_pc_get_mouseloc'
bl_label = 'NP PC Get Mouseloc'
bl_options = {'INTERNAL'}
def modal(self, context, event):
region = context.region
rv3d = context.region_data
co2d = ((event.mouse_region_x, event.mouse_region_y))
view_vector = view3d_utils.region_2d_to_vector_3d(region, rv3d, co2d)
enterloc = view3d_utils.region_2d_to_origin_3d(region, rv3d, co2d) + view_vector/5
NP020PC.enterloc = copy.deepcopy(enterloc)
#np_print('02_RadMouseloc_FINISHED', ';', 'flag = ', NP020PC.flag)
return{'FINISHED'}
def invoke(self,context,event):
args = (self,context)
context.window_manager.modal_handler_add(self)
#np_print('02_ReadMouseloc_INVOKED_FINISHED', ';', 'flag = ', NP020PC.flag)
return {'RUNNING_MODAL'}
# Defining the operator that will generate 'take' and 'place' points at the spot marked by mouse, preparing for translation:
class NPPCAddHelpers(bpy.types.Operator):
bl_idname = 'object.np_pc_add_helpers'
bl_label = 'NP PC Add Helpers'
bl_options = {'INTERNAL'}
def execute(self, context):
np_print('03_AddHelpers_START', ';', 'flag = ', NP020PC.flag)
enterloc = NP020PC.enterloc
bpy.ops.object.add(type = 'MESH',location = enterloc)
take = bpy.context.active_object
take.name = 'NP_PC_take'
NP020PC.take = take
bpy.ops.object.add(type = 'MESH',location = enterloc)
place = bpy.context.active_object
place.name = 'NP_PC_place'
NP020PC.place = place
return{'FINISHED'}
# Defining the operator that will change some of the system settings and prepare objects for the operation:
class NPPCPrepareContext(bpy.types.Operator):
bl_idname = 'object.np_pc_prepare_context'
bl_label = 'NP PC Prepare Context'
bl_options = {'INTERNAL'}
def execute(self, context):
take = NP020PC.take
place = NP020PC.place
take.select_set(True)
place.select_set(True)
bpy.context.view_layer.objects.active = place
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
bpy.context.tool_settings.use_snap = False
bpy.context.tool_settings.snap_element = 'VERTEX'
bpy.context.tool_settings.snap_target = 'ACTIVE'
bpy.context.space_data.pivot_point = 'ACTIVE_ELEMENT'
bpy.context.space_data.transform_orientation = 'GLOBAL'
NP020PC.flag = 'RUNTRANSZERO'
return{'FINISHED'}
# Defining the operator that will let the user translate take and place points to the desired 'take' location. It also uses some listening operators that clean up the leftovers should the user interrupt the command. Many thanks to CoDEmanX and lukas_t:
class NPPCRunTranslate(bpy.types.Operator):
bl_idname = 'object.np_pc_run_translate'
bl_label = 'NP PC Run Translate'
bl_options = {'INTERNAL'}
#np_print('04_RunTrans_START',';','NP020PC.flag = ', NP020PC.flag)
count = 0
def modal(self,context,event):
context.area.tag_redraw()
flag = NP020PC.flag
take = NP020PC.take
place = NP020PC.place
selob = NP020PC.selob
self.count += 1
if self.count == 1:
bpy.ops.transform.translate('INVOKE_DEFAULT')
np_print('04_RunTrans_count_1_INVOKE_DEFAULT', ';', 'flag = ', NP020PC.flag)
elif event.type in ('LEFTMOUSE','RET','NUMPAD_ENTER') and event.value == 'RELEASE':
bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
if flag == 'RUNTRANSZERO':
take.select_set(False)
place.select_set(False)
NP020PC.firsttake3d = copy.deepcopy(take.location)
for ob in selob:
bpy.ops.object.duplicate()
NP020PC.nextob = bpy.context.selected_objects
NP020PC.prevob = selob
NP020PC.flag = 'RUNTRANSFIRST_break'
elif flag == 'RUNTRANSFIRST':
NP020PC.deltavec_safe = copy.deepcopy(NP020PC.deltavec)
np_print('deltavec_safe = ', NP020PC.deltavec_safe)
NP020PC.ar13d = copy.deepcopy(take.location)
NP020PC.ar23d = copy.deepcopy(place.location)
bpy.ops.object.duplicate()
prevob = NP020PC.prevob
nextob = NP020PC.nextob
NP020PC.arob = prevob
NP020PC.prevob = nextob
NP020PC.nextob = bpy.context.selected_objects
NP020PC.selob = nextob
take.location = copy.deepcopy(place.location)
NP020PC.flag = 'RUNTRANSNEXT_break'
elif flag == 'RUNTRANSNEXT':
NP020PC.deltavec_safe = copy.deepcopy(NP020PC.deltavec)
np_print('deltavec_safe = ', NP020PC.deltavec_safe)
NP020PC.ar13d = copy.deepcopy(take.location)
NP020PC.ar23d = copy.deepcopy(place.location)
bpy.ops.object.duplicate()
prevob = NP020PC.prevob
nextob = NP020PC.nextob
NP020PC.arob = prevob
NP020PC.prevob = nextob
NP020PC.nextob = bpy.context.selected_objects
NP020PC.selob = nextob
take.location = copy.deepcopy(place.location)
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
NP020PC.flag = 'RUNTRANSNEXT_break'
else:
np_print('UNKNOWN FLAG')
NP020PC.flag = 'EXIT'
np_print('04_RunTrans_left_FINISHED',';','flag = ', NP020PC.flag)
return{'FINISHED'}
elif event.type == 'SPACE' and event.value == 'RELEASE':
bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
take.hide = True
place.hide = True
self.co2d = ((event.mouse_region_x, event.mouse_region_y))
co2d = self.co2d
region = context.region
rv3d = context.region_data
away = view3d_utils.region_2d_to_origin_3d(region, rv3d, co2d) - place.location
away = away.length
placeloc3d = NP020PC.placeloc3d
awayloc = copy.deepcopy(placeloc3d)
NP020PC.awayloc = awayloc
NP020PC.away = copy.deepcopy(away)
if flag == 'RUNTRANSZERO':
NP020PC.flag = 'NAVTRANSZERO'
elif flag == 'RUNTRANSFIRST':
nextob = NP020PC.nextob
for ob in nextob:
ob.hide = True
NP020PC.flag = 'NAVTRANSFIRST'
elif flag == 'RUNTRANSNEXT':
nextob = NP020PC.nextob
for ob in nextob:
ob.hide = True
else:
np_print('UNKNOWN FLAG')
NP020PC.flag = 'EXIT'
np_print('04_RunTrans_space_FINISHED',';','flag = ', NP020PC.flag)
return{'FINISHED'}
elif event.type == 'RIGHTMOUSE':
bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
if flag == 'RUNTRANSZERO':
NP020PC.flag = 'EXIT'
elif flag == 'RUNTRANSFIRST':
prevob = NP020PC.prevob
nextob = NP020PC.nextob
bpy.ops.object.delete('EXEC_DEFAULT')
for ob in nextob:
NP020PC.selob = prevob
NP020PC.flag = 'EXIT'
elif flag == 'RUNTRANSNEXT':
prevob = NP020PC.prevob
nextob = NP020PC.nextob
bpy.ops.object.delete('EXEC_DEFAULT')
for ob in nextob:
NP020PC.selob = prevob
NP020PC.flag = 'EXIT'
else:
np_print('UNKNOWN FLAG')
NP020PC.flag = 'EXIT'
np_print('04_RunTrans_rmb_FINISHED',';','flag = ', NP020PC.flag)
return{'FINISHED'}
elif event.type == 'ESC':
bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
if flag == 'RUNTRANSZERO':
NP020PC.flag = 'EXIT'
elif flag == 'RUNTRANSFIRST':
prevob = NP020PC.prevob
nextob = NP020PC.nextob
bpy.ops.object.delete('EXEC_DEFAULT')
for ob in prevob:
NP020PC.flag = 'EXIT'
elif flag == 'RUNTRANSNEXT':
prevob = NP020PC.prevob
nextob = NP020PC.nextob
NP020PC.selob = prevob
bpy.ops.object.delete('EXEC_DEFAULT')
for ob in prevob:
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
NP020PC.flag = 'EXIT'
else:
np_print('UNKNOWN FLAG')
NP020PC.flag = 'EXIT'
np_print('04_RunTrans_rmb_FINISHED',';','flag = ', NP020PC.flag)
return{'FINISHED'}
np_print('04_RunTrans_count_PASS_THROUGH',';','flag = ', NP020PC.flag)
return{'PASS_THROUGH'}
def invoke(self, context, event):
#np_print('04_RunTrans_INVOKE_START')
flag = NP020PC.flag
selob = NP020PC.selob
#np_print('flag = ', flag)
if context.area.type == 'VIEW_3D':
if flag in ('RUNTRANSZERO', 'RUNTRANSFIRST', 'RUNTRANSNEXT'):
args = (self, context)
self._handle = bpy.types.SpaceView3D.draw_handler_add(DRAW_RunTranslate, args, 'WINDOW', 'POST_PIXEL')
context.window_manager.modal_handler_add(self)
np_print('04_RunTrans_INVOKED_RUNNING_MODAL',';','flag = ', NP020PC.flag)
return {'RUNNING_MODAL'}
else:
#np_print('04_RunTrans_INVOKE_DECLINED_FINISHED',';','flag = ', flag)
return {'FINISHED'}
else:
self.report({'WARNING'}, "View3D not found, cannot run operator")
flag = 'WARNING3D'
NP020PC.flag = flag
np_print('04_RunTrans_INVOKE_DECLINED_FINISHED',';','flag = ', NP020PC.flag)
# Defining the set of instructions that will draw the OpenGL elements on the screen during the execution of RunTranslate operator:
def DRAW_RunTranslate(self, context):
np_print('04_DRAW_RunTrans_START',';','flag = ', NP020PC.flag)
addon_prefs = context.preferences.addons[__package__].preferences
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
flag = NP020PC.flag
takeloc3d = NP020PC.takeloc3d
placeloc3d = NP020PC.placeloc3d
region = context.region
rv3d = context.region_data
if flag in ('RUNTRANSZERO', 'RUNTRANSFIRST', 'RUNTRANSNEXT'):
takeloc2d = view3d_utils.location_3d_to_region_2d(region, rv3d, takeloc3d)
placeloc2d = view3d_utils.location_3d_to_region_2d(region, rv3d, placeloc3d)
if flag == 'NAVTRANSZERO':
takeloc2d = self.co2d
placeloc2d = self.co2d
if flag in ('NAVTRANSFIRST', 'NAVTRANSNEXT'):
takeloc2d = view3d_utils.location_3d_to_region_2d(region, rv3d, takeloc3d)
placeloc2d = self.co2d
'''
if flag in ('RUNTRANSNEXT', 'NAVTRANSNEXT'):
ardist_num = NP020PC.ar23d - NP020PC.ar13d
ardist_num = ardist_num.length * dist_scale
ar12d = view3d_utils.location_3d_to_region_2d(region, rv3d, NP020PC.ar13d)
ar22d = view3d_utils.location_3d_to_region_2d(region, rv3d, NP020PC.ar23d)
ardist_num = abs(round(ardist_num,2))
if suffix is not None:
ardist = str(ardist_num)+suffix
else:
ardist = str(ardist_num)
NP020PC.ardist = ardist
ardist_loc = (ar12d + ar22d) /2
'''
# DRAWING START:
bgl.glEnable(bgl.GL_BLEND)
if flag == 'RUNTRANSZERO':
instruct = 'select the take point'
keys_aff = 'LMB - confirm, CTRL - snap, MMB - lock axis, NUMPAD - value'
keys_nav = 'SPACE - navigate'
keys_neg = 'ESC / RMB - cancel copy'
badge_mode = 'RUN'
message_main = 'CTRL+SNAP'
message_aux = None
aux_num = None
aux_str = None
elif flag == 'RUNTRANSFIRST':
instruct = 'select the placement point'
keys_aff = 'LMB - confirm, CTRL - snap, MMB - lock axis, NUMPAD - value'
keys_nav = 'SPACE - navigate'
keys_neg = 'ESC / RMB - cancel copy'
badge_mode = 'RUN'
message_main = 'CTRL+SNAP'
message_aux = None
aux_num = None
aux_str = None
elif flag == 'RUNTRANSNEXT':
instruct = 'select the placement point'
keys_aff = 'LMB - confirm, CTRL - snap, MMB - lock axis, NUMPAD - value'
keys_nav = 'SPACE - navigate'
keys_neg = 'ESC / RMB - cancel current'
badge_mode = 'RUN'
message_main = 'CTRL+SNAP'
message_aux = None
aux_num = None
aux_str = None
elif flag == 'NAVTRANSZERO':
instruct = 'navigate for better placement of take point'
keys_aff = 'MMB / SCROLL - navigate'
keys_nav = 'LMB / SPACE - leave navigate'
keys_neg = 'ESC / RMB - cancel copy'
badge_mode = 'NAV'
message_main = 'NAVIGATE'
message_aux = None
aux_num = None
aux_str = None
elif flag == 'NAVTRANSFIRST':
instruct = 'navigate for better selection of placement point'
keys_aff = 'MMB / SCROLL - navigate'
keys_nav = 'LMB / SPACE - leave navigate'
keys_neg = 'ESC / RMB - cancel copy'
badge_mode = 'NAV'
message_main = 'NAVIGATE'
message_aux = None
aux_num = None
aux_str = None
elif flag == 'NAVTRANSNEXT':
instruct = 'navigate for better selection of placement point'
keys_aff = 'MMB / SCROLL - navigate'
keys_nav = 'LMB / SPACE - leave navigate'
keys_neg = 'ESC / RMB - cancel current'
badge_mode = 'NAV'
message_main = 'NAVIGATE'
message_aux = None
aux_num = None
aux_str = None
# ON-SCREEN INSTRUCTIONS:
display_instructions(region, rv3d, instruct, keys_aff, keys_nav, keys_neg)
# MOUSE BADGE:
co2d = placeloc2d
symbol = [[23, 34], [23, 32], [19, 32], [19, 36], [21, 36], [21, 38], [25, 38], [25, 34], [23, 34], [23, 36], [21, 36]]
display_cursor_badge(co2d, symbol, badge_mode, message_main, message_aux, aux_num, aux_str)
# LINE:
display_line_between_two_points(region, rv3d, takeloc3d, placeloc3d)
# DISTANCE:
display_distance_between_two_points(region, rv3d, takeloc3d, placeloc3d)
NP020PC.deltavec = copy.deepcopy(display_distance_between_two_points(region, rv3d, takeloc3d, placeloc3d)[0])
#DRAWING END:
bgl.glLineWidth(1)
bgl.glDisable(bgl.GL_BLEND)
bgl.glColor4f(0.0, 0.0, 0.0, 1.0)
np_print('04_DRAW_RunTrans_FINISHED',';','flag = ', NP020PC.flag)
# Defining the operator that will enable navigation if user calls it:
class NPPCNavTranslate(bpy.types.Operator):
bl_idname = "object.np_pc_nav_translate"
bl_label = "NP PC Nav Translate"
bl_options = {'INTERNAL'}
np_print('04a_NavTrans_START',';','flag = ', NP020PC.flag)
def modal(self,context,event):
context.area.tag_redraw()
flag = NP020PC.flag
take = NP020PC.take
place = NP020PC.place
if event.type == 'MOUSEMOVE':
self.co2d = ((event.mouse_region_x, event.mouse_region_y))
region = context.region
rv3d = context.region_data
co2d = self.co2d
view_vector = view3d_utils.region_2d_to_vector_3d(region, rv3d, co2d)
pointloc = view3d_utils.region_2d_to_origin_3d(region, rv3d, co2d) + view_vector * NP020PC.away
NP020PC.placeloc3d = copy.deepcopy(pointloc)
np_print('04a_NavTrans_mousemove',';','flag = ', NP020PC.flag)
elif event.type in {'LEFTMOUSE', 'SPACE'} and event.value == 'PRESS':
bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
self.co2d = ((event.mouse_region_x, event.mouse_region_y))
region = context.region
view_vector = view3d_utils.region_2d_to_vector_3d(region, rv3d, co2d)
enterloc = view3d_utils.region_2d_to_origin_3d(region, rv3d, co2d) + view_vector*NP020PC.away
placeloc3d = NP020PC.placeloc3d
navdelta = enterloc - NP020PC.awayloc
take.hide = False
place.hide = False
np_print('flag = ', flag)
if flag == 'NAVTRANSZERO':
takeloc3d = enterloc
placeloc3d = enterloc
take.location = enterloc
place.location = enterloc
NP020PC.flag = 'RUNTRANSZERO'
elif flag == 'NAVTRANSFIRST':
takeloc3d = NP020PC.takeloc3d
placeloc3d = enterloc
place.location = enterloc
nextob = NP020PC.nextob
for ob in nextob:
ob.hide = False
bpy.ops.transform.translate(value = navdelta)
NP020PC.flag = 'RUNTRANSFIRST'
elif flag == 'NAVTRANSNEXT':
takeloc3d = NP020PC.takeloc3d
placeloc3d = enterloc
place.location = enterloc
nextob = NP020PC.nextob
for ob in nextob:
ob.hide = False
bpy.ops.transform.translate(value = navdelta)
NP020PC.flag = 'RUNTRANSNEXT'
else:
np_print('UNKNOWN FLAG')
NP020PC.flag = 'EXIT'
NP020PC.take = take
NP020PC.place = place
NP020PC.takeloc3d = takeloc3d
NP020PC.placeloc3d = placeloc3d
np_print('04a_NavTrans_left_space_FINISHED',';','flag = ', NP020PC.flag)
return {'FINISHED'}
elif event.type == 'RIGHTMOUSE':
bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
take.hide = False
place.hide = False
if flag == 'NAVTRANSZERO':
NP020PC.flag = 'EXIT'
elif flag == 'NAVTRANSFIRST':
prevob = NP020PC.prevob
nextob = NP020PC.nextob
for ob in nextob:
ob.hide = False
bpy.ops.object.delete('EXEC_DEFAULT')
for ob in prevob:
NP020PC.flag = 'EXIT'
elif flag == 'NAVTRANSNEXT':
prevob = NP020PC.prevob
nextob = NP020PC.nextob
for ob in nextob:
ob.hide = False
bpy.ops.object.delete('EXEC_DEFAULT')
for ob in prevob:
bpy.ops.object.delete('EXEC_DEFAULT')
NP020PC.flag = 'ARRAYTRANS'
else:
np_print('UNKNOWN FLAG')
NP020PC.flag = 'EXIT'
np_print('04a_NavTrans_rmb_FINISHED',';','flag = ', NP020PC.flag)
return{'FINISHED'}
elif event.type == 'ESC':
bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
take.hide = False
place.hide = False
if flag == 'NAVTRANSZERO':
NP020PC.flag = 'EXIT'
elif flag == 'NAVTRANSFIRST':
prevob = NP020PC.prevob
nextob = NP020PC.nextob
for ob in nextob:
ob.hide = False
bpy.ops.object.delete('EXEC_DEFAULT')
for ob in prevob:
NP020PC.flag = 'EXIT'
elif flag == 'NAVTRANSNEXT':
prevob = NP020PC.prevob
nextob = NP020PC.nextob
NP020PC.selob = prevob
for ob in nextob:
ob.hide = False
bpy.ops.object.delete('EXEC_DEFAULT')
for ob in prevob:
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
NP020PC.flag = 'EXIT'
else:
np_print('UNKNOWN FLAG')
NP020PC.flag = 'EXIT'
np_print('04a_NavTrans_esc_FINISHED',';','flag = ', NP020PC.flag)
return{'FINISHED'}
elif event.type in {'MIDDLEMOUSE', 'WHEELUPMOUSE', 'WHEELDOWNMOUSE'}:
np_print('04a_NavTrans_middle_wheel_any_PASS_THROUGH')
return {'PASS_THROUGH'}
np_print('04a_NavTrans_INVOKED_RUNNING_MODAL',';','flag = ', NP020PC.flag)
return {'RUNNING_MODAL'}
def invoke(self, context, event):
#np_print('04a_NavTrans_INVOKE_START')
flag = NP020PC.flag
#np_print('flag = ', flag)
self.co2d = ((event.mouse_region_x, event.mouse_region_y))
if flag in ('NAVTRANSZERO', 'NAVTRANSFIRST', 'NAVTRANSNEXT'):
args = (self, context)
self._handle = bpy.types.SpaceView3D.draw_handler_add(DRAW_RunTranslate, args, 'WINDOW', 'POST_PIXEL')
context.window_manager.modal_handler_add(self)
np_print('04a_run_NAV_INVOKE_a_RUNNING_MODAL',';','flag = ', NP020PC.flag)
return {'RUNNING_MODAL'}
else:
#np_print('04a_run_NAV_INVOKE_a_FINISHED',';','flag = ', flag)
return {'FINISHED'}
# Defining the operator that will enable the return to RunTrans cycle by reseting the 'break' flag:
class NPPCPrepareNext(bpy.types.Operator):
bl_idname = 'object.np_pc_prepare_next'
bl_label = 'NP PC Prepare Next'
bl_options = {'INTERNAL'}
def execute(self, context):
np_print('05_PrepareNext_START',';','flag = ', NP020PC.flag)
if NP020PC.flag == 'RUNTRANSFIRST_break':
NP020PC.flag = 'RUNTRANSFIRST'
if NP020PC.flag == 'RUNTRANSNEXT_break':
NP020PC.flag = 'RUNTRANSNEXT'
np_print('05_PrepareNext_FINISHED',';','flag = ', NP020PC.flag)
return{'FINISHED'}
'''
# Defining the operator that will collect the necessary data and the generate the array with an input dialogue for number of items:
class NPPCArrayTranslate(bpy.types.Operator):
bl_idname = "object.np_pc_array_translate"
bl_label = "NP PC Array Translate"
bl_options = {'INTERNAL'}
np_print('06_ArrayTrans_START',';','flag = ', NP020PC.flag)
def modal(self,context,event):
np_print('06_ArrayTrans_START',';','flag = ', NP020PC.flag)
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
context.area.tag_redraw()
flag = NP020PC.flag
ardict = NP020PC.ardict
arob = NP020PC.arob
np_print('ardict = ', ardict)
if event.type == 'MOUSEMOVE':
self.co2d = ((event.mouse_region_x, event.mouse_region_y))
np_print('04a_NavTrans_mousemove',';','flag = ', NP020PC.flag)
elif event.type in ('LEFTMOUSE', 'RIGHTMOUSE') and event.value == 'PRESS':
bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
NP020PC.flag = 'EXIT'
np_print('06_ArrayTrans_rmb_FINISHED',';','flag = ', NP020PC.flag)
return{'FINISHED'}
elif event.ctrl and event.type == 'WHEELUPMOUSE' or event.type == 'UP_ARROW' and event.value == 'PRESS':
for ob in arob:
ar = ardict[ob][0]
deltavec_start = Vector(ardict[ob][1])
count = ardict[ob][2]
if ar.fit_type == 'FIXED_COUNT':
ar.count = ar.count+1
count = count + 1
elif ar.fit_type == 'FIT_LENGTH' and count == 3:
ar.fit_type = 'FIXED_COUNT'
ar.constant_offset_displace = deltavec_start
ar.count = 2
count = 2
elif ar.fit_type == 'FIT_LENGTH' and count >3:
count = count - 1
ar.constant_offset_displace.length = ar.fit_length/(count-1)
ardict[ob][2] = count
NP020PC.fit_type = ar.fit_type
NP020PC.count = count
elif event.ctrl and event.type == 'WHEELDOWNMOUSE' or event.type == 'DOWN_ARROW' and event.value == 'PRESS':
for ob in arob:
ar = ardict[ob][0]
deltavec_start = Vector(ardict[ob][1])
count = ardict[ob][2]
if ar.fit_type == 'FIXED_COUNT' and count > 2:
ar.count = ar.count-1
count = count - 1
elif ar.fit_type == 'FIXED_COUNT' and count == 2:
ar.fit_type = 'FIT_LENGTH'
ar.fit_length = deltavec_start.length
ar.constant_offset_displace.length = ar.fit_length/2
count = 3
elif ar.fit_type == 'FIT_LENGTH':
count = count + 1
ar.constant_offset_displace.length = ar.fit_length/(count-1)
ardict[ob][2] = count
NP020PC.fit_type = ar.fit_type
NP020PC.count = count
elif event.type in ('RET', 'NUMPAD_ENTER') and event.value == 'PRESS':
bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
selob = bpy.context.selected_objects
bpy.ops.object.select_all(action='DESELECT')
for ob in arob:
ob.select = True
bpy.ops.object.modifier_apply(modifier = ardict[ob][0].name)
ob.select = False
for ob in selob:
ob.select = True
NP020PC.flag = 'EXIT'
np_print('06_ArrayTrans_enter_FINISHED',';','flag = ', NP020PC.flag)
return{'FINISHED'}
elif event.ctrl and event.type == 'TAB' and event.value == 'PRESS':
bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
if NP020PC.fit_type == 'FIXED_COUNT':
value = NP020PC.ar23d - NP020PC.ar13d
else:
value = (NP020PC.ar23d - NP020PC.ar13d)/(NP020PC.count - 1)
selob = bpy.context.selected_objects
bpy.ops.object.select_all(action='DESELECT')
for ob in arob:
ob.select = True
ob.modifiers.remove(ardict[ob][0])
np_print('NP020PC.count', NP020PC.count)
for i in range(1, NP020PC.count):
bpy.ops.object.duplicate(linked = True)
bpy.ops.transform.translate(value = value)
bpy.ops.object.select_all(action='DESELECT')
for ob in selob:
ob.select = True
NP020PC.flag = 'EXIT'
np_print('06_ArrayTrans_ctrl_tab_FINISHED',';','flag = ', NP020PC.flag)
return{'FINISHED'}
elif event.type == 'TAB' and event.value == 'PRESS':
bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
if NP020PC.fit_type == 'FIXED_COUNT':
value = NP020PC.ar23d - NP020PC.ar13d
else:
value = (NP020PC.ar23d - NP020PC.ar13d)/(NP020PC.count - 1)
selob = bpy.context.selected_objects
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
bpy.ops.object.select_all(action='DESELECT')
for ob in arob:
ob.select = True
ob.modifiers.remove(ardict[ob][0])
np_print('NP020PC.count', NP020PC.count)
for i in range(1, NP020PC.count):
bpy.ops.object.duplicate()
bpy.ops.transform.translate(value = value)
bpy.ops.object.select_all(action='DESELECT')
for ob in selob:
ob.select = True
NP020PC.flag = 'EXIT'
np_print('06_ArrayTrans_tab_FINISHED',';','flag = ', NP020PC.flag)
return{'FINISHED'}
elif event.type == 'ESC' and event.value == 'PRESS':
bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
for ob in arob:
ob.modifiers.remove(ardict[ob][0])
NP020PC.flag = 'EXIT'
np_print('06_ArrayTrans_esc_FINISHED',';','flag = ', NP020PC.flag)
return{'FINISHED'}
elif event.type in {'MIDDLEMOUSE', 'WHEELUPMOUSE', 'WHEELDOWNMOUSE'}:
np_print('06_ArrayTrans_middle_wheel_any_PASS_THROUGH')
return {'PASS_THROUGH'}
np_print('06_ArrayTrans_INVOKED_RUNNING_MODAL',';','flag = ', NP020PC.flag)
def invoke(self, context, event):
np_print('06_ArrayTrans_INVOKE_START')
flag = NP020PC.flag
self.co2d = ((event.mouse_region_x, event.mouse_region_y))
if flag == 'ARRAYTRANS':
arob = NP020PC.arob
np_print('deltavec_safe = ', NP020PC.deltavec_safe)
ardict = {}
for ob in arob:
deltavec = copy.deepcopy(NP020PC.deltavec_safe)
np_print('deltavec = ', deltavec)
loc, rot, sca = ob.matrix_world.decompose()
rot = ob.rotation_euler
rot = rot.to_quaternion()
sca = ob.scale
np_print(loc, rot, sca, ob.matrix_world)
np_print('deltavec = ', deltavec)
deltavec.rotate(rot.conjugated())
np_print('sca.length', sca.length)
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
deltavec[0] = deltavec[0] / sca[0]
deltavec[1] = deltavec[1] / sca[1]
deltavec[2] = deltavec[2] / sca[2]
np_print('deltavec = ', deltavec)
deltavec_trans = deltavec.to_tuple(4)
arcur = ob.modifiers.new(name = '', type = 'ARRAY')
arcur.fit_type = 'FIXED_COUNT'
arcur.use_relative_offset = False
arcur.use_constant_offset = True
arcur.constant_offset_displace = deltavec_trans
arcur.count = 5
ardict[ob] = []
ardict[ob].append(arcur)
ardict[ob].append(deltavec_trans)
ardict[ob].append(arcur.count)
NP020PC.selob = arob
NP020PC.ardict = ardict
NP020PC.count = 5
NP020PC.fit_type = 'FIXED_COUNT'
selob = NP020PC.selob
lenselob = len(selob)
for i, ob in enumerate(selob):
ob.select = True
if i == lenselob-1:
bpy.context.scene.objects.active = ob
args = (self, context)
self._handle = bpy.types.SpaceView3D.draw_handler_add(DRAW_ArrayTrans, args, 'WINDOW', 'POST_PIXEL')
context.window_manager.modal_handler_add(self)
np_print('06_ArayTrans_INVOKE_a_RUNNING_MODAL',';','flag = ', NP020PC.flag)
return {'RUNNING_MODAL'}
else:
np_print('06_ArrayTrans_INVOKE_DENIED',';','flag = ', NP020PC.flag)
'''
'''
# Defining the set of instructions that will draw the OpenGL elements on the screen during the execution of ArrayTrans operator:
def DRAW_ArrayTrans(self, context):
np_print('06a_DRAW_ArrayTrans_START',';','flag = ', NP020PC.flag)
addon_prefs = context.preferences.addons[__package__].preferences
badge = addon_prefs.nppc_badge
badge_size = addon_prefs.nppc_badge_size
# DRAWING START:
bgl.glEnable(bgl.GL_BLEND)
# MOUSE BADGE:
if badge == True:
square = [[17, 30], [17, 40], [27, 40], [27, 30]]
rectangle = [[27, 30], [27, 40], [67, 40], [67, 30]]
icon = copy.deepcopy(NP020PC.icon)
np_print('icon', icon)
ipx = 29
ipy = 33
for co in square:
co[0] = round((co[0] * badge_size),0) -(badge_size*10) + self.co2d[0]
co[1] = round((co[1] * badge_size),0) -(badge_size*25) + self.co2d[1]
for co in rectangle:
co[0] = round((co[0] * badge_size),0) -(badge_size*10) + self.co2d[0]
co[1] = round((co[1] * badge_size),0) -(badge_size*25) + self.co2d[1]
for co in icon: