Cleanup: Nodes: unify data type conversion functions

Pull Request: https://projects.blender.org/blender/blender/pulls/113744
This commit is contained in:
Iliya Katueshenock
2023-10-16 19:09:07 +02:00
committed by Jacques Lucke
parent b47372356d
commit bd6db0acea
17 changed files with 65 additions and 66 deletions

View File

@@ -328,6 +328,9 @@ extern bNodeTreeType NodeTreeTypeUndefined;
extern bNodeType NodeTypeUndefined;
extern bNodeSocketType NodeSocketTypeUndefined;
std::optional<eCustomDataType> socket_type_to_custom_data_type(eNodeSocketDatatype type);
std::optional<eNodeSocketDatatype> custom_data_type_to_socket_type(eCustomDataType type);
/**
* Contains information about a specific kind of zone (e.g. simulation or repeat zone in geometry
* nodes). This allows writing code that works for all kinds of zones automatically, reducing

View File

@@ -4104,6 +4104,52 @@ void node_type_base_custom(bNodeType *ntype,
namespace blender::bke {
std::optional<eCustomDataType> socket_type_to_custom_data_type(eNodeSocketDatatype type)
{
switch (type) {
case SOCK_FLOAT:
return CD_PROP_FLOAT;
case SOCK_VECTOR:
return CD_PROP_FLOAT3;
case SOCK_RGBA:
return CD_PROP_COLOR;
case SOCK_BOOLEAN:
return CD_PROP_BOOL;
case SOCK_ROTATION:
return CD_PROP_QUATERNION;
case SOCK_INT:
return CD_PROP_INT32;
case SOCK_STRING:
return CD_PROP_STRING;
default:
return std::nullopt;
}
}
std::optional<eNodeSocketDatatype> custom_data_type_to_socket_type(eCustomDataType type)
{
switch (type) {
case CD_PROP_FLOAT:
return SOCK_FLOAT;
case CD_PROP_INT32:
return SOCK_INT;
case CD_PROP_FLOAT3:
return SOCK_VECTOR;
case CD_PROP_FLOAT2:
return SOCK_VECTOR;
case CD_PROP_BOOL:
return SOCK_BOOLEAN;
case CD_PROP_COLOR:
return SOCK_RGBA;
case CD_PROP_BYTE_COLOR:
return SOCK_RGBA;
case CD_PROP_QUATERNION:
return SOCK_ROTATION;
default:
return std::nullopt;
}
}
struct SocketTemplateIdentifierCallbackData {
bNodeSocketTemplate *list;
bNodeSocketTemplate *ntemp;

View File

@@ -435,27 +435,6 @@ static bool socket_can_be_viewed(const bNode &node, const bNodeSocket &socket)
SOCK_RGBA);
}
static eCustomDataType socket_type_to_custom_data_type(const eNodeSocketDatatype socket_type)
{
switch (socket_type) {
case SOCK_FLOAT:
return CD_PROP_FLOAT;
case SOCK_INT:
return CD_PROP_INT32;
case SOCK_VECTOR:
return CD_PROP_FLOAT3;
case SOCK_BOOLEAN:
return CD_PROP_BOOL;
case SOCK_RGBA:
return CD_PROP_COLOR;
case SOCK_ROTATION:
return CD_PROP_QUATERNION;
default:
/* Fallback. */
return CD_AUTO_FROM_NAME;
}
}
/**
* Find the socket to link to in a viewer node.
*/
@@ -474,8 +453,8 @@ static bNodeSocket *node_link_viewer_get_socket(bNodeTree &ntree,
return viewer_socket;
}
NodeGeometryViewer *storage = (NodeGeometryViewer *)viewer_node.storage;
const eCustomDataType data_type = socket_type_to_custom_data_type(
(eNodeSocketDatatype)src_socket.type);
const eCustomDataType data_type = *bke::socket_type_to_custom_data_type(
eNodeSocketDatatype(src_socket.type));
BLI_assert(data_type != CD_AUTO_FROM_NAME);
storage->data_type = data_type;
viewer_node.typeinfo->updatefunc(&ntree, &viewer_node);

View File

