13 Commits

Author SHA1 Message Date
Colin Basnett
89fd3937e5 Initial commit for import functionality 2022-08-08 01:31:31 -07:00
Colin Basnett
e2e3905e2e Fixed a bug where collision meshes would not properly be exported in WYSIWYG mode 2022-05-20 16:40:55 -07:00
Colin Basnett
77f5aa21a5 Removed defunct comment 2022-05-16 17:29:10 -07:00
Colin Basnett
92f588b760 Initial commit for WYSIWYG export. Modifiers are automatically applied. 2022-05-11 17:12:29 -07:00
Colin Basnett
2b467b2da4 Minor formatting fix and comments 2022-05-01 02:05:48 -07:00
Colin Basnett
4b6231a094 Incremented version to 1.1.1 2022-02-09 14:30:15 -08:00
Colin Basnett
50a7d003cb Added manifold and convexity tests for collision objects. You will now be unable to export a concave or non-manifold collision shapes. 2022-02-08 21:42:26 -08:00
Colin Basnett
bd1a7257fd Added support for vertex colors. 2022-01-22 14:42:57 -08:00
Colin Basnett
7ebeb0d262 Added support for exporting multiple UV layers. 2021-12-27 02:07:50 -08:00
Colin Basnett
8edfac9bfb Fixed a bug where exporting more than two mesh objects would result in a corrupted ASE file. 2021-12-16 15:28:52 -08:00
Colin Basnett
1121b18fcb Incremented version. 2021-08-23 23:24:02 -07:00
Colin Basnett
dcd8c3ea65 Fix for bug #2 (missing unit settings on export in Blender 2.93) 2021-08-23 23:22:37 -07:00
Colin Basnett
283a44aec5 Removed zip file from the repository 🤦 2021-08-13 12:54:05 -07:00
11 changed files with 369 additions and 72 deletions

View File

