-
Notifications
You must be signed in to change notification settings - Fork 3
/
meshToArrays.py
78 lines (54 loc) · 2.18 KB
/
meshToArrays.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
# meshToArrays.py
#
if 'cmds' not in locals():
import maya.cmds as cmds
import json
import maya.OpenMaya as OpenMaya
# similar to obj format
def getDataArrays( mesh, prec=6 ):
# get the dag path from the mesh name
selection = OpenMaya.MSelectionList()
selection.add( mesh )
iterSel = OpenMaya.MItSelectionList(selection, OpenMaya.MFn.kGeometric)
dagPath = OpenMaya.MDagPath()
iterSel.getDagPath( dagPath )
# our mesh function set
meshFn = OpenMaya.MFnMesh(dagPath)
# vertex data
points = OpenMaya.MPointArray()
normalArray = OpenMaya.MFloatVectorArray()
uArray = OpenMaya.MFloatArray()
vArray = OpenMaya.MFloatArray()
# vertex face counts
vertexCountMIntArray = OpenMaya.MIntArray()
normalCountMIntArray = OpenMaya.MIntArray()
uvCountMIntArray = OpenMaya.MIntArray()
# face vertex index arrays
vertexIndexArray = OpenMaya.MIntArray()
normalIndexArray = OpenMaya.MIntArray()
uvIdsMIntArray = OpenMaya.MIntArray()
# get the vertex values
meshFn.getPoints( points, OpenMaya.MSpace.kWorld )
meshFn.getNormals( normalArray, OpenMaya.MSpace.kWorld )
meshFn.getUVs( uArray, vArray )
# get the face vertex indices
meshFn.getVertices( vertexCountMIntArray, vertexIndexArray )
meshFn.getNormalIds( normalCountMIntArray, normalIndexArray )
meshFn.getAssignedUVs( uvCountMIntArray, uvIdsMIntArray )
output = {
'position' : [], # [[x,y,z], ... ]
'normal' : [], # [[x,y,z], ... ]
'uv' : [], # [[u,v], ... ]
'faceIndices' : [] # [[positionIndex,normalIndex,uvIndex], ... ]
}
for i in range(points.length()):
output['position'].append( [round(points[i][0], prec), round(points[i][1], prec), round(points[i][2], prec)] )
for i in range(normalArray.length()):
output['normal'].append( [round(normalArray[i][0], prec), round(normalArray[i][1], prec), round(normalArray[i][2], prec)] )
for i in range(uArray.length()):
output['uv'].append( [round(uArray[i], prec), round(vArray[i], prec)] )
for i in range(vertexIndexArray.length()):
output['faceIndices'].append( [vertexIndexArray[i], normalIndexArray[i], uvIdsMIntArray[i] ] )
return output
for mesh in cmds.ls(sl=1,fl=1):
print getDataArrays(mesh)