Blender  V3.3
editmesh_knife_project.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2013 Blender Foundation. All rights reserved. */
3 
8 #include "DNA_curve_types.h"
9 #include "DNA_object_types.h"
10 
11 #include "BLI_linklist.h"
12 #include "BLI_listbase.h"
13 #include "BLI_math.h"
14 
15 #include "BKE_context.h"
16 #include "BKE_curve.h"
17 #include "BKE_customdata.h"
18 #include "BKE_editmesh.h"
19 #include "BKE_layer.h"
20 #include "BKE_lib_id.h"
21 #include "BKE_mesh.h"
22 #include "BKE_mesh_runtime.h"
23 #include "BKE_object.h"
24 #include "BKE_report.h"
25 
26 #include "DEG_depsgraph.h"
27 #include "DEG_depsgraph_query.h"
28 
29 #include "RNA_access.h"
30 #include "RNA_define.h"
31 
32 #include "MEM_guardedalloc.h"
33 
34 #include "WM_types.h"
35 
36 #include "ED_mesh.h"
37 #include "ED_screen.h"
38 #include "ED_view3d.h"
39 
40 #include "mesh_intern.h" /* own include */
41 
43  Scene *scene,
44  Object *ob,
45  LinkNode *polys)
46 {
48  ARegion *region = CTX_wm_region(C);
49  const struct Mesh *me_eval;
50  bool me_eval_needs_free;
51 
52  if (ob->type == OB_MESH || ob->runtime.data_eval) {
54  me_eval = BKE_object_get_evaluated_mesh(ob_eval);
55  if (me_eval == NULL) {
56  Scene *scene_eval = (Scene *)DEG_get_evaluated_id(depsgraph, &scene->id);
57  me_eval = mesh_get_eval_final(depsgraph, scene_eval, ob_eval, &CD_MASK_BAREMESH);
58  }
59  me_eval_needs_free = false;
60  }
61  else if (ELEM(ob->type, OB_FONT, OB_CURVES_LEGACY, OB_SURF)) {
63  me_eval = BKE_mesh_new_nomain_from_curve(ob_eval);
64  me_eval_needs_free = true;
65  }
66  else {
67  me_eval = NULL;
68  }
69 
70  if (me_eval) {
71  ListBase nurbslist = {NULL, NULL};
72  float projmat[4][4];
73 
74  BKE_mesh_to_curve_nurblist(me_eval, &nurbslist, 0); /* wire */
75  BKE_mesh_to_curve_nurblist(me_eval, &nurbslist, 1); /* boundary */
76 
77  ED_view3d_ob_project_mat_get(region->regiondata, ob, projmat);
78 
79  if (nurbslist.first) {
80  Nurb *nu;
81  for (nu = nurbslist.first; nu; nu = nu->next) {
82  if (nu->bp) {
83  int a;
84  BPoint *bp;
85  bool is_cyclic = (nu->flagu & CU_NURB_CYCLIC) != 0;
86  float(*mval)[2] = MEM_mallocN(sizeof(*mval) * (nu->pntsu + is_cyclic), __func__);
87 
88  for (bp = nu->bp, a = 0; a < nu->pntsu; a++, bp++) {
89  ED_view3d_project_float_v2_m4(region, bp->vec, mval[a], projmat);
90  }
91  if (is_cyclic) {
92  copy_v2_v2(mval[a], mval[0]);
93  }
94 
95  BLI_linklist_prepend(&polys, mval);
96  }
97  }
98  }
99 
100  BKE_nurbList_free(&nurbslist);
101 
102  if (me_eval_needs_free) {
103  BKE_id_free(NULL, (ID *)me_eval);
104  }
105  }
106 
107  return polys;
108 }
109 
111 {
113  const bool cut_through = RNA_boolean_get(op->ptr, "cut_through");
114 
115  LinkNode *polys = NULL;
116 
117  CTX_DATA_BEGIN (C, Object *, ob, selected_objects) {
118  if (BKE_object_is_in_editmode(ob)) {
119  continue;
120  }
121  polys = knifeproject_poly_from_object(C, scene, ob, polys);
122  }
123  CTX_DATA_END;
124 
125  if (polys == NULL) {
126  BKE_report(op->reports,
127  RPT_ERROR,
128  "No other selected objects have wire or boundary edges to use for projection");
129  return OPERATOR_CANCELLED;
130  }
131 
132  ViewContext vc;
133  em_setup_viewcontext(C, &vc);
134 
135  uint objects_len;
137  vc.view_layer, vc.v3d, &objects_len);
138 
139  EDBM_mesh_knife(&vc, objects, objects_len, polys, true, cut_through);
140 
141  for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
142  Object *obedit = objects[ob_index];
144  BMEditMesh *em = BKE_editmesh_from_object(obedit);
145 
146  /* select only tagged faces */
148 
150 
152 
154  }
155  MEM_freeN(objects);
156 
157  BLI_linklist_freeN(polys);
158 
159  return OPERATOR_FINISHED;
160 }
161 
163 {
164  /* description */
165  ot->name = "Knife Project";
166  ot->idname = "MESH_OT_knife_project";
167  ot->description = "Use other objects outlines and boundaries to project knife cuts";
168 
169  /* callbacks */
172 
173  /* flags */
175 
176  /* parameters */
178  "cut_through",
179  false,
180  "Cut Through",
181  "Cut through all faces, not just visible ones");
182 }
typedef float(TangentPoint)[2]
struct Scene * CTX_data_scene(const bContext *C)
Definition: context.c:1090
#define CTX_DATA_BEGIN(C, Type, instance, member)
Definition: BKE_context.h:269
struct Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C)
Definition: context.c:1528
struct ARegion * CTX_wm_region(const bContext *C)
Definition: context.c:749
#define CTX_DATA_END
Definition: BKE_context.h:278
void BKE_nurbList_free(struct ListBase *lb)
Definition: curve.cc:649
CustomData interface, see also DNA_customdata_types.h.
const CustomData_MeshMasks CD_MASK_BAREMESH
Definition: customdata.cc:2051
BMEditMesh * BKE_editmesh_from_object(struct Object *ob)
Return the BMEditMesh for a given object.
Definition: editmesh.c:58
#define BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, v3d, r_len)
Definition: BKE_layer.h:542
void BKE_id_free(struct Main *bmain, void *idv)
struct Mesh * BKE_mesh_new_nomain_from_curve(const struct Object *ob)
void BKE_mesh_to_curve_nurblist(const struct Mesh *me, struct ListBase *nurblist, int edge_users_test)
struct Mesh * mesh_get_eval_final(struct Depsgraph *depsgraph, const struct Scene *scene, struct Object *ob, const struct CustomData_MeshMasks *dataMask)
General operations, lookup, etc. for blender objects.
struct Mesh * BKE_object_get_evaluated_mesh(const struct Object *object)
bool BKE_object_is_in_editmode(const struct Object *ob)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition: report.c:83
MINLINE void copy_v2_v2(float r[2], const float a[2])
unsigned int uint
Definition: BLI_sys_types.h:67
#define ELEM(...)
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:35
struct ID * DEG_get_evaluated_id(const struct Depsgraph *depsgraph, struct ID *id)
struct Object * DEG_get_evaluated_object(const struct Depsgraph *depsgraph, struct Object *object)
@ CU_NURB_CYCLIC
Object is a sort of wrapper for general info.
@ OB_SURF
@ OB_FONT
@ OB_MESH
@ OB_CURVES_LEGACY
#define SCE_SELECT_VERTEX
#define SCE_SELECT_EDGE
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
void em_setup_viewcontext(struct bContext *C, struct ViewContext *vc)
bool EDBM_selectmode_disable(struct Scene *scene, struct BMEditMesh *em, short selectmode_disable, short selectmode_fallback)
bool ED_operator_editmesh_region_view3d(struct bContext *C)
Definition: screen_ops.c:447
void ED_view3d_viewcontext_init_object(struct ViewContext *vc, struct Object *obact)
void ED_view3d_ob_project_mat_get(const struct RegionView3D *v3d, const struct Object *ob, float r_pmat[4][4])
void ED_view3d_project_float_v2_m4(const struct ARegion *region, const float co[3], float r_co[2], const float mat[4][4])
Read Guarded memory(de)allocation.
#define C
Definition: RandGen.cpp:25
@ OPTYPE_BLOCKING
Definition: WM_types.h:150
@ OPTYPE_UNDO
Definition: WM_types.h:148
@ OPTYPE_REGISTER
Definition: WM_types.h:146
@ BM_FACE
Definition: bmesh_class.h:386
@ BM_VERT
Definition: bmesh_class.h:383
@ BM_EDGE
Definition: bmesh_class.h:384
@ BM_ELEM_SELECT
Definition: bmesh_class.h:471
@ BM_ELEM_TAG
Definition: bmesh_class.h:484
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)
Scene scene
const Depsgraph * depsgraph
static bool is_cyclic(const Nurb *nu)
void EDBM_mesh_knife(ViewContext *vc, Object **objects, const int objects_len, LinkNode *polys, bool use_tag, bool cut_through)
void MESH_OT_knife_project(wmOperatorType *ot)
static int knifeproject_exec(bContext *C, wmOperator *op)
static LinkNode * knifeproject_poly_from_object(const bContext *C, Scene *scene, Object *ob, LinkNode *polys)
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:33
static unsigned a[3]
Definition: RandGen.cpp:78
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:4863
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, bool default_value, const char *ui_name, const char *ui_description)
Definition: rna_define.c:3493
void * regiondata
struct BMesh * bm
Definition: BKE_editmesh.h:40
float vec[4]
Definition: DNA_ID.h:368
void * first
Definition: DNA_listBase.h:31
short flagu
struct Nurb * next
BPoint * bp
struct ID * data_eval
Object_Runtime runtime
struct ViewLayer * view_layer
Definition: ED_view3d.h:66
struct View3D * v3d
Definition: ED_view3d.h:70
const char * name
Definition: WM_types.h:888
const char * idname
Definition: WM_types.h:890
bool(* poll)(struct bContext *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:943
struct StructRNA * srna
Definition: WM_types.h:969
const char * description
Definition: WM_types.h:893
int(* exec)(struct bContext *, struct wmOperator *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:903
struct ReportList * reports
struct PointerRNA * ptr
wmOperatorType * ot
Definition: wm_files.c:3479