-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathobject_transform.py
465 lines (371 loc) · 15.7 KB
/
object_transform.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
import bpy
from mathutils import Vector
from .object_ops import SwitchObjectMode, Find3DViewContext
from .select import FocusObject, SelectObject, ActivateObject
def MoveAllFailsafe(context, move_target, destination):
"""
Moves every object in the scene safely.
BLENDER 2.8 - This also uses a region to ensure that it moves in a 3D View region that hasnt been deallocated.
"""
# ///////////////////////////////
# WARNING - WARNING - WARNING - WARNING - WARNING - WARNING
# The context override is known to crash only when testing.
# Under normal circumstances this function is completely safe.
# ///////////////////////////////
# This doesnt need the cursor, and will ensure nothing is animated
# in the process
location = [0.0, 0.0, 0.0]
location[0] = destination[0]
location[1] = destination[1]
location[2] = destination[2]
# Prevent auto keyframing and location lock from being active
auto_key = context.scene.tool_settings.use_keyframe_insert_auto
lock_location_record = move_target.lock_location
context.scene.tool_settings.use_keyframe_insert_auto = False
move_target.lock_location = (False, False, False)
# Save the current cursor location
cursor_loc = bpy.data.scenes[context.scene.name].cursor.location
previous_cursor_loc = [cursor_loc[0], cursor_loc[1], cursor_loc[2]]
override = Find3DViewContext()
with context.temp_override(area = override['area']):
# Calculate the translation vector using the 3D cursor
bpy.ops.object.select_all(action = 'DESELECT')
FocusObject(move_target)
bpy.ops.view3d.snap_cursor_to_selected()
root_location = (0.0, 0.0, 0.0)
root_location = context.scene.cursor.location
# Calculate the movement difference
location_diff = Vector((0.0, 0.0, 0.0))
location_diff.x = (location[0] - root_location[0])
location_diff.y = (location[1] - root_location[1])
location_diff.z = (location[2] - root_location[2])
bpy.ops.object.select_all(action= 'SELECT')
ActivateObject(move_target)
previous_mode = bpy.context.active_object.mode
# This form of translation is required in order to avoid any issues related to parent/child
# and constraints when moving objects individually.
bpy.ops.transform.translate(
value = location_diff,
constraint_axis = (False, False, False),
orient_type = 'GLOBAL',
mirror = False,
use_proportional_edit = False,
snap = False,
release_confirm = False,
)
bpy.ops.object.mode_set(mode = previous_mode)
# Position the cursor back to it's original location
bpy.data.scenes[bpy.context.scene.name].cursor.location = previous_cursor_loc
# Restore the previous setting
context.scene.tool_settings.use_keyframe_insert_auto = auto_key
move_target.lock_location = lock_location_record
print('Move finished.')
def MoveObjectFailsafe(target, context, location):
"""
Safely moves the given object to the given location. Will ensure nothing is animated or screwed up in the process.
WORKAROUND FOR BLENDER 2.8 CRASH FUNTIMES.
"""
# This doesnt need the cursor, and will ensure nothing is animated
# in the process
#print(">>>>>> Moving Object <<<<<<")
override = Find3DViewContext()
with context.temp_override(window = override['window'], area = override['area'],
region = override['region']):
copy_location = Vector((location[0], location[1], location[2]))
# Prevent auto keyframing from being active
auto_key = context.scene.tool_settings.use_keyframe_insert_auto
lock_transform = target.lock_location
context.scene.tool_settings.use_keyframe_insert_auto = False
target.lock_location = (False, False, False)
# Save the current cursor location
cursor_loc = bpy.data.scenes[bpy.context.scene.name].cursor.location
previous_cursor_loc = [cursor_loc[0], cursor_loc[1], cursor_loc[2]]
# This line is actually super-important, not sure why though...
# FocusObject should fill the role of deselection...
bpy.ops.object.select_all(action= 'DESELECT')
# Calculate the translation vector using the 3D cursor
FocusObject(target)
bpy.ops.view3d.snap_cursor_to_selected()
translation_loc = Vector((0.0, 0.0, 0.0))
translation_loc = bpy.context.scene.cursor.location
previous_mode = bpy.context.active_object.mode
# Calculate the movement difference
location_diff = copy_location - translation_loc
# NEW Translate
bpy.ops.transform.translate(
value = location_diff,
constraint_axis = (False, False, False),
orient_type = 'GLOBAL',
mirror = False,
use_proportional_edit = False,
snap = False,
release_confirm = False,
)
bpy.ops.object.mode_set(mode=previous_mode)
# Position the cursor back to it's original location
bpy.data.scenes[bpy.context.scene.name].cursor.location = previous_cursor_loc
# Restore the previous setting
context.scene.tool_settings.use_keyframe_insert_auto = auto_key
target.lock_location = lock_transform
def MoveBone(target, bone, context, location):
"""
Safely moves the given bone to the given location. Will ensure nothing is animated or screwed up in the process.
"""
# This doesnt need the cursor, and will ensure nothing is animated
# in the process
#print(">>> Moving Object <<<")
copy_location = Vector((0.0, 0.0, 0.0))
copy_location[0] = location[0]
copy_location[1] = location[1]
copy_location[2] = location[2]
# Prevent auto keyframing from being active
auto_key = context.scene.tool_settings.use_keyframe_insert_auto
lock_transform = target.lock_location
context.scene.tool_settings.use_keyframe_insert_auto = False
target.lock_location = (False, False, False)
# Save the current cursor location
cursor_loc = bpy.data.scenes[bpy.context.scene.name].cursor.location
previous_cursor_loc = [cursor_loc[0], cursor_loc[1], cursor_loc[2]]
# This line is actually super-important, not sure why though...
# FocusObject should fill the role of deselection...
bpy.ops.object.select_all(action= 'DESELECT')
# Calculate the translation vector using the 3D cursor
prevMode = SwitchObjectMode('POSE', target)
bpy.data.objects[target.name].data.bones.active = bpy.data.objects[target.name].pose.bones[bone.name].bone
bpy.ops.view3d.snap_cursor_to_selected()
translation_loc = Vector((0.0, 0.0, 0.0))
#print("RAWR")
translation_loc = bpy.context.scene.cursor.location
# for area in context.screen.areas:
# if area.type == 'VIEW_3D':
# translation_loc = area.spaces[0].cursor.location
#print(translation_loc)
# Calculate the movement difference
location_diff = copy_location - cursor.location
bpy.ops.transform.translate(
value=location_diff,
constraint_axis= (False, False, False),
orient_type = 'GLOBAL',
mirror= False,
use_proportional_edit= 'DISABLED',
snap= False,
snap_target= 'CLOSEST',
snap_point= (0.0, 0.0, 0.0),
snap_align= False,
snap_normal= (0.0, 0.0, 0.0),
gpencil_strokes= False,
texture_space= False,
remove_on_cancel= False,
release_confirm= False)
#print("Object", bone.name, "moved.... ", bone.location)
SwitchObjectMode(prevMode, target)
# Position the cursor back to it's original location
#bpy.data.scenes[bpy.context.scene.name].cursor.location = previous_cursor_loc
# Restore the previous setting
#context.scene.tool_settings.use_keyframe_insert_auto = auto_key
#target.lock_location = lock_transform
def RotateObjectSafe(target, context, rotation, forward):
"""
Safely rotates the given objects with the given rotation. Will ensure nothing is animated or screwed up in the process.
"""
# Prevent auto keyframing and location lock from being active
auto_key = context.scene.tool_settings.use_keyframe_insert_auto
context.scene.tool_settings.use_keyframe_insert_auto = False
FocusObject(target)
# Obtain the current rotation mode
order = target.rotation_mode
# Sort out how we're going to filter through the rotation order
rotationOrder = []
rotationComponents = []
axisX = [0, (1.0, 0.0, 0.0), (True, False, False)]
axisY = [1, (0.0, 1.0, 0.0), (False, True, False)]
axisZ = [2, (0.0, 0.0, 1.0), (False, False, True)]
if order == 'ZYX':
if forward is True:
rotationComponents = [axisZ, axisY, axisX]
else:
rotationComponents = [axisX, axisY, axisZ]
elif order == 'ZXY':
if forward is True:
rotationComponents = [axisZ, axisX, axisY]
else:
rotationComponents = [axisY, axisX, axisZ]
elif order == 'YZX':
if forward is True:
rotationComponents = [axisY, axisZ, axisX]
else:
rotationComponents = [axisX, axisZ, axisY]
elif order == 'YXZ':
if forward is True:
rotationComponents = [axisY, axisX, axisZ]
else:
rotationComponents = [axisZ, axisX, axisY]
elif order == 'XZY':
if forward is True:
rotationComponents = [axisX, axisZ, axisY]
else:
rotationComponents = [axisY, axisZ, axisX]
elif order == 'XYZ':
if forward is True:
rotationComponents = [axisX, axisY, axisZ]
else:
rotationComponents = [axisZ, axisY, axisX]
# Set the pivot to be the target object
backupPivot = 'CURSOR'
backupAlign = False
for area in context.screen.areas:
if area.type == 'VIEW_3D':
backupPivot = area.spaces[0].pivot_point
backupAlign = area.spaces[0].use_pivot_point_align
area.spaces[0].pivot_point = 'ACTIVE_ELEMENT'
area.spaces[0].use_pivot_point_align = False
# Rotate in Euler order
for i, item in enumerate(rotationComponents):
if rotation[item[0]] != 1:
bpy.ops.transform.rotate(
value=rotation[item[0]],
axis=item[1],
constraint_axis=item[2],
orient_type = 'GLOBAL',
release_confirm= True
)
# Restore the pivot
for area in context.screen.areas:
if area.type == 'VIEW_3D':
area.spaces[0].pivot_point = backupPivot
area.spaces[0].use_pivot_point_align = backupAlign
# Restore the previous setting
context.scene.tool_settings.use_keyframe_insert_auto = auto_key
def RotateAll(target, context, rotation, constraintAxis):
"""
???
"""
# Prevent auto keyframing and location lock from being active
auto_key = context.scene.tool_settings.use_keyframe_insert_auto
context.scene.tool_settings.use_keyframe_insert_auto = False
bpy.ops.object.select_all(action= 'SELECT')
ActivateObject(target)
# Set the pivot to be the target object
backupPivot = 'CURSOR'
backupAlign = False
for area in context.screen.areas:
if area.type == 'VIEW_3D':
backupPivot = area.spaces[0].pivot_point
backupAlign = area.spaces[0].use_pivot_point_align
area.spaces[0].pivot_point = 'ACTIVE_ELEMENT'
area.spaces[0].use_pivot_point_align = False
bpy.ops.transform.rotate(
value=radians(rotation),
axis= (1.0, 1.0, 1.0),
constraint_axis=constraintAxis,
orient_type = 'GLOBAL',
release_confirm= True
)
# Restore the pivot
for area in context.screen.areas:
if area.type == 'VIEW_3D':
area.spaces[0].pivot_point = backupPivot
area.spaces[0].use_pivot_point_align = backupAlign
# Restore the previous setting
context.scene.tool_settings.use_keyframe_insert_auto = auto_key
def RotateAllSafe(target, context, rotation, forward):
"""
???
"""
# Prevent auto keyframing and location lock from being active
auto_key = context.scene.tool_settings.use_keyframe_insert_auto
context.scene.tool_settings.use_keyframe_insert_auto = False
bpy.ops.object.select_all(action= 'SELECT')
ActivateObject(target)
# Obtain the current rotation mode
order = target.rotation_mode
# Sort out how we're going to filter through the rotation order
rotationOrder = []
rotationComponents = []
axisX = [0, (1.0, 0.0, 0.0), (True, False, False)]
axisY = [1, (0.0, 1.0, 0.0), (False, True, False)]
axisZ = [2, (0.0, 0.0, 1.0), (False, False, True)]
if order == 'ZYX':
if forward is True:
rotationComponents = [axisZ, axisY, axisX]
else:
rotationComponents = [axisX, axisY, axisZ]
elif order == 'ZXY':
if forward is True:
rotationComponents = [axisZ, axisX, axisY]
else:
rotationComponents = [axisY, axisX, axisZ]
elif order == 'YZX':
if forward is True:
rotationComponents = [axisY, axisZ, axisX]
else:
rotationComponents = [axisX, axisZ, axisY]
elif order == 'YXZ':
if forward is True:
rotationComponents = [axisY, axisX, axisZ]
else:
rotationComponents = [axisZ, axisX, axisY]
elif order == 'XZY':
if forward is True:
rotationComponents = [axisX, axisZ, axisY]
else:
rotationComponents = [axisY, axisZ, axisX]
elif order == 'XYZ':
if forward is True:
rotationComponents = [axisX, axisY, axisZ]
else:
rotationComponents = [axisZ, axisY, axisX]
# Set the pivot to be the target object
backupPivot = 'CURSOR'
backupAlign = False
for area in context.screen.areas:
if area.type == 'VIEW_3D':
backupPivot = area.spaces[0].pivot_point
backupAlign = area.spaces[0].use_pivot_point_align
area.spaces[0].pivot_point = 'ACTIVE_ELEMENT'
area.spaces[0].use_pivot_point_align = False
# Rotate in Euler order
for i, item in enumerate(rotationComponents):
if rotation[item[0]] != 1:
bpy.ops.transform.rotate(
value=rotation[item[0]],
axis=item[1],
constraint_axis=item[2],
orient_type = 'GLOBAL',
release_confirm= True
)
# Restore the pivot
for area in context.screen.areas:
if area.type == 'VIEW_3D':
area.spaces[0].pivot_point = backupPivot
area.spaces[0].use_pivot_point_align = backupAlign
# Restore the previous setting
context.scene.tool_settings.use_keyframe_insert_auto = auto_key
def ScaleAll(context, scale, constraintAxis):
"""
???
"""
# Prevent auto keyframing and location lock from being active
auto_key = context.scene.tool_settings.use_keyframe_insert_auto
context.scene.tool_settings.use_keyframe_insert_auto = False
bpy.ops.object.select_all(action= 'SELECT')
bpy.ops.transform.resize(
value=scale,
constraint_axis=constraintAxis,
orient_type = 'GLOBAL',
mirror= False,
proportional= 'DISABLED',
proportional_edit_falloff= 'SMOOTH',
proportional_size= 1.0,
snap= False,
snap_target= 'CLOSEST',
snap_point= (0.0, 0.0, 0.0),
snap_align= False,
snap_normal= (0.0, 0.0, 0.0),
gpencil_strokes= False,
texture_space= False,
remove_on_cancel= False,
release_confirm= False
)
# Restore the previous setting
context.scene.tool_settings.use_keyframe_insert_auto = auto_key