Blender V5.3
curves/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 "DNA_scene_types.h"
10
11#include "BKE_context.hh"
12#include "BKE_instances.hh"
13#include "BKE_report.hh"
14
15#include "DEG_depsgraph.hh"
17
18#include "WM_api.hh"
19#include "WM_types.hh"
20
21#include "ED_curves.hh"
22#include "ED_object.hh"
23
25
26namespace blender::ed::curves {
27
29{
30 Main *bmain = CTX_data_main(C);
31 Scene *scene = CTX_data_scene(C);
32 Object *active_object = CTX_data_active_object(C);
33 BLI_assert(active_object);
34 BLI_assert(active_object->type == OB_CURVES);
35 Curves &active_curves = *id_cast<Curves *>(active_object->data);
36 const float4x4 &world_to_active = active_object->world_to_object();
37
38 Vector<Object *> objects{active_object};
39 bool active_object_selected = false;
40 CTX_DATA_BEGIN (C, Object *, object, selected_editable_objects) {
41 if (object == active_object) {
42 active_object_selected = true;
43 continue;
44 }
45 if (object->type != OB_CURVES) {
46 continue;
47 }
48 objects.append(object);
49 }
51
52 if (!active_object_selected) {
53 BKE_report(op->reports, RPT_WARNING, "Active object is not a selected curves object");
54 return OPERATOR_CANCELLED;
55 }
56
57 bke::Instances instances;
58 instances.resize(objects.size());
59 MutableSpan<float4x4> transforms = instances.transforms_for_write();
60 MutableSpan<int> references = instances.reference_handles_for_write();
61 Map<const Curves *, int> reference_by_orig_curves;
62 for (const int i : objects.index_range()) {
63 transforms[i] = world_to_active * objects[i]->object_to_world();
64 const Curves *orig_curves = id_cast<const Curves *>(objects[i]->data);
65 references[i] = reference_by_orig_curves.lookup_or_add_cb(orig_curves, [&]() {
67 return instances.add_new_reference(std::move(geometry));
68 });
69 }
70
75 .geometry;
76
77 if (!realized_geometry.has_curves()) {
78 BKE_report(op->reports, RPT_WARNING, "No curves data to join");
79 return OPERATOR_CANCELLED;
80 }
81
82 Curves *realized_curves = realized_geometry.get_curves_for_write();
83 active_curves.geometry.wrap() = std::move(realized_curves->geometry.wrap());
84
85 for (Object *object : objects.as_span().drop_front(1)) {
86 object::base_free_and_unlink(bmain, scene, object);
87 }
88
94
95 return OPERATOR_FINISHED;
96}
97
98} // namespace blender::ed::curves
#define CTX_DATA_BEGIN(C, Type, instance, member)
#define CTX_DATA_END
#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
wmOperatorStatus join_objects_exec(bContext *C, wmOperator *op)
void base_free_and_unlink(Main *bmain, Scene *scene, Object *ob)
RealizeInstancesResult realize_instances(bke::GeometrySet geometry_set, const RealizeInstancesOptions &options)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
@ RPT_WARNING
Definition BKE_report.hh:40
struct Curves * BKE_curves_copy_for_eval(const struct Curves *curves_src)
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)
static GeometrySet from_curves(Curves *curves, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
static GeometrySet from_instances(std::unique_ptr< Instances > instances)
struct ReportList * reports