Skip to content

Commit

Permalink
Merge pull request #60 from Robert-N7/develop
Browse files Browse the repository at this point in the history
Added has_uv_matrix to JSON and encoding uv_matrix indices
  • Loading branch information
Robert-N7 authored May 12, 2021
2 parents 0a55b3b + 6f31f16 commit 3f70fc3
Show file tree
Hide file tree
Showing 17 changed files with 164 additions and 78 deletions.
12 changes: 6 additions & 6 deletions abmatt/brres/lib/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,12 @@ def decode_polygon(polygon, influences=None):
influence = influences[linked_bone.weight_id]
g_verts.apply_affine_matrix(np.array(linked_bone.get_transform_matrix()), apply=True)
influence_collection = InfluenceCollection({0: influence})
for x in polygon.uv_mtx_indices:
if x >= 0:
AutoFix.warn('{} uv matrices data lost in export.'.format(polygon.name))
indices = face_point_indices[:, :, x] // 3
if (indices < 10).any():
print('Less than 10!')
# for x in polygon.uv_mtx_indices:
# if x >= 0:
# AutoFix.warn('{} uv matrices data lost in export.'.format(polygon.name))
# indices = face_point_indices[:, :, x] // 3
# if (indices < 10).any():
# print('Less than 10!')
from abmatt.converters.geometry import Geometry
geometry = Geometry(polygon.name, polygon.get_material().name, g_verts,
triangles=None, influences=influence_collection,
Expand Down
2 changes: 1 addition & 1 deletion abmatt/build/config.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
build_name = main # build output name
build_type = onedir # the type of build, (auto|onefile|onedir)
version = 0.9.7
version = 0.9.8
run_integration_tests = False
run_unit_tests = False
7 changes: 4 additions & 3 deletions abmatt/converters/convert_dae.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ def load_model(self, model_name=None):
self.__parse_nodes(dae.get_scene(), material_geometry_map, matrix)
self.__combine_bones_map()
self.__parse_controllers(material_geometry_map)

self._before_encoding()
self.influences.encode_bone_weights(self.mdl0)
for material in material_geometry_map:
if material not in material_names:
self._encode_material(material.Material(material))
material_names.add(material)

self._before_encoding()
self.influences.encode_bone_weights(self.mdl0)
for material in material_geometry_map:
geometries = material_geometry_map[material]
for x in geometries:
self.__encode_geometry(x)
Expand Down
22 changes: 19 additions & 3 deletions abmatt/converters/convert_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,17 @@ def _start_loading(self, model_name):
return self._init_mdl0(brres_name, os.path.splitext(name)[0], model_name)

def _before_encoding(self):
if os.path.exists(self.json_file):
converter = MatsToJsonConverter(self.json_file)
converter.load_into(self.mdl0.materials)
self.json_polygon_encoding = converter.polygons_by_name
else:
self.json_polygon_encoding = None
if self.encoder:
self.encoder.before_encoding(self)

def _end_loading(self):
mdl0 = self.mdl0
if os.path.exists(self.json_file):
MatsToJsonConverter(self.json_file).load_into(mdl0.materials)
import_path_map = self.__normalize_image_path_map(self.import_textures_map)
self._import_images(import_path_map)
mdl0.rebuild_header()
Expand Down Expand Up @@ -262,8 +266,20 @@ def _encode_geometry(self, geometry):
geometry.normals = None
if self.flags & self.NO_UVS:
geometry.texcoords = []
has_uv_mtx = priority = None
json_data = self.json_polygon_encoding.get(geometry.name) if self.json_polygon_encoding else None
if json_data:
has_uv_mtx = json_data.get('has_uv_matrix')
priority = json_data.get('draw_priority')
elif self.replacement_model:
replace_geometry = [x for x in self.replacement_model.objects if x.name == geometry.name]
if replace_geometry:
has_uv_mtx = [replace_geometry[0].has_uv_matrix(i) for i in range(8)]
priority = replace_geometry[0].priority
encoder = self.encoder.get_encoder(geometry) if self.encoder else None
return geometry.encode(self.mdl0, encoder=encoder)
return geometry.encode(self.mdl0, encoder=encoder,
priority=priority,
has_uv_mtx=has_uv_mtx)

def convert(self):
if self.encode:
Expand Down
23 changes: 22 additions & 1 deletion abmatt/converters/convert_mats_to_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def __init__(self, filename):
"""
self.filename = filename
self.materials_by_name = {}
self.polygons_by_name = {}

def export(self, materials):
""" Exports the materials to a JSON file
Expand Down Expand Up @@ -67,8 +68,15 @@ def __load_material(self, material, data):
if pat0_data is not None:
material.add_pat0()
self.__load_pat0(material.pat0, pat0_data)
poly_data = data.get('polygons')
if poly_data:
self.__load_polygons(poly_data)
return material

def __load_polygons(self, poly_data):
for x in poly_data:
self.polygons_by_name[x] = poly_data[x]

def __load_srt0(self, srt0, data):
self.__load_settings(srt0, data.get('settings'))
d = data.get('texture_animations')
Expand Down Expand Up @@ -135,13 +143,26 @@ def __load_settings(item, settings):
def __get_material_str(self, material):
x = {'settings': self.__get_settings(material),
'layers': self.__get_items_str(material.layers),
'shader': self.__get_shader_str(material.shader)}
'shader': self.__get_shader_str(material.shader),
'polygons': self.__get_polygons_str(material.polygons)}
if material.srt0 is not None:
x['srt0'] = self.__get_srt0_str(material.srt0)
if material.pat0 is not None:
x['pat0'] = self.__get_pat0_str(material.pat0)
return x

