p2or
12/9/2016 - 11:26 AM

blender-export-empty-coords-to-txt.py

import bpy
import os

def write_some_data(context, base_dir, use_some_setting):
    print("running write_some_data...")
    
    empties_in_selection = [ob for ob in context.selected_objects if ob.type=="EMPTY"]

    if empties_in_selection:
        for ob in empties_in_selection:
            print ("Empty: {}".format(ob.name))
            
            # get all relevant properties
            scn = bpy.context.scene
            ob_name = ob.name
            ob_loc = ob.location
            frame = scn.frame_start
            
            # generate file name based on scene and object name
            file_name = '{0}_{1}_{2}.txt'.format(scn.name, ob.name, "Location")
            file_path = os.path.join(base_dir, file_name)
            
            f = open(file_path, 'w', encoding='utf-8')
            while frame <= scn.frame_end:
                scn.frame_set(frame)
                x, y, z = ob_loc
                f.write('{0}\t{1}\t{2}\t{3}\n'.format(frame, x, y, z))
                frame += 1
            f.close()
            
    else:
        print ("No Empties in Selection")
    return {'FINISHED'}


# ExportHelper is a helper class, defines filename and
# invoke() function which calls the file selector.
from bpy_extras.io_utils import ExportHelper
from bpy.props import StringProperty, BoolProperty, EnumProperty
from bpy.types import Operator

class ExportSomeData(Operator, ExportHelper):
    """This appears in the tooltip of the operator and in the generated docs"""
    bl_idname = "export_test.some_data"  # important since its how bpy.ops.import_test.some_data is constructed
    bl_label = "Export Some Data"
    bl_options = {'PRESET', 'UNDO'}

    # ExportHelper mixin class uses this
    filename_ext = "blubber.txt"
    
    filter_glob = StringProperty(
            default="*.txt",
            options={'HIDDEN'},
            )

    # List of operator properties, the attributes will be assigned
    # to the class instance from the operator settings before calling.
    use_setting = BoolProperty(
            name="Example Boolean",
            description="Example Tooltip",
            default=True,
            )

    def execute(self, context):
        return write_some_data(context, os.path.dirname(self.filepath), self.use_setting)


# Only needed if you want to add into a dynamic menu
def menu_func_export(self, context):
    self.layout.operator(ExportSomeData.bl_idname, text="Text Export Operator")


def register():
    bpy.utils.register_class(ExportSomeData)
    bpy.types.INFO_MT_file_export.append(menu_func_export)


def unregister():
    bpy.utils.unregister_class(ExportSomeData)
    bpy.types.INFO_MT_file_export.remove(menu_func_export)


if __name__ == "__main__":
    register()

    # test call
    bpy.ops.export_test.some_data('INVOKE_DEFAULT')