| 390 | |
| 391 | |
| 392 | class Material: |
| 393 | def __init__(self, name:str) -> None: |
| 394 | self.name = name |
| 395 | self.texture = None |
| 396 | self.normal_map = None |
| 397 | self.texture_path = None |
| 398 | self.normal_map_path = None |
| 399 | |
| 400 | |
| 401 | def load_mtl(obj:Object, filename:str, dir:str) -> bool: |
| 402 | if not isfile(dir + filename): |
| 403 | print("\033[1;31mWARNING: MTL FILE NO FOUND.\n\033[0m") |
| 404 | sleep(2) |
| 405 | return False |
| 406 | with open(dir + filename, "r") as mtl_file: |
| 407 | for line in mtl_file.readlines(): |
| 408 | if line.startswith("#"): |
| 409 | continue |
| 410 | elif line.startswith("newmtl "): |
| 411 | name = line.strip()[7:] |
| 412 | if name in obj.materials: |
| 413 | raise Exception("Name Collision: " + |
| 414 | "2 or more materials share the same name.") |
| 415 | else: |
| 416 | current_material = Material(name) |
| 417 | obj.materials[name] = current_material |
| 418 | elif line.startswith("map_Kd "): |
| 419 | img = line.strip()[7:].replace("\\", "/") |
| 420 | # Absolute path |
| 421 | if "/" in img: |
| 422 | if not isfile(img): |
| 423 | print("\033[1;31m" + |
| 424 | "WARNING: TEXTURE FILE NO FOUND.\n") |
| 425 | sleep(2) |
| 426 | else: |
| 427 | current_material.texture = png.Png(img, "") |
| 428 | current_material.texture_path = img |
| 429 | # width and height will be used in uv mapping |
| 430 | # u/v * width/height |
| 431 | # if not substracted by 1, |
| 432 | # the index will be out of range when u/v == 1 |
| 433 | current_material.texture.width -= 1 |
| 434 | current_material.texture.height -= 1 |
| 435 | |
| 436 | # Relative path |
| 437 | else: |
| 438 | if not isfile(dir + img): |
| 439 | print("\033[1;31m" + |
| 440 | "WARNING: TEXTURE FILE NO FOUND.\n") |
| 441 | sleep(2) |
| 442 | else: |
| 443 | current_material.texture = png.Png(img, dir) |
| 444 | current_material.texture_path = dir + img |
| 445 | # width and height will be used in uv mapping |
| 446 | # u/v * width/height |
| 447 | # if not substracted by 1, |
| 448 | # the index will be out of range when u/v == 1 |
| 449 | current_material.texture.width -= 1 |