OBJ_Importer Module
This module contains the classes and functions related to handling 4D objects and importing them from OBJ files.
In this module we are dealing with the following optional code snippet of the Engine Loop:
mainmethod1def main(self): 2 3 ... 4 5 file_path = r"utils\resources\VideoShip.obj" 6 struc = Structure_Generator.load_from_obj(file_path) 7 self.mesh_list.extend(struc) 8 9 ...
Note
The Triangle4D class is defined in the Shape Module.
The OBJ_Importer class provides methods to import 3D vertices and faces from an OBJ file and create corresponding 4D points and triangles.
If the OBJ file is structured as a triangle mesh, the file contains v entries representing the vertices of an object. It also contains f entries, which represent faces. These faces contain numbers referring to the vertices, allowing a vertex to be reused multiple times.
.. class:: OBJ_Importer
- load_from_obj(filename: str) list
This method loads a 3D model from an OBJ file and converts the vertices and faces into 4D points and triangles.
- Parameters:
filename (str): The path to the OBJ file.
- Returns:
list: A list of Triangle4D objects representing the 3D model.
Example Usage:
from utils.shape import Triangle4D import numpy as np tris = OBJ_Importer.load_from_obj("path/to/model.obj") for tri in tris: print(tri)Full Code:
@staticmethod def load_from_obj(filename): try: with open(filename, 'r') as f: verts = [] tris = [] for line in f: if line.startswith('v '): parts = line.strip().split() v = OBJ_Importer.create_point(float(parts[1]), float(parts[2]), float(parts[3])) verts.append(v) if line.startswith('f '): parts = line.strip().split() f = [int(parts[1]), int(parts[2]), int(parts[3])] tris.append(Triangle4D(verts[f[0] - 1], verts[f[1] - 1], verts[f[2] - 1])) return tris except Exception as e: print(f"Failed to load {e}")Warning
Ensure that the OBJ file is well-formatted and contains only triangular faces. Check the
Triangulated Meshoption on how to export Wavefront OBJ Blender