ply export patch 5576 applied with parts rewritten.
BPyMesh getMeshFromObject bugfix in some cases non mesh object types could be a problem.
This commit is contained in:
@@ -408,11 +408,9 @@ def getMeshFromObject(ob, container_mesh=None, apply_modifiers=True, vgroups=Tru
|
|||||||
Dont apply modifiers, copy the mesh.
|
Dont apply modifiers, copy the mesh.
|
||||||
So we can transform the data. its easiest just to get a copy of the mesh.
|
So we can transform the data. its easiest just to get a copy of the mesh.
|
||||||
'''
|
'''
|
||||||
tempob= Blender.Object.New('Mesh')
|
tempob= scn.objects.new(ob.getData(mesh=1))
|
||||||
tempob.shareFrom(ob)
|
|
||||||
scn.link(tempob)
|
|
||||||
mesh.getFromObject(tempob)
|
mesh.getFromObject(tempob)
|
||||||
scn.unlink(tempob)
|
scn.objects.unlink(tempob)
|
||||||
|
|
||||||
if type == 'Mesh':
|
if type == 'Mesh':
|
||||||
if vgroups:
|
if vgroups:
|
||||||
|
|||||||
@@ -8,13 +8,15 @@ Tooltip: 'Export active object to Stanford PLY format'
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import Blender
|
import Blender
|
||||||
|
from Blender import Mesh, Scene, Window, sys, Image, Draw
|
||||||
import BPyMesh
|
import BPyMesh
|
||||||
|
|
||||||
__author__ = "Bruce Merry"
|
__author__ = "Bruce Merry"
|
||||||
__version__ = "0.9"
|
__version__ = "0.92"
|
||||||
__bpydoc__ = """\
|
__bpydoc__ = """\
|
||||||
This script exports Stanford PLY files from Blender. It supports per-vertex
|
This script exports Stanford PLY files from Blender. It supports normals,
|
||||||
normals and per-face colours and texture coordinates.
|
colours, and texture coordinates per face or per vertex.
|
||||||
|
Only one mesh can be exported at a time.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Copyright (C) 2004, 2005: Bruce Merry, bmerry@cs.uct.ac.za
|
# Copyright (C) 2004, 2005: Bruce Merry, bmerry@cs.uct.ac.za
|
||||||
@@ -32,108 +34,181 @@ normals and per-face colours and texture coordinates.
|
|||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with this program; if not, write to the Free Software Foundation,
|
# along with this program; if not, write to the Free Software Foundation,
|
||||||
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
|
|
||||||
# Vector rounding se we can use as keys
|
# Vector rounding se we can use as keys
|
||||||
def rvec3d(v): return round(v.x, 6), round(v.y, 6), round(v.z, 6)
|
#
|
||||||
def rvec2d(v): return round(v.x, 6), round(v.y, 6)
|
#
|
||||||
def rcol(c): return c.r, c.g, c.b
|
# Updated on Jan 1, 2007 by Gabe Ghearing
|
||||||
|
# - fixed normals so they are correctly smooth/flat
|
||||||
|
# - fixed crash when the model doesn't have uv coords or vertex colors
|
||||||
|
# - fixed crash when the model has vertex colors but doesn't have uv coords
|
||||||
|
# - changed float32 to float and uint8 to uchar for compatibility
|
||||||
|
# Errata/Notes as of Jan 1, 2007
|
||||||
|
# - script exports texture coords if they exist even if TexFace isn't selected (not a big deal to me)
|
||||||
|
# - ST(R) should probably be renamed UV(T) like in most PLY files (importer needs to be updated to take either)
|
||||||
|
#
|
||||||
|
# Updated on Jan 3, 2007 by Gabe Ghearing
|
||||||
|
# - fixed "sticky" vertex UV exporting
|
||||||
|
# - added pupmenu to enable/disable exporting normals, uv coords, and colors
|
||||||
|
# Errata/Notes as of Jan 3, 2007
|
||||||
|
# - ST(R) coords should probably be renamed UV(T) like in most PLY files (importer needs to be updated to take either)
|
||||||
|
# - edges should be exported since PLY files support them
|
||||||
|
# - code is getting spaghettish, it should be refactored...
|
||||||
|
#
|
||||||
|
|
||||||
|
|
||||||
|
def rvec3d(v): return round(v[0], 6), round(v[1], 6), round(v[2], 6)
|
||||||
|
def rvec2d(v): return round(v[0], 6), round(v[1], 6)
|
||||||
|
|
||||||
def file_callback(filename):
|
def file_callback(filename):
|
||||||
|
|
||||||
if not filename.lower().endswith('.ply'):
|
if not filename.lower().endswith('.ply'):
|
||||||
filename += '.ply'
|
filename += '.ply'
|
||||||
|
|
||||||
scn= Blender.Scene.GetCurrent()
|
scn= Blender.Scene.GetCurrent()
|
||||||
object= scn.getActiveObject()
|
ob= scn.objects.active
|
||||||
if not object:
|
if not ob:
|
||||||
Blender.Draw.PupMenu('Error%t|Select 1 active object')
|
Blender.Draw.PupMenu('Error%t|Select 1 active object')
|
||||||
return
|
return
|
||||||
|
|
||||||
file = open(filename, 'wb')
|
file = open(filename, 'wb')
|
||||||
|
|
||||||
mesh = BPyMesh.getMeshFromObject(object, None, True, False, scn)
|
EXPORT_APPLY_MODIFIERS = Draw.Create(1)
|
||||||
|
EXPORT_NORMALS = Draw.Create(1)
|
||||||
|
EXPORT_UV = Draw.Create(1)
|
||||||
|
EXPORT_COLORS = Draw.Create(1)
|
||||||
|
#EXPORT_EDGES = Draw.Create(0)
|
||||||
|
|
||||||
|
pup_block = [\
|
||||||
|
('Apply Modifiers', EXPORT_APPLY_MODIFIERS, 'Use transformed mesh data.'),\
|
||||||
|
('Normals', EXPORT_NORMALS, 'Export vertex normal data.'),\
|
||||||
|
('UVs', EXPORT_UV, 'Export texface UV coords.'),\
|
||||||
|
('Colors', EXPORT_COLORS, 'Export vertex Colors.'),\
|
||||||
|
#('Edges', EXPORT_EDGES, 'Edges not connected to faces.'),\
|
||||||
|
]
|
||||||
|
|
||||||
|
if not Draw.PupBlock('Export...', pup_block):
|
||||||
|
return
|
||||||
|
|
||||||
|
Window.WaitCursor(1)
|
||||||
|
|
||||||
|
EXPORT_APPLY_MODIFIERS = EXPORT_APPLY_MODIFIERS.val
|
||||||
|
EXPORT_NORMALS = EXPORT_NORMALS.val
|
||||||
|
EXPORT_UV = EXPORT_UV.val
|
||||||
|
EXPORT_COLORS = EXPORT_COLORS.val
|
||||||
|
#EXPORT_EDGES = EXPORT_EDGES.val
|
||||||
|
|
||||||
|
mesh = BPyMesh.getMeshFromObject(ob, None, EXPORT_APPLY_MODIFIERS, False, scn)
|
||||||
|
|
||||||
if not mesh:
|
if not mesh:
|
||||||
Blender.Draw.PupMenu('Error%t|Could not get mesh data from active object')
|
Blender.Draw.PupMenu('Error%t|Could not get mesh data from active object')
|
||||||
return
|
return
|
||||||
|
|
||||||
mesh.transform(object.matrixWorld)
|
|
||||||
|
|
||||||
if mesh.vertexColors or mesh.faceUV:
|
mesh.transform(ob.matrixWorld)
|
||||||
mesh.faceUV= 1
|
|
||||||
have_uv= True
|
|
||||||
else:
|
|
||||||
have_uv= False
|
|
||||||
|
|
||||||
|
faceUV = mesh.faceUV
|
||||||
|
vertexUV = mesh.vertexUV
|
||||||
|
vertexColors = mesh.vertexColors
|
||||||
|
|
||||||
|
if (not faceUV) and (not vertexUV): EXPORT_UV = False
|
||||||
|
if not vertexColors: EXPORT_COLORS = False
|
||||||
|
|
||||||
|
if not EXPORT_UV: faceUV = vertexUV = False
|
||||||
|
if not EXPORT_COLORS: vertexColors = False
|
||||||
|
|
||||||
|
# incase
|
||||||
|
color = uvcoord = uvcoord_key = normal = normal_key = None
|
||||||
|
|
||||||
verts = [] # list of dictionaries
|
verts = [] # list of dictionaries
|
||||||
vdict = {} # (index, normal, uv) -> new index
|
# vdict = {} # (index, normal, uv) -> new index
|
||||||
|
vdict = [{} for i in xrange(len(mesh.verts))]
|
||||||
|
vert_count = 0
|
||||||
for i, f in enumerate(mesh.faces):
|
for i, f in enumerate(mesh.faces):
|
||||||
f_col= f.col
|
smooth = f.smooth
|
||||||
f_uv= f.uv
|
if not smooth:
|
||||||
for j, v in enumerate(f.v):
|
normal = tuple(f.no)
|
||||||
index = v.index
|
normal_key = rvec3d(normal)
|
||||||
|
|
||||||
if have_uv:
|
if faceUV: uv = f.uv
|
||||||
vdata = v.co, v.no
|
if vertexColors: col = f.col
|
||||||
uv= f_uv[j]
|
for j, v in enumerate(f):
|
||||||
col= f_col[j]
|
if smooth:
|
||||||
vdata = v.co, v.no, uv, col
|
normal= v.no
|
||||||
key = index, rvec3d(v.no), rcol(col), rvec2d(uv)
|
normal_key = rvec3d(normal)
|
||||||
else:
|
|
||||||
vdata = v.co, v.no
|
if faceUV:
|
||||||
key = index, rvec3d(v.no)
|
uvcoord= tuple(uv[j])
|
||||||
|
uvcoord_key = rvec2d(uvcoord)
|
||||||
if not vdict.has_key(key):
|
elif vertexUV:
|
||||||
vdict[key] = len(verts);
|
uvcoord= tuple(v.uvco)
|
||||||
verts.append(vdata)
|
uvcoord_key = rvec2d(uvcoord)
|
||||||
|
|
||||||
|
if vertexColors: color= col[j].r, col[j].g, col[j].b
|
||||||
|
|
||||||
|
|
||||||
|
key = normal_key, uvcoord_key, color
|
||||||
|
|
||||||
|
vdict_local = vdict[v.index]
|
||||||
|
|
||||||
|
if (not vdict_local) or (not vdict_local.has_key(key)):
|
||||||
|
vdict_local[key] = vert_count;
|
||||||
|
verts.append( (tuple(v.co), normal, uvcoord, color) )
|
||||||
|
vert_count += 1
|
||||||
|
|
||||||
|
|
||||||
file.write('ply\n')
|
file.write('ply\n')
|
||||||
file.write('format ascii 1.0\n')
|
file.write('format ascii 1.0\n')
|
||||||
file.write('Created by Blender3D %s - www.blender.org, source file: %s\n' % (Blender.Get('version'), Blender.Get('filename').split('/')[-1].split('\\')[-1] ))
|
file.write('Created by Blender3D %s - www.blender.org, source file: %s\n' % (Blender.Get('version'), Blender.Get('filename').split('/')[-1].split('\\')[-1] ))
|
||||||
|
|
||||||
file.write('element vertex %d\n' % len(verts))
|
file.write('element vertex %d\n' % len(verts))
|
||||||
|
|
||||||
file.write('property float32 x\n')
|
file.write('property float x\n')
|
||||||
file.write('property float32 y\n')
|
file.write('property float y\n')
|
||||||
file.write('property float32 z\n')
|
file.write('property float z\n')
|
||||||
file.write('property float32 nx\n')
|
if EXPORT_NORMALS:
|
||||||
file.write('property float32 ny\n')
|
file.write('property float nx\n')
|
||||||
file.write('property float32 nz\n')
|
file.write('property float ny\n')
|
||||||
|
file.write('property float nz\n')
|
||||||
|
|
||||||
if have_uv:
|
if EXPORT_UV:
|
||||||
file.write('property float32 s\n')
|
file.write('property float s\n')
|
||||||
file.write('property float32 t\n')
|
file.write('property float t\n')
|
||||||
file.write('property uint8 red\n')
|
if EXPORT_COLORS:
|
||||||
file.write('property uint8 green\n')
|
file.write('property uchar red\n')
|
||||||
file.write('property uint8 blue\n')
|
file.write('property uchar green\n')
|
||||||
|
file.write('property uchar blue\n')
|
||||||
|
|
||||||
file.write('element face %d\n' % len(mesh.faces))
|
file.write('element face %d\n' % len(mesh.faces))
|
||||||
file.write('property list uint8 int32 vertex_indices\n')
|
file.write('property list uchar uint vertex_indices\n')
|
||||||
file.write('end_header\n')
|
file.write('end_header\n')
|
||||||
|
|
||||||
for i, v in enumerate(verts):
|
for i, v in enumerate(verts):
|
||||||
file.write('%.6f %.6f %.6f ' % tuple(v[0])) # co
|
file.write('%.6f %.6f %.6f ' % v[0]) # co
|
||||||
file.write('%.6f %.6f %.6f ' % tuple(v[1])) # no
|
if EXPORT_NORMALS:
|
||||||
|
file.write('%.6f %.6f %.6f ' % v[1]) # no
|
||||||
|
|
||||||
if have_uv:
|
if EXPORT_UV:
|
||||||
file.write('%.6f %.6f %u %u %u' % (v[2].x, v[2].y, v[3].r, v[3].g, v[3].b)) # uv/col
|
file.write('%.6f %.6f ' % v[2]) # uv
|
||||||
|
if EXPORT_COLORS:
|
||||||
|
file.write('%u %u %u' % v[3]) # col
|
||||||
file.write('\n')
|
file.write('\n')
|
||||||
|
|
||||||
for (i, f) in enumerate(mesh.faces):
|
for (i, f) in enumerate(mesh.faces):
|
||||||
file.write('%d ' % len(f))
|
file.write('%d ' % len(f))
|
||||||
|
smooth = f.smooth
|
||||||
|
if not smooth: no = rvec3d(f.no)
|
||||||
|
|
||||||
f_col= f.col
|
if faceUV: uv = f.uv
|
||||||
f_uv= f.uv
|
if vertexColors: col = f.col
|
||||||
|
for j, v in enumerate(f):
|
||||||
for j, v in enumerate(f.v):
|
if f.smooth: normal= rvec3d(v.no)
|
||||||
index = v.index
|
else: normal= no
|
||||||
|
if faceUV: uvcoord= rvec2d(uv[j])
|
||||||
|
elif vertexUV: uvcoord= rvec2d(v.uvco)
|
||||||
|
if vertexColors: color= col[j].r, col[j].g, col[j].b
|
||||||
|
co = v.co
|
||||||
|
|
||||||
if have_uv:
|
file.write('%d ' % vdict[v.index][normal, uvcoord, color])
|
||||||
uv= f_uv[j]
|
|
||||||
col= f_col[j]
|
|
||||||
key = index, rvec3d(v.no), rcol(col), rvec2d(uv)
|
|
||||||
else:
|
|
||||||
key = index, rvec3d(v.no)
|
|
||||||
|
|
||||||
file.write('%d ' % vdict[key])
|
|
||||||
file.write('\n')
|
file.write('\n')
|
||||||
file.close()
|
file.close()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user