Create layered material with python

Sub forum for help and tutorials.

Moderators: ChrisHekman, aoktar

Post Reply
worldcreator
Licensed Customer
Posts: 2
Joined: Thu Jul 19, 2018 12:59 pm

Hey,
I'm trying to create a layered material via python. So far adding the Layered Material, Material Layer and general nodes works fine. But I encounter an issue when I try to connect everything together...

Code: Select all

from typing import Optional
import c4d

doc: c4d.documents.BaseDocument
op: Optional[c4d.BaseObject]

OCTANE_MAT_LAYERED_ID = 1052933
OCTANE_IMAGE_ID = 1029508
OCTANE_LAYER_ID = 1052932
OCTANE_SUB_MATERIAL_ID = 1040369

def main() -> None:
    mat = c4d.BaseMaterial(OCTANE_MAT_LAYERED_ID)
    doc.InsertMaterial(mat)
    mat[c4d.LAYEREDMAT_NUM_OF_MATERIALS] = 2

    mask_tex = c4d.BaseShader(OCTANE_IMAGE_ID)
    mat.InsertShader(mask_tex)
    mask_tex[c4d.IMAGETEXTURE_FILE] = "" #some path
    mask_tex[c4d.IMAGETEXTURE_MODE] = 0
    mask_tex[c4d.IMAGETEXTURE_GAMMA] = 2.2
    mask_tex[c4d.IMAGETEX_BORDER_MODE] = 0

    layer = c4d.BaseShader(OCTANE_LAYER_ID)
    mat.InsertShader(layer)
    layer[c4d.MATLAYER_LAYER_TYPE] = 1
    layer[c4d.MATLAYER_DIFFUSE_LNK] = mask_tex
    layer[c4d.MATLAYER_LAYER_OPACITY] = 0.2

    mat[c4d.LAYEREDMAT_MAT2_LINK] = layer

    c4d.EventAdd()

if __name__ == '__main__':
    main()
In the material generated from the code both the ImageTexture and Material Layer nodes aren't connected to their planned parent node.

Connecting the layered material with sub materials instead of material layers seems to work but I would like to use material layers so that I can further group multiple material layers a level below.
Any idea on why this isn't working or how I could circumvent this? Thanks already for your help :)
worldcreator
Licensed Customer
Posts: 2
Joined: Thu Jul 19, 2018 12:59 pm

Quick update. I was able to connect shaders and access the values of the MaterialLayer shader. The IDs in MaterialLayer.h seem to be outdated (at least in OctaneRender For Cinema 4D 2022.1-[R10]).

The correct IDs would be:

Code: Select all

MATLAYER_DIFFUSE_LNK = 1406         # shader
MATLAYER_TRANSMISSION_LNK = 1415    # shader
MATLAYER_ROUGHNESS_LNK = 1408       # shader
MATLAYER_BUMP_LNK = 1414            # shader
MATLAYER_NORMAL_LNK = 1418          # shader
MATLAYER_LAYER_OPACITY_LNK = 1413   # shader

MATLAYER_DIFFUSE_COL = 1437         # c4d.Vector
MATLAYER_LAYER_OPACITY = 1443       # float
MATLAYER_ROUGHNESS = 1439           # float 
Regarding the connection between a MaterialLayer and a LayeredMaterial I wasn't able to get anywhere closer. The ID LAYEREDMAT_MAT2_LINK of 1301 in OctaneLayeredMaterial.h seems to be right but a MaterialLayer isn't stored when assigned to it, however a Submaterial is, which is kind of confusing...
Post Reply

Return to “Help / Tutorials”