-
Notifications
You must be signed in to change notification settings - Fork 0
/
offset_parent_matrix_helpers.py
298 lines (220 loc) · 8.83 KB
/
offset_parent_matrix_helpers.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
import maya.cmds as cmds
import maya.api.OpenMaya as om
def offset_parent_matrix_parent_with_offset():
"""Constrain two objects using offset parent matrix keeping the current position of the driven object.
Parameters
----------
driver_object: selection
A transform object
driven_object: selection
A transform object
Returns
-------
No return.
"""
if not __valid_parent_selection():
om.MGlobal.displayWarning("Select first the parent node and then the child.")
return
nodes = cmds.ls(selection=True)
parent = nodes[0]
child = nodes[1]
__opm_parent_with_offset(parent, child)
def transformations_to_offset_parent_matrix(node):
"""Move the transformations values to the offset parent matrix.
Parameters
----------
node: str
A transform node
Returns
-------
No return.
"""
__cp_transformations_to_offset_parent_matrix(node)
__zero_out_transformations(node)
def transformations_to_offset_parent_matrix_by_selection(hierarchy = False):
"""Move the transformations values to the offset parent matrix.
Parameters
----------
node: selection
A transform node
hierarchy: bool, optional
Apply this method to all the object hierarchy.
Returns
-------
No return.
"""
if not __valid_selection():
om.MGlobal.displayWarning("Select at least one node.")
return
nodes = __get_nodes(hierarchy)
for node in nodes:
__cp_transformations_to_offset_parent_matrix(node)
__zero_out_transformations(node)
def offset_parent_matrix_to_transformations(hierarchy = False):
"""Move the offset parent matrix values to the transformations.
Parameters
----------
node: selection
A transform node
hierarchy: bool, optional
Apply this method to all the object hierarchy.
Returns
-------
No return.
"""
if not __valid_selection():
om.MGlobal.displayWarning("Select at least one node.")
return
nodes = __get_nodes(hierarchy)
for node in nodes:
__cp_offset_parent_matrix_to_transformations(node)
__zero_out_offset_parent_matrix(node)
def zero_out_all(hierarchy = False):
"""Zero out the transformations and offset parent matrix values.
Parameters
----------
node: selection
A transform node
hierarchy: bool, optional
Apply this method to all the object hierarchy.
Returns
-------
No return.
"""
if not __valid_selection():
om.MGlobal.displayWarning("Select at least one node.")
return
nodes = __get_nodes(hierarchy)
for node in nodes:
__zero_out_offset_parent_matrix(node)
__zero_out_transformations(node)
def zero_out_opm(hierarchy = False):
"""Zero out the offset parent matrix values.
Parameters
----------
node: selection
A transform node
hierarchy: bool, optional
Apply this method to all the object hierarchy.
Returns
-------
No return.
"""
if not __valid_selection():
om.MGlobal.displayWarning("Select at least one node.")
return
nodes = __get_nodes(hierarchy)
for node in nodes:
__zero_out_offset_parent_matrix(node)
def zero_out_opm_keeping_position(hierarchy = False):
"""Zero out the offset parent matrix values but keeping the transform on the same position.
Parameters
----------
node: selection
A transform node
hierarchy: bool, optional
Apply this method to all the object hierarchy.
Returns
-------
No return.
"""
if not __valid_selection():
om.MGlobal.displayWarning("Select at least one node.")
return
nodes = __get_nodes(hierarchy)
for node in nodes:
__zero_out_opm_keeping_position(node)
def offset_parent_matrix_parent():
"""Constrain two objects using offset parent matrix.
Parameters
----------
driver_object: selection
A transform object
driven_object: selection
A transform object
Returns
-------
No return.
"""
if not __valid_parent_selection():
om.MGlobal.displayWarning("Select first the parent node and then the child.")
return
nodes = cmds.ls(selection=True)
parent = nodes[0]
child = nodes[1]
__opm_parent(parent, child)
__zero_out_transformations(child)
def __zero_out_transformations(node):
cmds.setAttr("{0}.translateX".format(node), 0)
cmds.setAttr("{0}.translateY".format(node), 0)
cmds.setAttr("{0}.translateZ".format(node), 0)
cmds.setAttr("{0}.rotateX".format(node), 0)
cmds.setAttr("{0}.rotateY".format(node), 0)
cmds.setAttr("{0}.rotateZ".format(node), 0)
if cmds.nodeType(node) == "joint":
cmds.setAttr("{0}.jointOrientX".format(node), 0)
cmds.setAttr("{0}.jointOrientY".format(node), 0)
cmds.setAttr("{0}.jointOrientZ".format(node), 0)
def __cp_transformations_to_offset_parent_matrix(node):
source = "{0}.xformMatrix".format(node)
target = "{0}.offsetParentMatrix".format(node)
cmds.connectAttr(source, target, force=True)
cmds.disconnectAttr(source, target)
def __cp_offset_parent_matrix_to_transformations(node):
decompose_matrix_node = cmds.createNode("decomposeMatrix", name="temporaryDecomposeMatrix", skipSelect=True)
source_opm = "{0}.offsetParentMatrix".format(node)
target_decompose_matrix = "{0}.inputMatrix".format(decompose_matrix_node)
source_opm_translations = "{0}.outputTranslate".format(decompose_matrix_node)
target_node_translations = "{0}.translate".format(node)
source_opm_rotations = "{0}.outputRotate".format(decompose_matrix_node)
target_node_rotations = "{0}.rotate".format(node)
source_opm_scale = "{0}.outputScale".format(decompose_matrix_node)
target_node_scale = "{0}.scale".format(node)
cmds.connectAttr(source_opm, target_decompose_matrix, force=True)
cmds.connectAttr(source_opm_translations, target_node_translations, force=True)
cmds.connectAttr(source_opm_rotations, target_node_rotations, force=True)
cmds.connectAttr(source_opm_scale, target_node_scale, force=True)
cmds.disconnectAttr(source_opm, target_decompose_matrix)
cmds.delete(decompose_matrix_node)
def __zero_out_offset_parent_matrix(node):
identity_matrix = [1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]
cmds.setAttr("{0}.offsetParentMatrix".format(node), *identity_matrix, type="matrix")
def __get_nodes_by_hierarchy():
all_nodes = []
all_descendents = cmds.ls(cmds.listRelatives(allDescendents=True, type="transform"))
selected = cmds.ls(selection=True)
all_nodes.extend(all_descendents)
all_nodes.extend(selected)
return all_nodes
def __valid_selection():
return len(cmds.ls(selection=True)) > 0
def __valid_parent_selection():
return len(cmds.ls(selection=True)) == 2
def __get_nodes(hierarchy):
nodes = None
if hierarchy:
nodes = __get_nodes_by_hierarchy()
else:
nodes = cmds.ls(selection=True)
return nodes
def __zero_out_opm_keeping_position(node):
node_local_matrix = om.MMatrix(cmds.xform(node, query=True, objectSpace=True, matrix=True))
node_opm = om.MMatrix(cmds.getAttr("{0}.offsetParentMatrix".format(node)))
node_new_trasnsformation = node_local_matrix * node_opm
cmds.xform(node, matrix=node_new_trasnsformation)
__zero_out_offset_parent_matrix(node)
def __opm_parent(parent, child):
parent_world_matrix = "{0}.worldMatrix".format(parent)
child_opm = "{0}.offsetParentMatrix".format(child)
cmds.connectAttr(parent_world_matrix, child_opm, force=True)
def __opm_parent_with_offset(parent, child):
__zero_out_opm_keeping_position(child)
parent_inverse_world_hold = cmds.createNode("holdMatrix", name="{0}_{1}_opmParentHold".format(parent, child), skipSelect=True)
cmds.connectAttr("{0}.worldInverseMatrix".format(parent), "{0}.inMatrix".format(parent_inverse_world_hold), force=True)
cmds.disconnectAttr("{0}.worldInverseMatrix".format(parent), "{0}.inMatrix".format(parent_inverse_world_hold))
parent_offset_multi = cmds.createNode("multMatrix", name="{0}_{1}_opmParentMult".format(parent, child), skipSelect=True)
cmds.connectAttr("{0}.outMatrix".format(parent_inverse_world_hold), "{0}.matrixIn[0]".format(parent_offset_multi), force=True)
cmds.connectAttr("{0}.worldMatrix".format(parent), "{0}.matrixIn[1]".format(parent_offset_multi), force=True)
cmds.connectAttr("{0}.matrixSum".format(parent_offset_multi), "{0}.offsetParentMatrix".format(child), force=True)
if __name__ == "__main__":
offset_parent_matrix_parent_with_offset()