简体   繁体   English

(混合器)(Python)如何使用Python代码为混合节点中的因子值设置动画?

[英](Blender) (Python)How can I animate the factor value in the mix node with Python code?

What I want is a way to handle the 'factor' value in the mixRGB node like a normal object, like for example a cube, so with fcurves, fmodifiers and so on. 我想要的是一种在混合RGB节点中像普通对象(例如多维数据集)那样使用fcurves,fmodifiers等处理“ factor”值的方法。 All this via Python code made in the Text Editor 所有这些都是通过文本编辑器中的Python代码完成的

The first step is to find the mix node you want. 第一步是找到所需的混合节点。 Within a material you can access each node by name, while the first mixRGB node is named 'Mix', following mix nodes will have a numerical extension added to the name. 在材质中,您可以按名称访问每个节点,而第一个mixRGB节点名为“ Mix”,随后的混合节点将在名称上添加数字扩展名。 The name may also be changed manually by the user (or python script). 用户也可以手动更改名称(或python脚本)。 By showing the properties region (press N ) you can see the name of the active node in the node properties. 通过显示属性区域(按N ),您可以在节点属性中看到活动节点的名称。

节点属性

To adjust the fac value you alter the default_value of the fac input. 要调整fac值,请更改fac输入的default_value To keyframe the mix factor you tell the fac input to insert a keyframe with a data_path of default_value 要设置混合因子的关键帧,您要告诉data_path输入插入一个带有default_valuedata_path的关键帧

import bpy
cur_frame = bpy.context.scene.frame_current
mat_nodes = bpy.data.materials['Material'].node_tree.nodes
mix_factor = mat_nodes['Mix.002'].inputs['Fac']

mix_factor.default_value = 0.5
mix_factor.keyframe_insert('default_value', frame=cur_frame)

Of course you may specify any frame number for the keyframe not just the current frame. 当然,您可以为关键帧指定任何帧号,而不仅仅是当前帧。

If you have many mix nodes, you can loop over the nodes and add each mix shader to a list 如果您有很多混合节点,则可以遍历节点并将每个混合着色器添加到列表中

mix_nodes = [n for n in mat_nodes if n.type == 'MIX_RGB']

You can then loop over them and keyframe as desired. 然后,您可以根据需要遍历它们和关键帧。

for m in mix_nodes:
    m.inputs['Fac'].default_value = 0.5
    m.inputs['Fac'].keyframe_insert('default_value', frame=cur_frame)

Finding the fcurves after adding them is awkward for nodes. 添加节点后查找fcurves对于节点来说很麻烦。 While you tell the input socket to insert a keyframe, the fcurve is stored in the node_tree so after keyframe_insert() you would use 当您告诉输入套接字插入关键帧时,fcurve存储在node_tree中,因此在keyframe_insert()您将使用

bpy.data.materials['Material'].node_tree.animation_data.action.fcurves.find()

Knowing the data path you want to search for can be tricky, as the data path for the Fac input of node Mix.002 will be nodes["Mix.002"].inputs[0].default_value 知道要搜索的数据路径可能很棘手,因为节点Mix.002的Fac输入的数据路径将是nodes["Mix.002"].inputs[0].default_value

If you want to find an fcurve after adding it to adjust values or add modifiers you will most likely find it easier to keep a list of them as you add the keyframes. 如果要在添加ff曲线以调整值或添加修饰符后找到它,您很可能会发现在添加关键帧时更容易保留它们的列表。 After keyframe_insert() the new fcurve should be at keyframe_insert() ,新的fcurve应该位于

material.node_tree.animation_data.action.fcurves[-1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM