Skip to content

Commit

Permalink
Fix material file search
Browse files Browse the repository at this point in the history
When searching for material file of model the name of the file can sometimes be in different cases than the name of stored file (usually in lower case). This is fixed by searching for file by the name in case insensitive way.
  • Loading branch information
smlu committed May 7, 2020
1 parent 19399aa commit 00aa606
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 20 deletions.
4 changes: 4 additions & 0 deletions ijim/model/model3doImporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ def _make_mesh(mesh3do: ModelMesh, mat_list: List):
# Set face material index
mat_name = mat_list[face3do.materialIdx]
mat = getGlobalMaterial(mat_name)
if mat is None:
print("\nWarning: could not find material file '{}'".format(mat_name))
mat = makeNewGlobalMaterial(mat_name)

if not mat.name in mesh.materials:
mesh.materials.append(mat)
face.material_index = mesh.materials.find(mat.name)
Expand Down
19 changes: 4 additions & 15 deletions ijim/model/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from ijim.material.material import importMatFile
from ijim.types.vector import Vector3f
from ijim.utils.utils import *
from .model3do import GeometryMode

import math
Expand Down Expand Up @@ -139,20 +140,8 @@ def importMaterials(mat_names: List, search_paths: List):
for name in mat_names:
if name in bpy.data.materials:
continue

mat_imported = False
for path in search_paths:
mat_path = path + '/' + name
if os.path.isfile(mat_path) and os.access(mat_path, os.R_OK):
mat_path = getFilePathInDir(name, path)
if mat_path is not None:
importMatFile(mat_path)
mat_imported = True
break

if not mat_imported:
print("\nWarning: could not find material file '{}'".format(name))
mat = bpy.data.materials.new(name)
mat.texture_slots.add()
ts = mat.texture_slots[0]
print(ts.name)
ts.texture_coords = 'UV'
ts.uv_layer = 'UVMap'
break
56 changes: 51 additions & 5 deletions ijim/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,59 @@ def getBmeshFaceLayer(faces: bmesh.types.BMFaceSeq, name: str):
return faces.layers.string.get(name) or faces.layers.string.new(name)

def getDefaultMatFolders(model_path):
path1 = os.path.dirname(model_path) + '/' + 'mat'
path2 = os.path.abspath(os.path.join(os.path.dirname(model_path), os.pardir)) + '/' + 'mat'
os.path.join(os.path.dirname(model_path), 'mat')
path1 = os.path.join(os.path.dirname(model_path), 'mat')
path2 = os.path.abspath(os.path.join(os.path.dirname(model_path), os.pardir))
path2 = os.path.join(path2, 'mat')
return [path1, path2]


def getGlobalMaterial(name):
return bpy.data.materials[name]
def getFilePathInDir(fileName: str, dirPath: str, insensitive=True):
"Returns string file path in dir if file exists otherwise None"

if not os.path.isdir(dirPath) or len(fileName) < 1:
return None

def file_exists(file_path):
return os.path.isfile(file_path) and os.access(file_path, os.R_OK)

file_path = os.path.join(dirPath, fileName)
if file_exists(file_path):
return file_path

if insensitive:
# Try to find the file by lower-cased name
fileName = fileName.lower()
file_path = os.path.join(dirPath, fileName)
if file_exists(file_path):
return file_path

# Ok, now let's go through all files in folder and
# try to find file by case insensitive comparing it.
# to other file names.
for f in os.listdir(dirPath):
file_path = os.path.join(dirPath, f)
if file_exists(file_path) and f.lower() == fileName:
return file_path

def getGlobalMaterial(name: str):
if name in bpy.data.materials:
return bpy.data.materials[name]

name = name.lower()
if name in bpy.data.materials:
return bpy.data.materials[name]

for mat in bpy.data.materials:
if(mat.name.lower() == name):
return mat

def makeNewGlobalMaterial(name: str):
mat = bpy.data.materials.new(name)
mat.texture_slots.add()
ts = mat.texture_slots[0]
ts.texture_coords = 'UV'
ts.uv_layer = 'UVMap'
return mat

def clearSceneAnimData(scene):
scene.timeline_markers.clear()
Expand Down

0 comments on commit 00aa606

Please sign in to comment.