file can either be the name of the file or the path If path is entered, dir should be None
(self, file:str, dir:str=None)
| 78 | |
| 79 | |
| 80 | def load_obj(self, file:str, dir:str=None): |
| 81 | """ |
| 82 | file can either be the name of the file or the path |
| 83 | If path is entered, dir should be None |
| 84 | |
| 85 | """ |
| 86 | dir = dir.replace("\\", "/") if type(dir) == str else None |
| 87 | if dir is None: |
| 88 | dir = Object.default_obj_dir |
| 89 | elif not dir.endswith("/") and dir != "": |
| 90 | dir += "/" |
| 91 | if not file.endswith(".obj"): |
| 92 | file += ".obj" |
| 93 | file = file.replace("\\", "/") |
| 94 | # file is given as path |
| 95 | if "/" in file: |
| 96 | # dir should be None, but if it is None, it has been changed to |
| 97 | # Object.default_obj_dir by now |
| 98 | if dir != Object.default_obj_dir: |
| 99 | raise Exception("dir should remain None if file is given as a path") |
| 100 | # Separate the name and the dir |
| 101 | file = file.split("/") |
| 102 | dir = "/".join(file[:-1]) + "/" |
| 103 | file = file[-1] |
| 104 | filepath = dir + file |
| 105 | print(f"Starts to load obj file: {file} \n(Path: {filepath})") |
| 106 | with open(filepath, "r") as obj_file: |
| 107 | # Will be added to the f value |
| 108 | convert_to_left_hand = False |
| 109 | # Blender uses right hand coordinate system, whereas my |
| 110 | # renderer uses left hand one. |
| 111 | if obj_file.readline(9) == "# Blender": |
| 112 | convert_to_left_hand = True |
| 113 | # The index stored in obj starts at 1 instead of 0, |
| 114 | # so later the value stored in self.faces will be |
| 115 | # substracted by 1. Given that it also needs to be |
| 116 | # substracted by the length of the already stored value, |
| 117 | # I think it's a good idea to merge the two steps so |
| 118 | # It won't be necessary to do one more subtraction |
| 119 | # every time it add a vertex information. |
| 120 | v_count_plus_1 = 1 |
| 121 | vt_count_plus_1 = 1 |
| 122 | vn_count_plus_1 = 1 |
| 123 | for line in obj_file.readlines(): |
| 124 | line = line.strip() |
| 125 | if line.startswith("#"): |
| 126 | continue |
| 127 | elif line.startswith("mtllib "): |
| 128 | print(f"Loading MTL file: {dir + line[7:]}") |
| 129 | mtl_loaded = Material.load_mtl(self, line[7:], dir) |
| 130 | print(f"Finish loading {dir + line[7:]}") |
| 131 | elif line.startswith("o "): |
| 132 | current_obejct = Object(name = line[2:]) |
| 133 | self.objects.append(current_obejct) |
| 134 | print(f"Loading {current_obejct.name}") |
| 135 | v_offset = v_count_plus_1 |
| 136 | vt_offset = vt_count_plus_1 |
| 137 | vn_offset = vn_count_plus_1 |
nothing calls this directly
no test coverage detected