Geometry Nodes - Object Info: option to apply obj transform to the geometry

By design the modified object transformations should still work and
affect the geometry nodes results. The current behaviour, however, would
make the geometry from the object info to not be affected by the
modified object transformations. This patch changes that by default.

In a similar fashion the Location, Rotation and Scale sockets outputs
should be aware of whether the output should be in the global space or in the
space of the nodetree.

To solve this, the patch introduces a new transformation space "enum"
where users can pick "Original" or "Relative" space.

Original
--------
Output the geometry relative to the input object transform, and the
location, rotation and scale relative to the world origin.

Relative
--------
Bring the input object geometry, location, rotation and scale into the
modified object maintaining the relative position between the two objects in
the scene.

Relative space violates a bit the design of the nodetree. The geometry in this
case is transformed so that moving the modified object doesn't interfere with
the geometry. This is particularly useful for the boolean node for instance.

"Original" is the default space, but old files are set to "Relative" for
backwards compatibility.

Differential Revision: https://developer.blender.org/D10124
This commit is contained in:
Dalai Felinto 2021-01-19 17:30:44 +01:00
parent a9203e25a2
commit 8b777ee6d6
Notes: blender-bot 2023-02-14 11:29:52 +01:00
Referenced by commit c3207d88c2, Object Info: Fix tooltip for the transform space option
Referenced by issue #85049, Geometry Nodes: How to handle instances with shear?
7 changed files with 96 additions and 5 deletions

View File

@ -39,7 +39,7 @@ extern "C" {
/* Blender file format version. */
#define BLENDER_FILE_VERSION BLENDER_VERSION
#define BLENDER_FILE_SUBVERSION 13
#define BLENDER_FILE_SUBVERSION 14
/* Minimum Blender version that supports reading file written with the current
* version. Older Blender versions will test this and show a warning if the file

View File

@ -1572,6 +1572,23 @@ void blo_do_versions_290(FileData *fd, Library *UNUSED(lib), Main *bmain)
}
}
if (!MAIN_VERSION_ATLEAST(bmain, 292, 14)) {
FOREACH_NODETREE_BEGIN (bmain, ntree, id) {
if (ntree->type != NTREE_GEOMETRY) {
continue;
}
LISTBASE_FOREACH (bNode *, node, &ntree->nodes) {
if (node->type == GEO_NODE_OBJECT_INFO && node->storage == NULL) {
NodeGeometryObjectInfo *data = (NodeGeometryObjectInfo *)MEM_callocN(
sizeof(NodeGeometryObjectInfo), __func__);
data->transform_space = GEO_NODE_TRANSFORM_SPACE_RELATIVE;
node->storage = data;
}
}
}
FOREACH_NODETREE_END;
}
/**
* Versioning code until next subversion bump goes here.
*

View File

@ -3284,6 +3284,11 @@ static void node_geometry_buts_point_scale(uiLayout *layout, bContext *UNUSED(C)
uiItemR(layout, ptr, "input_type", DEFAULT_FLAGS, IFACE_("Type"), ICON_NONE);
}
static void node_geometry_buts_object_info(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
{
uiItemR(layout, ptr, "transform_space", UI_ITEM_R_EXPAND, NULL, ICON_NONE);
}
static void node_geometry_set_butfunc(bNodeType *ntype)
{
switch (ntype->type) {
@ -3335,6 +3340,9 @@ static void node_geometry_set_butfunc(bNodeType *ntype)
case GEO_NODE_POINT_SCALE:
ntype->draw_buttons = node_geometry_buts_point_scale;
break;
case GEO_NODE_OBJECT_INFO:
ntype->draw_buttons = node_geometry_buts_object_info;
break;
}
}

View File

@ -1167,6 +1167,13 @@ typedef struct NodeGeometryPointTranslate {
char _pad[7];
} NodeGeometryPointTranslate;
typedef struct NodeGeometryObjectInfo {
/* GeometryNodeTransformSpace. */
uint8_t transform_space;
char _pad[7];
} NodeGeometryObjectInfo;
/* script node mode */
#define NODE_SCRIPT_INTERNAL 0
#define NODE_SCRIPT_EXTERNAL 1
@ -1608,6 +1615,11 @@ typedef enum GeometryNodeAlignRotationToVectorAxis {
GEO_NODE_ALIGN_ROTATION_TO_VECTOR_AXIS_Z = 2,
} GeometryNodeAlignRotationToVectorAxis;
typedef enum GeometryNodeTransformSpace {
GEO_NODE_TRANSFORM_SPACE_ORIGINAL = 0,
GEO_NODE_TRANSFORM_SPACE_RELATIVE = 1,
} GeometryNodeTransformSpace;
#ifdef __cplusplus
}
#endif

View File

