Blender V5.3
pointcloud/intern/join.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_map.hh"
10
11#include "DNA_scene_types.h"
12
13#include "BKE_context.hh"
14#include "BKE_instances.hh"
15#include "BKE_pointcloud.hh"
16#include "BKE_report.hh"
17
18#include "DEG_depsgraph.hh"
20
21#include "WM_api.hh"
22#include "WM_types.hh"
23
24#include "ED_object.hh"
25#include "ED_pointcloud.hh"
26
28
30
32{
33 Main *bmain = CTX_data_main(C);
34 Scene *scene = CTX_data_scene(C);
35 Object *active_object = CTX_data_active_object(C);
36 BLI_assert(active_object);
37 BLI_assert(active_object->type == OB_POINTCLOUD);
38 PointCloud &active_pointcloud = *id_cast<PointCloud *>(active_object->data);
39 const float4x4 &world_to_active = active_object->world_to_object();
40
41 Vector<Object *> objects{active_object};
42 bool active_object_selected = false;
43 CTX_DATA_BEGIN (C, Object *, object, selected_editable_objects) {
44 if (object == active_object) {
45 active_object_selected = true;
46 continue;
47 }
48 if (object->type != OB_POINTCLOUD) {
49 continue;
50 }
51 objects.append(object);
52 }
54
55 if (!active_object_selected) {
56 BKE_report(op->reports, RPT_WARNING, "Active object is not a selected point cloud object");
57 return OPERATOR_CANCELLED;
58 }
59
60 bke::Instances instances;
61 instances.resize(objects.size());
62 MutableSpan<float4x4> transforms = instances.transforms_for_write();
63 MutableSpan<int> references = instances.reference_handles_for_write();
64 Map<const PointCloud *, int> reference_by_orig_points;
65 for (const int i : objects.index_range()) {
66 transforms[i] = world_to_active * objects[i]->object_to_world();
67 const PointCloud *orig_points = id_cast<const PointCloud *>(objects[i]->data);
68 references[i] = reference_by_orig_points.lookup_or_add_cb(orig_points, [&]() {
70 return instances.add_new_reference(std::move(geometry));
71 });
72 }
73
78 .geometry;
79
80 if (!realized_geometry.has_pointcloud()) {
81 BKE_report(op->reports, RPT_WARNING, "No point cloud data to join");
82 return OPERATOR_CANCELLED;
83 }
84
85 PointCloud *realized_points =
86 realized_geometry.get_component_for_write<bke::PointCloudComponent>().release();
87 BKE_pointcloud_nomain_to_pointcloud(realized_points, &active_pointcloud);
88
89 for (Object *object : objects.as_span().drop_front(1)) {
90 object::base_free_and_unlink(bmain, scene, object);
91 }
92
98
99 return OPERATOR_FINISHED;
100}
101
102} // namespace blender::ed::pointcloud
#define CTX_DATA_BEGIN(C, Type, instance, member)
#define CTX_DATA_END
General operations for point clouds.
#define C
Definition RandGen.cpp:29
#define ND_OB_ACTIVE
Definition WM_types.hh:459
#define NC_SCENE
Definition WM_types.hh:391
#define ND_LAYER_CONTENT
Definition WM_types.hh:472
Value & lookup_or_add_cb(const Key &key, const CreateValueF &create_value)
Definition BLI_map.hh:620
int64_t size() const
void append(const T &value)
IndexRange index_range() const
Span< T > as_span() const
MutableSpan< int > reference_handles_for_write()
Definition instances.cc:229
int add_new_reference(InstanceReference reference)
Definition instances.cc:284
void resize(int size)
Definition instances.cc:216
MutableSpan< float4x4 > transforms_for_write()
Definition instances.cc:242
void base_free_and_unlink(Main *bmain, Scene *scene, Object *ob)
wmOperatorStatus join_objects_exec(bContext *C, wmOperator *op)
RealizeInstancesResult realize_instances(bke::GeometrySet geometry_set, const RealizeInstancesOptions &options)
PointCloud * BKE_pointcloud_copy_for_eval(const PointCloud *pointcloud_src)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
@ RPT_WARNING
Definition BKE_report.hh:40
void BKE_pointcloud_nomain_to_pointcloud(PointCloud *pointcloud_src, PointCloud *pointcloud_dst)
MatBase< float, 4, 4 > float4x4
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
BMesh const char void * data
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
Main * CTX_data_main(const bContext *C)
@ ID_RECALC_SELECT
Definition DNA_ID.h:1101
@ ID_RECALC_TRANSFORM
Definition DNA_ID.h:1054
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
void DEG_relations_tag_update(Main *bmain)
Scene * CTX_data_scene(const bContext *C)
Object * CTX_data_active_object(const bContext *C)
GeometryComponent & get_component_for_write(GeometryComponent::Type component_type)
static GeometrySet from_instances(std::unique_ptr< Instances > instances)
static GeometrySet from_pointcloud(PointCloud *pointcloud, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
struct ReportList * reports