Blender  V2.93
editmesh_knife_project.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2013 Blender Foundation.
17  * All rights reserved.
18  */
19 
24 #include "DNA_curve_types.h"
25 #include "DNA_object_types.h"
26 
27 #include "BLI_linklist.h"
28 #include "BLI_listbase.h"
29 #include "BLI_math.h"
30 
31 #include "BKE_context.h"
32 #include "BKE_curve.h"
33 #include "BKE_editmesh.h"
34 #include "BKE_lib_id.h"
35 #include "BKE_mesh.h"
36 #include "BKE_mesh_runtime.h"
37 #include "BKE_object.h"
38 #include "BKE_report.h"
39 
40 #include "DEG_depsgraph.h"
41 #include "DEG_depsgraph_query.h"
42 
43 #include "RNA_access.h"
44 #include "RNA_define.h"
45 
46 #include "MEM_guardedalloc.h"
47 
48 #include "WM_types.h"
49 
50 #include "ED_mesh.h"
51 #include "ED_screen.h"
52 #include "ED_view3d.h"
53 
54 #include "mesh_intern.h" /* own include */
55 
57  Scene *scene,
58  Object *ob,
59  LinkNode *polys)
60 {
62  ARegion *region = CTX_wm_region(C);
63  struct Mesh *me_eval;
64  bool me_eval_needs_free;
65 
66  if (ob->type == OB_MESH || ob->runtime.data_eval) {
68  me_eval = BKE_object_get_evaluated_mesh(ob_eval);
69  if (me_eval == NULL) {
70  Scene *scene_eval = (Scene *)DEG_get_evaluated_id(depsgraph, &scene->id);
71  me_eval = mesh_get_eval_final(depsgraph, scene_eval, ob_eval, &CD_MASK_BAREMESH);
72  }
73  me_eval_needs_free = false;
74  }
75  else if (ELEM(ob->type, OB_FONT, OB_CURVE, OB_SURF)) {
77  me_eval = BKE_mesh_new_nomain_from_curve(ob_eval);
78  me_eval_needs_free = true;
79  }
80  else {
81  me_eval = NULL;
82  }
83 
84  if (me_eval) {
85  ListBase nurbslist = {NULL, NULL};
86  float projmat[4][4];
87 
88  BKE_mesh_to_curve_nurblist(me_eval, &nurbslist, 0); /* wire */
89  BKE_mesh_to_curve_nurblist(me_eval, &nurbslist, 1); /* boundary */
90 
91  ED_view3d_ob_project_mat_get(region->regiondata, ob, projmat);
92 
93  if (nurbslist.first) {
94  Nurb *nu;
95  for (nu = nurbslist.first; nu; nu = nu->next) {
96  if (nu->bp) {
97  int a;
98  BPoint *bp;
99  bool is_cyclic = (nu->flagu & CU_NURB_CYCLIC) != 0;
100  float(*mval)[2] = MEM_mallocN(sizeof(*mval) * (nu->pntsu + is_cyclic), __func__);
101 
102  for (bp = nu->bp, a = 0; a < nu->pntsu; a++, bp++) {
103  ED_view3d_project_float_v2_m4(region, bp->vec, mval[a], projmat);
104  }
105  if (is_cyclic) {
106  copy_v2_v2(mval[a], mval[0]);
107  }
108 
109  BLI_linklist_prepend(&polys, mval);
110  }
111  }
112  }
113 
114  BKE_nurbList_free(&nurbslist);
115 
116  if (me_eval_needs_free) {
117  BKE_id_free(NULL, (ID *)me_eval);
118  }
119  }
120 
121  return polys;
122 }
123 
125 {
127  Object *obedit = CTX_data_edit_object(C);
128  BMEditMesh *em = BKE_editmesh_from_object(obedit);
129  const bool cut_through = RNA_boolean_get(op->ptr, "cut_through");
130 
131  LinkNode *polys = NULL;
132 
133  CTX_DATA_BEGIN (C, Object *, ob, selected_objects) {
134  if (BKE_object_is_in_editmode(ob)) {
135  continue;
136  }
137  BLI_assert(ob != obedit);
138  polys = knifeproject_poly_from_object(C, scene, ob, polys);
139  }
140  CTX_DATA_END;
141 
142  if (polys) {
143  EDBM_mesh_knife(C, polys, true, cut_through);
144 
145  /* select only tagged faces */
147 
149 
151 
153 
154  BLI_linklist_freeN(polys);
155 
156  return OPERATOR_FINISHED;
157  }
158 
159  BKE_report(op->reports,
160  RPT_ERROR,
161  "No other selected objects have wire or boundary edges to use for projection");
162  return OPERATOR_CANCELLED;
163 }
164 
166 {
167  /* description */
168  ot->name = "Knife Project";
169  ot->idname = "MESH_OT_knife_project";
170  ot->description = "Use other objects outlines and boundaries to project knife cuts";
171 
172  /* callbacks */
175 
176  /* flags */
178 
179  /* parameters */
181  "cut_through",
182  false,
183  "Cut Through",
184  "Cut through all faces, not just visible ones");
185 }
typedef float(TangentPoint)[2]
struct Scene * CTX_data_scene(const bContext *C)
Definition: context.c:1034
struct Object * CTX_data_edit_object(const bContext *C)
Definition: context.c:1296
#define CTX_DATA_BEGIN(C, Type, instance, member)
Definition: BKE_context.h:252
struct Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C)
Definition: context.c:1424
struct ARegion * CTX_wm_region(const bContext *C)
Definition: context.c:725
#define CTX_DATA_END
Definition: BKE_context.h:260
void BKE_nurbList_free(struct ListBase *lb)
Definition: curve.c:660
const CustomData_MeshMasks CD_MASK_BAREMESH
Definition: customdata.c:1919
BMEditMesh * BKE_editmesh_from_object(struct Object *ob)
Return the BMEditMesh for a given object.
Definition: editmesh.c:85
void BKE_id_free(struct Main *bmain, void *idv)
void BKE_mesh_to_curve_nurblist(const struct Mesh *me, struct ListBase *nurblist, const int edge_users_test)
struct Mesh * BKE_mesh_new_nomain_from_curve(struct Object *ob)
Definition: mesh_convert.c:572
struct Mesh * mesh_get_eval_final(struct Depsgraph *depsgraph, 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(struct Object *object)
Definition: object.c:4459
bool BKE_object_is_in_editmode(const struct Object *ob)
void BKE_report(ReportList *reports, ReportType type, const char *message)
Definition: report.c:104
#define BLI_assert(a)
Definition: BLI_assert.h:58
MINLINE void copy_v2_v2(float r[2], const float a[2])
#define ELEM(...)
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:51
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_CURVE
#define SCE_SELECT_VERTEX
#define SCE_SELECT_EDGE
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
bool EDBM_selectmode_disable_multi(struct bContext *C, const short selectmode_disable, const short selectmode_fallback)
bool ED_operator_editmesh_region_view3d(struct bContext *C)
Definition: screen_ops.c:418
void ED_view3d_ob_project_mat_get(const struct RegionView3D *v3d, 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], float mat[4][4])
Read Guarded memory(de)allocation.
#define C
Definition: RandGen.cpp:39
@ OPTYPE_BLOCKING
Definition: WM_types.h:157
@ OPTYPE_UNDO
Definition: WM_types.h:155
@ OPTYPE_REGISTER
Definition: WM_types.h:153
@ 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
void EDBM_mesh_knife(bContext *C, 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_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
static unsigned a[3]
Definition: RandGen.cpp:92
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:6261
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, bool default_value, const char *ui_name, const char *ui_description)
Definition: rna_define.c:3481
void * regiondata
struct BMesh * bm
Definition: BKE_editmesh.h:52
float vec[4]
Definition: DNA_ID.h:273
void * first
Definition: DNA_listBase.h:47
short flagu
struct Nurb * next
BPoint * bp
struct ID * data_eval
Object_Runtime runtime
const char * name
Definition: WM_types.h:721
const char * idname
Definition: WM_types.h:723
bool(* poll)(struct bContext *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:776
struct StructRNA * srna
Definition: WM_types.h:802
const char * description
Definition: WM_types.h:726
int(* exec)(struct bContext *, struct wmOperator *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:736
struct ReportList * reports
struct PointerRNA * ptr
wmOperatorType * ot
Definition: wm_files.c:3156