Blender  V2.93
editmesh_extrude_screw.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) 2004 by Blender Foundation.
17  * All rights reserved.
18  */
19 
24 #include "MEM_guardedalloc.h"
25 
26 #include "DNA_object_types.h"
27 
28 #include "BLI_math.h"
29 
30 #include "BKE_context.h"
31 #include "BKE_editmesh.h"
32 #include "BKE_layer.h"
33 #include "BKE_report.h"
34 
35 #include "RNA_access.h"
36 #include "RNA_define.h"
37 
38 #include "WM_types.h"
39 
40 #include "ED_mesh.h"
41 #include "ED_screen.h"
42 #include "ED_view3d.h"
43 
44 #include "mesh_intern.h" /* own include */
45 
46 /* -------------------------------------------------------------------- */
51 {
52  BMEdge *eed;
53  BMVert *eve, *v1, *v2;
54  BMIter iter, eiter;
55  float dvec[3], nor[3], cent[3], axis[3], v1_co_global[3], v2_co_global[3];
56  int steps, turns;
57  int valence;
58  uint objects_empty_len = 0;
59  uint failed_axis_len = 0;
60  uint failed_vertices_len = 0;
61 
62  turns = RNA_int_get(op->ptr, "turns");
63  steps = RNA_int_get(op->ptr, "steps");
64  RNA_float_get_array(op->ptr, "center", cent);
65  RNA_float_get_array(op->ptr, "axis", axis);
66 
67  uint objects_len = 0;
68  ViewLayer *view_layer = CTX_data_view_layer(C);
70  view_layer, CTX_wm_view3d(C), &objects_len);
71 
72  for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
73  Object *obedit = objects[ob_index];
75  BMesh *bm = em->bm;
76 
77  if (bm->totvertsel < 2) {
78  if (bm->totvertsel == 0) {
79  objects_empty_len++;
80  }
81  continue;
82  }
83 
84  if (is_zero_v3(axis)) {
85  failed_axis_len++;
86  continue;
87  }
88 
89  /* find two vertices with valence count == 1, more or less is wrong */
90  v1 = NULL;
91  v2 = NULL;
92 
93  BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
94  valence = 0;
95  BM_ITER_ELEM (eed, &eiter, eve, BM_EDGES_OF_VERT) {
97  valence++;
98  }
99  }
100 
101  if (valence == 1) {
102  if (v1 == NULL) {
103  v1 = eve;
104  }
105  else if (v2 == NULL) {
106  v2 = eve;
107  }
108  else {
109  v1 = NULL;
110  break;
111  }
112  }
113  }
114 
115  if (v1 == NULL || v2 == NULL) {
116  failed_vertices_len++;
117  continue;
118  }
119 
120  copy_v3_v3(nor, obedit->obmat[2]);
121 
122  /* calculate dvec */
123  mul_v3_m4v3(v1_co_global, obedit->obmat, v1->co);
124  mul_v3_m4v3(v2_co_global, obedit->obmat, v2->co);
125  sub_v3_v3v3(dvec, v1_co_global, v2_co_global);
126  mul_v3_fl(dvec, 1.0f / steps);
127 
128  if (dot_v3v3(nor, dvec) > 0.0f) {
129  negate_v3(dvec);
130  }
131 
132  BMOperator spinop;
133  if (!EDBM_op_init(
134  em,
135  &spinop,
136  op,
137  "spin geom=%hvef cent=%v axis=%v dvec=%v steps=%i angle=%f space=%m4 use_duplicate=%b",
139  cent,
140  axis,
141  dvec,
142  turns * steps,
143  DEG2RADF(360.0f * turns),
144  obedit->obmat,
145  false)) {
146  continue;
147  }
148 
149  BMO_op_exec(bm, &spinop);
152  bm, spinop.slots_out, "geom_last.out", BM_ALL_NOLOOP, BM_ELEM_SELECT, true);
153 
154  if (!EDBM_op_finish(em, &spinop, op, true)) {
155  continue;
156  }
157 
158  EDBM_update_generic(obedit->data, true, true);
159  }
160  MEM_freeN(objects);
161 
162  if (failed_axis_len == objects_len - objects_empty_len) {
163  BKE_report(op->reports, RPT_ERROR, "Invalid/unset axis");
164  }
165  else if (failed_vertices_len == objects_len - objects_empty_len) {
166  BKE_report(op->reports, RPT_ERROR, "You have to select a string of connected vertices too");
167  }
168 
169  return OPERATOR_FINISHED;
170 }
171 
172 /* get center and axis, in global coords */
173 static int edbm_screw_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
174 {
177 
178  PropertyRNA *prop;
179  prop = RNA_struct_find_property(op->ptr, "center");
180  if (!RNA_property_is_set(op->ptr, prop)) {
182  }
183  if (rv3d) {
184  prop = RNA_struct_find_property(op->ptr, "axis");
185  if (!RNA_property_is_set(op->ptr, prop)) {
186  RNA_property_float_set_array(op->ptr, prop, rv3d->viewinv[1]);
187  }
188  }
189 
190  return edbm_screw_exec(C, op);
191 }
192 
194 {
195  /* identifiers */
196  ot->name = "Screw";
197  ot->description =
198  "Extrude selected vertices in screw-shaped rotation around the cursor in indicated viewport";
199  ot->idname = "MESH_OT_screw";
200 
201  /* api callbacks */
205 
206  /* flags */
208 
209  /* props */
210  RNA_def_int(ot->srna, "steps", 9, 1, 100000, "Steps", "Steps", 3, 256);
211  RNA_def_int(ot->srna, "turns", 1, 1, 100000, "Turns", "Turns", 1, 256);
212 
214  "center",
215  3,
216  NULL,
217  -1e12f,
218  1e12f,
219  "Center",
220  "Center in global view space",
221  -1e4f,
222  1e4f);
224  ot->srna, "axis", 3, NULL, -1.0f, 1.0f, "Axis", "Axis in global view space", -1.0f, 1.0f);
225 }
226 
struct Scene * CTX_data_scene(const bContext *C)
Definition: context.c:1034
struct ViewLayer * CTX_data_view_layer(const bContext *C)
Definition: context.c:1044
struct View3D * CTX_wm_view3d(const bContext *C)
Definition: context.c:760
BMEditMesh * BKE_editmesh_from_object(struct Object *ob)
Return the BMEditMesh for a given object.
Definition: editmesh.c:85
#define BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, v3d, r_len)
Definition: BKE_layer.h:426
void BKE_report(ReportList *reports, ReportType type, const char *message)
Definition: report.c:104
void mul_v3_m4v3(float r[3], const float M[4][4], const float v[3])
Definition: math_matrix.c:742
#define DEG2RADF(_deg)
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void mul_v3_fl(float r[3], float f)
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE bool is_zero_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void negate_v3(float r[3])
unsigned int uint
Definition: BLI_sys_types.h:83
#define UNUSED(x)
Object is a sort of wrapper for general info.
@ OPERATOR_FINISHED
void EDBM_update_generic(struct Mesh *me, const bool do_tessellation, const bool is_destructive)
void EDBM_flag_disable_all(struct BMEditMesh *em, const char hflag)
bool ED_operator_editmesh(struct bContext *C)
Definition: screen_ops.c:404
struct RegionView3D * ED_view3d_context_rv3d(struct bContext *C)
Definition: space_view3d.c:89
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble v1
Read Guarded memory(de)allocation.
#define C
Definition: RandGen.cpp:39
@ OPTYPE_UNDO
Definition: WM_types.h:155
@ OPTYPE_REGISTER
Definition: WM_types.h:153
#define BM_ALL_NOLOOP
Definition: bmesh_class.h:411
@ BM_ELEM_SELECT
Definition: bmesh_class.h:471
#define BM_elem_flag_test(ele, hflag)
Definition: bmesh_inline.h:26
#define BM_ITER_ELEM(ele, iter, data, itype)
#define BM_ITER_MESH(ele, iter, bm, itype)
@ BM_VERTS_OF_MESH
@ BM_EDGES_OF_VERT
ATTR_WARN_UNUSED_RESULT BMesh * bm
void BMO_slot_buffer_hflag_enable(BMesh *bm, BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name, const char htype, const char hflag, const bool do_flush)
BMO_FLAG_BUFFER.
void BMO_op_exec(BMesh *bm, BMOperator *op)
BMESH OPSTACK EXEC OP.
ATTR_WARN_UNUSED_RESULT const BMVert * v2
Scene scene
static int edbm_screw_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
static int edbm_screw_exec(bContext *C, wmOperator *op)
void MESH_OT_screw(wmOperatorType *ot)
bool EDBM_op_init(BMEditMesh *em, BMOperator *bmop, wmOperator *op, const char *fmt,...)
bool EDBM_op_finish(BMEditMesh *em, BMOperator *bmop, wmOperator *op, const bool do_report)
uint nor
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
bool RNA_property_is_set(PointerRNA *ptr, PropertyRNA *prop)
Definition: rna_access.c:6655
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
Definition: rna_access.c:866
void RNA_float_get_array(PointerRNA *ptr, const char *name, float *values)
Definition: rna_access.c:6378
int RNA_int_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:6308
void RNA_property_float_set_array(PointerRNA *ptr, PropertyRNA *prop, const float *values)
Definition: rna_access.c:3132
PropertyRNA * RNA_def_float_vector(StructOrFunctionRNA *cont_, const char *identifier, int len, const float *default_value, float hardmin, float hardmax, const char *ui_name, const char *ui_description, float softmin, float softmax)
Definition: rna_define.c:3851
PropertyRNA * RNA_def_float_vector_xyz(StructOrFunctionRNA *cont_, const char *identifier, int len, const float *default_value, float hardmin, float hardmax, const char *ui_name, const char *ui_description, float softmin, float softmax)
Definition: rna_define.c:3883
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, int default_value, int hardmin, int hardmax, const char *ui_name, const char *ui_description, int softmin, int softmax)
Definition: rna_define.c:3585
static const int steps
Definition: sky_nishita.cpp:28
struct BMesh * bm
Definition: BKE_editmesh.h:52
struct BMOpSlot slots_out[BMO_OP_MAX_SLOTS]
float co[3]
Definition: bmesh_class.h:99
int totvertsel
Definition: bmesh_class.h:298
float obmat[4][4]
void * data
float viewinv[4][4]
View3DCursor cursor
int(* invoke)(struct bContext *, struct wmOperator *, const struct wmEvent *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:752
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