Skip to content

Commit

Permalink
ALB-29: fixed arcs not being able to be exported when they contained …
Browse files Browse the repository at this point in the history
…mods with the same name but in different folders: now all mods are imported with their folder as name
  • Loading branch information
Brachi committed Mar 21, 2016
1 parent d34f683 commit 484817a
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 25 deletions.
12 changes: 7 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ language: python
python:
- '3.4'


before_install:
- sudo add-apt-repository -y ppa:thomas-schiex/blender
- sudo apt-get update
- sudo apt-get install -y blender
- sudo ln /dev/null /dev/raw1394

install:
- pip install -r requirements.txt

Expand All @@ -16,11 +23,6 @@ script:

after_success: coveralls

before_install:
- sudo add-apt-repository -y ppa:thomas-schiex/blender
- sudo apt-get update
- sudo apt-get install -y blender
- sudo ln /dev/null /dev/raw1394

env:
global:
Expand Down
3 changes: 1 addition & 2 deletions albam/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ class AlbamImportedItemName(bpy.types.PropertyGroup):
class AlbamImportedItem(bpy.types.PropertyGroup):
name = bpy.props.StringProperty(options={'HIDDEN'})
source_path = bpy.props.StringProperty(options={'HIDDEN'})
# TODO: remove this and use a 'folder' attribute instead
source_path_is_absolute = bpy.props.BoolProperty(options={'HIDDEN'})
folder = bpy.props.StringProperty(options={'HIDDEN'}) # Always in posix format
data = bpy.props.StringProperty(options={'HIDDEN'}, subtype='BYTE_STRING')
file_type = bpy.props.StringProperty(options={'HIDDEN'})

Expand Down
12 changes: 8 additions & 4 deletions albam/mtframework/blender_export.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ctypes
from io import BytesIO
import posixpath
import ntpath
import os
import tempfile
Expand Down Expand Up @@ -35,6 +36,7 @@
get_vertex_count_from_blender_objects,
get_bone_indices_and_weights_per_vertex,
get_uvs_per_vertex,
ensure_ntpath,
)


Expand All @@ -49,12 +51,14 @@ def export_arc(blender_object):

for child in blender_object.children:
try:
mod_dirpath = child.albam_imported_item.source_path
# TODO: This could lead to errors if imported in Windows and exported in posix?
mod_filepath = os.path.join(mod_dirpath, child.name)
basename = posixpath.basename(child.name)
folder = child.albam_imported_item.folder
if os.sep == ntpath.sep: # Windows
mod_filepath = ntpath.join(ensure_ntpath(folder), basename)
else:
mod_filepath = os.path.join(folder, basename)
except AttributeError:
raise ExportError('Object {0} did not come from the original arc'.format(child.name))
assert child.albam_imported_item.source_path_is_absolute is False
assert child.albam_imported_item.file_type == 'mtframework.mod'
mod, textures = export_mod156(child)
mods[mod_filepath] = (mod, textures)
Expand Down
17 changes: 9 additions & 8 deletions albam/mtframework/blender_import.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from itertools import chain
import ntpath
import posixpath
import os

try:
Expand All @@ -22,6 +23,7 @@
strip_triangles_to_triangles_list,
y_up_to_z_up,
create_mesh_name,
ensure_posixpath,
)


Expand All @@ -44,20 +46,17 @@ def import_arc(file_path, extraction_dir=None, context_scene=None):

mod_files = [os.path.join(root, f) for root, _, files in os.walk(out)
for f in files if f.endswith('.mod')]
mod_dirs = [os.path.dirname(mod_file.split(out)[-1]) for mod_file in mod_files]
mod_folders = [os.path.dirname(mod_file.split(out)[-1]) for mod_file in mod_files]

# Saving arc to main object
parent = bpy.data.objects.new(arc_name, None)
bpy.context.scene.objects.link(parent)
parent.albam_imported_item['data'] = bytes(arc)
parent.albam_imported_item.name = arc_name
parent.albam_imported_item.source_path = file_path
parent.albam_imported_item.source_path = file_path
parent.albam_imported_item.source_path_is_absolute = True
parent.albam_imported_item.file_type = 'mtframework.arc'
for i, mod_file in enumerate(mod_files):
mod_dir = mod_dirs[i]
import_mod(mod_file, out, parent, mod_dir)
import_mod(mod_file, out, parent, mod_folders[i])

