Using OpenGL with Python: Loading .Obj Models

Posted by

OpenGL with Python 5: Loading Obj Models

OpenGL with Python 5: Loading Obj Models

In this tutorial, we will learn how to load Obj models in OpenGL using Python. Obj models are a popular file format for representing 3D models with vertices, texture coordinates, and normals.

First, we need to install the required libraries for this tutorial. We will use PyOpenGL, PyOpenGL-accelerate, and PyObj for loading and rendering the Obj models. You can install these libraries using pip:


pip install PyOpenGL PyOpenGL-accelerate PyObj

Once you have installed the necessary libraries, let’s move on to loading the Obj models in our OpenGL program. We will use the below code snippet to load an Obj model:


import objloader

obj = objloader.OBJ("model.obj")
vertices, normals, texcoords, faces = obj.unpack('T2', 'N3', 'VT2', faces=True)

With the above code snippet, we have loaded the Obj model and extracted its vertices, normals, texture coordinates, and faces. Now, we can render this model in our OpenGL program.

Below is a simple code snippet to render the loaded Obj model:


import OpenGL.GL as gl

def render():
gl.glBegin(gl.GL_TRIANGLES)
for face in faces:
for i in range(3):
vertex_index, texture_index, normal_index = face[i]
gl.glVertex3fv(vertices[vertex_index])
gl.glNormal3fv(normals[normal_index])
gl.glTexCoord2fv(texcoords[texture_index])
gl.glEnd()

Finally, we need to call the render function in our OpenGL program to display the loaded Obj model on the screen. You can combine this code with other OpenGL features like lighting, shading, textures, etc., to create a visually appealing 3D rendering.

That’s it! You have now learned how to load Obj models in OpenGL using Python. Feel free to experiment with different Obj models and OpenGL functionalities to create stunning 3D visuals in your applications. Happy coding!

0 0 votes
Article Rating
7 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@ZwaneMakki
3 months ago

I am using Ren'Py and I want load a glb file and show it in my game. Pygame_SDL2 is used by Ren'py. Nothing on the openGL? Does this work in Ren'py. Why is t so difficult to just show this basic thing and power of python? Weird. Can anyone help. Thanks

@nhakanasi2038
3 months ago

May I ask, when following your code, some of my .obj files don't have any vn values but the f value still have 3 digit in it, so when it call the method make_corner(), this line will result in an error since there are not enough index:
for element in vn[int(v_vt_vn[2]) – 1]:

vertices.append(element)
I think my obj file is fine since I can open it in blender and other programs, is there anyway to fix it ?

@gigios8607
3 months ago

Thank you so much for your great videos. I'm having great fun following them.
I just felt like suggesting 2 improvements.

1) in the `make_corner` method, you can make it look prettier as well as get a slight speed
boost by using the List.extend() method, instead of the for-loops, as follows:

def make_corner(self, corner_description, v, vt, vn, vertices):
v_vt_vn = corner_description.split('/')
vertices.extend( v[int(v_vt_vn[0]) – 1])
vertices.extend(vt[int(v_vt_vn[1]) – 1])
vertices.extend(vn[int(v_vt_vn[2]) – 1])

2) to get a bonus boost, in the 3 "read_data" methods you can return a tuple instead of a list of floats. This way, the extend method is even faster, plus, creating lists is more expensive. Also, we know these collections will always remain of fixed lenght.

def read_vertex_data(self, words):
return (
float(words[1]),
float(words[2]),
float(words[3])
)

Thanks again for all your efforts!

@xz3n
3 months ago

Will you make a tutorial on another OpenGL Python library called ModernGL? It's faster than PyOpenGL and has Pythonic syntax

@XFajk
3 months ago

Great video love your tutorials Want to ask will you make a wgpu tutorial?

@sugarboy4280
3 months ago

Thanks for your tutorials, they're great.

@SkyFly19853
3 months ago

Oh…
Python videos are back… 😏✅✅✅