A new version of Cineversity has been launched. This legacy site and its tutorials will remain accessible for a limited transition period

Visit the New Cineversity

Navigation

 ·   Wiki Home
 ·   Categories
 ·   Title List
 ·   Uncategorized Pages
 ·   Random Page
 ·   Recent Changes
 ·   RSS
 ·   Atom
 ·   What Links Here

Search:

 

Create or Find Page:

 

View Python: Shader Tree

Here’s an example of how to recursively iterate through C4D’s shaders. First you have to loop through all the materials in the document. Then for each material, you can get the first shader. This comes as a GeListNode instance, which is a 4D list.

 

 

import c4d, os

############################################################
# recursive function where we do stuff to the shaders
############################################################
def shadertree(shader):
    # Loop through the BaseList
    while(shader):
        
        # This is where you do stuff
        print shader.GetName()
        # If it's a bitmap, we'll look at the filename
        if shader.GetType() == c4d.Xbitmap:
            filename = shader[c4d.BITMAPSHADER_FILENAME]
            print filename
            # for instance we can set the filename to just the file part
            filename = os.path.basename(filename)
            shader[c4d.BITMAPSHADER_FILENAME] = filename
        
        # Check for child shaders & recurse
        if shader.GetDown(): shadertree(shader.GetDown())
        # Get the Next Shader
        shader = shader.GetNext()
        
############################################################
# main function
############################################################
def main():
    # Get the first material
    mat = doc.GetFirstMaterial()
    # Loop through materials
    while(mat):
        # Get the first shader
        # Note - this is a 4D list - you've gotta GetDown
        shd = mat.GetFirstShader()
        # Use our recursive function to parse the shader tree
        shadertree(shd)
        # Get the Next material
        mat = mat.GetNext()

if __name__=='__main__':
    main()

Category:Scripting

Categories: