From eb4a266295c42cdd2f55b094eeed2478391edb4d Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Thu, 14 Apr 2011 08:49:26 +0000 Subject: common option to export paths for FBX and OBJ --- io_scene_fbx/__init__.py | 4 +- io_scene_fbx/export_fbx.py | 101 ++++++++------------------------------------- 2 files changed, 21 insertions(+), 84 deletions(-) (limited to 'io_scene_fbx') diff --git a/io_scene_fbx/__init__.py b/io_scene_fbx/__init__.py index 191f5824..54433a65 100644 --- a/io_scene_fbx/__init__.py +++ b/io_scene_fbx/__init__.py @@ -41,6 +41,7 @@ if "bpy" in locals(): import bpy from bpy.props import StringProperty, BoolProperty, FloatProperty, EnumProperty +import io_utils from io_utils import ExportHelper @@ -69,7 +70,6 @@ class ExportFBX(bpy.types.Operator, ExportHelper): EXP_MESH = BoolProperty(name="Meshes", description="Export mesh objects", default=True) EXP_MESH_APPLY_MOD = BoolProperty(name="Modifiers", description="Apply modifiers to mesh objects", default=True) # EXP_MESH_HQ_NORMALS = BoolProperty(name="HQ Normals", description="Generate high quality normals", default=True) - EXP_IMAGE_COPY = BoolProperty(name="Copy Image Files", description="Copy image files to the destination path", default=False) # armature animation ANIM_ENABLE = BoolProperty(name="Enable Animation", description="Export keyframe animation", default=True) ANIM_OPTIMIZE = BoolProperty(name="Optimize Keyframes", description="Remove double keyframes", default=True) @@ -87,6 +87,8 @@ class ExportFBX(bpy.types.Operator, ExportHelper): BATCH_OWN_DIR = BoolProperty(name="Own Dir", description="Create a dir for each exported file", default=True) use_metadata = BoolProperty(name="Use Metadata", default=True, options={'HIDDEN'}) + path_mode = io_utils.path_reference_mode + @property def check_extension(self): return self.batch_mode == 'OFF' diff --git a/io_scene_fbx/export_fbx.py b/io_scene_fbx/export_fbx.py index c4b03ddb..8c533d82 100644 --- a/io_scene_fbx/export_fbx.py +++ b/io_scene_fbx/export_fbx.py @@ -35,34 +35,6 @@ import bpy from mathutils import Vector, Matrix -# XXX not used anymore, images are copied one at a time -def copy_images(dest_dir, textures): - import shutil - - if not dest_dir.endswith(os.sep): - dest_dir += os.sep - - image_paths = set() - for tex in textures: - image_paths.add(bpy.path.abspath(tex.filepath)) - - # Now copy images - copyCount = 0 - for image_path in image_paths: - if Blender.sys.exists(image_path): - # Make a name for the target path. - dest_image_path = dest_dir + image_path.split('\\')[-1].split('/')[-1] - if not Blender.sys.exists(dest_image_path): # Image isnt already there - print("\tCopying %r > %r" % (image_path, dest_image_path)) - try: - shutil.copy(image_path, dest_image_path) - copyCount += 1 - except: - print("\t\tWarning, file failed to copy, skipping.") - - print('\tCopied %d images' % copyCount) - - # I guess FBX uses degrees instead of radians (Arystan). # Call this function just before writing to FBX. # 180 / math.pi == 57.295779513 @@ -157,25 +129,6 @@ def sane_takename(data): def sane_groupname(data): return sane_name(data, sane_name_mapping_group) -# def derived_paths(fname_orig, basepath, FORCE_CWD=False): -# ''' -# fname_orig - blender path, can be relative -# basepath - fname_rel will be relative to this -# FORCE_CWD - dont use the basepath, just add a ./ to the filepath. -# use when we know the file will be in the basepath. -# ''' -# fname = bpy.path.abspath(fname_orig) -# # fname = Blender.sys.expandpath(fname_orig) -# fname_strip = os.path.basename(fname) -# # fname_strip = strip_path(fname) -# if FORCE_CWD: -# fname_rel = '.' + os.sep + fname_strip -# else: -# fname_rel = bpy.path.relpath(fname, basepath) -# # fname_rel = Blender.sys.relpath(fname, basepath) -# if fname_rel.startswith('//'): fname_rel = '.' + os.sep + fname_rel[2:] -# return fname, fname_strip, fname_rel - def mat4x4str(mat): return '%.15f,%.15f,%.15f,%.15f,%.15f,%.15f,%.15f,%.15f,%.15f,%.15f,%.15f,%.15f,%.15f,%.15f,%.15f,%.15f' % tuple([f for v in mat for f in v]) @@ -257,27 +210,28 @@ def save_single(operator, scene, filepath="", EXP_LAMP=True, EXP_CAMERA=True, EXP_EMPTY=True, - EXP_IMAGE_COPY=False, ANIM_ENABLE=True, ANIM_OPTIMIZE=True, ANIM_OPTIMIZE_PRECISSION=6, ANIM_ACTION_ALL=False, use_metadata=True, + path_mode='AUTO', ): - # testing - mtx_x90 = Matrix.Rotation(math.pi / 2.0, 3, 'X') # used + import io_utils + + mtx_x90 = Matrix.Rotation(math.pi / 2.0, 3, 'X') mtx4_z90 = Matrix.Rotation(math.pi / 2.0, 4, 'Z') if GLOBAL_MATRIX is None: GLOBAL_MATRIX = Matrix() - # end batch support - # Use this for working out paths relative to the export location - basepath = os.path.dirname(filepath) or '.' - basepath += os.sep -# basepath = Blender.sys.dirname(filepath) + base_src = os.path.dirname(bpy.data.filepath) + base_dst = os.path.dirname(filepath) + + # collect images to copy + copy_set = set() # ---------------------------------------------- # storage classes @@ -1139,22 +1093,6 @@ def save_single(operator, scene, filepath="", file.write('\n\t\t}') file.write('\n\t}') - def copy_image(image): - fn = bpy.path.abspath(image.filepath) - fn_strip = os.path.basename(fn) - - if EXP_IMAGE_COPY: - rel = fn_strip - fn_abs_dest = os.path.join(basepath, fn_strip) - if not os.path.exists(fn_abs_dest): - shutil.copy(fn, fn_abs_dest) - elif bpy.path.is_subdir(fn, basepath): - rel = os.path.relpath(fn, basepath) - else: - rel = fn - - return (rel, fn_strip) - # tex is an Image (Arystan) def write_video(texname, tex): # Same as texture really! @@ -1168,10 +1106,10 @@ def save_single(operator, scene, filepath="", Property: "Width", "int", "",0 Property: "Height", "int", "",0''') if tex: - fname_rel, fname_strip = copy_image(tex) -# fname, fname_strip, fname_rel = derived_paths(tex.filepath, basepath, EXP_IMAGE_COPY) + fname_rel = io_utils.path_reference(tex.filepath, base_src, base_dst, path_mode, "", copy_set) + fname_strip = os.path.basename(fname_rel) else: - fname = fname_strip = fname_rel = '' + fname_strip = fname_rel = "" file.write('\n\t\t\tProperty: "Path", "charptr", "", "%s"' % fname_strip) @@ -1188,8 +1126,6 @@ def save_single(operator, scene, filepath="", UseMipMap: 0''') file.write('\n\t\tFilename: "%s"' % fname_strip) - if fname_strip: - fname_strip = '/' + fname_strip file.write('\n\t\tRelativeFilename: "%s"' % fname_rel) # make relative file.write('\n\t}') @@ -1229,10 +1165,10 @@ def save_single(operator, scene, filepath="", file.write('\n\t\tMedia: "Video::%s"' % texname) if tex: - fname_rel, fname_strip = copy_image(tex) -# fname, fname_strip, fname_rel = derived_paths(tex.filepath, basepath, EXP_IMAGE_COPY) + fname_rel = io_utils.path_reference(tex.filepath, base_src, base_dst, path_mode, "", copy_set) + fname_strip = os.path.basename(fname_rel) else: - fname = fname_strip = fname_rel = '' + fname_strip = fname_rel = "" file.write('\n\t\tFileName: "%s"' % fname_strip) file.write('\n\t\tRelativeFilename: "%s"' % fname_rel) # need some make relative command @@ -2810,12 +2746,11 @@ Takes: {''') ob_meshes[:] = [] ob_null[:] = [] - # copy images if enabled -# if EXP_IMAGE_COPY: -# # copy_images( basepath, [ tex[1] for tex in textures if tex[1] != None ]) -# bpy.util.copy_images( [ tex[1] for tex in textures if tex[1] != None ], basepath) file.close() + # copy all collected files. + io_utils.path_reference_copy(copy_set) + print('export finished in %.4f sec.' % (time.clock() - start_time)) return {'FINISHED'} -- cgit v1.2.3