export_xyz.py
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# ##### END GPL LICENSE BLOCK #####
# <pep8 compliant>
"""
Name: 'XYZ (.xyz)...'
Blender: 248
Group: 'Export'
Tooltip: 'Save a Generic XYZ File'
"""
__author__ = "Ben Syverson"
__url__ = ['www.blender.org']
__version__ = "1.0"
__bpydoc__ = """\
Exports XYZ data
"""
import os
import time
import bpy
import Mathutils
def splitExt(path):
dotidx = path.rfind('.')
if dotidx == -1:
return path, ''
else:
return path[:dotidx], path[dotidx:]
def fixName(name):
if name == None:
return 'None'
else:
return name.replace(' ', '_')
def write(filename, objects, scene,
EXPORT_APPLY_MODIFIERS=True,
EXPORT_VERTS=True,
EXPORT_PARTICLES_AS_POINT_CLOUD=False):
'''
Basic write function. The context and options must be alredy set
This can be accessed externaly
eg.
write( 'c:\ est\\foobar.xyz', Blender.Object.GetSelected() ) # Using default options.
'''
import math
print('XYZ Export path: "%s"' % filename)
temp_mesh_name = '~tmp-mesh'
time1 = time.clock()
file = open(filename, "w")
# Write Header
version = "2.5"
file.write('# Blender3D v%s XYZ File: %s
' % (version, bpy.data.filename.split('/')[-1].split('\\')[-1] ))
file.write('# www.blender3d.org
')
# Get all meshes
for ob_main in objects:
obs = []
if ob_main.dupli_type != 'NONE':
# XXX
print('creating dupli_list on', ob_main.name)
ob_main.create_dupli_list()
obs = [(dob.object, dob.matrix) for dob in ob_main.dupli_list]
else:
obs = [(ob_main, ob_main.matrix)]
for ob, ob_mat in obs:
if ob.type != 'MESH':
continue
me = ob.create_mesh(EXPORT_APPLY_MODIFIERS, 'PREVIEW')
# Vert
if EXPORT_VERTS:
file.write('# %s
' % ob_main.name) # Write Object name
for v in me.verts:
file.write('%.6f %.6f %.6f
' % tuple(v.co))
# Write particles
if EXPORT_PARTICLES_AS_POINT_CLOUD:
particle_systems = ob.particle_systems;
for particle_system in particle_systems:
file.write('# %s
' % particle_system.name) # Write Object name
for particle in particle_system.particles:
file.write('%.6f %.6f %.6f
' % tuple(particle.location))
# clean up
bpy.data.remove_mesh(me)
if ob_main.dupli_type != 'NONE':
ob_main.free_dupli_list()
file.close()
print("XYZ Export time: %.2f" % (time.clock() - time1))
def do_export(filename, context,
EXPORT_APPLY_MODIFIERS = True, # not used
EXPORT_SEL_ONLY = True, # ok
EXPORT_VERTS = True,
EXPORT_PARTICLES_AS_POINT_CLOUD = False):
base_name, ext = splitExt(filename)
context_name = [base_name, '', '', ext] # Base name, scene name, frame number, extension
orig_scene = context.scene
export_scenes = [orig_scene]
if EXPORT_SEL_ONLY:
export_objects = context.selected_objects
else:
export_objects = context.scene.objects
full_path= ''.join(context_name)
write(full_path, export_objects, context.scene, EXPORT_APPLY_MODIFIERS, EXPORT_VERTS, EXPORT_PARTICLES_AS_POINT_CLOUD)
from bpy.props import *
class ExportXYZ(bpy.types.Operator):
'''Save a XYZ File'''
bl_idname = "export.xyz"
bl_label = 'Export XYZ'
path = StringProperty(name="File Path", description="File path used for exporting the XYZ file", maxlen= 1024, default= "")
# context group
use_selection = BoolProperty(name="Selection Only", description="", default= False)
# object group
use_modifiers = BoolProperty(name="Apply Modifiers", description="", default= True)
use_verts = BoolProperty(name="Vertices", description="Export Vertices", default=True)
use_particles = BoolProperty(name="Particles", description="Export Particles as Point Cloud", default=False)
def execute(self, context):
do_export(self.properties.path, context,
EXPORT_APPLY_MODIFIERS=self.properties.use_modifiers,
EXPORT_SEL_ONLY=self.properties.use_selection,
EXPORT_VERTS=self.properties.use_verts,
EXPORT_PARTICLES_AS_POINT_CLOUD=self.properties.use_particles)
return ('FINISHED',)
def invoke(self, context, event):
wm = context.manager
wm.add_fileselect(self)
return ('RUNNING_MODAL',)
bpy.ops.add(ExportXYZ)
import dynamic_menu
def menu_func(self, context):
default_path = bpy.data.filename.replace(".blend", ".xyz")
self.layout.operator(ExportXYZ.bl_idname, text="XYZ (.xyz)...").path = default_path
menu_item = dynamic_menu.add(bpy.types.INFO_MT_file_export, menu_func)
if __name__ == "__main__":
bpy.ops.EXPORT_OT_XYZ(filename="/tmp/test.xyz")