Python rendering engine

Hello, devs!

I need to complete a project for the Software Engineering class and I was thinking that I can write my own render engine in Python. I have no idea how to do it, but I was always keen to look under the hood of this process.

It could be an extremely basic and super slow engine but at a bare minimum, it should be able to render provided model (say in obj format) with some basic lighting and material.

There is an awesome book Raytracing in One Weekend and it looks like I just need to reproduce it.

It’s feasible for a junior/mid-level programmer to complete this task in a couple of months? How hard would be to substitute a procedural sphere used in the examples with real geometry? Do you have other cool resources regarding this topic? What could be potential blockers for finalizing the project?

1 Like

Hey kiryha,

This is definitely possible and there have been a few developers out there who have embarked on this journey. While there are some game engines for python and other libraries, a few have decided to try out pure* python renderers.

Here are a few links to check out:
Graphics Engine from scratch in Python
Rendeer (Python renderer build by a ~16 year old)

1 Like

The Coding Train has some great videos about raytracing / raymarching. It’s not Python, but the same concepts apply.

1 Like

@BenWall
Hey, Ben, The Coding Train has tons of videos,
Are these one and two that you meant? Did I miss anything else?

This should be pretty doable, though a Python-only version will be pretty slow. If you decide to try it, think hard about:

  1. memory layout – can you do this without creating and destroying lots of temporary objects, which is a big perf drain
  2. limiting state – can you set it up without having to pass around lots of big heavy weight objects (ie, do you really need a class for points, or can you do it with a tuple, that sort of thing)
  3. You can and should happily cheat on the side problems: if you’re wrting a raytracer, concentrate on raytracing and don’t get bogged down in writing your own graphics file read-write code; use libraries for that sort of thing
1 Like

Thanks, Theodox,

I am not concerned about the speed or memory consumption (at least at this point). The goal is to build something simple and understand how rendering works on the lowest level, where the magic of pure math happening :slight_smile:

I get some progress, the render of the background image 4x2 pixels without any geometry, and even at this first step there are a lot of things that I need to understand (how camera and ray casting is working)

render_01

And the procedural sphere. Hello, World :slight_smile:
render_02

1 Like

hello!
yes, it’s possible but as it was previously said it will be pretty slow.

I would probably stick with just spheres. Rendering a complete OBJ model would take forever.
I also made my own raytracer in Python for a class, you can see it here:

With multi-threading it takes like 17 seconds to render an image. I made a short animation like 8 seconds long and took like 5 days to render haha, but I did it in a computer in a lab from my university remotely so it wasn’t a problem at all. But I think using multi-threads would take like a day maximum.

And I have been writing a tutorial on how to make a Raytracer in Python. It’s in Spanish, but maybe you can read it using Google Chrome with translate activated, the images and code are in English:

1 Like

The tutorial looks promising, thanks!

I don’t care about the speed at all, I just wanted to understand how rendering works under the hood using the language I know.