|
| 1 | +# Copyright (C) 2021 David Cattermole. |
| 2 | +# |
| 3 | +# This file is part of mmSolver. |
| 4 | +# |
| 5 | +# mmSolver is free software: you can redistribute it and/or modify it |
| 6 | +# under the terms of the GNU Lesser General Public License as |
| 7 | +# published by the Free Software Foundation, either version 3 of the |
| 8 | +# License, or (at your option) any later version. |
| 9 | +# |
| 10 | +# mmSolver is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +# GNU Lesser General Public License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU Lesser General Public License |
| 16 | +# along with mmSolver. If not, see <https://www.gnu.org/licenses/>. |
| 17 | +# |
| 18 | +""" |
| 19 | +Export 2D Markers from Blender. |
| 20 | +""" |
| 21 | + |
| 22 | +import os |
| 23 | + |
| 24 | +import bpy |
| 25 | +from bpy.props import StringProperty, IntProperty, BoolProperty, EnumProperty |
| 26 | +from bpy_extras.io_utils import ExportHelper |
| 27 | + |
| 28 | +from . import utils |
| 29 | +from . import uvtrack_format_blender as uv_fmt |
| 30 | + |
| 31 | + |
| 32 | +class MmSolver_OT_exportSelectedMarkers(bpy.types.Operator, ExportHelper): |
| 33 | + """Save a .uv file for 2D Tracks.""" |
| 34 | + bl_idname = 'mmsolver.export_selected_markers' |
| 35 | + bl_label = 'Export Selected 2D Tracks' |
| 36 | + |
| 37 | + filename_ext = '.uv' |
| 38 | + filter_glob: StringProperty( |
| 39 | + default='*.uv', |
| 40 | + options={'HIDDEN'}, |
| 41 | + maxlen=255, # Max internal buffer length, longer would be clamped. |
| 42 | + ) |
| 43 | + |
| 44 | + frame_start: IntProperty(name='Start Frame', |
| 45 | + description='Start frame for export', |
| 46 | + default=1, min=1, max=300000) |
| 47 | + frame_end: IntProperty(name='End Frame', |
| 48 | + description='End frame for export', |
| 49 | + default=250, min=1, max=300000) |
| 50 | + |
| 51 | + def execute(self, context): |
| 52 | + file_path = self.filepath |
| 53 | + if file_path is None or len(file_path) == 0: |
| 54 | + msg = 'mmSolver: Export 2D Tracks: Could not write file data.' |
| 55 | + self.report({'ERROR'}, msg) |
| 56 | + return {'FINISHED'} |
| 57 | + |
| 58 | + clip, cam, tracks = utils.get_selected_clip_objects(context) |
| 59 | + if clip is None or cam is None or tracks is None: |
| 60 | + msg = ( |
| 61 | + 'mmSolver: Export 2D Tracks: ' |
| 62 | + 'Please select 2D Tracks in the Movie Clip Editor. ' |
| 63 | + 'Could not get selected 2D Tracks.' |
| 64 | + ) |
| 65 | + self.report({'WARNING'}, msg) |
| 66 | + return {'CANCELLED'} |
| 67 | + |
| 68 | + start_frame = clip.frame_start |
| 69 | + end_frame = start_frame + clip.frame_duration |
| 70 | + frame_range = (max(self.frame_start, start_frame), |
| 71 | + min(self.frame_end, end_frame)) |
| 72 | + |
| 73 | + data_str = uv_fmt.generate( |
| 74 | + context, |
| 75 | + clip, cam, tracks, |
| 76 | + frame_range, |
| 77 | + fmt=uv_fmt.UV_TRACK_FORMAT_VERSION_PREFERRED) |
| 78 | + if data_str is None or len(data_str) == 0: |
| 79 | + msg = 'mmSolver: Export 2D Tracks: Could not generate file data.' |
| 80 | + self.report({'ERROR'}, msg) |
| 81 | + return {'FINISHED'} |
| 82 | + |
| 83 | + file_path = utils.write_text_data_to_file(data_str, file_path) |
| 84 | + msg = 'mmSolver: Export 2D Tracks: Successfully written file: {}' |
| 85 | + self.report({'INFO'}, msg.format(file_path)) |
| 86 | + return {'FINISHED'} |
| 87 | + |
| 88 | + def invoke(self, context, event): |
| 89 | + self.frame_start = context.scene.frame_start |
| 90 | + self.frame_end = context.scene.frame_end |
| 91 | + |
| 92 | + wm = context.window_manager |
| 93 | + wm.fileselect_add(self) |
| 94 | + return {'RUNNING_MODAL'} |
| 95 | + |
| 96 | + |
| 97 | +def menu_export(self, context): |
| 98 | + default_path = os.path.splitext(bpy.data.filepath)[0] + '.uv' |
| 99 | + op = self.layout.operator( |
| 100 | + MmSolver_OT_exportSelectedMarkers.bl_idname, |
| 101 | + text='Export Selected 2D Tracks (MM Solver .uv)') |
| 102 | + op.filepath = default_path |
| 103 | + |
| 104 | + |
| 105 | +def register(): |
| 106 | + bpy.utils.register_class(MmSolver_OT_exportSelectedMarkers) |
| 107 | + bpy.types.TOPBAR_MT_file_export.append(menu_export) |
| 108 | + |
| 109 | + |
| 110 | +def unregister(): |
| 111 | + bpy.utils.unregister_class(MmSolver_OT_exportSelectedMarkers) |
| 112 | + bpy.types.TOPBAR_MT_file_export.remove(menu_export) |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == '__main__': |
| 116 | + register() |
0 commit comments