# Addding the name of the imported item so then it can be selected
# from a list for exporting. Exporting models without a base model,
Expand All @@ -68,8 +67,10 @@ def import_arc(file_path, extraction_dir=None, context_scene=None):
new_albam_imported_item.name = os.path.basename(file_path)


def import_mod(file_path, base_dir, parent=None, mod_dir_path=None):
def import_mod(file_path, base_dir, parent=None, mod_folder=None):
model_name = os.path.basename(file_path)
if mod_folder:
model_name = posixpath.join(ensure_posixpath(mod_folder), model_name)
mod = Mod156(file_path=file_path)
if mod.version == 156:
textures = _create_blender_textures_from_mod(mod, base_dir)
Expand Down Expand Up @@ -98,8 +99,8 @@ def import_mod(file_path, base_dir, parent=None, mod_dir_path=None):

# saving imported data for export use (format not 100% figured out yet)
root.albam_imported_item['data'] = bytes(mod)
root.albam_imported_item.source_path = mod_dir_path # e.g. pawn/pl/pl00/model
parent.albam_imported_item.source_path_is_absolute = False
root.albam_imported_item.folder = ensure_posixpath(mod_folder) # e.g. pawn/pl/pl00/model
root.albam_imported_item.source_path = file_path
root.albam_imported_item.name = model_name
root.albam_imported_item.file_type = 'mtframework.mod'

Expand Down
19 changes: 19 additions & 0 deletions albam/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
from copy import copy
import ctypes
from ctypes import c_float
import ntpath
import os
import posixpath
import struct



def get_offset(struct_ob, name):
return getattr(struct_ob.__class__, name).offset

Expand Down Expand Up @@ -367,6 +370,22 @@ def get_uvs_per_vertex(blender_mesh, uv_layer):
return vertices


def ensure_posixpath(path):
'''If the path given is not posix, convert it and return it, else return it'''
splitted = path.split(ntpath.sep)
if len(splitted) == 1:
return path
return posixpath.join(*splitted)


def ensure_ntpath(path):
'''If the path given is not nt, convert it and return it, else return it'''
splitted = path.split(posixpath.sep)
if len(splitted) == 1:
return path
return ntpath.join(*splitted)


def triangles_list_to_triangles_strip(blender_mesh):
"""
Export triangle strips from a blender mesh.
Expand Down
5 changes: 4 additions & 1 deletion tests/test_mtframework_blender.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
logging.debug('Importing {import_arc_filepath}')
# TODO: use the UI panels directly?
register()
try:
register()
except ValueError: # The addon is installed in blender.
pass
try:
start = time.time()
Expand Down
34 changes: 29 additions & 5 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
import struct
import os

import pytest

from albam.utils import BaseStructure, unpack_half_float, pack_half_float
from albam.utils import (BaseStructure, unpack_half_float, pack_half_float,
ensure_posixpath, ensure_ntpath)


def test_base_structure(tmpdir):
Expand Down Expand Up @@ -40,11 +39,36 @@ def test_unpack_pack_half_float():
# https://en.wikipedia.org/wiki/Half-precision_floating-point_format
expected_fail = 0
for short_input in range(0, 65535):
if (short_input in range(31745, 33792) or short_input in range(1, 1024)
or short_input in range(64512, 65535)):
if (short_input in range(31745, 33792) or
short_input in range(1, 1024) or
short_input in range(64512, 65535)):
expected_fail += 1
else:
float_output = unpack_half_float(short_input)
short_again = pack_half_float(float_output)
assert short_input == short_again
assert expected_fail == 4093


def test_ensure_posixpath_from_ntpath():
path = 'foo\\bar\\spam\\eggs'

assert ensure_posixpath(path) == 'foo/bar/spam/eggs'


def test_ensure_posixpath_from_posixpath():
path = 'foo/bar/spam/eggs'

assert ensure_posixpath(path) == 'foo/bar/spam/eggs'


def test_ensure_ntpath_from_posixpath():
path = 'foo/bar/spam/eggs'

assert ensure_ntpath(path) == 'foo\\bar\\spam\\eggs'


def test_ensure_ntpath_from_ntpath():
path = 'foo\\bar\\spam\\eggs'

assert ensure_ntpath(path) == 'foo\\bar\\spam\\eggs'

0 comments on commit 484817a

Please sign in to comment.