@@ -12,6 +12,7 @@
#include "BKE_context.h"
#include "BKE_mesh.hh"
#include "BKE_mesh_runtime.hh"
#include "BKE_node.hh"
#include "BKE_pointcloud.h"
#include "NOD_rna_define.hh"
@@ -21,33 +22,6 @@
namespace blender::nodes {
std::optional<eCustomDataType> node_data_type_to_custom_data_type(const eNodeSocketDatatype type)
{
switch (type) {
case SOCK_FLOAT:
return CD_PROP_FLOAT;
case SOCK_VECTOR:
return CD_PROP_FLOAT3;
case SOCK_RGBA:
return CD_PROP_COLOR;
case SOCK_BOOLEAN:
return CD_PROP_BOOL;
case SOCK_ROTATION:
return CD_PROP_QUATERNION;
case SOCK_INT:
return CD_PROP_INT32;
case SOCK_STRING:
return CD_PROP_STRING;
default:
return {};
}
}
std::optional<eCustomDataType> node_socket_to_custom_data_type(const bNodeSocket &socket)
{
return node_data_type_to_custom_data_type(eNodeSocketDatatype(socket.type));
}
bool check_tool_context_and_error(GeoNodeExecParams &params)
{
if (!params.user_data()->operator_data) {

View File

@@ -67,9 +67,6 @@ void get_closest_in_bvhtree(BVHTreeFromMesh &tree_data,
int apply_offset_in_cyclic_range(IndexRange range, int start_index, int offset);
std::optional<eCustomDataType> node_data_type_to_custom_data_type(eNodeSocketDatatype type);
std::optional<eCustomDataType> node_socket_to_custom_data_type(const bNodeSocket &socket);
#ifdef WITH_OPENVDB
/**
* Initializes the VolumeComponent of a GeometrySet with a new Volume from points.

View File

@@ -98,7 +98,7 @@ static void node_gather_link_searches(GatherLinkSearchOpParams &params)
search_link_ops_for_declarations(params, declaration.outputs.as_span().take_front(1));
const bNodeType &node_type = params.node_type();
const std::optional<eCustomDataType> type = node_data_type_to_custom_data_type(
const std::optional<eCustomDataType> type = bke::socket_type_to_custom_data_type(
eNodeSocketDatatype(params.other_socket().type));
if (type && *type != CD_PROP_STRING) {
if (params.in_out() == SOCK_OUT) {

View File

@@ -91,7 +91,7 @@ static void node_gather_link_searches(GatherLinkSearchOpParams &params)
/* Weight and Iterations inputs don't change based on the data type. */
search_link_ops_for_declarations(params, declaration.inputs.as_span().take_back(2));
const std::optional<eCustomDataType> new_node_type = node_data_type_to_custom_data_type(
const std::optional<eCustomDataType> new_node_type = bke::socket_type_to_custom_data_type(
eNodeSocketDatatype(params.other_socket().type));
if (!new_node_type.has_value()) {
return;

View File

@@ -126,7 +126,7 @@ static void node_gather_link_searches(GatherLinkSearchOpParams &params)
search_link_ops_for_declarations(params, declaration.inputs.as_span().take_front(4));
search_link_ops_for_declarations(params, declaration.outputs.as_span().take_front(3));
const std::optional<eCustomDataType> type = node_data_type_to_custom_data_type(
const std::optional<eCustomDataType> type = bke::socket_type_to_custom_data_type(
eNodeSocketDatatype(params.other_socket().type));
if (type && *type != CD_PROP_STRING) {
/* The input and output sockets have the same name. */

View File

@@ -125,7 +125,7 @@ static void node_update(bNodeTree *ntree, bNode *node)
static void node_gather_link_searches(GatherLinkSearchOpParams &params)
{
const bNodeType &node_type = params.node_type();
const std::optional<eCustomDataType> type = node_data_type_to_custom_data_type(
const std::optional<eCustomDataType> type = bke::socket_type_to_custom_data_type(
eNodeSocketDatatype(params.other_socket().type));
if (type && *type != CD_PROP_STRING) {
params.add_item(IFACE_("Value"), [node_type, type](LinkSearchOpParams &params) {

View File

@@ -85,7 +85,7 @@ static void node_update(bNodeTree *ntree, bNode *node)
static void node_gather_link_searches(GatherLinkSearchOpParams &params)
{
const bNodeType &node_type = params.node_type();
const std::optional<eCustomDataType> type = node_data_type_to_custom_data_type(
const std::optional<eCustomDataType> type = bke::socket_type_to_custom_data_type(
eNodeSocketDatatype(params.other_socket().type));
if (type && *type != CD_PROP_STRING) {
params.add_item(IFACE_("Value"), [node_type, type](LinkSearchOpParams &params) {

View File

@@ -70,7 +70,7 @@ static void node_gather_link_searches(GatherLinkSearchOpParams &params)
const bNodeType &node_type = params.node_type();
if (params.in_out() == SOCK_OUT) {
const std::optional<eCustomDataType> type = node_data_type_to_custom_data_type(
const std::optional<eCustomDataType> type = bke::socket_type_to_custom_data_type(
eNodeSocketDatatype(params.other_socket().type));
if (type && *type != CD_PROP_STRING) {
/* The input and output sockets have the same name. */

View File

@@ -113,7 +113,7 @@ static void node_gather_link_searches(GatherLinkSearchOpParams &params)
search_link_ops_for_declarations(params, declaration.inputs.as_span().take_back(3));
search_link_ops_for_declarations(params, declaration.outputs.as_span().take_front(4));
const std::optional<eCustomDataType> type = node_data_type_to_custom_data_type(
const std::optional<eCustomDataType> type = bke::socket_type_to_custom_data_type(
eNodeSocketDatatype(params.other_socket().type));
if (type && *type != CD_PROP_STRING) {
/* The input and output sockets have the same name. */

View File

@@ -133,7 +133,7 @@ static void node_gather_link_searches(GatherLinkSearchOpParams &params)
search_link_ops_for_declarations(params, declaration.inputs.as_span().take_back(1));
search_link_ops_for_declarations(params, declaration.inputs.as_span().take_front(1));
const std::optional<eCustomDataType> type = node_data_type_to_custom_data_type(
const std::optional<eCustomDataType> type = bke::socket_type_to_custom_data_type(
eNodeSocketDatatype(params.other_socket().type));
if (type && *type != CD_PROP_STRING) {
/* The input and output sockets have the same name. */

View File

@@ -95,7 +95,7 @@ static void node_gather_link_searches(GatherLinkSearchOpParams &params)
search_link_ops_for_declarations(params, declaration.inputs.as_span().take_back(2));
search_link_ops_for_declarations(params, declaration.inputs.as_span().take_front(1));
const std::optional<eCustomDataType> type = node_data_type_to_custom_data_type(
const std::optional<eCustomDataType> type = bke::socket_type_to_custom_data_type(
eNodeSocketDatatype(params.other_socket().type));
if (type && *type != CD_PROP_STRING) {
/* The input and output sockets have the same name. */

View File

@@ -105,7 +105,7 @@ static void node_gather_link_searches(GatherLinkSearchOpParams &params)
search_link_ops_for_declarations(params, declaration.inputs.as_span().take_front(1));
search_link_ops_for_declarations(params, declaration.outputs.as_span().take_back(1));
const std::optional<eCustomDataType> type = node_data_type_to_custom_data_type(
const std::optional<eCustomDataType> type = bke::socket_type_to_custom_data_type(
eNodeSocketDatatype(params.other_socket().type));
if (type && *type != CD_PROP_STRING) {
/* The input and output sockets have the same name. */

View File

@@ -87,7 +87,7 @@ static void node_gather_link_searches(GatherLinkSearchOpParams &params)
search_link_ops_for_declarations(params, declaration.outputs.as_span().take_front(1));
if (params.in_out() == SOCK_IN) {
const std::optional<eCustomDataType> type = node_data_type_to_custom_data_type(
const std::optional<eCustomDataType> type = bke::socket_type_to_custom_data_type(
eNodeSocketDatatype(params.other_socket().type));
if (type && *type != CD_PROP_STRING) {
/* The input and output sockets have the same name. */

View File

@@ -96,8 +96,8 @@ static void node_gather_link_searches(GatherLinkSearchOpParams &params)
ed::viewer_path::activate_geometry_node(*bmain, *snode, viewer_node);
};
const std::optional<eCustomDataType> type = node_socket_to_custom_data_type(
params.other_socket());
const eNodeSocketDatatype socket_type = eNodeSocketDatatype(params.other_socket().type);
const std::optional<eCustomDataType> type = bke::socket_type_to_custom_data_type(socket_type);
if (params.in_out() == SOCK_OUT) {
/* The viewer node only has inputs. */
return;