Пример #1
0
class MyTestCase(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        self.brres = Brres('../brres_files/beginner_course.brres')
        self.original_model = self.brres.models[0]
        self.test_file = '../brres_files/test.brres'
        if os.path.exists(self.test_file):
            os.remove(self.test_file)
        self.brres.save(self.test_file, True)
        if not os.path.exists(self.test_file):
            raise Exception('Failed to save test file')
        self.test = Brres(self.test_file)
        self.test_model = self.test.models[0]

    def test_correct_amount_of_objects(self):
        self.assertEqual(len(self.brres.models), 1)
        self.assertEqual(len(self.brres.textures), 29)
        self.assertEqual(len(self.brres.pat0), 1)
        self.assertEqual(len(self.brres.srt0), 1)

    def test_materials_equal(self):
        for mat in self.original_model.materials:
            self.assertEqual(mat, self.test_model.get_material_by_name(mat.name))

    def test_polygons_equal(self):
        for x in self.original_model.polygons:
            self.assertEqual(x, )
Пример #2
0
 def trace_path(self,
                brres_path=None,
                model_name=None,
                material_name=None,
                return_new_brres_flag=False):
     brres = mdl0 = material = None
     if brres_path is None:
         brres_path, model_name, material_name = self.split_path()
     if not brres_path:  # No such file
         return brres, mdl0, material
     brres = Brres.get_brres(brres_path)
     new_brres = False
     if not brres:
         brres = Brres(brres_path, readFile=False)
         new_brres = True
     if brres:
         if model_name:
             mdl0 = brres.getModel(model_name)
             if mdl0:
                 if material_name:
                     material = mdl0.get_material_by_name(material_name)
     if not return_new_brres_flag:
         return brres, mdl0, material
     else:
         return brres, mdl0, material, new_brres
Пример #3
0
 def __init__(self, *args, **kwargs):
     self.brres = Brres('../brres_files/beginner_course.brres')
     self.poly = self.brres.models[0].objects[0]
     self.mat = self.poly.get_material()
     self.simple = Brres('../brres_files/simple.brres')
     self.output = '../brres_files/test.brres'
     super().__init__(*args, **kwargs)
Пример #4
0
 def __init__(self,
              brres,
              mdl_file,
              flags=0,
              encode=True,
              mdl0=None,
              encoder=None,
              include=None,
              exclude=None):
     if not brres:
         # filename = Brres.getExpectedBrresFileName(mdl_file)
         d, f = os.path.split(mdl_file)
         filename = os.path.join(d, os.path.splitext(f)[0] + '.brres')
         brres = Brres.get_brres(filename, True)
     elif type(brres) == str:
         brres = Brres.get_brres(brres, True)
     self.brres = brres
     self.include = include
     self.exclude = exclude
     self.patch_existing = False
     self.texture_library = brres.get_texture_map()
     self.mdl_file = mdl_file
     self.mdl0 = mdl0 if type(mdl0) == Mdl0 else brres.get_model(mdl0)
     self.flags = flags
     self.image_dir = None
     self.replacement_model = None
     self.encode = encode
     self.encoder = encoder
Пример #5
0
 def test_save_all(self):
     tmp = self._get_tmp()
     for x in gather_files(self.root):
         try:
             original = Brres(x)
             original.save(tmp, overwrite=True)
             new = Brres(tmp)
             self.assertEqual(original, new)
         except:
             print(f'ERROR saving {x}')
             raise
Пример #6
0
 def setUpClass(self):
     self.brres = Brres('../brres_files/beginner_course.brres')
     self.original_model = self.brres.models[0]
     self.test_file = '../brres_files/test.brres'
     if os.path.exists(self.test_file):
         os.remove(self.test_file)
     self.brres.save(self.test_file, True)
     if not os.path.exists(self.test_file):
         raise Exception('Failed to save test file')
     self.test = Brres(self.test_file)
     self.test_model = self.test.models[0]
Пример #7
0
 def __init__(self, path):
     if self.LIBRARY is not None:
         raise RuntimeError('Already Initialized library!')
     if path is None:
         path = os.path.join(os.getcwd(), 'mat_lib.brres')
         self.LIBRARY_PATH = path
     try:
         self.brres = Brres(path)
     except FileNotFoundError as e:
         AutoFix.get().info(f'Material library "{path}" not found, Creating file.')
         self.brres = Brres(path, readFile=False)
         self.brres.add_mdl0(Mdl0('lib', self.brres))
     self.on_brres_update()
Пример #8
0
 def test_open_close(self):
     dir = os.path.join(self.base_path, 'brres_files')
     output = os.path.join(dir, 'test.brres')
     for x in os.listdir(dir):
         current_file = os.path.join(dir, x)
         if current_file != output:
             if os.path.exists(output):
                 os.remove(output)
             if x.endswith('.brres'):
                 b = Brres(current_file)
                 b.save(output, True)
             self.assertTrue(os.path.exists(output))
     return True
Пример #9
0
class MaterialLibrary(ClipableObserver):
    LIBRARY = None
    LIBRARY_PATH = None

    @staticmethod
    def get():
        if not MaterialLibrary.LIBRARY:
            if MaterialLibrary.LIBRARY_PATH is None:
                return None
            MaterialLibrary.LIBRARY = MaterialLibrary(MaterialLibrary.LIBRARY_PATH)
        return MaterialLibrary.LIBRARY

    def __init__(self, path):
        if self.LIBRARY is not None:
            raise RuntimeError('Already Initialized library!')
        if path is None:
            path = os.path.join(os.getcwd(), 'mat_lib.brres')
            self.LIBRARY_PATH = path
        try:
            self.brres = Brres(path)
        except FileNotFoundError as e:
            AutoFix.info(f'Material library "{path}" not found.')
            self.brres = Brres(path, read_file=False)
            self.brres.add_mdl0(Mdl0('lib', self.brres))
        self.on_brres_update()

    def on_node_update(self, node):
        if type(node) != Material:
            self.on_brres_update()

    def on_rename_update(self, node, old_name):
        if type(node) == Material:
            try:
                self.materials[node.name] = self.materials.pop(old_name)
            except KeyError:
                pass

    def is_modified(self):
        return self.brres.is_modified()

    def on_brres_update(self):
        self.materials = {}
        self.brres.register_observer(self)
        for model in self.brres.models:
            model.register_observer(self)
            for material in model.materials:
                material.register_observer(self)
                if material.name not in self.materials:
                    self.materials[material.name] = material
                else:
                    AutoFix.warn('Multiple materials named {} in material library'.format(material.name))
Пример #10
0
 def test_load_older_version(self):
     # This is just to test that it can convert, not that they convert equal
     converter = DaeConverter(self._get_brres('old_mario_gc_hayasi.brres'),
                              self._get_tmp('.dae'),
                              encode=False).convert()
     converter = DaeConverter(
         Brres(self._get_tmp('.brres'), read_file=False),
         converter.mdl_file).convert()
Пример #11
0
def create_mirror(brres_file_name,
                  working_folder='tmp',
                  mdl0_name=None,
                  new_file_name=None,
                  mirror_encoder=None):
    """Creates a mirror of the brres file by decoding and re-encoding the file
    :returns (original_brres, new_brres)
    """
    original = Brres(brres_file_name)
    if new_file_name is None:
        new_file_name = brres_file_name
    base_name = os.path.splitext(os.path.basename(new_file_name))[0]
    dae_file = os.path.join(working_folder, base_name + '.dae')
    DaeConverter(original, mdl_file=dae_file, mdl0=mdl0_name,
                 encode=False).convert()
    new_file_name = os.path.join(working_folder, base_name + '.brres')
    shutil.copyfile(brres_file_name, new_file_name)
    new_brres = Brres(new_file_name)
    my_mdl0 = original.get_model(
        mdl0_name) if mdl0_name else original.models[0]
    if mirror_encoder is None:
        mirror_encoder = MirrorEncoder(my_mdl0)
    else:
        mirror_encoder.mdl0 = my_mdl0
    DaeConverter(new_brres,
                 mdl_file=dae_file,
                 mdl0=mdl0_name,
                 encode=True,
                 encoder=mirror_encoder).convert()
    new_brres.save(overwrite=True)
    return original, new_brres
Пример #12
0
 def test_convert_with_json(self):
     brres = self._get_brres('beginner_course.brres')
     dae_file = self._get_test_fname('beginner.dae')
     converter = DaeConverter(
         Brres(self._get_tmp('.brres'), read_file=False), dae_file)
     converter.load_model()
     self._test_mats_equal(converter.brres.models[0].materials,
                           brres.models[0].materials,
                           sort=True)
Пример #13
0
 def test_convert_multi_material_geometry(self):
     """Tests converting dae file that has multiple materials associated with geometry"""
     converter = DaeConverter(
         Brres(self._get_tmp('.brres'), read_file=False),
         self._get_test_fname('blender.dae'))
     converter.load_model()
     mdl0 = converter.brres.models[0]
     self.assertEqual(len(mdl0.materials), 2)
     self.assertEqual(len(mdl0.objects), 2)
Пример #14
0
 def test_export_all_obj(self):
     tmp = self._get_tmp('.obj')
     for x in gather_files(self.root):
         try:
             converter = ObjConverter(Brres(x), tmp)
             for model in converter.brres.models:
                 converter.save_model(model)
         except:
             print(f'ERROR exporting {x}')
             raise
Пример #15
0
 def test_convert_cow(self):
     original = self._get_brres('cow.brres').models[1]
     converter = DaeConverter(
         Brres(self._get_tmp('.brres'), read_file=False),
         self._get_test_fname('cow.dae'))
     converter.load_model()
     mdl0 = converter.mdl0
     # converter.brres.save(overwrite=True)
     # Ensure that the bones are correct
     self.assertTrue(
         CheckPositions.bones_equal(original.bones, mdl0.bones, 0.0001))
Пример #16
0
 def setUpClass(cls):
     cls.brres = cls._get_brres('beginner_course.brres')
     cls.original_model = cls.brres.models[0]
     cls.test_file = cls._get_brres_fname('test.brres')
     if os.path.exists(cls.test_file):
         os.remove(cls.test_file)
     cls.brres.save(cls.test_file, True)
     if not os.path.exists(cls.test_file):
         raise Exception('Failed to save test file')
     cls.test = Brres(cls.test_file)
     cls.test_model = cls.test.models[0]
Пример #17
0
 def test_export_import_dae_eq(self):
     AutoFix.set_fix_level(0, load_config.turn_off_fixes)
     tmp = self._get_tmp('.dae')
     tmp_brres = self._get_tmp('.brres')
     for x in gather_files(self.root):
         try:
             converter = DaeConverter(Brres(x), tmp)
             for model in converter.brres.models:
                 converter.save_model(model)
                 importer = DaeConverter(Brres(tmp_brres, read_file=False),
                                         tmp)
                 importer.load_model()
                 mats = sorted(model.materials, key=lambda x: x.name)
                 imported_mats = sorted(importer.mdl0.materials,
                                        key=lambda x: x.name)
                 self.assertTrue(node_eq(mats, imported_mats))
         except:
             print(f'ERROR converting {x}')
             if converter.brres.version == 11:
                 raise
Пример #18
0
 def test_convert_single_bind_flip_y_z(self):
     original = self._get_brres('simple_multi_bone_single_bind.brres')
     converter = DaeConverter(
         Brres(self._get_tmp('.brres'), read_file=False),
         self._get_test_fname('blender_simple_multi_bone_single_bind.dae'))
     converter.load_model()
     # converter.brres.save(overwrite=True)
     original_polys = original.models[0].objects
     new_polys = converter.mdl0.objects
     for i in range(len(original_polys)):
         self.assertEqual(original_polys[i].linked_bone.index,
                          new_polys[i].linked_bone.index)
Пример #19
0
 def test_convert_single_bone(self):
     original = self._get_brres('simple.brres')
     converter = DaeConverter(
         Brres(self._get_tmp('.brres'), read_file=False),
         self._get_test_fname('simple_multi_bone_single_bind.dae'),
         flags=DaeConverter.SINGLE_BONE)
     converter.load_model()
     mdl0 = converter.mdl0
     self.assertEqual(len(mdl0.bones), 1)
     self.assertTrue(
         CheckPositions.positions_equal(original.models[0].vertices,
                                        mdl0.vertices))
Пример #20
0
class ChangePolyMatTest(unittest.TestCase):
    def __init__(self, *args, **kwargs):
        self.brres = Brres('../brres_files/beginner_course.brres')
        self.poly = self.brres.models[0].objects[0]
        self.mat = self.poly.get_material()
        self.simple = Brres('../brres_files/simple.brres')
        self.output = '../brres_files/test.brres'
        super().__init__(*args, **kwargs)

    def test_from_same_brres(self):
        mat = self.brres.models[0].materials[0]
        self.poly.set_material(mat)
        # Test the material is correct
        self.assertTrue(self.poly.get_material().name == mat.name)
        # Test that the previous material is gone
        self.assertIsNone(self.poly.parent.get_material_by_name(self.mat.name))
        self.brres.save(self.output, True)

    def test_from_diff_brres(self):
        mat = self.simple.models[0].materials[0]
        self.poly.set_material(mat)
        # Test the material
        self.assertTrue(self.poly.get_material().name == mat.name)
        self.brres.save(self.output, True)

    def test_no_mat_removal(self):
        # in this test change a poly that has a shared material
        # and make sure the material still exists
        # First set up a shared material
        mat = self.brres.models[0].materials[0]
        self.poly.set_material(mat)  # now mat should have 2 polys
        self.assertTrue(len(mat.polygons) == 2)
        self.poly.set_material(self.brres.models[0].materials[2])
        self.assertIsNotNone(self.brres.models[0].get_material_by_name(mat.name))
        self.brres.save(self.output, True)
Пример #21
0
 def create_or_open(filename):
     amount = len(Command.OPEN_FILES) - Command.MAX_FILES_OPEN
     if amount > 0:
         Command.auto_close(amount, [filename])
     if os.path.exists(filename):
         files = Command.updateFile(filename)
         b = files[0] if len(files) else None
     else:
         b = Brres(filename, readFile=False)
         Command.OPEN_FILES[filename] = b
         Command.ACTIVE_FILES = [b]
         Command.MODELS = []
     return b
Пример #22
0
 def test_convert_to_correct_position(self):
     # Get reference to beginner_course to test against
     beginner_brres = self._get_brres('beginner_course.brres')
     original_vertices = beginner_brres.models[0].vertices
     # Convert dae
     brres = Brres(self._get_tmp('.brres'), read_file=False)
     dae = self._get_test_fname('beginner.dae')
     converter = DaeConverter(brres, dae)
     converter.load_model()
     # brres.save(overwrite=True)
     new_vertices = brres.models[0].vertices
     self.assertTrue(CheckPositions().positions_equal(
         original_vertices, new_vertices))
Пример #23
0
 def __init__(self, brres, mdl_file, flags=0, encode=True, mdl0=None):
     if not brres:
         # filename = Brres.getExpectedBrresFileName(mdl_file)
         d, f = os.path.split(mdl_file)
         filename = os.path.join(d, os.path.splitext(f)[0] + '.brres')
         brres = Brres.get_brres(filename, True)
     self.brres = brres
     self.texture_library = brres.get_texture_map()
     self.mdl_file = mdl_file
     self.mdl0 = mdl0
     self.flags = flags
     self.image_library = set()
     self.replacement_model = None
     self.encode = encode
Пример #24
0
def compare_brres_files(filenames, ignore_offsets=None, start_at=None, target=None):
    if len(filenames) < 1:
        print('Nothing to compare!')
        return
    brres = {}
    for x in filenames:
        brres[x] = Brres(x)
    for i in range(len(filenames) - 1):
        f1 = filenames[i]
        for j in range(i + 1, len(filenames)):
            f2 = filenames[j]
            compare_two_brres(brres[f1], brres[f2])
            CompareBins((brres[f1], brres[f2]),
                        ignore_offsets=ignore_offsets, start_at=start_at, target=target).compare_two_binfiles()
Пример #25
0
 def dropEvent(self, a0):
     data = a0.mimeData()
     if data.hasUrls():
         burls = self.get_brres_urls(data.urls())
         if burls:
             for path in burls:
                 self.add_new_brres_materials(Brres.get_brres(path, True))
             a0.accept()
             return
     elif data.hasText() and self.can_add_material(data.text()):
         mat = get_material_by_url(data.text(), True)
         if mat:
             self.add_new_material(mat)
             a0.accept()
             return
     a0.ignore()
Пример #26
0
 def test_convert_multi_bone_single_bind(self):
     original = self._get_brres(
         'simple_multi_bone_single_bind.brres').models[0]
     converter = DaeConverter(
         Brres(self._get_tmp('.brres'), read_file=False),
         self._get_test_fname('simple_multi_bone_single_bind.dae'))
     converter.load_model()
     mdl0 = converter.mdl0
     # test bones
     self.assertEqual(4, len(mdl0.bones))
     CheckPositions.bones_equal(mdl0.bones, original.bones, 0.01, 0.001)
     # Ensure that the bones are correctly linked
     expected_linked_bones = {
         'BlackNWhite1': 'simple001_BNW',
         'BlackNWhite2': 'simple011',
         'GreenCloud': 'simple002_Green',
         'Yellow': 'simple'
     }
     for x in mdl0.objects:
         self.assertEqual(expected_linked_bones[x.name], x.linked_bone.name)
     self.assertTrue(
         CheckPositions.positions_equal(original.vertices, mdl0.vertices))
Пример #27
0
 def openFiles(filenames):
     opened = Command.OPEN_FILES
     max = Command.MAX_FILES_OPEN  # max that can remain open
     if max - len(filenames) < 0:
         raise MaxFileLimit()
     to_open = [f for f in filenames if f not in opened]
     total = len(to_open) + len(opened)
     if total > max:
         active = Command.auto_close(total - max, to_open)
         for x in active:
             to_open.remove(x.name)
     else:
         active = []
     # open any that aren't opened
     for f in to_open:
         # try:
         brres = Brres.get_brres(f, True)
         opened[f] = brres
         active.append(brres)
     # except UnpackingError as e:
     #     AutoFix.get().error(e)
     return active
Пример #28
0
def cmdline_convert(args, ext, converter_klass):
    file_in, file_out, overwrite = arg_parse(args)
    if file_in.ext == '.brres':
        if not file_out.ext:
            file_out.ext = ext
        brres = Brres(file_in.get_path())
        converter = converter_klass(brres, file_out.get_path())
        converter.save_model()
    elif file_in.ext.lower() == ext:
        b_path = file_out.get_path()
        brres = Brres(b_path, None, os.path.exists(b_path))
        converter = converter_klass(brres, file_in.get_path())
        converter.load_model()
        brres.save(None, overwrite)
    else:
        print('Unknown file {}, is the file extension {}?'.format(
            ext, file_in.ext))
        sys.exit(1)
Пример #29
0
 def _get_brres(filename):
     if not filename.endswith('.brres'):
         filename += '.brres'
     return Brres(AbmattTest._get_brres_fname(filename))
Пример #30
0
 def setUpClass(cls):
     cls.brres = Brres(cls._get_brres_fname('beginner_course.brres'))