Skip to content

Commit

Permalink
Use logging instead of print
Browse files Browse the repository at this point in the history
  • Loading branch information
StanleySweet committed Aug 4, 2024
1 parent bbcc4c8 commit 5ca203a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 33 deletions.
68 changes: 36 additions & 32 deletions io_scene_pyrogenesis/ImportPyrogenesisActor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import bpy
import bpy_extras
import logging
import os
from . import MaxColladaFixer

Expand Down Expand Up @@ -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

Expand All @@ -60,7 +64,7 @@ def set_copy_transform_constraint(self, obj, parent):

if(str(type(parent)) == '<class \'bpy_types.Bone\'>'):
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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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':
Expand Down Expand Up @@ -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]
Expand All @@ -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:
Expand All @@ -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"


Expand Down Expand Up @@ -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.
Expand All @@ -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:
Expand All @@ -482,17 +486,17 @@ 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:
bone.name = bone.name.replace('prop.','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
Expand All @@ -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'])

Expand All @@ -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'])
Expand All @@ -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):
Expand Down
5 changes: 4 additions & 1 deletion io_scene_pyrogenesis/MaxColladaFixer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}'
Expand All @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 5ca203a

Please sign in to comment.