I have been interested in finding my way around Blender’s source code and looking for a feature I could try to implement.
Recently I have been made aware of a add-on that adds preview to shader nodes (https://blendermarket.com/products/node-preview). I thought that sounds like something that could (should?) be built into Blender.
Compositor node tree already has functionality like this for “Image” node for example.
I started digging. I started off trying to get a shader node to have a preview image like this. Picked a Voronoi Noise one for proof of concept target.
First part was quite promising, just adding “NODE_PREVIEW” to flags when node is created:
sh_node_type_base(&ntype, SH_NODE_TEX_VORONOI, "Voronoi Texture", NODE_CLASS_TEXTURE, NODE_PREVIEW);
That has gotten me much further than I thought, as that already added the clickable “expand preview” image, it already started allocating space for preview image inside the node.
The next step I had was to just populate preview image with “something” (just a full red image) the preview.
And that’s where I started getting lost in where I should be injecting things.
I tried looking at https://wiki.blender.org/wiki/Source, but I couldn’t find overview of how the nodes editor and node trees are working.
I found that “previews” (bNodePreview
) are property of “node tree” as a whole, and not something associated with bNode
itself.
So the ntree object has the node structure under it and separately it stores previews for those nodes.
I see that one must call BKE_node_preview_init_tree
to create hash map for preview objects.
This is where I started getting lost. It looks like BKE_node_preview_init_tree
is not called for shader node trees at all. And I can’t grasp where the shader node tree is constructed/allocated (as I was thinking call to BKE_node_preview_init_tree
should be part of construction).
The other problem I hit is that I am not clear where “refresh of preview” should happen. Looking at drawnode.cc
and node_draw.cc
they don’t seem to handle refresh. When that code runs it seems to just copy over what already is in bNodePreview
.
I can’t however find the place where bNodePreview
buffer is filled.
I thought that node_shader_update_tex_voronoi
would be the place to update the preview (it sounds like a function that gets called every time something about input to node changes).
The snag with this is that I can’t work out how to look up bNodePreview
given bNode
and bNodeTree
.
There is BKE_node_instance_hash_lookup
, but that takes a bNodeInstanceKey
. This one seems to be something I could get by calling BKE_node_instance_key
, but this one takes an unexpected (by me) argument “parent_key”, and I am not quite clear how I would work that one out. At least without, what seems like really inefficient way of doing it, recursing over bNodeTree
looking for the specific bNode
.
Please correct me if I got something wrong.
So with all this context I was hoping someone could help me with those questions:
- is this even right forum to ask this sort of question?
- where in code should I be looking at to add
BKE_node_preview_init_tree
to shader node trees? - where in code should I be looking at to update
bNodePreview
for shader nodes?