要在Blender 4.0 API中访问节点树输入值,您可以使用以下代码示例中的方法:
import bpy
def update_func(self, context):
node_tree = bpy.context.space_data.node_tree
value = node_tree.nodes["Node Name"].inputs["Input Name"].default_value
print(value)
bpy.types.Scene.custom_value = bpy.props.FloatProperty(update=update_func)
class MyPanel(bpy.types.Panel):
bl_label = "My Panel"
bl_idname = "OBJECT_PT_my_panel"
bl_space_type = 'NODE_EDITOR'
bl_region_type = 'UI'
bl_category = 'My Addon'
def draw(self, context):
layout = self.layout
scene = context.scene
layout.prop(scene, "custom_value")
# 注册面板和属性
def register():
bpy.utils.register_class(MyPanel)
def unregister():
bpy.utils.unregister_class(MyPanel)
if __name__ == "__main__":
register()
在上述示例中,我们定义了一个更新函数update_func
,它通过bpy.context.space_data.node_tree
访问当前节点树。您可以通过更改"Node Name"
和"Input Name"
来访问特定节点和输入值。在这个例子中,我们只是打印了输入值。
然后,我们使用bpy.props.FloatProperty
定义了一个自定义属性custom_value
,并将更新函数update_func
指定为其更新函数。
最后,我们定义了一个面板MyPanel
,并将自定义属性custom_value
添加到面板中的布局layout.prop
中。这样,您就可以在面板中访问和更改节点树输入值。
请注意,您需要将上述代码保存为.py
文件,并将其作为插件安装到Blender中,才能在节点编辑器中使用这个面板。