-
Notifications
You must be signed in to change notification settings - Fork 29
/
advanced_usage.py
156 lines (126 loc) · 7 KB
/
advanced_usage.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
from maya import cmds
cmds.loadPlugin("boneDynamicsNode.mll", qt=True)
def create_dynamics_node(
bone,
end,
scalable=False,
target_bone=None,
offset_node=None,
colliders=[],
visualize=True
):
if not bone in cmds.listRelatives(end, p=True):
print("Exit: {} is not {}'s parent.".format(bone, end))
return
boneDynamicsNode = cmds.createNode("boneDynamicsNode")
cmds.connectAttr('time1.outTime', boneDynamicsNode + '.time', force=True)
cmds.connectAttr(bone + '.translate', boneDynamicsNode + '.boneTranslate', f=True)
cmds.connectAttr(bone + '.parentMatrix[0]', boneDynamicsNode + '.boneParentMatrix', f=True)
cmds.connectAttr(bone + '.parentInverseMatrix[0]', boneDynamicsNode + '.boneParentInverseMatrix', f=True)
cmds.connectAttr(bone + '.jointOrient', boneDynamicsNode + '.boneJointOrient', f=True)
cmds.connectAttr(end + '.translate', boneDynamicsNode + '.endTranslate', f=True)
cmds.connectAttr(boneDynamicsNode + '.outputRotate', bone + '.rotate', f=True)
if scalable:
cmds.connectAttr(bone + '.scale', boneDynamicsNode + '.boneScale', f=True)
cmds.connectAttr(bone + '.inverseScale', boneDynamicsNode + '.boneInverseScale', f=True)
cmds.connectAttr(end + '.scale', boneDynamicsNode + '.endScale', f=True)
if target_bone:
if cmds.objExists(target_bone):
cmds.connectAttr(target_bone + '.rotate', boneDynamicsNode + '.rotationOffset', f=True)
if offset_node:
if cmds.objExists(offset_node):
cmds.connectAttr(offset_node + '.worldMatrix[0]', boneDynamicsNode + '.offsetMatrix', f=True)
if visualize:
# angle limit
angle_cone = cmds.createNode("implicitCone")
angle_cone_tm = cmds.listRelatives(angle_cone, p=True)[0]
angle_cone_ro = cmds.createNode("transform", n="{}_cone_ro".format(bone))
angle_cone_root = cmds.createNode("transform", n="{}_cone_root".format(bone))
cmds.setAttr(angle_cone_tm + '.ry', -90)
cmds.parent(angle_cone_tm, angle_cone_ro, r=True)
cmds.parent(angle_cone_ro, angle_cone_root, r=True)
bone_parent = cmds.listRelatives(bone, p=True)
if bone_parent:
cmds.parent(angle_cone_root, bone_parent[0], r=True)
cmds.connectAttr(boneDynamicsNode + '.boneTranslate', angle_cone_root + '.translate', f=True)
cmds.connectAttr(boneDynamicsNode + '.boneJointOrient', angle_cone_root + '.rotate', f=True)
cmds.connectAttr(boneDynamicsNode + '.rotationOffset', angle_cone_ro + '.rotate', f=True)
cmds.connectAttr(boneDynamicsNode + '.enableAngleLimit', angle_cone_root + '.v', f=True)
cmds.connectAttr(boneDynamicsNode + '.angleLimit', angle_cone + '.coneAngle', f=True)
cmds.setAttr(angle_cone + '.coneCap', 2)
cmds.setAttr(angle_cone_tm + '.overrideEnabled', 1)
cmds.setAttr(angle_cone_tm + '.overrideDisplayType', 2)
# collision radius
radius_sphere = cmds.createNode("implicitSphere")
cmds.connectAttr(boneDynamicsNode + '.radius', radius_sphere + '.radius', f=True)
radius_sphere_tm = cmds.listRelatives(radius_sphere, p=True)[0]
cmds.parent(radius_sphere_tm, end, r=True)
cmds.setAttr(radius_sphere_tm + '.overrideEnabled', 1)
cmds.setAttr(radius_sphere_tm + '.overrideDisplayType', 2)
cmds.connectAttr(boneDynamicsNode + '.iterations', radius_sphere_tm + '.v', f=True)
sphere_col_idx = 0
capsule_col_idx = 0
iplane_col_idx = 0
mesh_col_idx = 0
for col in colliders:
if not cmds.objExists(col):
print("Skip: {} is not found.".format(col))
continue
if not cmds.attributeQuery('colliderType', n=col, ex=True):
col_shape = cmds.listRelatives(col, s=True, f=True)
if col_shape:
if cmds.nodeType(col_shape[0]) == 'mesh':
cmds.connectAttr(col_shape[0] + '.worldMesh[0]', boneDynamicsNode + '.meshCollider[{}]'.format(mesh_col_idx), f=True)
mesh_col_idx += 1
continue
print("Skip: {} has no 'colliderType' attribute.".format(col))
continue
colliderType = cmds.getAttr(col + '.colliderType')
if colliderType == 'sphere':
cmds.connectAttr(col + ".worldMatrix[0]", boneDynamicsNode + ".sphereCollider[{}].sphereColMatrix".format(sphere_col_idx), f=True)
cmds.connectAttr(col + ".radius", boneDynamicsNode + ".sphereCollider[{}].sphereColRadius".format(sphere_col_idx), f=True)
sphere_col_idx += 1
elif colliderType in ['capsule', 'capsule2']:
radius_attr_a = ".radius" if colliderType == 'capsule' else ".radiusA"
radius_attr_b = ".radius" if colliderType == 'capsule' else ".radiusB"
a = cmds.listConnections(col + '.sphereA', d=0)[0]
b = cmds.listConnections(col + '.sphereB', d=0)[0]
cmds.connectAttr(a + ".worldMatrix[0]", boneDynamicsNode + ".capsuleCollider[{}].capsuleColMatrixA".format(capsule_col_idx), f=True)
cmds.connectAttr(b + ".worldMatrix[0]", boneDynamicsNode + ".capsuleCollider[{}].capsuleColMatrixB".format(capsule_col_idx), f=True)
cmds.connectAttr(col + radius_attr_a, boneDynamicsNode + ".capsuleCollider[{}].capsuleColRadiusA".format(capsule_col_idx), f=True)
cmds.connectAttr(col + radius_attr_b, boneDynamicsNode + ".capsuleCollider[{}].capsuleColRadiusB".format(capsule_col_idx), f=True)
capsule_col_idx += 1
elif colliderType == 'infinitePlane':
cmds.connectAttr(col + ".worldMatrix[0]", boneDynamicsNode + ".infinitePlaneCollider[{}].infinitePlaneColMatrix".format(iplane_col_idx), f=True)
iplane_col_idx += 1
return boneDynamicsNode
if __name__ == "__main__":
# Select in order from root to tip of the joint-chain.
joints = cmds.ls(sl=True)
# Enable per-section scaling.
scalable = True
# Place the collider created by expcol as a child of 'collider_grp'.
colliders = []
if cmds.objExists("collider_grp"):
colliders = cmds.ls(cmds.listRelatives("collider_grp", c=True), tr=True)
# Duplicate the joint-chain to be simulated and add '_target' to the postfix.
target_bone_postfix = "_target"
# Name of the node to offset the transform.
offset_node_name = "offset"
# ---------------------------------------------------
set_name = "boneDynamicsNodeSet"
if not cmds.objExists(set_name):
cmds.select(cl=True)
cmds.sets(name=set_name)
for bone, end in zip(joints[:-1], joints[1:]):
boneDynamicsNode = create_dynamics_node(
bone,
end,
scalable=scalable,
target_bone=bone+target_bone_postfix,
offset_node=offset_node_name,
colliders=colliders,
visualize=True
)
if boneDynamicsNode:
cmds.sets(boneDynamicsNode, addElement=set_name)