Welcome to mirror list, hosted at ThFree Co, Russian Federation.

node_geo_object_info.cc « nodes « geometry « nodes « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bb8e5f7e29b5200073e104af96f3e9480c1f4c70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "BLI_math_matrix.h"

#include "UI_interface.h"
#include "UI_resources.h"

#include "node_geometry_util.hh"

namespace blender::nodes {

static void geo_node_object_info_declare(NodeDeclarationBuilder &b)
{
  b.add_input<decl::Object>(N_("Object")).hide_label();
  b.add_input<decl::Bool>(N_("As Instance"))
      .description(N_("Output the entire object as single instance. "
                      "This allows instancing non-geometry object types"));
  b.add_output<decl::Vector>(N_("Location"));
  b.add_output<decl::Vector>(N_("Rotation"));
  b.add_output<decl::Vector>(N_("Scale"));
  b.add_output<decl::Geometry>(N_("Geometry"));
}

static void geo_node_object_info_layout(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
{
  uiItemR(layout, ptr, "transform_space", UI_ITEM_R_EXPAND, nullptr, ICON_NONE);
}

static void geo_node_object_info_exec(GeoNodeExecParams params)
{
  const bNode &bnode = params.node();
  NodeGeometryObjectInfo *node_storage = (NodeGeometryObjectInfo *)bnode.storage;
  const bool transform_space_relative = (node_storage->transform_space ==
                                         GEO_NODE_TRANSFORM_SPACE_RELATIVE);

  auto default_transform = [&]() {
    params.set_output("Location", float3(0));
    params.set_output("Rotation", float3(0));
    params.set_output("Scale", float3(0));
  };
  auto default_geometry = [&]() { params.set_output("Geometry", GeometrySet()); };

  Object *object = params.get_input<Object *>("Object");

  const Object *self_object = params.self_object();
  if (object == nullptr) {
    default_transform();
    default_geometry();
    return;
  }

  const float4x4 &object_matrix = object->obmat;
  const float4x4 transform = float4x4(self_object->imat) * object_matrix;

  if (transform_space_relative) {
    params.set_output("Location", transform.translation());
    params.set_output("Rotation", transform.to_euler());
    params.set_output("Scale", transform.scale());
  }
  else {
    params.set_output("Location", object_matrix.translation());
    params.set_output("Rotation", object_matrix.to_euler());
    params.set_output("Scale", object_matrix.scale());
  }

  if (params.output_is_required("Geometry")) {
    if (object == self_object) {
      params.error_message_add(NodeWarningType::Error,
                               TIP_("Geometry cannot be retrieved from the modifier object"));
      default_geometry();
      return;
    }

    GeometrySet geometry_set;
    if (params.get_input<bool>("As Instance")) {
      InstancesComponent &instances = geometry_set.get_component_for_write<InstancesComponent>();
      const int handle = instances.add_reference(*object);
      if (transform_space_relative) {
        instances.add_instance(handle, transform);
      }
      else {
        instances.add_instance(handle, float4x4::identity());
      }
    }
    else {
      geometry_set = bke::object_get_evaluated_geometry_set(*object);
      if (transform_space_relative) {
        transform_geometry_set(geometry_set, transform, *params.depsgraph());
      }
    }

    params.set_output("Geometry", geometry_set);
  }
}

static void geo_node_object_info_node_init(bNodeTree *UNUSED(tree), bNode *node)
{
  NodeGeometryObjectInfo *data = (NodeGeometryObjectInfo *)MEM_callocN(
      sizeof(NodeGeometryObjectInfo), __func__);
  data->transform_space = GEO_NODE_TRANSFORM_SPACE_ORIGINAL;
  node->storage = data;
}

}  // namespace blender::nodes

void register_node_type_geo_object_info()
{
  static bNodeType ntype;

  geo_node_type_base(&ntype, GEO_NODE_OBJECT_INFO, "Object Info", NODE_CLASS_INPUT, 0);
  node_type_init(&ntype, blender::nodes::geo_node_object_info_node_init);
  node_type_storage(
      &ntype, "NodeGeometryObjectInfo", node_free_standard_storage, node_copy_standard_storage);
  ntype.geometry_node_execute = blender::nodes::geo_node_object_info_exec;
  ntype.draw_buttons = blender::nodes::geo_node_object_info_layout;
  ntype.declare = blender::nodes::geo_node_object_info_declare;
  nodeRegisterType(&ntype);
}