use mix-in classes for import export operators, these define the filepath property and invoke function at the moment.

This commit is contained in:
Campbell Barton 2010-09-01 02:25:49 +00:00
parent d67eedcef9
commit 5036a9d20c
12 changed files with 138 additions and 142 deletions

@ -1107,15 +1107,16 @@ def write(filename, context):
#primary.dump()
# # write('/test_b.3ds')
from bpy.props import *
class Export3DS(bpy.types.Operator):
from io_utils import ExportHelper
class Export3DS(bpy.types.Operator, ExportHelper):
'''Export to 3DS file format (.3ds)'''
bl_idname = "export.autodesk_3ds"
bl_label = 'Export 3DS'
filepath = StringProperty(name="File Path", description="Filepath used for exporting the 3DS file", maxlen= 1024, default= "")
check_existing = BoolProperty(name="Check Existing", description="Check and warn on overwriting existing files", default=True, options={'HIDDEN'})
filename_ext = ".3ds"
@classmethod
def poll(cls, context): # Poll isnt working yet
@ -1123,19 +1124,11 @@ class Export3DS(bpy.types.Operator):
def execute(self, context):
filepath = self.properties.filepath
filepath = bpy.path.ensure_ext(filepath, ".3ds")
filepath = bpy.path.ensure_ext(filepath, self.filename_ext)
write(filepath, context)
return {'FINISHED'}
def invoke(self, context, event):
import os
if not self.properties.is_property_set("filepath"):
self.properties.filepath = os.path.splitext(bpy.data.filepath)[0] + ".3ds"
context.manager.add_fileselect(self)
return {'RUNNING_MODAL'}
# Add to a menu
def menu_func(self, context):

@ -3319,19 +3319,21 @@ def write_ui():
# GLOBALS.clear()
from bpy.props import *
class ExportFBX(bpy.types.Operator):
from io_utils import ExportHelper
class ExportFBX(bpy.types.Operator, ExportHelper):
'''Selection to an ASCII Autodesk FBX'''
bl_idname = "export.fbx"
bl_label = "Export FBX"
filename_ext = ".fbx"
# List of operator properties, the attributes will be assigned
# to the class instance from the operator settings before calling.
filepath = StringProperty(name="File Path", description="Filepath used for exporting the FBX file", maxlen= 1024, default="")
check_existing = BoolProperty(name="Check Existing", description="Check and warn on overwriting existing files", default=True, options={'HIDDEN'})
EXP_OBS_SELECTED = BoolProperty(name="Selected Objects", description="Export selected objects on visible layers", default=True)
# EXP_OBS_SCENE = BoolProperty(name="Scene Objects", description="Export all objects in this scene", default=True)
TX_SCALE = FloatProperty(name="Scale", description="Scale all data, (Note! some imports dont support scaled armatures)", min=0.01, max=1000.0, soft_min=0.01, soft_max=1000.0, default=1.0)
@ -3368,7 +3370,7 @@ class ExportFBX(bpy.types.Operator):
raise Exception("filepath not set")
filepath = self.properties.filepath
filepath = bpy.path.ensure_ext(filepath, ".fbx")
filepath = bpy.path.ensure_ext(filepath, self.filename_ext)
GLOBAL_MATRIX = mtx4_identity
GLOBAL_MATRIX[0][0] = GLOBAL_MATRIX[1][1] = GLOBAL_MATRIX[2][2] = self.properties.TX_SCALE
@ -3401,16 +3403,6 @@ class ExportFBX(bpy.types.Operator):
return {'FINISHED'}
def invoke(self, context, event):
import os
if not self.properties.is_property_set("filepath"):
self.properties.filepath = os.path.splitext(bpy.data.filepath)[0] + ".fbx"
context.manager.add_fileselect(self)
return {'RUNNING_MODAL'}
# if __name__ == "__main__":
# bpy.ops.EXPORT_OT_ply(filepath="/tmp/test.ply")

