-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursive_apply_transform.py
More file actions
195 lines (147 loc) · 6.49 KB
/
recursive_apply_transform.py
File metadata and controls
195 lines (147 loc) · 6.49 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
import bpy
from mathutils import Vector, Matrix, Euler, Quaternion
from math import radians
bl_info = {
"name": "Recursive Apply Transform",
"author": "HALBY",
"version": (0, 1),
"blender": (2, 80, 0),
"location": "3D Viewport > Sidebar",
"description": "Apply root transform while maintaining children transforms.",
"category": "Object"
}
class HALBY_OT_RecursiveApplyTransformButton(bpy.types.Operator):
bl_idname = "object.recursive_apply_transform"
bl_label = "Recursive Apply Transform"
bl_description = "Apply root transform while maintaining children transforms."
bl_options = {'REGISTER', 'UNDO'}
@staticmethod
def mul(a, b):
return Vector((a.x * b.x, a.y * b.y, a.z * b.z))
@staticmethod
def div(a, b):
return Vector((a.x / b.x, a.y / b.y, a.z / b.z))
def __init__(self):
self.meshes = []
def first_transform(self, obj, affine):
obj.matrix_local = obj.matrix_local @ affine
affine_inverse = affine.inverted()
if obj.type == "MESH":
self.meshes.append(obj.data)
for v in obj.data.vertices:
v.co = (affine_inverse @ v.co.to_4d()).to_3d()
if obj.data.shape_keys != None:
for kb in obj.data.shape_keys.key_blocks:
if kb.name != "Basis":
for idx, dat in enumerate(kb.data):
dat.co = (affine_inverse @ dat.co.to_4d()).to_3d()
elif obj.type == "ARMATURE":
for b in obj.data.bones:
b.head = (affine_inverse @ b.head.to_4d()).to_3d()
b.tail = (affine_inverse @ b.tail.to_4d()).to_3d()
for child in obj.children:
self.recursive_transform(child, affine)
def recursive_transform(self, obj, affine):
obj.location = (affine.inverted() @ obj.location.to_4d()).to_3d()
rs_inverse = affine.inverted().to_3x3().to_4x4()
if obj.type == "MESH" and not obj.data in self.meshes:
self.meshes.append(obj.data)
for v in obj.data.vertices:
v.co = (rs_inverse @ v.co.to_4d()).to_3d()
if obj.data.shape_keys != None:
for kb in obj.data.shape_keys.key_blocks:
if kb.name != "Basis":
for idx, dat in enumerate(kb.data):
dat.co = (affine_inverse @ dat.co.to_4d()).to_3d()
elif obj.type == "ARMATURE":
bpy.context.view_layer.objects.active = obj
bpy.ops.object.mode_set(mode="EDIT")
bones = []
for b in obj.data.edit_bones:
bones.append((
(rs_inverse @ b.head.to_4d()).to_3d(),
(rs_inverse @ b.tail.to_4d()).to_3d()
))
for i in range(len(obj.data.edit_bones)):
obj.data.edit_bones[i].head, obj.data.edit_bones[i].tail = bones[i]
bpy.ops.object.mode_set(mode="OBJECT")
for child in obj.children:
self.recursive_transform(child, affine)
def execute(self, context):
t = context.scene.halby_recursive_apply_transform
o = context.active_object
# Object Affine
object_affine = o.matrix_local
# Target Affine
loc = Matrix.Translation((t.transform_x_location, t.transform_y_location, t.transform_z_location))
rot = Euler((t.transform_x_rotation, t.transform_y_rotation, t.transform_z_rotation)).to_matrix().to_4x4()
sca = Matrix((
[t.transform_x_scale, 0, 0, 0],
[0, t.transform_y_scale, 0, 0],
[0, 0, t.transform_z_scale, 0],
[0, 0, 0, 1]
))
target_affine = loc @ rot @ sca
self.first_transform(context.active_object, object_affine.inverted() @ target_affine)
return {'FINISHED'}
class HALBY_PT_RecursiveApplyTransformUI(bpy.types.Panel):
bl_label = "Recursive Apply Transform"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = "Edit"
bl_context = "objectmode"
@classmethod
def poll(cls, context):
for o in bpy.data.objects:
if o.select_get():
return True
return False
def draw_header(self, context):
layout = self.layout
layout.label(text="", icon='OUTLINER_OB_EMPTY')
def draw(self, context):
scn = context.scene.halby_recursive_apply_transform
layout = self.layout
layout.label(text="Location")
col = layout.column(align=True)
col.prop(scn, "transform_x_location")
col.prop(scn, "transform_y_location")
col.prop(scn, "transform_z_location")
layout.separator()
layout.label(text="Rotation")
col = layout.column(align=True)
col.prop(scn, "transform_x_rotation")
col.prop(scn, "transform_y_rotation")
col.prop(scn, "transform_z_rotation")
layout.separator()
layout.label(text="Scale")
col = layout.column(align=True)
col.prop(scn, "transform_x_scale")
col.prop(scn, "transform_y_scale")
col.prop(scn, "transform_z_scale")
layout.separator()
layout.operator(HALBY_OT_RecursiveApplyTransformButton.bl_idname, text="Apply")
class HALBY_PG_RecursiveApplyTransformProps(bpy.types.PropertyGroup):
transform_x_location: bpy.props.FloatProperty(name="X", unit="LENGTH")
transform_y_location: bpy.props.FloatProperty(name="Y", unit="LENGTH")
transform_z_location: bpy.props.FloatProperty(name="Z", unit="LENGTH")
transform_x_rotation: bpy.props.FloatProperty(name="X", unit="ROTATION")
transform_y_rotation: bpy.props.FloatProperty(name="Y", unit="ROTATION")
transform_z_rotation: bpy.props.FloatProperty(name="Z", unit="ROTATION")
transform_x_scale: bpy.props.FloatProperty(name="X", default=1.0, min=0.0001)
transform_y_scale: bpy.props.FloatProperty(name="Y", default=1.0, min=0.0001)
transform_z_scale: bpy.props.FloatProperty(name="Z", default=1.0, min=0.0001)
classes = [
HALBY_OT_RecursiveApplyTransformButton,
HALBY_PT_RecursiveApplyTransformUI,
HALBY_PG_RecursiveApplyTransformProps
]
def register():
for c in classes:
bpy.utils.register_class(c)
bpy.types.Scene.halby_recursive_apply_transform = bpy.props.PointerProperty(type=HALBY_PG_RecursiveApplyTransformProps)
def unregister():
for c in classes:
bpy.utils.unregister_class(c)
if __name__ == "__main__":
register()