MCPcopy Create free account
hub / github.com/ICE27182/Python-3D-renderer / load_mtl

Method load_mtl

pyrender.py:401–490  ·  view source on GitHub ↗
(obj:Object, filename:str, dir:str)

Source from the content-addressed store, hash-verified

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
450 current_material.texture.height -= 1
451
452
453 elif (line.lower().startswith("map_bump ") or
454 line.lower().startswith("bump ")):
455 img = line.strip().split()[-1]
456 # Absolute path
457 if "/" in img:
458 if not isfile(img):

Callers 1

load_objMethod · 0.80

Calls 1

MaterialClass · 0.85

Tested by

no test coverage detected