-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.py
137 lines (111 loc) · 4.17 KB
/
testing.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
import maya.cmds as cmds
import maya.OpenMaya as OpenMaya
if __name__ == '__main__':
meshA, meshB = "pSphere1", "pSphere2"
#Add MSelection.
mSel = OpenMaya.MSelectionList()
mSel.add(meshA)
mSel.add(meshB)
cmds.polyColorPerVertex(
meshA, colorRGB=[0.0, 0.0, 1.0], alpha=1.0, colorDisplayOption=True)
#Set Mesh.
source_mDagPath = OpenMaya.MDagPath()
collider_mDagPath = OpenMaya.MDagPath()
# Get DagPath and extend to shape.
mSel.getDagPath(0, source_mDagPath)
source_mDagPath.extendToShape()
# Get DagPath and extend to shape.
mSel.getDagPath(1, collider_mDagPath)
collider_mDagPath.extendToShape()
#Get points.
collider_MfnMesh = OpenMaya.MFnMesh(collider_mDagPath)
collider_Pnts = OpenMaya.MFloatPointArray()
collider_MfnMesh.getPoints(collider_Pnts, OpenMaya.MSpace.kObject)
#Get Bounding box.
collider_BBVec = OpenMaya.MVector(
*cmds.getAttr("%s.boundingBox.boundingBoxSize" % meshB)[0])
#BondingBBSize and threshhold.
thresholdValue = collider_BBVec.length()*2
#Accelerator.
mmAccelParams = collider_MfnMesh.autoUniformGridParams()
#Get points.
source_MfnMesh = OpenMaya.MFnMesh(source_mDagPath)
source_Pnts = OpenMaya.MFloatPointArray()
source_MfnMesh.getPoints(source_Pnts, OpenMaya.MSpace.kWorld)
#Defining used variables.
checkCollision = 0
maxDeformation = 0.0
dummyFloatArray = OpenMaya.MFloatArray()
source_pntNormal = OpenMaya.MVector()
#Get Vertex color.
vertexColorList = OpenMaya.MColorArray()
source_MfnMesh.getVertexColors(vertexColorList)
collideColor = OpenMaya.MColor(1.0, 0.0, 0.0, 1.0)
lenVertexList = vertexColorList.length()
#Get Vert list.
fnComponent = OpenMaya.MFnSingleIndexedComponent()
fullComponent = fnComponent.create(OpenMaya.MFn.kMeshVertComponent)
fnComponent.setCompleteData(lenVertexList)
vertexIndexList = OpenMaya.MIntArray()
fnComponent.getElements(vertexIndexList)
# direct collision deformation:
for k in xrange(source_Pnts.length()):
source_MfnMesh.getVertexNormal(
k, source_pntNormal, OpenMaya.MSpace.kWorld)
# define an intersection ray from the mesh that should be deformed
raySource = OpenMaya.MFloatPoint(source_Pnts[k].x,
source_Pnts[k].y,
source_Pnts[k].z)
rayDirection = OpenMaya.MFloatVector(source_pntNormal)
# MeshFn.allIntersections variables
faceIds = None
triIds = None
idsSorted = True
space = OpenMaya.MSpace.kWorld
maxParam = thresholdValue
tolerance = 1e-9
testBothDirs = True
accelParams = mmAccelParams
sortHits = True
hitPoints = OpenMaya.MFloatPointArray()
hitRayParams = OpenMaya.MFloatArray(dummyFloatArray)
hitFaces = None
hitTriangles = None
hitBary1s = None
hitBary2s = None
gotHit = collider_MfnMesh.allIntersections(
raySource,
rayDirection,
faceIds,
triIds,
idsSorted,
space,
maxParam,
testBothDirs,
accelParams,
sortHits,
hitPoints,
hitRayParams,
hitFaces,
hitTriangles,
hitBary1s,
hitBary2s)
if gotHit:
hitCount = hitPoints.length()
#Check for direction (Negative means it is inside mesh.).
for i in xrange(hitCount-1):
if hitRayParams[i] * hitRayParams[i+1] < 0:
signChange = i
break
else:
signChange = -1000
collision = 0
#Check collision on.
if hitCount == 2 and signChange+1 == 1 and signChange != -1000:
collision = 1
elif hitCount > 2 and \
hitCount/(signChange+1) != 2 and signChange != -1000:
collision = 1
if collision == 1:
vertexColorList.set(collideColor, k)
source_MfnMesh.setVertexColors(vertexColorList, vertexIndexList, None)