forked from dictoon/mayaseed
/
ms_export_obj.py
122 lines (96 loc) · 4.47 KB
/
ms_export_obj.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
#
# Copyright (c) 2012 Jonathan Topf
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
import maya.cmds as cmds
import maya.OpenMaya as OpenMaya
import ms_commands
import os
SCRIPT_VERSION = '0.1.2'
def export(object_name, file_path, overwrite=True):
export_dir = os.path.split(file_path)[0]
if not os.path.exists(export_dir):
os.makedirs(export_dir)
if (not os.path.exists(file_path)) or overwrite:
try:
file_object = open(file_path, 'w')
except IOError:
error_msg = 'IO error: failed to open {0} for writing.'.format(file_path)
cmds.error(error_msg)
raise RuntimeError(error_msg)
#write file info
file_object.write('# File generated by ms_export_obj.py version {0} from mayaseed version: {1}\n'.format(SCRIPT_VERSION,ms_commands.MAYASEED_VERSION))
file_object.write('# {0}\n\n'.format(object_name))
sel = OpenMaya.MSelectionList()
sel.add(str(object_name))
mesh_dag_path = OpenMaya.MDagPath() #create dag path object
sel.getDagPath(0,mesh_dag_path) #retreive dag path of object
mesh = OpenMaya.MFnMesh(mesh_dag_path) #create mesh object
iter_polys = OpenMaya.MItMeshPolygon( mesh.object() ) #create iter object
transform_space_enum = 2 #2 = object_space
#cerate point array to hold the points from the mesh
point_array = OpenMaya.MPointArray()
mesh.getPoints(point_array)
#write the points to disk
i=0
while i < point_array.length():
point = point_array[i]
file_object.write('v {0} {1} {2}\n'.format(point.x, point.y, point.z))
i += 1
file_object.write('\n')
#create MFloatArray for u's and v's to store values
u_array = OpenMaya.MFloatArray()
v_array = OpenMaya.MFloatArray()
mesh.getUVs(u_array, v_array)
#write u's and v's to disk
i=0
while i < u_array.length():
file_object.write('vt {0} {1}\n'.format(u_array[i], v_array[i]))
i +=1
file_object.write('\n')
normal_array = OpenMaya.MFloatVectorArray()
mesh.getNormals(normal_array, transform_space_enum)
#write normals to disk
i = 0
while i < normal_array.length():
normal = normal_array[i]
file_object.write('vn {0} {1} {2}\n'.format(normal.x, normal.y, normal.z))
i += 1
file_object.write('\n')
#iterate over polys
while not iter_polys.isDone():
#start face definition
file_object.write('f ')
i=0
while i < iter_polys.polygonVertexCount():
#to get uv index we need to create an int pointer because the api is just wrappers for c++
util = OpenMaya.MScriptUtil()
util.createFromInt(0)
uv_pInt = util.asIntPtr()
uv_index = OpenMaya.MScriptUtil()
uv_index.createFromInt(0)
iter_polys.getUVIndex(i, uv_pInt)
file_object.write('{0}/{1}/{2} '.format(iter_polys.vertexIndex(i) + 1, uv_index.getInt(uv_pInt) + 1, iter_polys.normalIndex(i) + 1))
i += 1
#next could just write individual polys like this, but would be good if you could weed out shared verts
file_object.write('\n')
iter_polys.next()
file_object.close()
print 'finished exporting', object_name