-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathcursor_to_transform.py
More file actions
264 lines (202 loc) · 10.5 KB
/
cursor_to_transform.py
File metadata and controls
264 lines (202 loc) · 10.5 KB
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
"""
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 3 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, see <https://www.gnu.org/licenses/>.
"""
import bpy
import bmesh
import math
import mathutils as mu
import numpy as np
OLD_DRAW = None
OLD_OBJECT_ADD = None
bl_info = {
"name": "3D Cursor Enhancements",
"author": "Tommi Hyppänen (ambi)",
"version": (1, 0, 6),
"blender": (2, 80, 0),
"location": "View3D > Sidebar > View > 3D Cursor",
"description": "Various tools to use 3D cursor rotation for more purposes.",
"category": "Object",
}
def rotate_object(obj, q, point):
R = q.to_matrix().to_4x4()
T = mu.Matrix.Translation(point)
M = T @ R @ T.inverted()
obj.location = M @ obj.location
obj.rotation_euler.rotate(M)
class TFC_OT_SeriousTableFlip(bpy.types.Operator):
bl_idname = "object.serious_table_flip"
bl_label = "Serious table flip"
bl_description = "Transform all selected objects according to the 3D cursor"
bl_options = {"REGISTER", "UNDO"}
def execute(self, context):
mode = context.object.mode
bpy.ops.object.mode_set(mode="OBJECT")
to_qt = context.scene.cursor.rotation_euler.to_quaternion()
to_qt.invert()
to_tf = context.scene.cursor.location
for obj in context.selected_objects:
rotate_object(obj, to_qt, to_tf)
obj.location -= to_tf
context.scene.cursor.location = mu.Vector((0, 0, 0))
context.scene.cursor.rotation_euler = mu.Vector((0, 0, 0))
bpy.ops.object.mode_set(mode=mode)
return {"FINISHED"}
class TFC_OT_TransformFromCursor(bpy.types.Operator):
bl_idname = "object.transform_from_cursor"
bl_label = "Transform from cursor"
bl_description = "Copy 3D cursor rotation to object transform rotation"
bl_options = {"REGISTER", "UNDO"}
def execute(self, context):
mode = context.object.mode
bpy.ops.object.mode_set(mode="OBJECT")
bpy.ops.object.transform_apply(location=False, rotation=True, scale=False)
# obj = context.active_object
to_qt = context.scene.cursor.rotation_euler.to_quaternion()
to_qt.invert()
context.active_object.rotation_euler = to_qt.to_euler()
bpy.ops.object.transform_apply(location=False, rotation=True, scale=False)
to_qt.invert()
context.active_object.rotation_euler = to_qt.to_euler()
bpy.ops.object.mode_set(mode=mode)
return {"FINISHED"}
# classes = (TFC_OT_TransformFromCursor, TFC_OT_SeriousTableFlip)
classes = (TFC_OT_TransformFromCursor, )
def register():
for c in classes:
bpy.utils.register_class(c)
bpy.types.Scene.new_objects_to_cursor = bpy.props.BoolProperty(default=False)
bpy.types.Scene.new_objects_to_cursor_offset = bpy.props.FloatProperty(default=0.0, min=-100.0, max=100.0)
# ----- HACK: 3D cursor panel additions -----
global OLD_DRAW
if OLD_DRAW is None:
OLD_DRAW = bpy.types.VIEW3D_PT_view3d_cursor.draw
def draw(self, context):
layout = self.layout
cursor = context.scene.cursor
layout.column().prop(cursor, "location", text="Location")
rotation_mode = cursor.rotation_mode
if rotation_mode == "QUATERNION":
layout.column().prop(cursor, "rotation_quaternion", text="Rotation")
elif rotation_mode == "AXIS_ANGLE":
layout.column().prop(cursor, "rotation_axis_angle", text="Rotation")
else:
layout.column().prop(cursor, "rotation_euler", text="Rotation")
layout.prop(cursor, "rotation_mode", text="")
row = layout.row()
row.operator(TFC_OT_TransformFromCursor.bl_idname, text="Copy to object")
# row = layout.row()
# row.operator(TFC_OT_SeriousTableFlip.bl_idname, text="Serious table flip")
# row = layout.row()
# row.prop(context.scene, "new_objects_to_cursor", text="Align new objects to cursor")
# if context.scene.new_objects_to_cursor is True:
# row = layout.row()
# row.prop(context.scene, "new_objects_to_cursor_offset", text="Surface offset")
bpy.types.VIEW3D_PT_view3d_cursor.draw = draw
# # ----- HACK: Object add menu additions -----
# global OLD_OBJECT_ADD
# if OLD_OBJECT_ADD is None:
# OLD_OBJECT_ADD = bpy.types.VIEW3D_MT_mesh_add.draw
# def draw(self, context):
# layout = self.layout
# layout.operator_context = "INVOKE_REGION_WIN"
# if context.scene.new_objects_to_cursor is True:
# oft = context.scene.new_objects_to_cursor_offset
# step_1 = mu.Vector((0.0, 0.0, 1.0))
# cursor_rotation = context.scene.cursor.rotation_euler
# cursor_matrix = cursor_rotation.to_matrix()
# def op_sizes():
# sizes = {
# "plane": 2,
# "cube": 2,
# "circle": 1,
# "uv_sphere": 1,
# "ico_sphere": 1,
# "cylinder": 2,
# "cone": 2,
# "torus": 1,
# "grid": 2,
# "monkey": 2,
# }
# for entry in sizes:
# op_name = "MESH_OT_primitive_" + entry + "_add"
# op = bpy.data.window_managers[0].operator_properties_last(op_name)
# if op is not None:
# if entry in ["plane", "cube", "grid", "monkey"]:
# sizes[entry] = op.size / sizes[entry]
# if entry in ["circle", "uv_sphere", "ico_sphere"]:
# sizes[entry] = op.radius / sizes[entry]
# if entry in ["cylinder", "cone"]:
# sizes[entry] = op.depth / sizes[entry]
# if entry == "torus":
# sizes[entry] = op.major_radius / sizes[entry]
# return sizes
# sz = op_sizes()
# # print(sz)
# # print(bpy.data.window_managers[0].operators.keys())
# # print(bpy.data.window_managers[0].operators)
# op = layout.operator("mesh.primitive_plane_add", text="Plane", icon="MESH_PLANE")
# op.rotation = cursor_rotation
# op.location = context.scene.cursor.location + cursor_matrix @ step_1 * oft * sz["plane"]
# op = layout.operator("mesh.primitive_cube_add", text="Cube", icon="MESH_CUBE")
# op.rotation = cursor_rotation
# op.location = context.scene.cursor.location + cursor_matrix @ step_1 * oft * sz["cube"]
# op = layout.operator("mesh.primitive_circle_add", text="Circle", icon="MESH_CIRCLE")
# op.rotation = cursor_rotation
# op.location = context.scene.cursor.location + cursor_matrix @ step_1 * oft * sz["circle"]
# op = layout.operator("mesh.primitive_uv_sphere_add", text="UV Sphere", icon="MESH_UVSPHERE")
# op.rotation = cursor_rotation
# op.location = context.scene.cursor.location + cursor_matrix @ step_1 * oft * sz["uv_sphere"]
# op = layout.operator("mesh.primitive_ico_sphere_add", text="Ico Sphere", icon="MESH_ICOSPHERE")
# op.rotation = cursor_rotation
# op.location = context.scene.cursor.location + cursor_matrix @ step_1 * oft * sz["ico_sphere"]
# op = layout.operator("mesh.primitive_cylinder_add", text="Cylinder", icon="MESH_CYLINDER")
# op.rotation = cursor_rotation
# op.location = context.scene.cursor.location + cursor_matrix @ step_1 * oft * sz["cylinder"]
# op = layout.operator("mesh.primitive_cone_add", text="Cone", icon="MESH_CONE")
# op.rotation = cursor_rotation
# op.location = context.scene.cursor.location + cursor_matrix @ step_1 * oft * sz["cone"]
# op = layout.operator("mesh.primitive_torus_add", text="Torus", icon="MESH_TORUS")
# op.rotation = cursor_rotation
# op.location = context.scene.cursor.location + cursor_matrix @ step_1 * oft * sz["torus"]
# layout.separator()
# op = layout.operator("mesh.primitive_grid_add", text="Grid", icon="MESH_GRID")
# op.rotation = cursor_rotation
# op.location = context.scene.cursor.location + cursor_matrix @ step_1 * oft * sz["grid"]
# op = layout.operator("mesh.primitive_monkey_add", text="Monkey", icon="MESH_MONKEY")
# op.rotation = cursor_rotation
# op.location = context.scene.cursor.location + cursor_matrix @ step_1 * oft * sz["monkey"]
# else:
# op = layout.operator("mesh.primitive_plane_add", text="Plane", icon="MESH_PLANE")
# op = layout.operator("mesh.primitive_cube_add", text="Cube", icon="MESH_CUBE")
# op = layout.operator("mesh.primitive_circle_add", text="Circle", icon="MESH_CIRCLE")
# op = layout.operator("mesh.primitive_uv_sphere_add", text="UV Sphere", icon="MESH_UVSPHERE")
# op = layout.operator("mesh.primitive_ico_sphere_add", text="Ico Sphere", icon="MESH_ICOSPHERE")
# op = layout.operator("mesh.primitive_cylinder_add", text="Cylinder", icon="MESH_CYLINDER")
# op = layout.operator("mesh.primitive_cone_add", text="Cone", icon="MESH_CONE")
# op = layout.operator("mesh.primitive_torus_add", text="Torus", icon="MESH_TORUS")
# layout.separator()
# op = layout.operator("mesh.primitive_grid_add", text="Grid", icon="MESH_GRID")
# op = layout.operator("mesh.primitive_monkey_add", text="Monkey", icon="MESH_MONKEY")
# bpy.types.VIEW3D_MT_mesh_add.draw = draw
def unregister():
for c in reversed(classes):
bpy.utils.unregister_class(c)
del bpy.types.Scene.new_objects_to_cursor
del bpy.types.Scene.new_objects_to_cursor_offset
global OLD_DRAW
if OLD_DRAW is not None:
bpy.types.VIEW3D_PT_view3d_cursor.draw = OLD_DRAW
# global OLD_OBJECT_ADD
# if OLD_OBJECT_ADD is not None:
# bpy.types.VIEW3D_MT_mesh_add.draw = OLD_OBJECT_ADD
if __name__ == "__main__":
register()