def __get_polygons_str(self, polygons):
d = {}
for poly in polygons:
d[poly.name] = self.__get_polygon_str(poly)
return d

def __get_polygon_str(self, poly):
return {
'has_uv_matrix': [x >= 0 for x in poly.uv_mtx_indices],
'draw_priority': poly.priority
}

def __get_shader_str(self, shader):
return {'settings': self.__get_settings(shader),
'stages': self.__get_items_str(shader.stages)}
Expand Down
5 changes: 3 additions & 2 deletions abmatt/converters/convert_obj.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ def load_model(self, model_name=None):
bone = mdl.add_bone(mdl.name)
obj = Obj(self.mdl_file)
material_geometry_map = self.__collect_geometries(obj.geometries, bone)

self._before_encoding()
for material in material_geometry_map:
try:
self.__encode_material(obj.materials[material])
except KeyError:
self._encode_material(Material(material))

self._before_encoding()
for material in material_geometry_map:
super()._encode_geometry(material_geometry_map[material])
self.import_textures_map = self.__convert_set_to_map(obj.images)
return self._end_loading()
Expand Down
2 changes: 1 addition & 1 deletion abmatt/converters/dae.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ def __initialize_libraries(self, initial_name):
def __initialize_assets(self, root):
asset = XMLNode('asset', parent=root)
contributor = XMLNode('contributor', parent=asset)
authoring_tool = XMLNode('authoring_tool', 'ABMATT COLLADA exporter v0.9.7', parent=contributor)
authoring_tool = XMLNode('authoring_tool', 'ABMATT COLLADA exporter v0.9.8', parent=contributor)
time_stamp = datetime.now()
created = XMLNode('created', str(time_stamp), parent=asset)
modified = XMLNode('modified', str(time_stamp), parent=asset)
Expand Down
32 changes: 28 additions & 4 deletions abmatt/converters/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def __init__(self, name, material_name, vertices, texcoords=None, normals=None,
self.influences = influences
self.triangles = triangles
self.linked_bone = linked_bone
self.has_uv_mtx = None
self.priority = 0

def __eq__(self, other):
return other is not None and type(other) == Geometry and self.name == other.name and \
Expand Down Expand Up @@ -80,7 +82,13 @@ def ipp(self):
self.index += 1
return j

def encode(self, mdl, visible_bone=None, encoder=None, use_default_colors_if_none_found=True):
def encode(self, mdl, visible_bone=None, encoder=None,
use_default_colors_if_none_found=True,
priority=None, has_uv_mtx=None):
if priority is not None:
self.priority = priority
if has_uv_mtx is not None:
self.has_uv_mtx = has_uv_mtx
if not visible_bone:
if not mdl.bones:
mdl.add_bone(mdl.name)
Expand All @@ -95,6 +103,7 @@ def encode(self, mdl, visible_bone=None, encoder=None, use_default_colors_if_non
self.fmt_str = '>'
if self.__encode_influences(p, self.influences, mdl, visible_bone):
p.weight_index = self.ipp()
self.__encode_tex_matrices(p, self.has_uv_mtx)
if self.__encode_vertices(p, self.vertices, mdl):
p.vertex_index = self.ipp()
if self.__encode_normals(p, self.normals, mdl):
Expand All @@ -115,13 +124,20 @@ def encode(self, mdl, visible_bone=None, encoder=None, use_default_colors_if_non
p.encode_str = self.fmt_str
mdl.objects.append(p)
material = mdl.get_material_by_name(self.material_name)
mdl.add_definition(material, p, visible_bone)
mdl.add_definition(material, p, visible_bone, self.priority)
if self.colors:
material.enable_vertex_color()
if encoder:
encoder.after_encode(p)
return p

def __encode_tex_matrices(self, poly, has_uv_mtx):
if has_uv_mtx:
for i in range(8):
if has_uv_mtx[i]:
poly.uv_mtx_indices[i] = self.ipp()
self.fmt_str += 'B'

@staticmethod
def __encode_load_matrices_helper(data, indices, xf_address, matrix_len, cmd):
mtx_len_shifted = matrix_len - 1 << 12
Expand All @@ -139,22 +155,26 @@ def __encode_load_matrices(indices):
Geometry.__encode_load_matrices_helper(data, indices, 1024, 9, 0x28)
return data

@staticmethod
def __remap_inf_group(group, tris, matrices):
def __remap_inf_group(self, group, tris, matrices):
remapper = {}
uv_mtx_count = sum(self.has_uv_mtx) if self.has_uv_mtx is not None else 0
matrices = list(matrices)
for i in range(len(matrices)):
remapper[matrices[i]] = i * 3
n_group = []
for strip in group:
arr = np.array(strip)
for item in arr:
if uv_mtx_count:
item[1:1+uv_mtx_count] = (item[0] + 10) * 3
item[0] = remapper[item[0]]
n_group.append(arr)
n_tris = []
for tri in tris:
arr = np.array(tri.vertices)
for item in arr:
if uv_mtx_count:
item[1:1 + uv_mtx_count] = (item[0] + 10) * 3
item[0] = remapper[item[0]]
n_tris.append(arr)
return n_group, n_tris
Expand Down Expand Up @@ -275,6 +295,10 @@ def __construct_tris(self, polygon, weighted=False):
pos_indices = np.array([[remapper[y].influence_id for y in tri] for tri in self.vertices.face_indices])
polygon.bone_table = [i for i in range(np.max(pos_indices) + 1)]
tris.append(pos_indices)
if self.has_uv_mtx is not None:
for x in self.has_uv_mtx:
if x:
tris.append(pos_indices) # this just serves as a placeholder
tris.append(self.vertices.face_indices)
if self.normals:
tris.append(self.normals.face_indices)
Expand Down
4 changes: 2 additions & 2 deletions abmatt/converters/obj.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,15 @@ def save(self):
self.save_obj()

def save_mtllib(self, folder):
s = '# Wavefront MTL exported with ABMATT v0.9.7'
s = '# Wavefront MTL exported with ABMATT v0.9.8'
materials = self.materials
for x in materials:
s += '\n' + materials[x].get_save_str()
with open(os.path.join(folder, self.mtllib), 'w') as f:
f.write(s)

def save_obj(self):
s = '# Wavefront OBJ exported with ABMATT v0.9.7\n\nmtllib ' + self.mtllib + '\n\n'
s = '# Wavefront OBJ exported with ABMATT v0.9.8\n\nmtllib ' + self.mtllib + '\n\n'
vertex_index = 1
normal_index = 1
normal_offset = -1
Expand Down
2 changes: 1 addition & 1 deletion abmatt/dist/install-ubu.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
====================================================================================
ANOOB'S BRRES MATERIAL TOOL
Version: 0.9.7
Version: 0.9.8
Author: Robert Nelson
OS: linux
Github: https://github.com/Robert-N7/abmatt
Expand Down
2 changes: 1 addition & 1 deletion abmatt/dist/install-win.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
===================================================================================================
ANOOB'S BRRES MATERIAL TOOL
Version 0.9.7
Version 0.9.8
Robert Nelson
OS: Windows 10 64-bit
Github: https://github.com/Robert-N7/abmatt
Expand Down
2 changes: 1 addition & 1 deletion abmatt/dist/make_installer.nsi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
!define VERSION "0.9.7"
!define VERSION "0.9.8"
!define PROGRAM_NAME "ANoob's Brres Material Tool ${VERSION}"
InstallDir "$Documents\abmatt"
Name "${PROGRAM_NAME}"
Expand Down
2 changes: 1 addition & 1 deletion abmatt/load_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def load_config(app_dir, loudness=None, autofix_level=None):
return conf


VERSION = '0.9.7'
VERSION = '0.9.8'
USAGE = "USAGE: abmatt [command_line][--interactive -f <file> -b <brres-file> -d <destination> --overwrite]"


Expand Down
Loading

0 comments on commit 3f70fc3

Please sign in to comment.