Blender V5.3
pointcloud/intern/separate.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_index_mask.hh"
10#include "BLI_task.hh"
11#include "BLI_vector_set.hh"
12
13#include "BKE_context.hh"
14#include "BKE_layer.hh"
15#include "BKE_lib_id.hh"
16
17#include "BKE_pointcloud.hh"
18
20#include "ED_object.hh"
21#include "ED_pointcloud.hh"
22
23#include "DNA_layer_types.h"
25
26#include "DEG_depsgraph.hh"
27
28#include "WM_api.hh"
29
31
33{
34 Main *bmain = CTX_data_main(C);
35 Scene *scene = CTX_data_scene(C);
36 ViewLayer *view_layer = CTX_data_view_layer(C);
37
39 *bmain, scene, view_layer, CTX_wm_view3d(C));
40
41 VectorSet<PointCloud *> src_pointclouds;
42 for (Base *base_src : bases) {
43 src_pointclouds.add(id_cast<PointCloud *>(base_src->object->data));
44 }
45
46 /* Modify new point clouds and generate new point clouds in parallel. */
47 Array<PointCloud *> dst_pointclouds(src_pointclouds.size());
48 threading::parallel_for(dst_pointclouds.index_range(), 1, [&](const IndexRange range) {
49 for (const int i : range) {
50 IndexMaskMemory memory;
51 const IndexMask selection = retrieve_selected_points(*src_pointclouds[i], memory);
52 if (selection.is_empty()) {
53 dst_pointclouds[i] = nullptr;
54 continue;
55 }
56 dst_pointclouds[i] = copy_selection(*src_pointclouds[i], selection);
57 const IndexMask inverse = selection.complement(IndexRange(src_pointclouds[i]->totpoint),
58 memory);
59 BKE_pointcloud_nomain_to_pointcloud(copy_selection(*src_pointclouds[i], inverse),
60 src_pointclouds[i]);
61 }
62 });
63
64 /* Move new point clouds into main data-base. */
65 for (const int i : dst_pointclouds.index_range()) {
66 if (PointCloud *dst = dst_pointclouds[i]) {
67 dst_pointclouds[i] = BKE_pointcloud_add(bmain, BKE_id_name(src_pointclouds[i]->id));
68 pointcloud_copy_parameters(*src_pointclouds[i], *dst_pointclouds[i]);
69 BKE_pointcloud_nomain_to_pointcloud(dst, dst_pointclouds[i]);
70 }
71 }
72
73 /* Skip processing objects with no selected elements. */
74 bases.remove_if([&](Base *base) {
75 PointCloud *pointcloud = id_cast<PointCloud *>(base->object->data);
76 return dst_pointclouds[src_pointclouds.index_of(pointcloud)] == nullptr;
77 });
78
79 if (bases.is_empty()) {
80 return OPERATOR_CANCELLED;
81 }
82
83 /* Add new objects for the new point clouds. */
84 for (Base *base_src : bases) {
85 PointCloud *src = id_cast<PointCloud *>(base_src->object->data);
86 PointCloud *dst = dst_pointclouds[src_pointclouds.index_of(src)];
87
88 Base *base_dst = object::add_duplicate(
89 bmain, scene, view_layer, base_src, eDupli_ID_Flags(U.dupflag) & USER_DUP_ACT);
90 Object *object_dst = base_dst->object;
91 object_dst->mode = OB_MODE_OBJECT;
92 object_dst->data = id_cast<ID *>(dst);
93
96 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, base_src->object);
98 }
99
101 return OPERATOR_FINISHED;
102}
103
105{
106 ot->name = "Separate";
107 ot->idname = "POINTCLOUD_OT_separate";
108 ot->description = "Separate selected geometry into a new point cloud";
109
110 ot->exec = separate_exec;
112
114}
115
116} // namespace blender::ed::pointcloud
General operations for point clouds.
#define C
Definition RandGen.cpp:29
#define ND_DRAW
Definition WM_types.hh:502
#define NC_OBJECT
Definition WM_types.hh:392
IndexRange index_range() const
Definition BLI_array.hh:371
bool add(const Key &key)
int64_t size() const
Base * add_duplicate(Main *bmain, Scene *scene, ViewLayer *view_layer, Base *base, eDupli_ID_Flags dupflag)
bool editable_pointcloud_in_edit_mode_poll(bContext *C)
Definition operators.cc:73
static wmOperatorStatus separate_exec(bContext *C, wmOperator *)
void POINTCLOUD_OT_separate(wmOperatorType *ot)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
UserDef U
Definition blender.cc:58
void BKE_pointcloud_nomain_to_pointcloud(PointCloud *pointcloud_src, PointCloud *pointcloud_dst)
const char * BKE_id_name(const ID &id)
ViewLayer * CTX_data_view_layer(const bContext *C)
void DEG_id_tag_update(ID *id, unsigned int flags)
PointCloud * BKE_pointcloud_add(Main *bmain, const char *name)
void pointcloud_copy_parameters(const PointCloud &src, PointCloud &dst)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
Main * CTX_data_main(const bContext *C)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
void DEG_relations_tag_update(Main *bmain)
Scene * CTX_data_scene(const bContext *C)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
Vector< Base * > BKE_view_layer_array_from_bases_in_edit_mode(const Main &bmain, const Scene *scene, ViewLayer *view_layer, const View3D *v3d)
View3D * CTX_wm_view3d(const bContext *C)
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165