Hi everyone,
I literally started looking into C4D and Python yesterday. So please forgive if I don’t quite hit the right terminology here and there.
I’m setting out on integrating C4D into an existing toolchain via Python. Part of that is the need to create materials with complex shader trees from scratch via code only. I’m already at a stage where I can import the material config via JSON, create and connect material and shader nodes, as well as set all their parameters. But I’ve run into a problem that I didn’t foresee.
As I’ve learned, every shader node MUST be a child of the material/shader it is linked to and CANNOT be used as input by other materials/shaders that are not its parent. I wish to create material setups where the output of some shaders is reused by more than one other shader or material. Corona’s node editor solves this problem by automatically introducing invisible “shared nodes” between a shader output and its multiple users. When I probe a simple material I created via the node editor, I see something like this (A “Corona Physical” material with a single “Color” shader assigned to two different inputs):
BaseMaterial <c4d.BaseMaterial object called Test material/Corona Physical with ID 1056306 at 2107176123328> {
Slot 20209: # Roughness
Node <c4d.BaseShader object called Color1 (shared)/Corona Shared with ID 1040749 at 2107176157248> {
Slot 11901:
Node <c4d.BaseShader object called Color1/Color with ID 5832 at 2107176140352> {
}
}
Slot 20218: # Bump
Node <c4d.BaseShader object called Color1 (shared)/Corona Shared with ID 1040749 at 2107176148800> {
Slot 11901:
Node <c4d.BaseShader object called Color1/Color with ID 5832 at 2107176159296> {
}
}
}
This seems to indicate that the node editor internally actually does create duplicates of a reused shader node with a “Corona Shared” node for each (note the different memory addresses), and then uses some mechanism to keep these duplicates in sync, while presenting only one shader node to the user in the node editor. This makes sense as even a Corona Shared is subject to the one-parent-per-child restriction of c4d.BaseList2D().
So how does this all work under the hood? How can I create a material using this sharing mechanism when creating materials via Python? I looked far and wide for some sample code or explanation but found nothing. So, I really hope some Dev or other knowledgeable person will shed some light on this or at least point me in the right direction.
Any help is greatly appreciated!