Replace Python code with Rust

Since Rust is a low level language and it has high level abstractions to make development easier, I was wondering if some of the python code was replaced by Rust. Rust has a strict compile check so a lot of the logical errors would not deliberately compile so that the user knows that there is an error, I don’t believe such strict compile checks even exists in Python, therefore making Rust an easier language to use. Also I believe that Python code is a lot slower than Rust code so Blender would actually run a lot faster if some of their python code was replaced by Rust.

Take Mozilla Firefox for example, when Firefox Quantim came out, they replaced some of their C++ code with Rust and they were able to achieve certain things that they were not able to achieve through C++
https://blog.rust-lang.org/2017/11/14/Fearless-Concurrency-In-Firefox-Quantum.html

1 Like

Rust is not a low level language is a high level language like C/C++. It belongs to the special category of system programming languages, languages designed for the creation of OSes which is not the main focus of low level coding itself. Low coding is about direct access to the hardware. The only low level language I am aware of is Assembly, low level essentially means that it maps one to one with the hardware architecture. Essentially you don’t execute programming command but rather machine code instructions that look a bit English like.

Compilation checks , especially on data types is a highly debatable and biased argument that dynamic languages have been winning for a long time now judging from their increase in popularity proportionate to the increase of popularity of static languages like Rust , C/C++. Bottom line is that even a compiler as sophisticated as the one used by C++ is not smart enough to captures errors when data becomes highly dynamic. Dynamic data is also very popular in C++ through the usage of templates and in other language through the usage of generics. To make matters worse , Python now comes with optional static type declaration so essentially the argument of static vs dynamic is kinda pointless nowadays. As a matter of fact if one chooses to make C/C++/Rust code as dynamic as Python code, not only he loses the advantages of static type checks but more so will experience even worse performance than CPython. Hence why Rust makes a poor replacement for Python even though it may be good replacement for C++.

Problem with Rust is that it will take a Rust developer to pull it through and they are not many, more so Rust even though looks promising is still slower than C/C++ in some tasks. Even bigger problem is integration with libraries and existing C/C++ code even though language promise integration in reality it tends to be cumbersome. Top performance and high library integrations are huge priorities for Blender.

On the matter of Python performance it does not matter as much as you may think it does. Blender itself uses Python just to setup its UI so it does not affect the performance. The only problem exists for addons , but clever addon development depends on high performance libraries like numpy that are written in C and is included with Blender. Because much of the heavy lifting happens at C its kinda rare to experience a slow down because of the API, but you will experience in your own python code if you rely too much on pure python code for high performance tasks.

In any case addons are ill equiped to handle such tasks anyway , because optimal performance would required direct access to Blender source that the addons do not have. Hence many addons that aim for highest performance either rely on C/C++ libraries or end up being included as source code in Blender source or exist as seperate forks. An addon example of being included in the blender source is of course the very usual suspect, pie menus. Python code turned into C code that fully leverages the Blender internals. An example of an addon that was not included in the source code and became a fork, is Fracture modifier.

So in any case Rust does not offer to Blender anything that tempting and is highly unlikely it will be embraced as language any time soon. As a matter of fact the majority of Blender code is not even written in C++ but in C, making Rust even less appealing which is more a C++ alternative than a C alternative.

On the other hand, Blender is free software and you are more than welcomed to make your own fork and port to whatever extend you want its code to Rust. This is the best way to convince developers to join you if you can prove that your Rust port is more fast , as compatible , as highly flexible and stable. Because in that case your wont be arguing an opinion , idea or “fact” but rather have the proof to prove so.

2 Likes

Oh ok good points, thanks mate, one thing, why do you guys use C more than C++ just asking out of curiosity?

1 Like

I heard they will switch to C++ for Blender 3.0

1 Like

Blender used to be a commercial written in C, C++ was added down the line, I think after Blender went open source. I think new features are written in C++. No idea why Blender chose to go with C, instead of C++, C++ was always popular on Windows so maybe at the time, they had their reasons to choose C. Blender is around 2 million lines of code so switching languages is very hard to do.

It’s not impossible to imagine, C++ usage is increasing as is Python usage. And we are very far from version 3 anyway. Took 8 years to go from 2.5 to 2.8. So by the same rate will take another 6 years to reach version 3.

Currently 50% of the code is written in C. 30% on C++ and 20% on Python

https://www.openhub.net/p/blender

Of course if you take to account that a line of Python code usually equals to at least 2/3 lines of C/C++ , its really impressive how many lines of Python code, Blender has.

2.8 dropped a large chunk of C++ code (bge) so those stats are probably a little lower now for C++ .

Yes but BGE “will be back” anyway :wink:

Real time interactive graphics are the future afterall and the original BGE developer is already hired to bring interactive abilities to Blender. Hopefully with a much more coder friendly design. The real time rendering part is already handled by Eevee. The scripting part is already handled by Blender Python API. So the code may be gone for good, BGE “virus” on the other hand is spreading slowly and steadily :smiley: