- OBJ exporter now copies images

- added OBJ, FBX scripts to File -> Export, File -> Import menus
This commit is contained in:
Arystanbek Dyussenov 2009-08-05 09:49:59 +00:00
parent 7586990ace
commit bf90970eca
3 changed files with 58 additions and 43 deletions

@ -1323,6 +1323,19 @@ def write(filename, batch_objects = None, \
file.write('\n\t\t}') file.write('\n\t\t}')
file.write('\n\t}') file.write('\n\t}')
def copy_image(image):
rel = image.get_export_path(basepath, True)
base = os.path.basename(fname_rel)
if EXP_IMAGE_COPY:
src = bpy.sys.expandpath(image.filename)
absp = image.get_export_path(basepath, False)
if not os.path.exists(absp):
shutil.copy(src, absp)
return (rel, base)
# tex is an Image (Arystan) # tex is an Image (Arystan)
def write_video(texname, tex): def write_video(texname, tex):
# Same as texture really! # Same as texture really!
@ -1336,15 +1349,7 @@ def write(filename, batch_objects = None, \
Property: "Width", "int", "",0 Property: "Width", "int", "",0
Property: "Height", "int", "",0''') Property: "Height", "int", "",0''')
if tex: if tex:
src = bpy.sys.expandpath(tex.filename) fname_rel, fname_strip = copy_image(tex)
fname_rel = tex.get_export_path(basepath, True)
fname_abs = tex.get_export_path(basepath, False)
fname_strip = os.path.basename(fname_rel)
if EXP_IMAGE_COPY:
if !os.path.exists(fname_abs):
shutil.copy(src, fname_abs)
# fname, fname_strip, fname_rel = derived_paths(tex.filename, basepath, EXP_IMAGE_COPY) # fname, fname_strip, fname_rel = derived_paths(tex.filename, basepath, EXP_IMAGE_COPY)
else: else:
fname = fname_strip = fname_rel = '' fname = fname_strip = fname_rel = ''
@ -1409,15 +1414,7 @@ def write(filename, batch_objects = None, \
file.write('\n\t\tMedia: "Video::%s"' % texname) file.write('\n\t\tMedia: "Video::%s"' % texname)
if tex: if tex:
src = bpy.sys.expandpath(tex.filename) fname_rel, fname_strip = copy_image(tex)
fname_rel = tex.get_export_path(basepath, True)
fname_abs = tex.get_export_path(basepath, False)
fname_strip = os.path.basename(fname_rel)
if EXP_IMAGE_COPY:
if !os.path.exists(fname_abs):
shutil.copy(src, fname_abs)
# fname, fname_strip, fname_rel = derived_paths(tex.filename, basepath, EXP_IMAGE_COPY) # fname, fname_strip, fname_rel = derived_paths(tex.filename, basepath, EXP_IMAGE_COPY)
else: else:
fname = fname_strip = fname_rel = '' fname = fname_strip = fname_rel = ''

@ -47,7 +47,7 @@ will be exported as mesh data.
# import math and other in functions that use them for the sake of fast Blender startup # import math and other in functions that use them for the sake of fast Blender startup
# import math # import math
import os # os.sep import os
import bpy import bpy
import Mathutils import Mathutils
@ -85,11 +85,24 @@ def BPySys_cleanName(name):
# (material.name, image.name):matname_imagename # matname_imagename has gaps removed. # (material.name, image.name):matname_imagename # matname_imagename has gaps removed.
MTL_DICT = {} MTL_DICT = {}
def write_mtl(scene, filename): def write_mtl(scene, filename, copy_images):
world = scene.world world = scene.world
worldAmb = world.ambient_color worldAmb = world.ambient_color
dest_dir = os.path.dirname(filename)
def copy_image(image):
rel = image.get_export_path(dest_dir, True)
if copy_images:
abspath = image.get_export_path(dest_dir, False)
if not os.path.exists(abs_path):
shutil.copy(bpy.sys.expandpath(image.filename), abs_path)
return rel
file = open(filename, "w") file = open(filename, "w")
# XXX # XXX
# file.write('# Blender3D MTL File: %s\n' % Blender.Get('filename').split('\\')[-1].split('/')[-1]) # file.write('# Blender3D MTL File: %s\n' % Blender.Get('filename').split('\\')[-1].split('/')[-1])
@ -129,13 +142,17 @@ def write_mtl(scene, filename):
# Write images! # Write images!
if img: # We have an image on the face! if img: # We have an image on the face!
file.write('map_Kd %s\n' % img.filename.split('\\')[-1].split('/')[-1]) # Diffuse mapping image # write relative image path
rel = copy_image(img)
file.write('map_Kd %s\n' % rel) # Diffuse mapping image
# file.write('map_Kd %s\n' % img.filename.split('\\')[-1].split('/')[-1]) # Diffuse mapping image
elif mat: # No face image. if we havea material search for MTex image. elif mat: # No face image. if we havea material search for MTex image.
for mtex in mat.textures: for mtex in mat.textures:
if mtex and mtex.texure.type == 'IMAGE': if mtex and mtex.texure.type == 'IMAGE':
try: try:
filename = mtex.texture.image.filename.split('\\')[-1].split('/')[-1] filename = copy_image(mtex.texture.image)
# filename = mtex.texture.image.filename.split('\\')[-1].split('/')[-1]
file.write('map_Kd %s\n' % filename) # Diffuse mapping image file.write('map_Kd %s\n' % filename) # Diffuse mapping image
break break
except: except:
@ -146,6 +163,7 @@ def write_mtl(scene, filename):
file.close() file.close()
# XXX not used
def copy_file(source, dest): def copy_file(source, dest):
file = open(source, 'rb') file = open(source, 'rb')
data = file.read() data = file.read()
@ -156,6 +174,7 @@ def copy_file(source, dest):
file.close() file.close()
# XXX not used
def copy_images(dest_dir): def copy_images(dest_dir):
if dest_dir[-1] != os.sep: if dest_dir[-1] != os.sep:
dest_dir += os.sep dest_dir += os.sep
@ -181,7 +200,7 @@ def copy_images(dest_dir):
pass pass
# Now copy images # Now copy images
# copyCount = 0 copyCount = 0
# for bImage in uniqueImages.values(): # for bImage in uniqueImages.values():
# image_path = bpy.sys.expandpath(bImage.filename) # image_path = bpy.sys.expandpath(bImage.filename)
@ -193,7 +212,7 @@ def copy_images(dest_dir):
# copy_file(image_path, dest_image_path) # copy_file(image_path, dest_image_path)
# copyCount+=1 # copyCount+=1
paths= bpy.util.copy_images(uniqueImages.values(), dest_dir) # paths= bpy.util.copy_images(uniqueImages.values(), dest_dir)
print('\tCopied %d images' % copyCount) print('\tCopied %d images' % copyCount)
# print('\tCopied %d images' % copyCount) # print('\tCopied %d images' % copyCount)
@ -412,16 +431,11 @@ def write(filename, objects, scene,
if ob.type != 'MESH': if ob.type != 'MESH':
continue continue
if EXPORT_APPLY_MODIFIERS: me = ob.create_mesh(EXPORT_APPLY_MODIFIERS, 'PREVIEW')
me = ob.create_mesh('PREVIEW')
else:
me = ob.data.create_copy()
if EXPORT_ROTX90: if EXPORT_ROTX90:
print(ob_mat * mat_xrot90)
me.transform(ob_mat * mat_xrot90) me.transform(ob_mat * mat_xrot90)
else: else:
print(ob_mat)
me.transform(ob_mat) me.transform(ob_mat)
# # Will work for non meshes now! :) # # Will work for non meshes now! :)
@ -597,7 +611,7 @@ def write(filename, objects, scene,
# file.write('vt %.6f %.6f\n' % tuple(uv)) # file.write('vt %.6f %.6f\n' % tuple(uv))
uv_unique_count = len(uv_dict) uv_unique_count = len(uv_dict)
del uv, uvkey, uv_dict, f_index, uv_index # del uv, uvkey, uv_dict, f_index, uv_index
# Only need uv_unique_count and uv_face_mapping # Only need uv_unique_count and uv_face_mapping
# NORMAL, Smooth/Non smoothed. # NORMAL, Smooth/Non smoothed.
@ -783,16 +797,17 @@ def write(filename, objects, scene,
# Now we have all our materials, save them # Now we have all our materials, save them
if EXPORT_MTL: if EXPORT_MTL:
write_mtl(scene, mtlfilename) write_mtl(scene, mtlfilename, EXPORT_COPY_IMAGES)
if EXPORT_COPY_IMAGES: # if EXPORT_COPY_IMAGES:
dest_dir = filename # dest_dir = os.path.basename(filename)
# Remove chars until we are just the path. # # dest_dir = filename
while dest_dir and dest_dir[-1] not in '\\/': # # # Remove chars until we are just the path.
dest_dir = dest_dir[:-1] # # while dest_dir and dest_dir[-1] not in '\\/':
if dest_dir: # # dest_dir = dest_dir[:-1]
copy_images(dest_dir) # if dest_dir:
else: # copy_images(dest_dir)
print('\tError: "%s" could not be used as a base for an image path.' % filename) # else:
# print('\tError: "%s" could not be used as a base for an image path.' % filename)
print("OBJ Export time: %.2f" % (bpy.sys.time() - time1)) print("OBJ Export time: %.2f" % (bpy.sys.time() - time1))
# print "OBJ Export time: %.2f" % (sys.time() - time1) # print "OBJ Export time: %.2f" % (sys.time() - time1)
@ -898,7 +913,7 @@ class EXPORT_OT_obj(bpy.types.Operator):
bpy.props.StringProperty(attr="filename", name="File Name", description="File name used for exporting the PLY file", maxlen= 1024, default= ""), bpy.props.StringProperty(attr="filename", name="File Name", description="File name used for exporting the PLY file", maxlen= 1024, default= ""),
# context group # context group
bpy.props.BoolProperty(attr="use_selection", name="Selection Only", description="", default= True), bpy.props.BoolProperty(attr="use_selection", name="Selection Only", description="", default= False),
bpy.props.BoolProperty(attr="use_all_scenes", name="All Scenes", description="", default= False), bpy.props.BoolProperty(attr="use_all_scenes", name="All Scenes", description="", default= False),
bpy.props.BoolProperty(attr="use_animation", name="All Animation", description="", default= False), bpy.props.BoolProperty(attr="use_animation", name="All Animation", description="", default= False),

@ -74,6 +74,7 @@ class INFO_MT_file_import(bpy.types.Menu):
def draw(self, context): def draw(self, context):
layout = self.layout layout = self.layout
layout.itemO("import.obj", text="OBJ")
class INFO_MT_file_export(bpy.types.Menu): class INFO_MT_file_export(bpy.types.Menu):
__space_type__ = "USER_PREFERENCES" __space_type__ = "USER_PREFERENCES"
@ -82,6 +83,8 @@ class INFO_MT_file_export(bpy.types.Menu):
def draw(self, context): def draw(self, context):
layout = self.layout layout = self.layout
layout.itemO("export.fbx", text="FBX")
layout.itemO("export.obj", text="OBJ")
layout.itemO("export.ply", text="PLY") layout.itemO("export.ply", text="PLY")
class INFO_MT_file_external_data(bpy.types.Menu): class INFO_MT_file_external_data(bpy.types.Menu):