Blender V5.0
editmesh_knife_project.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2013 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "DNA_curve_types.h"
10#include "DNA_object_types.h"
11
12#include "BLI_linklist.h"
13#include "BLI_listbase.h"
14#include "BLI_math_vector.h"
15
16#include "BKE_context.hh"
17#include "BKE_curve.hh"
18#include "BKE_editmesh.hh"
19#include "BKE_layer.hh"
20#include "BKE_lib_id.hh"
21#include "BKE_mesh.hh"
22#include "BKE_object.hh"
23#include "BKE_report.hh"
24
25#include "DEG_depsgraph.hh"
27
28#include "RNA_access.hh"
29#include "RNA_define.hh"
30
31#include "MEM_guardedalloc.h"
32
33#include "WM_types.hh"
34
35#include "ED_mesh.hh"
36#include "ED_screen.hh"
37#include "ED_view3d.hh"
38
39#include "mesh_intern.hh" /* own include */
40
41using blender::Vector;
42
44{
46 ARegion *region = CTX_wm_region(C);
47 const Mesh *mesh_eval;
48 bool mesh_eval_needs_free;
49
50 if (ob->type == OB_MESH || ob->runtime->data_eval) {
51 const Object *ob_eval = DEG_get_evaluated(depsgraph, ob);
52 mesh_eval = BKE_object_get_evaluated_mesh(ob_eval);
53 mesh_eval_needs_free = false;
54 }
55 else if (ELEM(ob->type, OB_FONT, OB_CURVES_LEGACY, OB_SURF)) {
56 const Object *ob_eval = DEG_get_evaluated(depsgraph, ob);
57 mesh_eval = BKE_mesh_new_nomain_from_curve(ob_eval);
58 mesh_eval_needs_free = true;
59 }
60 else {
61 mesh_eval = nullptr;
62 }
63
64 if (mesh_eval) {
65 ListBase nurbslist = {nullptr, nullptr};
66
67 BKE_mesh_to_curve_nurblist(mesh_eval, &nurbslist, 0); /* wire */
68 BKE_mesh_to_curve_nurblist(mesh_eval, &nurbslist, 1); /* boundary */
69
71 static_cast<RegionView3D *>(region->regiondata), ob);
72
73 if (nurbslist.first) {
74 LISTBASE_FOREACH (Nurb *, nu, &nurbslist) {
75 if (nu->bp) {
76 int a;
77 BPoint *bp;
78 bool is_cyclic = (nu->flagu & CU_NURB_CYCLIC) != 0;
79 float (*mval)[2] = static_cast<float (*)[2]>(
80 MEM_mallocN(sizeof(*mval) * (nu->pntsu + is_cyclic), __func__));
81
82 for (bp = nu->bp, a = 0; a < nu->pntsu; a++, bp++) {
83 copy_v2_v2(mval[a], ED_view3d_project_float_v2_m4(region, bp->vec, projmat));
84 }
85 if (is_cyclic) {
86 copy_v2_v2(mval[a], mval[0]);
87 }
88
89 BLI_linklist_prepend(&polys, mval);
90 }
91 }
92 }
93
94 BKE_nurbList_free(&nurbslist);
95
96 if (mesh_eval_needs_free) {
97 BKE_id_free(nullptr, (ID *)mesh_eval);
98 }
99 }
100
101 return polys;
102}
103
105{
106 Scene *scene = CTX_data_scene(C);
107 const bool cut_through = RNA_boolean_get(op->ptr, "cut_through");
108
109 LinkNode *polys = nullptr;
110
111 CTX_DATA_BEGIN (C, Object *, ob, selected_objects) {
113 continue;
114 }
115 polys = knifeproject_poly_from_object(C, ob, polys);
116 }
118
119 if (polys == nullptr) {
121 RPT_ERROR,
122 "No other selected objects have wire or boundary edges to use for projection");
123 return OPERATOR_CANCELLED;
124 }
125
127
129 vc.scene, vc.view_layer, vc.v3d);
130
131 EDBM_mesh_knife(&vc, objects, polys, true, cut_through);
132
133 for (Object *obedit : objects) {
136
137 /* select only tagged faces */
139
141
143
145 }
146
147 BLI_linklist_freeN(polys);
148
149 return OPERATOR_FINISHED;
150}
151
153{
154 /* description */
155 ot->name = "Knife Project";
156 ot->idname = "MESH_OT_knife_project";
157 ot->description = "Use other objects outlines and boundaries to project knife cuts";
158
159 /* callbacks */
160 ot->exec = knifeproject_exec;
162
163 /* flags */
165
166 /* parameters */
167 RNA_def_boolean(ot->srna,
168 "cut_through",
169 false,
170 "Cut Through",
171 "Cut through all faces, not just visible ones");
172}
#define CTX_DATA_BEGIN(C, Type, instance, member)
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C)
Scene * CTX_data_scene(const bContext *C)
ARegion * CTX_wm_region(const bContext *C)
#define CTX_DATA_END
void BKE_nurbList_free(ListBase *lb)
Definition curve.cc:597
BMEditMesh * BKE_editmesh_from_object(Object *ob)
Return the BMEditMesh for a given object.
Definition editmesh.cc:61
blender::Vector< Object * > BKE_view_layer_array_from_objects_in_edit_mode_unique_data(const Scene *scene, ViewLayer *view_layer, const View3D *v3d)
void BKE_id_free(Main *bmain, void *idv)
Mesh * BKE_mesh_new_nomain_from_curve(const Object *ob)
void BKE_mesh_to_curve_nurblist(const Mesh *mesh, ListBase *nurblist, int edge_users_test)
General operations, lookup, etc. for blender objects.
bool BKE_object_is_in_editmode(const Object *ob)
Mesh * BKE_object_get_evaluated_mesh(const Object *object_eval)
@ RPT_ERROR
Definition BKE_report.hh:39
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:153
#define LISTBASE_FOREACH(type, var, list)
MINLINE void copy_v2_v2(float r[2], const float a[2])
#define ELEM(...)
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
@ CU_NURB_CYCLIC
Object is a sort of wrapper for general info.
@ OB_SURF
@ OB_FONT
@ OB_MESH
@ OB_CURVES_LEGACY
@ SCE_SELECT_VERTEX
@ SCE_SELECT_EDGE
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
bool EDBM_selectmode_disable(Scene *scene, BMEditMesh *em, short selectmode_disable, short selectmode_fallback)
ViewContext em_setup_viewcontext(bContext *C)
bool ED_operator_editmesh_region_view3d(bContext *C)
blender::float4x4 ED_view3d_ob_project_mat_get(const RegionView3D *rv3d, const Object *ob)
void ED_view3d_viewcontext_init_object(ViewContext *vc, Object *obact)
Read Guarded memory(de)allocation.
#define C
Definition RandGen.cpp:29
@ OPTYPE_BLOCKING
Definition WM_types.hh:184
@ OPTYPE_UNDO
Definition WM_types.hh:182
@ OPTYPE_REGISTER
Definition WM_types.hh:180
@ BM_ELEM_SELECT
@ BM_ELEM_TAG
void BM_mesh_select_mode_flush(BMesh *bm)
void BM_mesh_elem_hflag_disable_all(BMesh *bm, const char htype, const char hflag, const bool respecthide)
void BM_mesh_elem_hflag_enable_test(BMesh *bm, const char htype, const char hflag, const bool respecthide, const bool overwrite, const char hflag_test)
#define BM_FACE
#define BM_EDGE
#define BM_VERT
BPy_StructRNA * depsgraph
nullptr float
static bool is_cyclic(const Nurb *nu)
void EDBM_mesh_knife(ViewContext *vc, const Span< Object * > objects, LinkNode *polys, bool use_tag, bool cut_through)
static wmOperatorStatus knifeproject_exec(bContext *C, wmOperator *op)
static LinkNode * knifeproject_poly_from_object(const bContext *C, Object *ob, LinkNode *polys)
void MESH_OT_knife_project(wmOperatorType *ot)
void * MEM_mallocN(size_t len, const char *str)
Definition mallocn.cc:128
MatBase< float, 4, 4 > float4x4
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
void * regiondata
Definition DNA_ID.h:414
void * first
ObjectRuntimeHandle * runtime
Scene * scene
Definition ED_view3d.hh:73
ViewLayer * view_layer
Definition ED_view3d.hh:74
View3D * v3d
Definition ED_view3d.hh:78
struct ReportList * reports
struct PointerRNA * ptr
wmOperatorType * ot
Definition wm_files.cc:4237