@@ -3,3 +3,6 @@
This is a Blender addon allowing you to export static meshes to the now-defunct ASE (ASCII Scene Export) format still in use in legacy programs like Unreal Tournament 2004.
Check out [this video](https://www.youtube.com/watch?v=gpmBxCGHQjU) on how to install and use the addon.
Resources:
* https://wiki.beyondunreal.com/Legacy:ASE_File_Format

1
requirements.txt Normal file
View File

@@ -0,0 +1 @@
python-ase

View File

@@ -2,7 +2,7 @@ bl_info = {
'name': 'ASCII Scene Export',
'description': 'Export ASE (ASCII Scene Export) files',
'author': 'Colin Basnett (Darklight Games)',
'version': (1, 0, 1),
'version': (1, 1, 2),
'blender': (2, 90, 0),
'location': 'File > Import-Export',
'warning': 'This add-on is under development.',
@@ -12,40 +12,204 @@ bl_info = {
'category': 'Import-Export'
}
if 'bpy' in locals():
import subprocess
import os
import sys
from collections import namedtuple
import bpy
def install_pip():
"""
Installs pip if not already present. Please note that ensurepip.bootstrap() also calls pip, which adds the
environment variable PIP_REQ_TRACKER. After ensurepip.bootstrap() finishes execution, the directory doesn't exist
anymore. However, when subprocess is used to call pip, in order to install a package, the environment variables
still contain PIP_REQ_TRACKER with the now nonexistent path. This is a problem since pip checks if PIP_REQ_TRACKER
is set and if it is, attempts to use it as temp directory. This would result in an error because the
directory can't be found. Therefore, PIP_REQ_TRACKER needs to be removed from environment variables.
:return:
"""
try:
# Check if pip is already installed
subprocess.run([sys.executable, '-m', 'pip', '--version'], check=True)
except subprocess.CalledProcessError:
import ensurepip
ensurepip.bootstrap()
os.environ.pop('PIP_REQ_TRACKER', None)
def install_and_import_module(module_name, package_name=None, global_name=None):
"""
Installs the package through pip and attempts to import the installed module.
:param module_name: Module to import.
:param package_name: (Optional) Name of the package that needs to be installed. If None it is assumed to be equal
to the module_name.
:param global_name: (Optional) Name under which the module is imported. If None the module_name will be used.
This allows to import under a different name with the same effect as e.g. "import numpy as np" where "np" is
the global_name under which the module can be accessed.
:raises: subprocess.CalledProcessError and ImportError
"""
if package_name is None:
package_name = module_name
if global_name is None:
global_name = module_name
# Blender disables the loading of user site-packages by default. However, pip will still check them to determine
# if a dependency is already installed. This can cause problems if the packages is installed in the user
# site-packages and pip deems the requirement satisfied, but Blender cannot import the package from the user
# site-packages. Hence, the environment variable PYTHONNOUSERSITE is set to disallow pip from checking the user
# site-packages. If the package is not already installed for Blender's Python interpreter, it will then try to.
# The paths used by pip can be checked with `subprocess.run([bpy.app.binary_path_python, "-m", "site"], check=True)`
# Create a copy of the environment variables and modify them for the subprocess call
environ_copy = dict(os.environ)
environ_copy['PYTHONNOUSERSITE'] = '1'
subprocess.run([sys.executable, '-m', 'pip', 'install', package_name], check=True, env=environ_copy)
# The installation succeeded, attempt to import the module again
import_module(module_name, global_name)
class EXAMPLE_OT_install_dependencies(bpy.types.Operator):
bl_idname = 'io_scene_ase.install_dependencies'
bl_label = 'Install dependencies'
bl_description = ('Downloads and installs the required python packages for this add-on. '
'Internet connection is required. Blender may have to be started with '
'elevated permissions in order to install the package')
bl_options = {'REGISTER', 'INTERNAL'}
@classmethod
def poll(self, context):
# Deactivate when dependencies have been installed
return not dependencies_installed
def execute(self, context):
try:
install_pip()
for dependency in dependencies:
install_and_import_module(module_name=dependency.module,
package_name=dependency.package,
global_name=dependency.name)
except (subprocess.CalledProcessError, ImportError) as err:
self.report({'ERROR'}, str(err))
return {'CANCELLED'}
global dependencies_installed
dependencies_installed = True
# Register the panels, operators, etc. since dependencies are installed
for cls in classes:
bpy.utils.register_class(cls)
return {'FINISHED'}
class EXAMPLE_preferences(bpy.types.AddonPreferences):
bl_idname = __name__
def draw(self, context):
layout = self.layout
layout.operator(EXAMPLE_OT_install_dependencies.bl_idname, icon='CONSOLE')
Dependency = namedtuple('Dependency', ['module', 'package', 'name'])
dependencies = (Dependency(module='asepy', package=None, name=None),)
dependencies_installed = False
def import_module(module_name, global_name=None, reload=True):
"""
Import a module.
:param module_name: Module to import.
:param global_name: (Optional) Name under which the module is imported. If None the module_name will be used.
This allows to import under a different name with the same effect as e.g. "import numpy as np" where "np" is
the global_name under which the module can be accessed.
:raises: ImportError and ModuleNotFoundError
"""
if global_name is None:
global_name = module_name
if global_name in globals():
importlib.reload(globals()[global_name])
else:
# Attempt to import the module and assign it to globals dictionary. This allow to access the module under
# the given name, just like the regular import would.
globals()[global_name] = importlib.import_module(module_name)
preference_classes = (EXAMPLE_OT_install_dependencies,
EXAMPLE_preferences)
if __name__ == 'io_scene_ase':
if 'bpy' in locals():
import importlib
if 'ase' in locals(): importlib.reload(ase)
if 'builder' in locals(): importlib.reload(builder)
if 'writer' in locals(): importlib.reload(writer)
if 'exporter' in locals(): importlib.reload(exporter)
if 'reader' in locals(): importlib.reload(reader)
import bpy
import bpy.utils.previews
from bpy.props import IntProperty, CollectionProperty, StringProperty
import os
from . import ase
from . import builder
from . import writer
from . import exporter
import bpy
import bpy.utils.previews
from . import ase
from . import builder
from . import writer
from . import exporter
classes = (
print('dependencies installed??')
print(dependencies_installed)
if dependencies_installed:
from . import reader
classes = (
exporter.ASE_OT_ExportOperator,
)
)
if dependencies_installed:
classes += reader.classes
def menu_func_export(self, context):
def menu_func_export(self, context):
self.layout.operator(exporter.ASE_OT_ExportOperator.bl_idname, text='ASCII Scene Export (.ase)')
def menu_func_import(self, context):
self.layout.operator(reader.ASE_OT_ImportOperator.bl_idname, text='ASCII Scene Export (.ase)')
def register():
global dependencies_installed
for cls in preference_classes:
bpy.utils.register_class(cls)
try:
for dependency in dependencies:
import_module(module_name=dependency.module, global_name=dependency.name)
dependencies_installed = True
except ModuleNotFoundError:
# Don't register other panels, operators etc.
return
def register():
for cls in classes:
bpy.utils.register_class(cls)
bpy.types.TOPBAR_MT_file_export.append(menu_func_export)
if 'reader' in locals():
bpy.types.TOPBAR_MT_file_import.append(menu_func_import)
def unregister():
def unregister():
bpy.types.TOPBAR_MT_file_export.remove(menu_func_export)
if 'reader' in locals():
bpy.types.TOPBAR_MT_file_import.remove(menu_func_import)
for cls in classes:
bpy.utils.unregister_class(cls)

View File

@@ -26,14 +26,20 @@ def is_collision_name(name):
return name.startswith('MCDCX_')
class ASEUVLayer(object):
def __init__(self):
self.texture_vertices = []
class ASEGeometryObject(object):
def __init__(self):
self.name = ''
self.vertices = []
self.texture_vertices = []
self.uv_layers = []
self.faces = []
self.texture_vertex_faces = []
self.face_normals = []
self.vertex_colors = []
self.vertex_offset = 0
self.texture_vertex_offset = 0
@@ -46,4 +52,3 @@ class ASE(object):
def __init__(self):
self.materials = []
self.geometry_objects = []

View File

@@ -12,6 +12,7 @@ class ASEBuilderError(Exception):
class ASEBuilderOptions(object):
def __init__(self):
self.scale = 1.0
self.use_raw_mesh_data = False
class ASEBuilder(object):
@@ -19,34 +20,57 @@ class ASEBuilder(object):
ase = ASE()
main_geometry_object = None
for obj in context.selected_objects:
if obj is None or obj.type != 'MESH':
for selected_object in context.selected_objects:
if selected_object is None or selected_object.type != 'MESH':
continue
mesh_data = obj.data
# Evaluate the mesh after modifiers are applied
if options.use_raw_mesh_data:
mesh_object = selected_object
mesh_data = mesh_object.data
else:
depsgraph = context.evaluated_depsgraph_get()
bm = bmesh.new()
bm.from_object(selected_object, depsgraph)
mesh_data = bpy.data.meshes.new('')
bm.to_mesh(mesh_data)
del bm
mesh_object = bpy.data.objects.new('', mesh_data)
mesh_object.matrix_world = selected_object.matrix_world
if not is_collision_name(obj.name) and main_geometry_object is not None:
if not is_collision_name(selected_object.name) and main_geometry_object is not None:
geometry_object = main_geometry_object
else:
geometry_object = ASEGeometryObject()
geometry_object.name = obj.name
geometry_object.name = selected_object.name
if not geometry_object.is_collision:
main_geometry_object = geometry_object
ase.geometry_objects.append(geometry_object)
if not geometry_object.is_collision and len(mesh_data.materials) == 0:
raise ASEBuilderError(f'Mesh \'{obj.name}\' must have at least one material')
if geometry_object.is_collision:
# Test that collision meshes are manifold and convex.
bm = bmesh.new()
bm.from_mesh(mesh_object.data)
for edge in bm.edges:
if not edge.is_manifold:
del bm
raise ASEBuilderError(f'Collision mesh \'{selected_object.name}\' is not manifold')
if not edge.is_convex:
del bm
raise ASEBuilderError(f'Collision mesh \'{selected_object.name}\' is not convex')
geometry_object.vertex_offset += len(geometry_object.vertices)
vertex_transform = Matrix.Scale(options.scale, 4) @ Matrix.Rotation(math.pi, 4, 'Z') @ obj.matrix_world
if not geometry_object.is_collision and len(selected_object.data.materials) == 0:
raise ASEBuilderError(f'Mesh \'{selected_object.name}\' must have at least one material')
vertex_transform = Matrix.Scale(options.scale, 4) @ Matrix.Rotation(math.pi, 4, 'Z') @ mesh_object.matrix_world
for vertex_index, vertex in enumerate(mesh_data.vertices):
geometry_object.vertices.append(vertex_transform @ vertex.co)
material_indices = []
if not geometry_object.is_collision:
for mesh_material_index, material in enumerate(mesh_data.materials):
for mesh_material_index, material in enumerate(selected_object.data.materials):
if material is None:
raise ASEBuilderError(f'Material slot {mesh_material_index + 1} for mesh \'{obj.name}\' cannot be empty')
raise ASEBuilderError(f'Material slot {mesh_material_index + 1} for mesh \'{selected_object.name}\' cannot be empty')
try:
# Reuse existing material entries for duplicates
material_index = ase.materials.index(material.name)
@@ -69,11 +93,16 @@ class ASEBuilder(object):
face.material_index = material_indices[loop_triangle.material_index]
# The UT2K4 importer only accepts 32 smoothing groups. Anything past this completely mangles the
# smoothing groups and effectively makes the whole model use sharp-edge rendering.
# The fix is to constrain the smoothing group between 0 and 31 by applying a modulo of 32 to the actual
# smoothing group index.
# This may result in bad calculated normals on export in rare cases. For example, if a face with a
# smoothing group of 3 is adjacent to a face with a smoothing group of 35 (35 % 32 == 3), those faces
# will be treated as part of the same smoothing group.
face.smoothing = (poly_groups[loop_triangle.polygon_index] - 1) % 32
geometry_object.faces.append(face)
# Normals
if not geometry_object.is_collision:
# Normals
for face_index, loop_triangle in enumerate(mesh_data.loop_triangles):
face_normal = ASEFaceNormal()
face_normal.normal = loop_triangle.normal
@@ -85,17 +114,16 @@ class ASEBuilder(object):
face_normal.vertex_normals.append(vertex_normal)
geometry_object.face_normals.append(face_normal)
uv_layer = mesh_data.uv_layers.active.data
# Texture Coordinates
geometry_object.texture_vertex_offset += len(geometry_object.texture_vertices)
if not geometry_object.is_collision:
for i, uv_layer_data in enumerate([x.data for x in mesh_data.uv_layers]):
if i >= len(geometry_object.uv_layers):
geometry_object.uv_layers.append(ASEUVLayer())
uv_layer = geometry_object.uv_layers[i]
for loop_index, loop in enumerate(mesh_data.loops):
u, v = uv_layer[loop_index].uv
geometry_object.texture_vertices.append((u, v, 0.0))
u, v = uv_layer_data[loop_index].uv
uv_layer.texture_vertices.append((u, v, 0.0))
# Texture Faces
if not geometry_object.is_collision:
for loop_triangle in mesh_data.loop_triangles:
geometry_object.texture_vertex_faces.append((
geometry_object.texture_vertex_offset + loop_triangle.loops[0],
@@ -103,6 +131,16 @@ class ASEBuilder(object):
geometry_object.texture_vertex_offset + loop_triangle.loops[2]
))
# Vertex Colors
if len(mesh_data.vertex_colors) > 0:
vertex_colors = mesh_data.vertex_colors.active.data
for color in map(lambda x: x.color, vertex_colors):
geometry_object.vertex_colors.append(tuple(color[0:3]))
# Update data offsets for next iteration
geometry_object.texture_vertex_offset = len(mesh_data.loops)
geometry_object.vertex_offset = len(geometry_object.vertices)
if len(ase.geometry_objects) == 0:
raise ASEBuilderError('At least one mesh object must be selected')

View File

@@ -1,6 +1,6 @@
import bpy
import bpy_extras
from bpy.props import StringProperty, FloatProperty, EnumProperty
from bpy.props import StringProperty, FloatProperty, EnumProperty, BoolProperty
from .builder import *
from .writer import *
@@ -10,20 +10,19 @@ class ASE_OT_ExportOperator(bpy.types.Operator, bpy_extras.io_utils.ExportHelper
bl_label = 'Export ASE'
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
filename_ext = '.ase'
filter_glob: StringProperty(
default="*.ase",
options={'HIDDEN'},
maxlen=255, # Max internal buffer length, longer would be hilighted.
)
units = EnumProperty(
units: EnumProperty(
items=(('M', 'Meters', ''),
('U', 'Unreal', '')),
name='Units'
name='Units',
default='U'
)
use_raw_mesh_data: BoolProperty(default=False, name='Raw Mesh Data')
units_scale = {
'M': 60.352,
@@ -33,14 +32,16 @@ class ASE_OT_ExportOperator(bpy.types.Operator, bpy_extras.io_utils.ExportHelper
def draw(self, context):
layout = self.layout
layout.prop(self, 'units', expand=False)
layout.prop(self, 'use_raw_mesh_data')
def execute(self, context):
options = ASEBuilderOptions()
options.scale = self.units_scale[self.units]
options.use_raw_mesh_data = self.use_raw_mesh_data
try:
ase = ASEBuilder().build(context, options)
ASEWriter().write(self.filepath, ase)
self.report({'INFO'}, 'ASE exported successful')
self.report({'INFO'}, 'ASE export successful')
return {'FINISHED'}
except ASEBuilderError as e:
self.report({'ERROR'}, str(e))

Binary file not shown.

25
src/reader.py Normal file
View File

@@ -0,0 +1,25 @@
import bpy
from bpy.props import StringProperty
import bpy_extras
from asepy import read_ase
class ASE_OT_ImportOperator(bpy.types.Operator, bpy_extras.io_utils.ImportHelper):
bl_idname = 'io_scene_ase.ase_export' # important since its how bpy.ops.import_test.some_data is constructed
bl_label = 'Export ASE'
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
filename_ext = '.ase'
filter_glob: StringProperty(
default="*.ase",
options={'HIDDEN'},
maxlen=255,
)
classes = (
ASE_OT_ImportOperator
)

View File

@@ -0,0 +1,18 @@
import bpy
try:
bpy.ops.object.mode_set(mode='OBJECT')
except:
pass
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()
bpy.ops.mesh.primitive_monkey_add()
bpy.ops.object.select_all(action='SELECT')
mesh_object = bpy.context.view_layer.objects.active
material = bpy.data.materials.new('asd')
mesh_object.data.materials.append(material)
r = bpy.ops.io_scene_ase.ase_export(filepath=r'.\\flat.ase')

28
src/tests/tests.py Normal file
View File

@@ -0,0 +1,28 @@
import unittest
import os
from subprocess import run, PIPE, STDOUT
from ..reader import read_ase
from dotenv import load_dotenv
def run_blender_script(script_path: str, args=list()):
return run([os.environ['BLENDER_PATH'], '--background', '--python', script_path, '--'] + args)
class AseExportTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
load_dotenv()
def test_flat(self):
run_blender_script('src\\tests\\scripts\\export_flat_test.py')
read_ase('./flat.ase')
def test_smooth(self):
pass
if __name__ == '__main__':
print()
unittest.main()

View File

@@ -146,18 +146,20 @@ class ASEWriter(object):
face_node.push_sub_command('MESH_MTLID').push_datum(face.material_index)
# Texture Coordinates
if len(geometry_object.texture_vertices) > 0:
mesh_node.push_child('MESH_NUMTVERTEX').push_datum(len(geometry_object.texture_vertices))
tvertlist_node = mesh_node.push_child('MESH_TVERTLIST')
for tvert_index, tvert in enumerate(geometry_object.texture_vertices):
for i, uv_layer in enumerate(geometry_object.uv_layers):
parent_node = mesh_node if i == 0 else mesh_node.push_child('MESH_MAPPINGCHANNEL')
if i > 0:
parent_node.push_datum(i + 1)
parent_node.push_child('MESH_NUMTVERTEX').push_datum(len(uv_layer.texture_vertices))
tvertlist_node = parent_node.push_child('MESH_TVERTLIST')
for tvert_index, tvert in enumerate(uv_layer.texture_vertices):
tvert_node = tvertlist_node.push_child('MESH_TVERT')
tvert_node.push_datum(tvert_index)
tvert_node.push_data(list(tvert))
# Texture Faces
if len(geometry_object.texture_vertex_faces) > 0:
mesh_node.push_child('MESH_NUMTVFACES').push_datum(len(geometry_object.texture_vertex_faces))
texture_faces_node = mesh_node.push_child('MESH_TFACELIST')
parent_node.push_child('MESH_NUMTVFACES').push_datum(len(geometry_object.texture_vertex_faces))
texture_faces_node = parent_node.push_child('MESH_TFACELIST')
for texture_face_index, texture_face in enumerate(geometry_object.texture_vertex_faces):
texture_face_node = texture_faces_node.push_child('MESH_TFACE')
texture_face_node.push_data([texture_face_index] + list(texture_face))
@@ -174,6 +176,18 @@ class ASEWriter(object):
vertex_normal_node.push_datum(vertex_normal.vertex_index)
vertex_normal_node.push_data(list(vertex_normal.normal))
# Vertex Colors
if len(geometry_object.vertex_colors) > 0:
mesh_node.push_child('MESH_NUMCVERTEX').push_datum(len(geometry_object.vertex_colors))
cvert_list = mesh_node.push_child('MESH_CVERTLIST')
for i, vertex_color in enumerate(geometry_object.vertex_colors):
cvert_list.push_child('MESH_VERTCOL').push_datum(i).push_data(vertex_color)
parent_node.push_child('MESH_NUMCVFACES').push_datum(len(geometry_object.texture_vertex_faces))
texture_faces_node = parent_node.push_child('MESH_CFACELIST')
for texture_face_index, texture_face in enumerate(geometry_object.texture_vertex_faces):
texture_face_node = texture_faces_node.push_child('MESH_CFACE')
texture_face_node.push_data([texture_face_index] + list(texture_face))
geomobject_node.push_child('MATERIAL_REF').push_datum(0)
return root