2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2020-04-20 10:58:43 +02:00
|
|
|
|
2020-12-04 11:28:09 +01:00
|
|
|
#include <cstring>
|
2020-04-20 10:58:43 +02:00
|
|
|
|
2023-09-01 21:37:11 +02:00
|
|
|
#include "BLI_string.h"
|
|
|
|
|
|
2020-04-20 10:58:43 +02:00
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
2023-06-23 22:15:42 +02:00
|
|
|
#include "NOD_geometry.hh"
|
2020-04-20 10:58:43 +02:00
|
|
|
|
2023-11-16 11:41:55 +01:00
|
|
|
#include "BKE_context.hh"
|
2024-01-23 15:18:09 -05:00
|
|
|
#include "BKE_layer.hh"
|
2023-05-15 15:14:22 +02:00
|
|
|
#include "BKE_node.hh"
|
2023-10-09 23:41:53 +02:00
|
|
|
#include "BKE_object.hh"
|
2020-04-20 10:58:43 +02:00
|
|
|
|
2020-12-02 13:35:07 +01:00
|
|
|
#include "DNA_modifier_types.h"
|
2020-04-20 10:58:43 +02:00
|
|
|
#include "DNA_node_types.h"
|
2020-12-02 13:35:07 +01:00
|
|
|
#include "DNA_space_types.h"
|
2020-04-20 10:58:43 +02:00
|
|
|
|
2024-07-10 18:30:02 +02:00
|
|
|
#include "RNA_prototypes.hh"
|
2020-04-20 10:58:43 +02:00
|
|
|
|
2023-08-05 02:57:52 +02:00
|
|
|
#include "UI_resources.hh"
|
2021-12-24 22:47:58 -05:00
|
|
|
|
2024-02-09 18:59:42 +01:00
|
|
|
#include "BLT_translation.hh"
|
2023-05-22 16:59:51 +02:00
|
|
|
|
2021-01-16 13:09:19 -06:00
|
|
|
#include "node_common.h"
|
|
|
|
|
|
2024-05-13 16:07:12 +02:00
|
|
|
blender::bke::bNodeTreeType *ntreeType_Geometry;
|
2020-04-20 10:58:43 +02:00
|
|
|
|
2024-05-13 16:07:12 +02:00
|
|
|
static void geometry_node_tree_get_from_context(const bContext *C,
|
|
|
|
|
blender::bke::bNodeTreeType * /*treetype*/,
|
|
|
|
|
bNodeTree **r_ntree,
|
|
|
|
|
ID **r_id,
|
|
|
|
|
ID **r_from)
|
2020-12-02 13:35:07 +01:00
|
|
|
{
|
2023-06-29 13:57:54 +02:00
|
|
|
const SpaceNode *snode = CTX_wm_space_node(C);
|
2023-08-03 10:50:54 -04:00
|
|
|
if (snode->geometry_nodes_type == SNODE_GEOMETRY_TOOL) {
|
2023-09-27 15:47:22 +02:00
|
|
|
*r_ntree = snode->geometry_nodes_tool_tree;
|
2023-06-29 13:57:54 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-14 21:33:51 +02:00
|
|
|
const Scene *scene = CTX_data_scene(C);
|
2020-12-02 13:35:07 +01:00
|
|
|
ViewLayer *view_layer = CTX_data_view_layer(C);
|
2022-09-14 21:33:51 +02:00
|
|
|
BKE_view_layer_synced_ensure(scene, view_layer);
|
2022-09-01 10:00:53 +02:00
|
|
|
Object *ob = BKE_view_layer_active_object_get(view_layer);
|
2020-12-02 13:35:07 +01:00
|
|
|
|
|
|
|
|
if (ob == nullptr) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ModifierData *md = BKE_object_active_modifier(ob);
|
|
|
|
|
|
|
|
|
|
if (md == nullptr) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (md->type == eModifierType_Nodes) {
|
2022-09-25 17:39:45 +02:00
|
|
|
const NodesModifierData *nmd = reinterpret_cast<const NodesModifierData *>(md);
|
2020-12-02 13:35:07 +01:00
|
|
|
if (nmd->node_group != nullptr) {
|
|
|
|
|
*r_from = &ob->id;
|
|
|
|
|
*r_id = &ob->id;
|
|
|
|
|
*r_ntree = nmd->node_group;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-16 13:09:19 -06:00
|
|
|
static void geometry_node_tree_update(bNodeTree *ntree)
|
|
|
|
|
{
|
2024-08-19 20:27:37 +02:00
|
|
|
blender::bke::node_tree_set_output(ntree);
|
2021-04-16 11:37:49 +02:00
|
|
|
|
2021-01-16 13:09:19 -06:00
|
|
|
/* Needed to give correct types to reroutes. */
|
|
|
|
|
ntree_update_reroute_nodes(ntree);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-13 16:07:12 +02:00
|
|
|
static void foreach_nodeclass(void *calldata, blender::bke::bNodeClassCallback func)
|
2021-03-09 18:33:33 +01:00
|
|
|
{
|
|
|
|
|
func(calldata, NODE_CLASS_INPUT, N_("Input"));
|
|
|
|
|
func(calldata, NODE_CLASS_GEOMETRY, N_("Geometry"));
|
|
|
|
|
func(calldata, NODE_CLASS_ATTRIBUTE, N_("Attribute"));
|
|
|
|
|
func(calldata, NODE_CLASS_OP_COLOR, N_("Color"));
|
|
|
|
|
func(calldata, NODE_CLASS_OP_VECTOR, N_("Vector"));
|
2021-08-23 16:23:58 +02:00
|
|
|
func(calldata, NODE_CLASS_CONVERTER, N_("Converter"));
|
2021-03-09 18:33:33 +01:00
|
|
|
func(calldata, NODE_CLASS_LAYOUT, N_("Layout"));
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-15 09:51:57 -06:00
|
|
|
static bool geometry_node_tree_validate_link(eNodeSocketDatatype type_a,
|
|
|
|
|
eNodeSocketDatatype type_b)
|
2021-05-11 16:46:02 -05:00
|
|
|
{
|
2021-05-17 11:11:25 +02:00
|
|
|
/* Geometry, string, object, material, texture and collection sockets can only be connected to
|
|
|
|
|
* themselves. The other types can be converted between each other. */
|
2021-12-15 09:51:57 -06:00
|
|
|
if (ELEM(type_a, SOCK_FLOAT, SOCK_VECTOR, SOCK_RGBA, SOCK_BOOLEAN, SOCK_INT) &&
|
|
|
|
|
ELEM(type_b, SOCK_FLOAT, SOCK_VECTOR, SOCK_RGBA, SOCK_BOOLEAN, SOCK_INT))
|
|
|
|
|
{
|
2021-05-17 11:11:25 +02:00
|
|
|
return true;
|
2021-05-11 16:46:02 -05:00
|
|
|
}
|
2023-11-15 18:56:00 +01:00
|
|
|
if (ELEM(type_a, SOCK_FLOAT, SOCK_VECTOR) && type_b == SOCK_ROTATION) {
|
2023-11-27 09:37:29 +11:00
|
|
|
/* Floats and vectors implicitly convert to rotations. */
|
2023-11-15 18:56:00 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
2024-02-13 18:59:36 +01:00
|
|
|
|
|
|
|
|
/* Support implicit conversions between matrices and rotations. */
|
|
|
|
|
if (type_a == SOCK_MATRIX && type_b == SOCK_ROTATION) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (type_a == SOCK_ROTATION && type_b == SOCK_MATRIX) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-15 18:56:00 +01:00
|
|
|
if (type_a == SOCK_ROTATION && type_b == SOCK_VECTOR) {
|
|
|
|
|
/* Rotations implicitly convert to vectors. */
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2021-12-15 09:51:57 -06:00
|
|
|
return type_a == type_b;
|
2021-05-11 16:46:02 -05:00
|
|
|
}
|
|
|
|
|
|
2024-05-13 16:07:12 +02:00
|
|
|
static bool geometry_node_tree_socket_type_valid(blender::bke::bNodeTreeType * /*treetype*/,
|
|
|
|
|
blender::bke::bNodeSocketType *socket_type)
|
2021-04-29 23:36:46 -05:00
|
|
|
{
|
2024-08-19 20:27:37 +02:00
|
|
|
return blender::bke::node_is_static_socket_type(socket_type) && ELEM(socket_type->type,
|
|
|
|
|
SOCK_FLOAT,
|
|
|
|
|
SOCK_VECTOR,
|
|
|
|
|
SOCK_RGBA,
|
|
|
|
|
SOCK_BOOLEAN,
|
|
|
|
|
SOCK_ROTATION,
|
|
|
|
|
SOCK_MATRIX,
|
|
|
|
|
SOCK_INT,
|
|
|
|
|
SOCK_STRING,
|
|
|
|
|
SOCK_OBJECT,
|
|
|
|
|
SOCK_GEOMETRY,
|
|
|
|
|
SOCK_COLLECTION,
|
|
|
|
|
SOCK_TEXTURE,
|
|
|
|
|
SOCK_IMAGE,
|
|
|
|
|
SOCK_MATERIAL,
|
|
|
|
|
SOCK_MENU);
|
2021-04-29 23:36:46 -05:00
|
|
|
}
|
|
|
|
|
|
2021-12-07 23:12:13 -05:00
|
|
|
void register_node_tree_type_geo()
|
|
|
|
|
{
|
2024-05-13 16:07:12 +02:00
|
|
|
blender::bke::bNodeTreeType *tt = ntreeType_Geometry =
|
|
|
|
|
static_cast<blender::bke::bNodeTreeType *>(
|
|
|
|
|
MEM_callocN(sizeof(blender::bke::bNodeTreeType), "geometry node tree type"));
|
2020-12-02 13:25:25 +01:00
|
|
|
tt->type = NTREE_GEOMETRY;
|
2023-06-19 20:06:55 +10:00
|
|
|
STRNCPY(tt->idname, "GeometryNodeTree");
|
|
|
|
|
STRNCPY(tt->group_idname, "GeometryNodeGroup");
|
|
|
|
|
STRNCPY(tt->ui_name, N_("Geometry Node Editor"));
|
2022-05-10 19:21:26 +02:00
|
|
|
tt->ui_icon = ICON_GEOMETRY_NODES;
|
2023-06-19 20:06:55 +10:00
|
|
|
STRNCPY(tt->ui_description, N_("Geometry nodes"));
|
2020-12-02 13:25:25 +01:00
|
|
|
tt->rna_ext.srna = &RNA_GeometryNodeTree;
|
2021-01-16 13:09:19 -06:00
|
|
|
tt->update = geometry_node_tree_update;
|
2020-12-02 13:35:07 +01:00
|
|
|
tt->get_from_context = geometry_node_tree_get_from_context;
|
2021-03-09 18:33:33 +01:00
|
|
|
tt->foreach_nodeclass = foreach_nodeclass;
|
2021-04-29 23:36:46 -05:00
|
|
|
tt->valid_socket_type = geometry_node_tree_socket_type_valid;
|
2021-05-11 16:46:02 -05:00
|
|
|
tt->validate_link = geometry_node_tree_validate_link;
|
2020-12-02 13:35:07 +01:00
|
|
|
|
2024-08-19 20:27:37 +02:00
|
|
|
blender::bke::node_tree_type_add(tt);
|
2020-04-20 10:58:43 +02:00
|
|
|
}
|
2023-10-20 14:51:30 +02:00
|
|
|
|
|
|
|
|
bool is_layer_selection_field(const bNodeTreeInterfaceSocket &socket)
|
|
|
|
|
{
|
2024-05-13 16:07:12 +02:00
|
|
|
const blender::bke::bNodeSocketType *typeinfo = socket.socket_typeinfo();
|
2023-10-20 14:51:30 +02:00
|
|
|
BLI_assert(typeinfo != nullptr);
|
|
|
|
|
|
|
|
|
|
if (typeinfo->type != SOCK_BOOLEAN) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return (socket.flag & NODE_INTERFACE_SOCKET_LAYER_SELECTION) != 0;
|
|
|
|
|
}
|