Blender动画节点插件开发文档-性能

Blender动画节点插件开发文档-性能
推荐:将NSDT场景编辑器加入你的3D工具链

在本章中,我们希望为节点提供一些属性,以便 我们节点的用户可以决定他要从哪个轴复制 反对对方。我们从第一部分的结果开始,因为 我们真的不需要输出套接字来理解这一点。所以在这里 是原始代码:

import bpy
from animation_nodes.base_types import AnimationNode

class CopyLocationWithOffsetNode(bpy.types.Node, AnimationNode):
    bl_idname = "an_CopyLocationWithOffsetNode"
    bl_label = "Copy Location with Offset"

    def create(self):
        self.newInput("Object", "Source", "source")
        self.newInput("Object", "Target", "target")
        self.newInput("Vector", "Offset", "offset")

    def execute(self, source, target, offset):
        if source is None or target is None:
            return

        target.location = source.location + offset

为了创建属性,我们首先必须导入属性类型 Blender提供: .from bpy.props import *

现在我们要创建三个属性,每个轴一个。他们应该 具有 bool 类型,因为我们想打开或关闭特定的轴。

import bpy
from bpy.props import *
from animation_nodes.events import propertyChanged
from animation_nodes.base_types import AnimationNode

class CopyLocationWithOffsetNode(bpy.types.Node, AnimationNode):
    bl_idname = "an_CopyLocationWithOffsetNode"
    bl_label = "Copy Location with Offset"

    useX = BoolProperty(name = "Use X", default = False, update = propertyChanged)
    useY = BoolProperty(name = "Use Y", default = False, update = propertyChanged)
    useZ = BoolProperty(name = "Use Z", default = False, update = propertyChanged)

如您所见,我还导入了事件处理程序。这 是绝对必要的,但会带来更好的用户体验。这 当用户更改这些属性并且属性未更改时的方式 在“自动执行”面板中激活,将触发更新。 否则,动画节点无法知道这些属性已更改。propertyChanged

与往常一样,下一步是在方法中实现此功能。execute

def execute(self, source, target, offset):
    if source is None or target is None:
        return

    if self.useX: target.location.x = source.location.x + offset.x
    if self.useY: target.location.y = source.location.y + offset.y
    if self.useZ: target.location.z = source.location.z + offset.z

该节点再次完全正常运行,但仍存在一个问题。这 到目前为止,用户看不到属性。要改变这一点,我们必须 实现另一个调用的函数,该函数将指定 将在节点本身中绘制哪些属性。draw(self, layout)

def draw(self, layout):
    layout.prop(self, "useX")
    layout.prop(self, "useY")
    layout.prop(self, "useZ")

现在新功能已经完成,这里又是完整的代码:

import bpy
from bpy.props import *
from animation_nodes.events import propertyChanged
from animation_nodes.base_types import AnimationNode

class CopyLocationWithOffsetNode(bpy.types.Node, AnimationNode):
    bl_idname = "an_CopyLocationWithOffsetNode"
    bl_label = "Copy Location with Offset"

    useX = BoolProperty(name = "Use X", default = False, update = propertyChanged)
    useY = BoolProperty(name = "Use Y", default = False, update = propertyChanged)
    useZ = BoolProperty(name = "Use Z", default = False, update = propertyChanged)

    def create(self):
        self.newInput("Object", "Source", "source")
        self.newInput("Object", "Target", "target")
        self.newInput("Vector", "Offset", "offset")

    def draw(self, layout):
        layout.prop(self, "useX")
        layout.prop(self, "useY")
        layout.prop(self, "useZ")

    def execute(self, source, target, offset):
        if source is None or target is None:
            return

        if self.useX: target.location.x = source.location.x + offset.x
        if self.useY: target.location.y = source.location.y + offset.y
        if self.useZ: target.location.z = source.location.z + offset.z

3D建模学习工作室翻译整理,转载请标明出处!

上一篇:Blender动画节点插件开发文档 (mvrlink.com)

下一篇:Blender动画节点插件开发文档-动态套接字 (mvrlink.com)

NSDT场景编辑器 | NSDT 数字孪生 | GLTF在线编辑器 | 3D模型在线转换 | UnrealSynth虚幻合成数据生成器 | 3D模型自动纹理化工具
2023 power by nsdt©鄂ICP备2023000829号