From 5ca203a057c0bc492f59b62b884a32d9fb226b60 Mon Sep 17 00:00:00 2001 From: Stanislas Daniel Claude Dolcini Date: Sun, 4 Aug 2024 10:50:30 +0200 Subject: [PATCH] Use logging instead of print --- .../ImportPyrogenesisActor.py | 68 ++++++++++--------- io_scene_pyrogenesis/MaxColladaFixer.py | 5 +- 2 files changed, 40 insertions(+), 33 deletions(-) diff --git a/io_scene_pyrogenesis/ImportPyrogenesisActor.py b/io_scene_pyrogenesis/ImportPyrogenesisActor.py index c079a6c..054b5fb 100644 --- a/io_scene_pyrogenesis/ImportPyrogenesisActor.py +++ b/io_scene_pyrogenesis/ImportPyrogenesisActor.py @@ -4,6 +4,7 @@ import bpy import bpy_extras +import logging import os from . import MaxColladaFixer @@ -35,6 +36,9 @@ class ImportPyrogenesisActor(bpy.types.Operator, bpy_extras.io_utils.ImportHelpe default=-1 ) # type: ignore + def __init__(self): + self.logger = logging.getLogger('PyrogenesisActorImporter') + def draw(self, context): layout = self.layout @@ -60,7 +64,7 @@ def set_copy_transform_constraint(self, obj, parent): if(str(type(parent)) == ''): armature = self.find_parent_armature(parent) - print(obj.name + " -> " + armature.name + " -> " + parent.name) + self.logger.debug(obj.name + " -> " + armature.name + " -> " + parent.name) constraint = obj.constraints.new('COPY_LOCATION') constraint.show_expanded = False constraint.mute = False @@ -73,7 +77,7 @@ def set_copy_transform_constraint(self, obj, parent): constraint2.subtarget = parent.name return - print(obj.name + " -> " + parent.name) + self.logger.debug(obj.name + " -> " + parent.name) constraint = obj.constraints.new('COPY_LOCATION') constraint.show_expanded = False constraint.mute = False @@ -103,7 +107,7 @@ def create_new_material(self, textures, material): for texture in textures: fname = os.path.basename(texture.split('|')[1]) - print(fname) + self.logger.debug(fname) if fname not in bpy.data.images: continue @@ -178,7 +182,7 @@ def assign_material_to_object(self, ob, material_name): def import_pyrogenesis_actor(self, context): import xml.etree.ElementTree as ET - print('loading ' + self.filepath + '...') + self.logger.info('loading ' + self.filepath + '...') self.currentPath = (self.filepath[0:self.filepath.find('actors')]).replace("\\", "/") root = ET.parse(self.filepath).getroot() @@ -304,7 +308,7 @@ def parse_actor(self, root, proppoint="root", parentprops=[], rootObj=None, prop material_type = group.text if rootObj is not None: - print("Root object is:" + rootObj.name) + self.logger.debug("Root object is:" + rootObj.name) for group in root: if group.tag == 'material': @@ -351,9 +355,9 @@ def parse_actor(self, root, proppoint="root", parentprops=[], rootObj=None, prop for child in variant: if(child.tag == 'mesh' or child.tag == 'decal'): - print("=======================================================") - print("============== Gathering Mesh =========================") - print("=======================================================") + self.logger.info("=======================================================") + self.logger.info("============== Gathering Mesh =========================") + self.logger.info("=======================================================") # Get the objects prior to importing prior_objects = [Object for Object in bpy.context.scene.objects] @@ -370,7 +374,7 @@ def parse_actor(self, root, proppoint="root", parentprops=[], rootObj=None, prop fixer.execute() bpy.ops.wm.collada_import(filepath=mesh_path, import_units=True) except Exception: - print('Could not load' + mesh_path) + self.logger.error('Could not load' + mesh_path) else: bpy.ops.object.select_all(action='DESELECT') if material_type == 'default.xml' or 'terrain' in material_type: @@ -394,11 +398,11 @@ def parse_actor(self, root, proppoint="root", parentprops=[], rootObj=None, prop if b.data is None or b.data.uv_layers is None or not len(b.data.uv_layers): continue if len(b.data.uv_layers) > 0: - print("Renaming" + b.data.uv_layers[0].name + " to " + "UVMap") + self.logger.info("Renaming" + b.data.uv_layers[0].name + " to " + "UVMap") b.data.uv_layers[0].name = "UVMap" if len(b.data.uv_layers) > 1: - print("Renaming" + b.data.uv_layers[1].name + " to " + "AOMap") + self.logger.info("Renaming" + b.data.uv_layers[1].name + " to " + "AOMap") b.data.uv_layers[1].name = "AOMap" @@ -435,9 +439,9 @@ def parse_actor(self, root, proppoint="root", parentprops=[], rootObj=None, prop new_current_materials = [material for material in bpy.data.materials] for material in (set(new_current_materials) - set(prior_materials)): bpy.data.materials.remove(material) - print("=======================================================") - print("============== Setting Constraints ====================") - print("=======================================================") + self.logger.info("=======================================================") + self.logger.info("============== Setting Constraints ====================") + self.logger.info("=======================================================") for imported_object in imported_objects: # props are parented so they should follow their root object. @@ -458,21 +462,21 @@ def parse_actor(self, root, proppoint="root", parentprops=[], rootObj=None, prop if found: continue - print('' + imported_object.name + ' has no parent prop point named prop_' + proppoint + '. Root object name: ' + (rootObj.name if rootObj is not None else 'Undefined') + '') + self.logger.error('' + imported_object.name + ' has no parent prop point named prop_' + proppoint + '. Root object name: ' + (rootObj.name if rootObj is not None else 'Undefined') + '') if(child.tag == 'textures' and self.import_textures): - print("=======================================================") - print("============== Gathering Textures =====================") - print("=======================================================") + self.logger.info("=======================================================") + self.logger.info("============== Gathering Textures =====================") + self.logger.info("=======================================================") if(len(child) > 0): for texture in child: imported_textures.append(texture) if(child.tag == 'props' and self.import_props and (self.import_depth == -1 or (self.import_depth > propDepth and self.import_depth > 0))): - print("=======================================================") - print("============== Gathering Parent Props =================") - print("=======================================================") + self.logger.info("=======================================================") + self.logger.info("============== Gathering Parent Props =================") + self.logger.info("=======================================================") finalprops = imported_objects.copy() if len(finalprops) > 0: @@ -482,9 +486,9 @@ def parse_actor(self, root, proppoint="root", parentprops=[], rootObj=None, prop continue if hasattr(obj, 'type') and obj.type == 'ARMATURE': - print("=======================================================") - print("============== Gathering Armature Props ===============") - print("=======================================================") + self.logger.info("=======================================================") + self.logger.info("============== Gathering Armature Props ===============") + self.logger.info("=======================================================") for bone in obj.data.bones: if 'prop.' in bone.name: @@ -492,7 +496,7 @@ def parse_actor(self, root, proppoint="root", parentprops=[], rootObj=None, prop if 'prop-' in bone.name: bone.name = bone.name.replace('prop-','prop_') if 'prop_' in bone.name: - print(bone.name) + self.logger.debug(bone.name) finalprops.append(bone) continue @@ -501,14 +505,14 @@ def parse_actor(self, root, proppoint="root", parentprops=[], rootObj=None, prop finalprops.remove(obj) if rootObject is not None: - print(rootObject.name) + self.logger.debug(rootObject.name) for prop in child: imported_props.append(prop) mat_textures = [] for texture in imported_textures: - print("Loading " + texture.attrib['name'] + ": " + self.currentPath + 'textures/skins/' + texture.attrib['file']) + self.logger.info("Loading " + texture.attrib['name'] + ": " + self.currentPath + 'textures/skins/' + texture.attrib['file']) bpy.data.images.load(self.currentPath + 'textures/skins/' + texture.attrib['file'], check_existing=True) mat_textures.append(texture.attrib['name'] + '|' + self.currentPath + 'textures/skins/' + texture.attrib['file']) @@ -526,15 +530,15 @@ def parse_actor(self, root, proppoint="root", parentprops=[], rootObj=None, prop self.assign_material_to_object(obj, material_object) for prop in imported_props: - print("=======================================================") - print("============== Gathering Props ========================") - print("=======================================================") + self.logger.info("=======================================================") + self.logger.info("============== Gathering Props ========================") + self.logger.info("=======================================================") if prop.attrib['actor'] == "": continue try: prop_path = self.currentPath + 'actors/' + prop.attrib['actor'] - print('Loading ' + prop_path + '.') + self.logger.info('Loading ' + prop_path + '.') proproot = ET.parse(prop_path).getroot() propRootObj = self.find_prop_root_object(finalprops, prop.attrib['attachpoint']) @@ -543,7 +547,7 @@ def parse_actor(self, root, proppoint="root", parentprops=[], rootObj=None, prop self.parse_actor(proproot, prop.attrib['attachpoint'], meshprops if finalprops is None or len(finalprops) <= 0 else finalprops, rootObject, propDepth + 1) except Exception: - print('Could not load' + mesh_path) + self.logger.error('Could not load' + mesh_path) def find_prop_root_object(self, imported_objects, proppoint): diff --git a/io_scene_pyrogenesis/MaxColladaFixer.py b/io_scene_pyrogenesis/MaxColladaFixer.py index e4bbeb7..bcc0d4f 100644 --- a/io_scene_pyrogenesis/MaxColladaFixer.py +++ b/io_scene_pyrogenesis/MaxColladaFixer.py @@ -2,6 +2,8 @@ # This file is part of 0 A.D. # SPDX-License-Identifier: GPL-2.0-or-later +import logging + class MaxColladaFixer: file_path = None collada_prefix = '{http://www.collada.org/2005/11/COLLADASchema}' @@ -27,6 +29,7 @@ def indent(self, elem, level=0): def __init__(self, file_path=None): self.file_path = file_path + self.logger = logging.getLogger('PyrogenesisActorImporter.' % __name__) def execute(self): import xml.etree.ElementTree as ET @@ -37,7 +40,7 @@ def execute(self): root = tree.getroot() new_elements = [] for child in root: - print("Fixing Collada..." + child.tag) + self.logger.info("Fixing Collada..." + child.tag) for child in root[:]: if child.tag == self.collada_prefix + 'library_images': root.remove(child)