From 2ce9b3aa752c1f0e6acbce9c36f1a4e002cdabdf Mon Sep 17 00:00:00 2001 From: Pyogenics Date: Mon, 31 Mar 2025 20:48:35 +0100 Subject: [PATCH] Sloppy material importing for maps --- io_scene_a3d/A3DBlenderImporter.py | 16 +------- io_scene_a3d/BattleMapBlenderImporter.py | 48 ++++++++++++++++++++++-- io_scene_a3d/BlenderUtils.py | 36 ++++++++++++++++++ 3 files changed, 82 insertions(+), 18 deletions(-) create mode 100644 io_scene_a3d/BlenderUtils.py diff --git a/io_scene_a3d/A3DBlenderImporter.py b/io_scene_a3d/A3DBlenderImporter.py index cf47b7e..6326747 100644 --- a/io_scene_a3d/A3DBlenderImporter.py +++ b/io_scene_a3d/A3DBlenderImporter.py @@ -32,21 +32,7 @@ from .A3DObjects import ( A3D_VERTEXTYPE_COLOR, A3D_VERTEXTYPE_NORMAL2 ) - -def addImageTextureToMaterial(image, node_tree): - nodes = node_tree.nodes - links = node_tree.links - - # Check if this material already has a texture on it - if len(nodes) > 2: - return - - # Create nodes - principledBSDFNode = nodes[0] - textureNode = nodes.new(type="ShaderNodeTexImage") - links.new(textureNode.outputs["Color"], principledBSDFNode.inputs["Base Color"]) - # Apply image - if image != None: textureNode.image = image +from .BlenderUtils import addImageTextureToMaterial def mirrorUVY(uv): x, y = uv diff --git a/io_scene_a3d/BattleMapBlenderImporter.py b/io_scene_a3d/BattleMapBlenderImporter.py index 7bfb99f..340f503 100644 --- a/io_scene_a3d/BattleMapBlenderImporter.py +++ b/io_scene_a3d/BattleMapBlenderImporter.py @@ -23,10 +23,13 @@ SOFTWARE. from json import load import bpy +from bpy_extras.node_shader_utils import PrincipledBSDFWrapper +from bpy_extras.image_utils import load_image import bmesh from .A3D import A3D from .A3DBlenderImporter import A3DBlenderImporter +from .BlenderUtils import addImageTextureToMaterial class PropLibrary: propCache = {} @@ -64,7 +67,10 @@ class PropLibrary: self.propCache[name] = ob return self.propCache[name] - + + def getTexture(self, textureName): + im = load_image(textureName, self.directory) + return im class BattleMapBlenderImporter: # Allows subsequent map loads to be faster @@ -77,9 +83,16 @@ class BattleMapBlenderImporter: self.import_collision_geom = import_collision_geom self.import_spawn_points = import_spawn_points + self.materials = {} + def importData(self): print("Importing BattleMap data into blender") + # Process materials + for materialData in self.mapData.materials: + ma = self.createBlenderMaterial(materialData) + self.materials[materialData.ID] = ma + propObjects = [] if self.import_static_geom: # Load props @@ -145,7 +158,11 @@ class BattleMapBlenderImporter: propOB.rotation_mode = "XYZ" propOB.rotation_euler = propData.rotation propOB.scale = propData.scale - + + # Material + ma = self.materials[propData.materialID] + propOB.data.materials[0] = ma + return propOB def createBlenderCollisionTriangles(self, collisionTriangles): @@ -235,4 +252,29 @@ class BattleMapBlenderImporter: ob.rotation_mode = "XYZ" ob.rotation_euler = spawnPointData.rotation - return ob \ No newline at end of file + return ob + + def createBlenderMaterial(self, materialData): + ma = bpy.data.materials.new(f"{materialData.ID}_{materialData.name}") + + # Shader specific logic + if materialData.shader == "TankiOnline/SingleTextureShader": + # First check if we've already loaded the required prop library + if not "Remaster" in self.libraryCache: + # Load the proplib + libraryPath = f"{self.propLibrarySourcePath}/Remaster" # XXX: Get platform agnostic way of doing this + library = PropLibrary(libraryPath) + self.libraryCache["Remaster"] = library + + # Try load texture + textureParameter = materialData.textureParameters[0] + library = self.libraryCache["Remaster"] #XXX: libraryName is optional + image = library.getTexture(f"{textureParameter.textureName}.webp") + + # Apply texture + maWrapper = PrincipledBSDFWrapper(ma, is_readonly=False, use_nodes=True) + addImageTextureToMaterial(image, ma.node_tree) + elif materialData.shader == "TankiOnline/SpriteShader": + pass + + return ma \ No newline at end of file diff --git a/io_scene_a3d/BlenderUtils.py b/io_scene_a3d/BlenderUtils.py new file mode 100644 index 0000000..c8f8c26 --- /dev/null +++ b/io_scene_a3d/BlenderUtils.py @@ -0,0 +1,36 @@ +''' +Copyright (c) 2025 Pyogenics + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +''' + +def addImageTextureToMaterial(image, node_tree): + nodes = node_tree.nodes + links = node_tree.links + + # Check if this material already has a texture on it + if len(nodes) > 2: + return + + # Create nodes + principledBSDFNode = nodes[0] + textureNode = nodes.new(type="ShaderNodeTexImage") + links.new(textureNode.outputs["Color"], principledBSDFNode.inputs["Base Color"]) + # Apply image + if image != None: textureNode.image = image \ No newline at end of file