Permalink
Browse files

refactored scene export, added simple test case to check if scene exp…

…ort is working
  • Loading branch information...
Richard Plangger
Richard Plangger committed Aug 25, 2014
1 parent b593e26 commit cb95d455de3713c97504c8940f8a3378fe1a8ae3
Showing with 143 additions and 195 deletions.
  1. +0 −7 io_ogre/__init__.py
  2. +8 −8 io_ogre/config.py
  3. +8 −9 io_ogre/console.py
  4. +12 −42 io_ogre/ogre/export.py
  5. +28 −61 io_ogre/ogre/material.py
  6. +18 −11 io_ogre/ogre/mesh.py
  7. +44 −47 io_ogre/ogre/scene.py
  8. +5 −1 io_ogre/util.py
  9. +9 −6 test/base_test.py
  10. +11 −3 test/test_scene.py
View
@@ -48,10 +48,3 @@ def unregister():
if __name__ == "__main__":
register()
try:
index = sys.argv.index("--")
from . import auto
auto.export(sys.argv[index+1:])
except ValueError:
pass
View
@@ -13,29 +13,29 @@
CONFIG_FILEPATH = os.path.join(CONFIG_PATH, CONFIG_FILENAME)
_CONFIG_DEFAULTS_ALL = {
'TUNDRA_STREAMING' : True,
'MESH' : True,
'SCENE' : True,
'COPY_SHADER_PROGRAMS' : True,
'MAX_TEXTURE_SIZE' : 4096,
'SWAP_AXIS' : 'xz-y', # ogre standard
'ONLY_DEFORMABLE_BONES' : False,
'ONLY_KEYFRAMED_BONES' : False,
'OGRE_INHERIT_SCALE' : False,
'FORCE_IMAGE_FORMAT' : 'NONE',
'TOUCH_TEXTURES' : True,
'SEP_MATS' : True,
'SCENE' : True,
'SELONLY' : True,
'EXPORT_HIDDEN' : True,
'FORCE_CAMERA' : True,
'FORCE_LAMPS' : True,
'MESH' : True,
'MESH_OVERWRITE' : True,
'ONLY_DEFORMABLE_BONES' : False,
'ONLY_KEYFRAMED_BONES' : False,
'OGRE_INHERIT_SCALE' : False,
'FORCE_IMAGE_FORMAT' : 'NONE',
'TOUCH_TEXTURES' : True,
'ARM_ANIM' : True,
'SHAPE_ANIM' : True,
'ARRAY' : True,
'MATERIALS' : True,
'DDS_MIPS' : True,
'TRIM_BONE_WEIGHTS' : 0.01,
'TUNDRA_STREAMING' : True,
'lodLevels' : 0,
'lodDistance' : 300,
'lodPercent' : 40,
View
@@ -1,11 +1,9 @@
import bpy
import sys
import os
import re
from os.path import join, split
#sys.path.append(split(os.getcwd())[0])
#print(sys.path)
def export(object_names):
for name in object_names:
index = bpy.data.objects.find(name)
@@ -22,13 +20,14 @@ def export(object_names):
io_ogre_path = sys.argv[idx+1]
sys.path.append(io_ogre_path)
print(sys.path)
from ogre.scene import dot_scene
from ogre.mesh import dot_mesh
from ogre.mesh import dot_skeleton
from io_ogre import config
from io_ogre.ogre.scene import dot_scene
from io_ogre.ogre.mesh import dot_mesh
from io_ogre.ogre.skeleton import dot_skeleton
if argv[0] == "scene":
scene_name = argv[1]
match = re.compile("scene (.*)").match(argv[0])
if match:
scene_name = match.group(1)
dot_scene(path, scene_name=scene_name)
View
@@ -49,8 +49,10 @@ def invoke(self, context, event):
elif self.EXPORT_TYPE == "REX":
self.filepath = self.filepath.replace(".scene", ".txml")
# Update ui setting from the last export, or file config.
self.update_ui()
# update the interface with the config values
for key, value in config.CONFIG.items():
if getattr(self,"EX_" + key,None):
setattr(self,key,value)
wm = context.window_manager
fs = wm.fileselect_add(self) # writes to filepath
@@ -65,40 +67,6 @@ def execute(self, context):
self.ogre_export(self.filepath, context)
return {'FINISHED'}
def update_ui(self):
self.EX_SWAP_AXIS = config.get('SWAP_AXIS')
self.EX_SEP_MATS = config.get('SEP_MATS')
self.EX_ONLY_DEFORMABLE_BONES = config.get('ONLY_DEFORMABLE_BONES')
self.EX_ONLY_KEYFRAMED_BONES = config.get('ONLY_KEYFRAMED_BONES')
self.EX_OGRE_INHERIT_SCALE = config.get('OGRE_INHERIT_SCALE')
self.EX_SCENE = config.get('SCENE')
self.EX_EXPORT_HIDDEN = config.get('EXPORT_HIDDEN')
self.EX_SELONLY = config.get('SELONLY')
self.EX_FORCE_CAMERA = config.get('FORCE_CAMERA')
self.EX_FORCE_LAMPS = config.get('FORCE_LAMPS')
self.EX_MESH = config.get('MESH')
self.EX_MESH_OVERWRITE = config.get('MESH_OVERWRITE')
self.EX_ARM_ANIM = config.get('ARM_ANIM')
self.EX_SHAPE_ANIM = config.get('SHAPE_ANIM')
self.EX_TRIM_BONE_WEIGHTS = config.get('TRIM_BONE_WEIGHTS')
self.EX_ARRAY = config.get('ARRAY')
self.EX_MATERIALS = config.get('MATERIALS')
self.EX_FORCE_IMAGE_FORMAT = config.get('FORCE_IMAGE_FORMAT')
self.EX_DDS_MIPS = config.get('DDS_MIPS')
self.EX_COPY_SHADER_PROGRAMS = config.get('COPY_SHADER_PROGRAMS')
self.EX_lodLevels = config.get('lodLevels')
self.EX_lodDistance = config.get('lodDistance')
self.EX_lodPercent = config.get('lodPercent')
self.EX_nuextremityPoints = config.get('nuextremityPoints')
self.EX_generateEdgeLists = config.get('generateEdgeLists')
self.EX_generateTangents = config.get('generateTangents')
self.EX_tangentSemantic = config.get('tangentSemantic')
self.EX_tangentUseParity = config.get('tangentUseParity')
self.EX_tangentSplitMirrored = config.get('tangentSplitMirrored')
self.EX_tangentSplitRotated = config.get('tangentSplitRotated')
self.EX_reorganiseBuffers = config.get('reorganiseBuffers')
self.EX_optimiseAnimations = config.get('optimiseAnimations')
# Basic options
EX_SWAP_AXIS = EnumProperty(
items=config.AXIS_MODES,
@@ -379,17 +347,19 @@ def update_ui(self):
default="",
subtype='FILE_PATH')
def ogre_export(self, url, context)
print ("_"*80)
target_path, target_file_name = os.path.split(url)
target_file_name_no_ext = os.path.splitext(target_file_name)[0]
def ogre_export(self, url, context):
# Updating config to latest values?
kw = {}
for name in dir(self):
if name.startswith('EX_'):
kw[ name[3:] ] = getattr(self,name)
config.update(**kw)
print ("_"*80)
target_path, target_file_name = os.path.split(url)
target_file_name_no_ext = os.path.splitext(target_file_name)[0]
Report.reset()
scene.dot_scene(target_path, target_file_name_no_ext)
Report.show()
View
@@ -9,13 +9,12 @@
import tempfile
import shutil
def dot_materials(self, materials, path=None, separate_files=True):
def dot_materials(materials, path=None, separate_files=True):
"""
generate material files, or copy them into a single file
path: string - or None if one must use a temp file
separate_files: bool - each material gets it's own filename
"""
if not materials:
print('WARNING: no materials, not writting .material script')
@@ -24,51 +23,26 @@ def dot_materials(self, materials, path=None, separate_files=True):
if not path:
path = tempfile.mkdtemp(prefix='ogre_io')
M = MISSING_MATERIAL + '\n'
for mat in materials:
if mat is None:
continue
Report.materials.append( material_name(mat) )
data = generate_material( mat, path=path,
copy_programs=config.get('COPY_SHADER_PROGRAMS'),
touch_textures=config.get('TOUCH_TEXTURES') )
M += data
# Write own .material file per material
if separate_files:
url = self.dot_material_write_separate( mat, data, path )
material_files.append(url)
# Write one .material file for everything
if not separate_files:
try:
url = os.path.join(path, '%s.material' % mat_file_name)
with open(url, 'wb') as fd:
fd.write(bytes(M,'utf-8'))
print(' - Created material:', url)
material_files.append( url )
except Exception as e:
show_dialog("Invalid material object name: " + mat_file_name)
return material_files
def dot_material_write_separate( self, mat, data, path = '/tmp' ):
try:
clean_filename = clean_object_name(mat.name);
url = os.path.join(path, '%s.material' % clean_filename)
f = open(url, 'wb'); f.write( bytes(data,'utf-8') ); f.close()
print(' - Exported Material:', url)
return url
except Exception as e:
show_dialog("Invalid material object name: " + clean_filename)
return ""
def dot_material(obj, path, **kwargs):
if separate_files:
for mat in materials:
dot_material(mat, path, copy_programs=config.get('COPY_SHADER_PROGRAMS'),
touch_textures=config.get('TOUCH_TEXTURES'))
else:
target_file = os.path.join(path, '%s.material' % mat_file_name)
with open(target_file, 'wb') as fd:
fd.write(bytes(MISSING_MATERIAL + "\n",'utf-8'))
for mat in materials:
if mat is None:
continue
Report.materials.append( material_name(mat) )
data = generate_material( mat, path=path, copy_programs=config.get('COPY_SHADER_PROGRAMS'),
touch_textures=config.get('TOUCH_TEXTURES') )
fd.write(bytes(data+"\n",'utf-8'))
def dot_material(material, path, **kwargs):
"""
write the material file of a
obj: a blender object that has a mesh
material: a blender material
path: target directory to save the file to
kwargs:
@@ -77,18 +51,12 @@ def dot_material(obj, path, **kwargs):
* touch_textures - bool. Copy the images along to the material files.
"""
prefix = kwargs.get('prefix', '')
for material in obj.data.materials:
material_text = generate_material(material, path, **kwargs)
mat_name = material_name(material, prefix=prefix) + '.material'
with open(join(path, mat_name), 'wb') as fd:
fd.write(bytes(material_text,'utf-8'))
yield mat_name
if kwargs.get('copy_materials', False):
_copy_materials(obj.data.materials, path)
def _copy_materials(materials, path):
pass
material_text = generate_material(material, path, **kwargs)
mat_name = material_name(material, prefix=prefix) + '.material'
with open(join(path, mat_name), 'wb') as fd:
fd.write(bytes(material_text,'utf-8'))
return mat_name
# Make default material for missing materials:
# * Red flags for users so they can quickly see what they forgot to assign a material to.
# * Do not crash if no material on object - thats annoying for the user.
@@ -141,8 +109,6 @@ def load_user_materials():
scripts,progs = update_parent_material_path( config.get('USER_MATERIALS') )
for prog in progs:
logging.info('Ogre shader program', prog.name)
#else:
# logging.warn('Invalid my-shaders path %s' % config.get('USER_MATERIALS'))
def material_name( mat, clean = False, prefix='' ):
@@ -156,10 +122,11 @@ def material_name( mat, clean = False, prefix='' ):
else:
return prefix + clean_object_name(mat.name)
def generate_material(mat, path='/tmp', copy_programs=False, touch_textures=False, **kwargs):
def generate_material(mat, path, **kwargs):
''' returns generated material string '''
prefix = kwargs.get('prefix','')
copy_programs = kwargs.get('copy_programs', False)
touch_textures = kwargs.get('touch_textures', False)
safename = material_name(mat,prefix=prefix) # supports blender library linking
w = util.IndentedWriter()
w.line('// %s generated by blender2ogre %s' % (mat.name, datetime.now())).nl()
View
@@ -15,16 +15,27 @@ def dot_mesh( ob, path, force_name=None, ignore_shape_animation=False, normals=T
force_name: force a different name for this .mesh
kwargs:
* material_prefix - string. (optional)
* overwrite - bool. (optional) default False
"""
start = time.time()
name = force_name or ob.data.name
name = clean_object_name(name)
target_file = os.path.join(path, '%s.mesh.xml' % name )
material_prefix = kwargs.get('material_prefix', '')
overwrite = kwargs.get('overwrite', False)
if os.path.isfile(target_file) and not overwrite:
return []
# TODO ensure that before every call the path exists. there are very view calls I believe
if not os.path.isdir( path ):
logging.info('>> Creating working directory %s', path )
os.makedirs( path )
start = time.time()
# blender per default does not calculate these. when querying the quads/tris
# of the object blender would crash if calc_tessface was not updated
ob.data.update(calc_tessface=True)
Report.meshes.append( ob.data.name )
Report.faces += len( ob.data.tessfaces )
Report.orig_vertices += len( ob.data.vertices )
@@ -44,21 +55,17 @@ def dot_mesh( ob, path, force_name=None, ignore_shape_animation=False, normals=T
copy = ob
mesh = ob.data
name = force_name or ob.data.name
name = clean_object_name(name)
xmlfile = os.path.join(path, '%s.mesh.xml' % name )
if logging:
print(' - Generating:', '%s.mesh.xml' % name)
try:
with open(xmlfile, 'w') as f:
with open(target_file, 'w') as f:
f.flush()
except Exception as e:
show_dialog("Invalid mesh object name: " + name)
return
with open(xmlfile, 'w') as f:
with open(target_file, 'w') as f:
doc = SimpleSaxWriter(f, 'mesh', {})
# Very ugly, have to replace number of vertices later
@@ -576,11 +583,11 @@ def replaceInplace(f,searchExp,replaceExp):
sys.stdout.write(line)
fileinput.close() # reported by jakob
replaceInplace(xmlfile, '__TO_BE_REPLACED_VERTEX_COUNT__' + '"', str(numverts) + '"' )#+ ' ' * (ls - lr))
replaceInplace(target_file, '__TO_BE_REPLACED_VERTEX_COUNT__' + '"', str(numverts) + '"' )#+ ' ' * (ls - lr))
del(replaceInplace)
# Start .mesh.xml to .mesh convertion tool
OgreXMLConverter(xmlfile, has_uvs=dotextures)
OgreXMLConverter(target_file, has_uvs=dotextures)
# note that exporting the skeleton does not happen here anymore
# it moved to the function dot_skeleton in its own module
Oops, something went wrong.

0 comments on commit cb95d45

Please sign in to comment.