'Blender - How to Animate Material Nodes in Python
I am trying to animate, in Python, the colour of a node material using the Colour Ramp node, and I want to know the data path to give in keyframe statements.
I have made a material using the Colour Ramp node driven by a Math node. The Math node is animated to give the required colour changes. This works if I set up the system manually. Blender screen showing Material Nodes
However in Python, although I can set up the material, I cannot find how to make keyframes – apparently the data path is incorrect. I enclose my Python code to set up the node material and set the first keyframe.
> import bpy
> # green red colors = [(0,0,0,0), (0,1,0,1), (1,0,0,1)]
>
> #create single cube and colour it green obj = bpy.data.objects['Cube']
>
> mat1 = bpy.data.materials.new(name = 'Material1') mat1.use_nodes =
> True obj.active_material = mat1
>
> # Build shader nodes pNode = mat1.node_tree.nodes.get('Principled BSDF') pNode.inputs[0].default_value = (0,0,1,1) crNode =
> mat1.node_tree.nodes.new('ShaderNodeValToRGB') crNode.location =
> (-300, 200) mNode = mat1.node_tree.nodes.new('ShaderNodeMath')
> mNode.location = (-600, 200) # Build shader links link =
> mat1.node_tree.links.new link(mNode.outputs[0],crNode.inputs[0])
> link(crNode.outputs[0],pNode.inputs[0])
>
> # set up color ramp points 0 and 1 crNode.color_ramp.elements[0].color = (colors[1]) crNode.color_ramp.elements[1].color = (colors[2])
>
> #Variable which sets the colour within the specified colour range (0 to 1) mNode.inputs[0].default_value = 0 mNode.inputs[1].default_value
> = 0
>
> colNode = mat1.node_tree.nodes['Math']
>
> colNode.inputs[0].default_value = 1 mat1.keyframe_insert(data_path =
> "colNode.inputs[0]", frame = 20)
>
> #End
It crashes on the last line, with error message: File "C:\Users\Jim\aSk\GExercises\Test5a.blend\Test7CreateCubes.py", line 36, in TypeError: bpy_struct.keyframe_insert() property "colNode.inputs[0]" not found
I have tried different content for the data path, but all references I have looked at seem to suggest using the “node_tree.nodes” property which I am trying to do in the above code. So is there something wrong in my fundamental thinking? What is the correct data path in the keyframe statement:
mat1.keyframe_insert(data_path = "…..............", frame = 20)
and how it is derived?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
