def run_save(self): files_to_save = MATCHING.findAll(self.file, self.ACTIVE_FILES) if len(files_to_save) > 1 and self.destination is not None: raise SaveError('Detected {} files and only one destination "{}"'.format(len(files_to_save), self.destination)) else: for x in files_to_save: if not x.save(self.destination, self.overwrite): raise SaveError('File already Exists!')
def run_convert(self): if self.ext == '.dae': from abmatt.converters.convert_dae import DaeConverter2 klass = DaeConverter2 elif self.ext == '.obj': from abmatt.converters.convert_obj import ObjConverter klass = ObjConverter else: raise ParsingException('Unknown conversion format {}'.format(self.ext)) active_files = self.ACTIVE_FILES if not self.name: files = [x.name for x in active_files] else: files = self.getFiles(self.name) if not files: return False multiple_files = False if len(files) < 2 else True brres = None if self.is_import: if not self.destination: if active_files and not multiple_files: brres = active_files[0] else: brres = None else: brres = self.create_or_open(self.destination) for file in files: if not brres: converter = klass(self.create_or_open(convert_file_ext(file, '.brres')), file, self.flags) else: converter = klass(brres, file, self.flags) base_name = os.path.splitext(os.path.basename(converter.brres.name))[0] model = self.model if model and len(model) > len(base_name) and model.startswith(base_name + '-'): model = model[len(model) + 1:] mdl = converter.load_model(model) brres = converter.brres else: # export dest_auto = True if multiple_files or os.path.basename(self.destination).lower() == '*' + self.ext else False for file in files: destination = self.destination if not dest_auto \ else os.path.join(os.path.dirname(self.destination), convert_file_ext(os.path.basename(file), self.ext)) brres = self.create_or_open(file) converter = klass(brres, destination, self.flags) models = MATCHING.findAll(self.model, brres.models) if len(models) > 1: multi_model = True destination = destination[:-1 * len(self.ext)] else: multi_model = False for mdl0 in models: if multi_model: converter.mdl_file = destination + '-' + mdl0.name + self.ext converter.save_model(mdl0) if brres is not None: self.updateSelection(brres.name)
def removeLayerByName(self, name): """Removes the layer if found, (for removing only the animation)""" j = 0 matches = MATCHING.findAll(name, self.tex_animations) for i in range(len(self.texEnabled)): if self.texEnabled[i]: if self.tex_animations[j] in matches: self.texEnabled[i] = False self.tex_animations.pop(j) self.mark_modified() return j += 1 raise ValueError('{} No layer matching {}'.format(self.name, name))
def updateFile(filename): # check in opened files files = MATCHING.findAll(filename, Command.OPEN_FILES.values()) if files: Command.ACTIVE_FILES = files else: # try to find file path files = Command.getFiles(filename) if Command.DESTINATION: # check for multiple files with single destination outside_active = True for x in Command.ACTIVE_FILES: if files[0] == x.name: outside_active = False break if len(files) > 1 or Command.ACTIVE_FILES and outside_active: raise SaveError('Multiple files for single destination') Command.ACTIVE_FILES = Command.openFiles(files) Command.MODELS = [] Command.MATERIALS = [] return Command.ACTIVE_FILES
def updateTypeSelection(self): """ Updates the current selection by the type""" if self.updateType(): type = Command.SELECT_TYPE if type == 'material': Command.SELECTED = self.MATERIALS elif type == 'layer': if self.SELECT_ID_NUMERIC: selected = [] for x in self.MATERIALS: layer = x.getLayerI(self.SELECT_ID) if layer: selected.append(layer) Command.SELECTED = selected else: Command.SELECTED = [] for x in self.MATERIALS: layers = MATCHING.findAll(self.SELECT_ID, x.layers) if layers: Command.SELECTED.extend(layers) # if not Command.SELECTED: # Forcibly adding case if no selected found # # possible todo... make this optional? # Command.SELECTED = [x.forceAdd(self.SELECT_ID) for x in self.MATERIALS] elif type == 'shader' or type == 'stage': shaders = getShadersFromMaterials(self.MATERIALS) if type == 'shader': Command.SELECTED = shaders else: if self.SELECT_ID == '*': Command.SELECTED = [] for x in shaders: Command.SELECTED.extend(x.stages) else: Command.SELECTED = [x.getStage(self.SELECT_ID) for x in shaders] elif type == 'mdl0': if self.SELECT_ID_NUMERIC: brres = getBrresFromMaterials(self.MATERIALS) Command.SELECTED = {x.getModelI(self.SELECT_ID) for x in brres} if None in Command.SELECTED: Command.SELECTED.remove(None) else: Command.SELECTED = MATCHING.findAll(self.SELECT_ID, getParents(self.MATERIALS)) elif type == 'brres': if self.cmd in ('add', 'remove'): Command.SELECTED = getBrresFromMaterials(self.MATERIALS) else: Command.SELECTED = MATCHING.findAll(self.SELECT_ID, getBrresFromMaterials(self.MATERIALS)) elif 'srt0' in type: srts = [x.srt0 for x in self.MATERIALS if x.srt0] if 'layer' in type: Command.SELECTED = [] if self.SELECT_ID_NUMERIC: for x in srts: anim = x.getTexAnimationByID(self.SELECT_ID) if anim: Command.SELECTED.append(anim) else: for x in srts: anim = MATCHING.findAll(self.SELECT_ID, x.tex_animations) if anim: Command.SELECTED.extend(anim) else: # material animation Command.SELECTED = srts elif 'pat0' in type: Command.SELECTED = [x.pat0 for x in self.MATERIALS if x.pat0] elif 'tex0' == type: Command.SELECTED = [] for x in getBrresFromMaterials(self.MATERIALS): Command.SELECTED.extend(MATCHING.findAll(self.SELECT_ID, x.textures))
def getTextures(self, name): return MATCHING.findAll(name, self.textures)
def getModelsByName(self, name): return MATCHING.findAll(name, self.models)
def get_materials_by_name(self, name): return MATCHING.findAll(name, self.materials)
def get_models_by_name(self, name): return MATCHING.findAll(name, self.models)
def load_config(app_dir=None, loudness=None, autofix_level=None): if app_dir is None: app_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'etc', 'abmatt') conf = Config.get_instance(os.path.join(app_dir, 'config.conf')) tmp_dir = os.path.join(app_dir, 'temp_files') converter = ImgConverter(tmp_dir) Tex0.converter = converter if not loudness: loudness = conf['loudness'] if loudness: try: AutoFix.set_loudness(loudness) except ValueError: AutoFix.warn('Invalid loudness level {}'.format(loudness)) AutoFix.set_fix_level(autofix_level, turn_off_fixes) if not len(conf): AutoFix.warn('No configuration detected (etc/abmatt/config.conf).') return Command.set_max_brres_files(conf) # Matching stuff MATCHING.set_case_sensitive(conf['case_sensitive']) MATCHING.set_partial_matching(conf['partial_matching']) MATCHING.set_regex_enable(conf['regex_matching']) # Autofixes try: SubFile.FORCE_VERSION = validBool(conf['force_version']) except ValueError: pass try: Brres.REMOVE_UNUSED_TEXTURES = validBool( conf['remove_unused_textures']) except ValueError: pass try: Layer.MINFILTER_AUTO = validBool(conf['minfilter_auto']) except ValueError: pass set_rename_unknown(conf['rename_unknown_refs']) set_remove_unknown(conf['remove_unknown_refs']) set_remove_unused(conf['remove_unused_refs']) try: Mdl0.DETECT_MODEL_NAME = validBool(conf['detect_model_name']) except ValueError: pass try: Shader.MAP_ID_AUTO = validBool(conf['map_id_auto']) except ValueError: pass try: Material.DEFAULT_COLOR = parse_color(conf['default_material_color']) except ValueError: pass try: Tex0.RESIZE_TO_POW_TWO = validBool(conf['resize_pow_two']) except ValueError: pass try: Tex0.set_max_image_size(validInt(conf['max_image_size'], 0, 10000)) except (TypeError, ValueError): pass try: Geometry.ENABLE_VERTEX_COLORS = validBool(conf['enable_vertex_colors']) except ValueError: pass Converter.ENCODE_PRESET = conf['encode_preset'] resample = conf['img_resample'] if resample is not None: ImgConverterI.set_resample(resample) if conf['material_library']: MaterialLibrary.LIBRARY_PATH = conf.config.get('material_library') else: MaterialLibrary.LIBRARY_PATH = os.path.join(app_dir, 'mat_lib.brres') return conf
def getLayerByName(self, key): """Attempts to get layer(s) by string key""" layers = MATCHING.findAll(key, self.layers) if layers: return layers[0]