Material2Script : Generate your material's equivalent blender API code

Hello,

I was wondering how to easily transcript a material created in the shader editor into its equivalent code using the Blender API. There may be multiple applications but personnaly, I would use it to programmatically create node trees and easily tweak them inside an add-on’s code.
I have looked a bit around and didn’t find anything very recent or exhaustive on this subject. Maybe it already exists but I didn’t stumble upon it. Anyway this was a fun project :slight_smile:

So I created this script which loops over all the nodes and links in the active object’s active material, and selectively builds statements in order to recreate it from code only. At the end, the script creates a new text-block inside the blender file, named as the original material.
If you execute the code of this script in another blend file, the very same material will be added to the materials list.

I’d like to broadcast it in case anyone could benefit from it.

Don’t hesitate to leave some feedback if you used it and would like to see it expand with more functionality, I could very easily make it into a standalone add-on though for now it only has one single purpose.

The script is available on my github :

Cheers

3 Likes