@ -143,13 +143,16 @@ def write(filename, sce, ob, PREF_STARTFRAME, PREF_ENDFRAME, PREF_FPS):
sce.set_frame(orig_frame)
from bpy.props import *
from io_utils import ExportHelper
class ExportMDD(bpy.types.Operator):
class ExportMDD(bpy.types.Operator, ExportHelper):
'''Animated mesh to MDD vertex keyframe file'''
bl_idname = "export.mdd"
bl_label = "Export MDD"
filename_ext = ".mdd"
# get first scene to get min and max properties for frames, fps
minframe = 1
@ -159,8 +162,6 @@ class ExportMDD(bpy.types.Operator):
# List of operator properties, the attributes will be assigned
# to the class instance from the operator settings before calling.
filepath = StringProperty(name="File Path", description="Filepath used for exporting the MDD file", maxlen=1024)
check_existing = BoolProperty(name="Check Existing", description="Check and warn on overwriting existing files", default=True, options={'HIDDEN'})
fps = IntProperty(name="Frames Per Second", description="Number of frames/second", min=minfps, max=maxfps, default=25)
frame_start = IntProperty(name="Start Frame", description="Start frame for baking", min=minframe, max=maxframe, default=1)
frame_end = IntProperty(name="End Frame", description="End frame for baking", min=minframe, max=maxframe, default=250)
@ -172,7 +173,7 @@ class ExportMDD(bpy.types.Operator):
def execute(self, context):
filepath = self.properties.filepath
filepath = bpy.path.ensure_ext(filepath, ".mdd")
filepath = bpy.path.ensure_ext(filepath, self.filename_ext)
write(filepath,
context.scene,
@ -184,14 +185,6 @@ class ExportMDD(bpy.types.Operator):
return {'FINISHED'}
def invoke(self, context, event):
import os
if not self.properties.is_property_set("filepath"):
self.properties.filepath = os.path.splitext(bpy.data.filepath)[0] + ".mdd"
context.manager.add_fileselect(self)
return {'RUNNING_MODAL'}
def menu_func(self, context):
self.layout.operator(ExportMDD.bl_idname, text="Lightwave Point Cache (.mdd)")

@ -854,19 +854,20 @@ Currently the exporter lacks these features:
'''
from bpy.props import *
from io_utils import ExportHelper
class ExportOBJ(bpy.types.Operator):
class ExportOBJ(bpy.types.Operator, ExportHelper):
'''Save a Wavefront OBJ File'''
bl_idname = "export.obj"
bl_label = 'Export OBJ'
filename_ext = ".obj"
# List of operator properties, the attributes will be assigned
# to the class instance from the operator settings before calling.
filepath = StringProperty(name="File Path", description="Filepath used for exporting the OBJ file", maxlen= 1024, default= "", subtype='FILE_PATH')
check_existing = BoolProperty(name="Check Existing", description="Check and warn on overwriting existing files", default=True, options={'HIDDEN'})
# context group
use_selection = BoolProperty(name="Selection Only", description="Export selected objects only", default= False)
use_all_scenes = BoolProperty(name="All Scenes", description="", default= False)
@ -897,7 +898,7 @@ class ExportOBJ(bpy.types.Operator):
def execute(self, context):
filepath = self.properties.filepath
filepath = bpy.path.ensure_ext(filepath, ".obj")
filepath = bpy.path.ensure_ext(filepath, self.filename_ext)
write(filepath, context,
EXPORT_TRI=self.properties.use_triangles,
@ -921,14 +922,6 @@ class ExportOBJ(bpy.types.Operator):
return {'FINISHED'}
def invoke(self, context, event):
import os
if not self.properties.is_property_set("filepath"):
self.properties.filepath = os.path.splitext(bpy.data.filepath)[0] + ".obj"
context.manager.add_fileselect(self)
return {'RUNNING_MODAL'}
def menu_func(self, context):
self.layout.operator(ExportOBJ.bl_idname, text="Wavefront (.obj)")

@ -257,19 +257,19 @@ def write(filename, scene, ob, \
"""
from bpy.props import *
from io_utils import ExportHelper
class ExportPLY(bpy.types.Operator):
class ExportPLY(bpy.types.Operator, ExportHelper):
'''Export a single object as a stanford PLY with normals, colours and texture coordinates.'''
bl_idname = "export.ply"
bl_label = "Export PLY"
filename_ext = ".ply"
# List of operator properties, the attributes will be assigned
# to the class instance from the operator settings before calling.
filepath = StringProperty(name="File Path", description="Filepath used for exporting the PLY file", maxlen=1024, default="")
check_existing = BoolProperty(name="Check Existing", description="Check and warn on overwriting existing files", default=True, options={'HIDDEN'})
use_modifiers = BoolProperty(name="Apply Modifiers", description="Apply Modifiers to the exported mesh", default=True)
use_normals = BoolProperty(name="Normals", description="Export Normals for smooth and hard shaded faces", default=True)
use_uvs = BoolProperty(name="UVs", description="Exort the active UV layer", default=True)
@ -281,7 +281,7 @@ class ExportPLY(bpy.types.Operator):
def execute(self, context):
filepath = self.properties.filepath
filepath = bpy.path.ensure_ext(filepath, ".ply")
filepath = bpy.path.ensure_ext(filepath, self.filename_ext)
write(filepath, context.scene, context.active_object,\
EXPORT_APPLY_MODIFIERS=self.properties.use_modifiers,
@ -292,14 +292,6 @@ class ExportPLY(bpy.types.Operator):
return {'FINISHED'}
def invoke(self, context, event):
import os
if not self.properties.is_property_set("filepath"):
self.properties.filepath = os.path.splitext(bpy.data.filepath)[0] + ".ply"
context.manager.add_fileselect(self)
return {'RUNNING_MODAL'}
def draw(self, context):
layout = self.layout
props = self.properties

@ -1164,16 +1164,15 @@ def write(filename,
from bpy.props import *
from io_utils import ExportHelper
class ExportX3D(bpy.types.Operator):
class ExportX3D(bpy.types.Operator, ExportHelper):
'''Export selection to Extensible 3D file (.x3d)'''
bl_idname = "export.x3d"
bl_label = 'Export X3D'
# List of operator properties, the attributes will be assigned
# to the class instance from the operator settings before calling.
filepath = StringProperty(name="File Path", description="Filepath used for exporting the X3D file", maxlen= 1024, default= "")
check_existing = BoolProperty(name="Check Existing", description="Check and warn on overwriting existing files", default=True, options={'HIDDEN'})
filename_ext = ".x3d"
apply_modifiers = BoolProperty(name="Apply Modifiers", description="Use transformed mesh data from each object", default=True)
triangulate = BoolProperty(name="Triangulate", description="Triangulate quads.", default=False)
@ -1181,7 +1180,7 @@ class ExportX3D(bpy.types.Operator):
def execute(self, context):
filepath = self.properties.filepath
filepath = bpy.path.ensure_ext(filepath, ".x3d")
filepath = bpy.path.ensure_ext(filepath, self.filename_ext)
write(filepath,
context,
@ -1192,14 +1191,6 @@ class ExportX3D(bpy.types.Operator):
return {'FINISHED'}
def invoke(self, context, event):
import os
if not self.properties.is_property_set("filepath"):
self.properties.filepath = os.path.splitext(bpy.data.filepath)[0] + ".x3d"
context.manager.add_fileselect(self)
return {'RUNNING_MODAL'}
def menu_func(self, context):
self.layout.operator(ExportX3D.bl_idname, text="X3D Extensible 3D (.x3d)")

@ -554,14 +554,16 @@ def bvh_node_dict2armature(context, bvh_nodes, ROT_MODE='XYZ', IMPORT_START_FRAM
from bpy.props import *
from io_utils import ImportHelper
class BvhImporter(bpy.types.Operator):
class BvhImporter(bpy.types.Operator, ImportHelper):
'''Load a OBJ Motion Capture File'''
bl_idname = "import_anim.bvh"
bl_label = "Import BVH"
filepath = StringProperty(name="File Path", description="Filepath used for importing the OBJ file", maxlen=1024, default="")
filename_ext = ".bvh"
scale = FloatProperty(name="Scale", description="Scale the BVH by this value", min=0.0001, max=1000000.0, soft_min=0.001, soft_max=100.0, default=0.1)
frame_start = IntProperty(name="Start Frame", description="Starting frame for the animation", default=1)
loop = BoolProperty(name="Loop", description="Loop the animation playback", default=False)
@ -601,11 +603,6 @@ class BvhImporter(bpy.types.Operator):
print('Done in %.4f\n' % (time.time() - t1))
return {'FINISHED'}
def invoke(self, context, event):
wm = context.manager
wm.add_fileselect(self)
return {'RUNNING_MODAL'}
def menu_func(self, context):
self.layout.operator(BvhImporter.bl_idname, text="Motion Capture (.bvh)")

@ -939,16 +939,15 @@ else:
'''
from bpy.props import *
from io_utils import ImportHelper
class IMPORT_OT_autodesk_3ds(bpy.types.Operator):
class IMPORT_OT_autodesk_3ds(bpy.types.Operator, ImportHelper):
'''Import from 3DS file format (.3ds)'''
bl_idname = "import_scene.autodesk_3ds"
bl_label = 'Import 3DS'
# List of operator properties, the attributes will be assigned
# to the class instance from the operator settings before calling.
filepath = StringProperty(name="File Path", description="Filepath used for importing the 3DS file", maxlen= 1024, default= "")
filename_ext = ".3ds"
constrain_size = FloatProperty(name="Size Constraint", description="Scale the model by 10 until it reacehs the size constraint. Zero Disables.", min=0.0, max=1000.0, soft_min=0.0, soft_max=1000.0, default=10.0)
search_images = BoolProperty(name="Image Search", description="Search subdirectories for any assosiated images (Warning, may be slow)", default=True)
@ -963,11 +962,6 @@ class IMPORT_OT_autodesk_3ds(bpy.types.Operator):
return {'FINISHED'}
def invoke(self, context, event):
wm = context.manager
wm.add_fileselect(self)
return {'RUNNING_MODAL'}
def menu_func(self, context):
self.layout.operator(IMPORT_OT_autodesk_3ds.bl_idname, text="3D Studio (.3ds)")

@ -1455,17 +1455,15 @@ else:
'''
from bpy.props import *
from io_utils import ImportHelper
class IMPORT_OT_obj(bpy.types.Operator):
class IMPORT_OT_obj(bpy.types.Operator, ImportHelper):
'''Load a Wavefront OBJ File'''
bl_idname = "import_scene.obj"
bl_label = "Import OBJ"
# List of operator properties, the attributes will be assigned
# to the class instance from the operator settings before calling.
filepath = StringProperty(name="File Path", description="Filepath used for importing the OBJ file", maxlen= 1024, default= "", subtype='FILE_PATH')
filename_ext = ".obj"
CREATE_SMOOTH_GROUPS = BoolProperty(name="Smooth Groups", description="Surround smooth groups by sharp edges", default= True)
CREATE_FGONS = BoolProperty(name="NGons as FGons", description="Import faces with more then 4 verts as fgons", default= True)
@ -1498,10 +1496,6 @@ class IMPORT_OT_obj(bpy.types.Operator):
return {'FINISHED'}
def invoke(self, context, event):
context.manager.add_fileselect(self)
return {'RUNNING_MODAL'}
def menu_func(self, context):
self.layout.operator(IMPORT_OT_obj.bl_idname, text="Wavefront (.obj)")

@ -100,25 +100,16 @@ def mdd_import(filepath, ob, scene, PREF_START_FRAME=0, PREF_JUMP=1):
from bpy.props import *
from io_utils import ImportHelper
class importMDD(bpy.types.Operator):
class importMDD(bpy.types.Operator, ImportHelper):
'''Import MDD vertex keyframe file to shape keys'''
bl_idname = "import_shape.mdd"
bl_label = "Import MDD"
# get first scene to get min and max properties for frames, fps
minframe = 1
maxframe = 300000
minfps = 1
maxfps = 120
# List of operator properties, the attributes will be assigned
# to the class instance from the operator settings before calling.
filepath = StringProperty(name="File Path", description="Filepath used for importing the MDD file", maxlen=1024)
#fps = IntProperty(name="Frames Per Second", description="Number of frames/second", min=minfps, max=maxfps, default=25)
frame_start = IntProperty(name="Start Frame", description="Start frame for inserting animation", min=minframe, max=maxframe, default=0)
filename_ext = ".mdd"
frame_start = IntProperty(name="Start Frame", description="Start frame for inserting animation", min=-300000, max=300000, default=0)
@classmethod
def poll(cls, context):
@ -133,11 +124,6 @@ class importMDD(bpy.types.Operator):
return {'FINISHED'}
def invoke(self, context, event):
wm = context.manager
wm.add_fileselect(self)
return {'RUNNING_MODAL'}
def menu_func(self, context):
self.layout.operator(importMDD.bl_idname, text="Lightwave Point Cache (.mdd)")

@ -0,0 +1,68 @@
# ##### 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
# <pep8 compliant>
from bpy.props import *
class ExportHelper:
filepath = StringProperty(name="File Path", description="Filepath used for exporting the file", maxlen= 1024, default= "", subtype='FILE_PATH')
check_existing = BoolProperty(name="Check Existing", description="Check and warn on overwriting existing files", default=True, options={'HIDDEN'})
def invoke(self, context, event):
import os
if not self.properties.is_property_set("filepath"):
self.properties.filepath = os.path.splitext(context.main.filepath)[0] + self.file_extension
context.manager.add_fileselect(self)
return {'RUNNING_MODAL'}
class ImportHelper:
filepath = StringProperty(name="File Path", description="Filepath used for importing the file", maxlen= 1024, default= "", subtype='FILE_PATH')
def invoke(self, context, event):
wm = context.manager
wm.add_fileselect(self)
return {'RUNNING_MODAL'}
def unpack_list(list_of_tuples):
flat_list = []
flat_list_extend = flat_list.extend # a tich faster
for t in list_of_tuples:
flat_list_extend(t)
return l
# same as above except that it adds 0 for triangle faces
def unpack_face_list(list_of_tuples):
# allocate the entire list
flat_ls = [0] * (len(list_of_tuples) * 4)
i = 0
for t in list_of_tuples:
if len(t) == 3:
if t[2] == 0:
t = t[1], t[2], t[0]
else: # assuem quad
if t[3] == 0 or t[2] == 0:
t = t[2], t[3], t[0], t[1]
flat_ls[i:i + len(t)] = t
i += 4
return flat_ls

@ -27,7 +27,6 @@ class FILEBROWSER_HT_header(bpy.types.Header):
layout = self.layout
st = context.space_data
params = st.params
layout.template_header(menus=False)
@ -46,6 +45,10 @@ class FILEBROWSER_HT_header(bpy.types.Header):
row = layout.row(align=True)
row.operator("file.directory_new", text="", icon='NEWFOLDER')
params = st.params
# can be None when save/reload with a file selector open
if params:
layout.prop(params, "display_type", expand=True, text="")
layout.prop(params, "sort_method", expand=True, text="")