MCPcopy Index your code
hub / github.com/DLR-RM/BlenderProc / load_obj

Function load_obj

blenderproc/python/loader/ObjectLoader.py:15–113  ·  view source on GitHub ↗

Import all objects for the given file and returns the loaded objects In .obj files a list of objects can be saved in. In .ply files only one object can be saved so the list has always at most one element :param filepath: the filepath to the location where the data is stored :param

(filepath: str, cached_objects: Optional[Dict[str, List[MeshObject]]] = None, **kwargs)

Source from the content-addressed store, hash-verified

13
14
15def load_obj(filepath: str, cached_objects: Optional[Dict[str, List[MeshObject]]] = None, **kwargs) -> List[MeshObject]:
16 """ Import all objects for the given file and returns the loaded objects
17
18 In .obj files a list of objects can be saved in.
19 In .ply files only one object can be saved so the list has always at most one element
20
21 :param filepath: the filepath to the location where the data is stored
22 :param cached_objects: a dict of filepath to objects, which have been loaded before, to avoid reloading
23 (the dict is updated in this function)
24 :param kwargs: all other params are handed directly to the bpy loading fct. check the corresponding documentation
25 :return: The list of loaded mesh objects.
26 """
27 if not os.path.exists(filepath):
28 raise FileNotFoundError(f"The given filepath does not exist: {filepath}")
29
30 if cached_objects is not None and isinstance(cached_objects, dict):
31 if filepath in cached_objects.keys():
32 created_obj = []
33 for obj in cached_objects[filepath]:
34 # duplicate the object
35 created_obj.append(obj.duplicate())
36 return created_obj
37 loaded_objects = load_obj(filepath, cached_objects=None, **kwargs)
38 cached_objects[filepath] = loaded_objects
39 return loaded_objects
40 # save all selected objects
41 previously_selected_objects = bpy.context.selected_objects
42 if filepath.endswith(".obj"):
43 # Set validate_meshes to False per default to be backwards compatible
44 if "validate_meshes" not in kwargs:
45 kwargs["validate_meshes"] = False
46 # load an .obj file:
47 bpy.ops.wm.obj_import(filepath=filepath, **kwargs)
48 elif filepath.endswith(".ply"):
49 PLY_TEXTURE_FILE_COMMENT = "comment TextureFile "
50 model_name = os.path.basename(filepath)
51
52 # Read file
53 with open(filepath, "r", encoding="latin-1") as file:
54 ply_file_content = file.read()
55
56 # Check if texture file is given
57 if PLY_TEXTURE_FILE_COMMENT in ply_file_content:
58 # Find name of texture file
59 texture_file_name = re.search(f"{PLY_TEXTURE_FILE_COMMENT}(.*)\n", ply_file_content).group(1)
60
61 # Determine full texture file path
62 texture_file_path = os.path.join(os.path.dirname(filepath), texture_file_name)
63 material = create_material_from_texture(
64 texture_file_path, material_name=f"ply_{model_name}_texture_model"
65 )
66
67 # Change content of ply file to work with blender ply importer
68 new_ply_file_content = ply_file_content
69 new_ply_file_content = new_ply_file_content.replace("property float texture_u", "property float s")
70 new_ply_file_content = new_ply_file_content.replace("property float texture_v", "property float t")
71
72 # Create temporary .ply file

Callers 11

load_ikeaFunction · 0.90
load_shapenetFunction · 0.90
load_replicaFunction · 0.90
load_AMASSFunction · 0.90
load_objMethod · 0.90
load_matterport3dFunction · 0.90
load_geometryFunction · 0.90
load_meshMethod · 0.90
load_scenenetFunction · 0.90
load_pix3dFunction · 0.90
load_furniture_objsMethod · 0.90

Calls 7

convert_to_meshesFunction · 0.90
replaceMethod · 0.80
map_vertex_colorMethod · 0.80
set_cpMethod · 0.80
duplicateMethod · 0.45

Tested by

no test coverage detected