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

在这一部分中,我们要扩展此节点。首先我们想改变 节点,以便它输出目标对象的新位置。因此 我们需要在函数中写一个新行,如下所示:create(self)

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

很简单,对吧?现在我必须调整方法 这样它实际上返回了一个向量。目前它只是一直返回。现在始终返回一个向量非常重要。所有其他可能使用此值取决于 这是一个向量,不要显式检查类型。这意味着 如果 节点。新方法可能如下所示:executeNoneexecute

def execute(self, source, target, offset):
    if source is None or target is None:
        return Vector((0, 0, 0)) # this is the default value

    target.location = source.location + offset
    return target.location

如您所见,我们创建了此对象。每 单个向量应为此类型。为了做到这一点 代码工作我们需要导入文件顶部的类型:。Vector((0, 0, 0))from mathutils import Vector

该节点现在再次完全正常运行。

import bpy
from mathutils import Vector
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")
        self.newOutput("Vector", "Target Location", "targetLocation")

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

        target.location = source.location + offset
        return target.location
import bpy
from mathutils import Vector
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")
        self.newOutput("Vector", "Source Location", "sourceLocation")
        self.newOutput("Vector", "Target Location", "targetLocation")

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

        target.location = source.location + offset
        return source.location, target.location

现在我们要更改节点,以便它不仅输出位置 目标对象以及源对象的位置。我想 在这一点上,很明显我们必须如何改变方法。create(self)

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

Python具有出色的功能,您可以轻松返回多个 方法中的对象。为了返回两个对象,我们将完全使用 特征。关于这一点,您必须了解两件主要事情:

  1. 返回对象的数量始终必须与 输出套接字。
  2. 返回对象的顺序必须对应于 输出套接字。

为了满足这两个要求,该方法的代码可以 看起来像这样:execute

def execute(self, source, target, offset):
    if source is None or target is None:
        return Vector((0, 0, 0)), Vector((0, 0, 0)) # we need two defaults as well

    target.location = source.location + offset
    return source.location, target.location

同样,我们的节点功能齐全,所以我们结束了这一切 .part。

import bpy
from mathutils import Vector
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")
        self.newOutput("Vector", "Source Location", "sourceLocation")
        self.newOutput("Vector", "Target Location", "targetLocation")

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

        target.location = source.location + offset
        return source.location, target.location

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

下一篇:Blender动画节点插件开发文档-性能 (mvrlink.com)

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