@ -494,6 +494,22 @@ static const EnumPropertyItem rna_node_geometry_attribute_input_type_items_no_bo
{0, NULL, 0, NULL, NULL},
};
static const EnumPropertyItem rna_node_geometry_object_info_transform_space_items[] = {
{GEO_NODE_TRANSFORM_SPACE_ORIGINAL,
"ORIGINAL",
0,
"Original",
"Output the geometry relative to the input object transform, and the location, rotation and "
"scale relative to the world origin"},
{GEO_NODE_TRANSFORM_SPACE_RELATIVE,
"RELATIVE",
0,
"Relative",
"Bring the input object geometry, location, rotation and scale into the modified object, "
"maintaining the relative position between the two objects in the scene"},
{0, NULL, 0, NULL, NULL},
};
#endif
#undef ITEM_ATTRIBUTE
@ -8846,6 +8862,20 @@ static void def_geo_point_translate(StructRNA *srna)
RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_socket_update");
}
static void def_geo_object_info(StructRNA *srna)
{
PropertyRNA *prop;
RNA_def_struct_sdna_from(srna, "NodeGeometryObjectInfo", "storage");
prop = RNA_def_property(srna, "transform_space", PROP_ENUM, PROP_NONE);
RNA_def_property_enum_items(prop, rna_node_geometry_object_info_transform_space_items);
RNA_def_property_ui_text(prop,
"Transform Space",
"Determine the transformation applied to vector and geometry outputs");
RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_update");
}
/* -------------------------------------------------------------------------- */
static void rna_def_shader_node(BlenderRNA *brna)

View File

@ -275,7 +275,7 @@ DefNode(GeometryNode, GEO_NODE_SUBDIVISION_SURFACE, 0, "SUBDIVISION_SURFACE", Su
DefNode(GeometryNode, GEO_NODE_BOOLEAN, def_geo_boolean, "BOOLEAN", Boolean, "Boolean", "")
DefNode(GeometryNode, GEO_NODE_POINT_DISTRIBUTE, def_geo_point_distribute, "POINT_DISTRIBUTE", PointDistribute, "Point Distribute", "")
DefNode(GeometryNode, GEO_NODE_POINT_INSTANCE, def_geo_point_instance, "POINT_INSTANCE", PointInstance, "Point Instance", "")
DefNode(GeometryNode, GEO_NODE_OBJECT_INFO, 0, "OBJECT_INFO", ObjectInfo, "Object Info", "")
DefNode(GeometryNode, GEO_NODE_OBJECT_INFO, def_geo_object_info, "OBJECT_INFO", ObjectInfo, "Object Info", "")
DefNode(GeometryNode, GEO_NODE_ATTRIBUTE_RANDOMIZE, def_geo_attribute_randomize, "ATTRIBUTE_RANDOMIZE", AttributeRandomize, "Attribute Randomize", "")
DefNode(GeometryNode, GEO_NODE_ATTRIBUTE_MATH, def_geo_attribute_math, "ATTRIBUTE_MATH", AttributeMath, "Attribute Math", "")
DefNode(GeometryNode, GEO_NODE_JOIN_GEOMETRY, 0, "JOIN_GEOMETRY", JoinGeometry, "Join Geometry", "")

View File

@ -38,6 +38,11 @@ static bNodeSocketTemplate geo_node_object_info_out[] = {
namespace blender::nodes {
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);
bke::PersistentObjectHandle object_handle = params.extract_input<bke::PersistentObjectHandle>(
"Object");
Object *object = params.handle_map().lookup(object_handle);
@ -54,7 +59,12 @@ static void geo_node_object_info_exec(GeoNodeExecParams params)
mul_m4_m4m4(transform, self_object->imat, object->obmat);
float quaternion[4];
mat4_decompose(location, quaternion, scale, transform);
if (transform_space_relative) {
mat4_decompose(location, quaternion, scale, transform);
}
else {
mat4_decompose(location, quaternion, scale, object->obmat);
}
quat_to_eul(rotation, quaternion);
if (object != self_object) {
@ -66,8 +76,10 @@ static void geo_node_object_info_exec(GeoNodeExecParams params)
/* Make a copy because the life time of the other mesh might be shorter. */
Mesh *copied_mesh = BKE_mesh_copy_for_eval(mesh, false);
/* Transform into the local space of the object that is being modified. */
BKE_mesh_transform(copied_mesh, transform, true);
if (transform_space_relative) {
/* Transform into the local space of the object that is being modified. */
BKE_mesh_transform(copied_mesh, transform, true);
}
MeshComponent &mesh_component = geometry_set.get_component_for_write<MeshComponent>();
mesh_component.replace(copied_mesh);
@ -82,6 +94,15 @@ static void geo_node_object_info_exec(GeoNodeExecParams params)
params.set_output("Scale", scale);
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()
@ -90,6 +111,9 @@ void register_node_type_geo_object_info()
geo_node_type_base(&ntype, GEO_NODE_OBJECT_INFO, "Object Info", NODE_CLASS_INPUT, 0);
node_type_socket_templates(&ntype, geo_node_object_info_in, geo_node_object_info_out);
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;
nodeRegisterType(&ntype);
}