1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
|
/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2004 by Blender Foundation.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): Joseph Eagar
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file blender/editors/mesh/editmesh_add.c
* \ingroup edmesh
*/
#include "DNA_mesh_types.h"
#include "DNA_object_types.h"
#include "DNA_scene_types.h"
#include "RNA_define.h"
#include "RNA_access.h"
#include "BLI_math.h"
#include "BKE_context.h"
#include "BKE_depsgraph.h"
#include "BKE_library.h"
#include "BKE_tessmesh.h"
#include "WM_api.h"
#include "WM_types.h"
#include "ED_mesh.h"
#include "ED_screen.h"
#include "ED_object.h"
#include "mesh_intern.h"
/* uses context to figure out transform for primitive */
/* returns standard diameter */
static float new_primitive_matrix(bContext *C, float *loc, float *rot, float primmat[][4])
{
Object *obedit = CTX_data_edit_object(C);
View3D *v3d = CTX_wm_view3d(C);
float mat[3][3], rmat[3][3], cmat[3][3], imat[3][3];
unit_m4(primmat);
eul_to_mat3(rmat, rot);
invert_m3(rmat);
/* inverse transform for initial rotation and object */
copy_m3_m4(mat, obedit->obmat);
mul_m3_m3m3(cmat, rmat, mat);
invert_m3_m3(imat, cmat);
copy_m4_m3(primmat, imat);
/* center */
copy_v3_v3(primmat[3], loc);
sub_v3_v3(primmat[3], obedit->obmat[3]);
invert_m3_m3(imat, mat);
mul_m3_v3(imat, primmat[3]);
return v3d ? v3d->grid : 1.0f;
}
/* ********* add primitive operators ************* */
static void make_prim_init(bContext *C, const char *idname,
float *dia, float mat[][4],
int *state, float *loc, float *rot, unsigned int layer)
{
Object *obedit = CTX_data_edit_object(C);
*state = 0;
if (obedit == NULL || obedit->type != OB_MESH) {
obedit = ED_object_add_type(C, OB_MESH, loc, rot, FALSE, layer);
rename_id((ID *)obedit, idname);
rename_id((ID *)obedit->data, idname);
/* create editmode */
ED_object_enter_editmode(C, EM_DO_UNDO | EM_IGNORE_LAYER); /* rare cases the active layer is messed up */
*state = 1;
}
*dia *= new_primitive_matrix(C, loc, rot, mat);
}
static void make_prim_finish(bContext *C, int *state, int enter_editmode)
{
Object *obedit = CTX_data_edit_object(C);
BMEditMesh *em = BMEdit_FromObject(obedit);
/* Primitive has all verts selected, use vert select flush
* to push this up to edges & faces. */
EDBM_selectmode_flush_ex(em, SCE_SELECT_VERTEX);
EDBM_update_generic(C, em, TRUE);
/* userdef */
if (*state && !enter_editmode) {
ED_object_exit_editmode(C, EM_FREEDATA); /* adding EM_DO_UNDO messes up operator redo */
}
WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, obedit);
}
static int add_primitive_plane_exec(bContext *C, wmOperator *op)
{
Object *obedit;
Mesh *me;
BMEditMesh *em;
float loc[3], rot[3], mat[4][4], dia = 1.0f;
int enter_editmode;
int state;
unsigned int layer;
ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer, NULL);
make_prim_init(C, "Plane", &dia, mat, &state, loc, rot, layer);
obedit = CTX_data_edit_object(C);
me = obedit->data;
em = me->edit_btmesh;
if (!EDBM_op_call_and_selectf(em, op, "vertout",
"create_grid xsegments=%i ysegments=%i size=%f mat=%m4", 1, 1, dia, mat))
{
return OPERATOR_CANCELLED;
}
make_prim_finish(C, &state, enter_editmode);
return OPERATOR_FINISHED;
}
void MESH_OT_primitive_plane_add(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Add Plane";
ot->description = "Construct a filled planar mesh with 4 vertices";
ot->idname = "MESH_OT_primitive_plane_add";
/* api callbacks */
ot->invoke = ED_object_add_generic_invoke;
ot->exec = add_primitive_plane_exec;
ot->poll = ED_operator_scene_editable;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
ED_object_add_generic_props(ot, TRUE);
}
static int add_primitive_cube_exec(bContext *C, wmOperator *op)
{
Object *obedit;
Mesh *me;
BMEditMesh *em;
float loc[3], rot[3], mat[4][4], dia;
int enter_editmode;
int state;
unsigned int layer;
ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer, NULL);
make_prim_init(C, "Cube", &dia, mat, &state, loc, rot, layer);
obedit = CTX_data_edit_object(C);
me = obedit->data;
em = me->edit_btmesh;
if (!EDBM_op_call_and_selectf(em, op, "vertout", "create_cube mat=%m4 size=%f", mat, 2.0f)) {
return OPERATOR_CANCELLED;
}
/* BMESH_TODO make plane side this: M_SQRT2 - plane (diameter of 1.41 makes it unit size) */
make_prim_finish(C, &state, enter_editmode);
return OPERATOR_FINISHED;
}
void MESH_OT_primitive_cube_add(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Add Cube";
ot->description = "Construct a cube mesh";
ot->idname = "MESH_OT_primitive_cube_add";
/* api callbacks */
ot->invoke = ED_object_add_generic_invoke;
ot->exec = add_primitive_cube_exec;
ot->poll = ED_operator_scene_editable;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
ED_object_add_generic_props(ot, TRUE);
}
static const EnumPropertyItem fill_type_items[] = {
{0, "NOTHING", 0, "Nothing", "Don't fill at all"},
{1, "NGON", 0, "Ngon", "Use ngons"},
{2, "TRIFAN", 0, "Triangle Fan", "Use triangle fans"},
{0, NULL, 0, NULL, NULL}};
static int add_primitive_circle_exec(bContext *C, wmOperator *op)
{
Object *obedit;
Mesh *me;
BMEditMesh *em;
float loc[3], rot[3], mat[4][4], dia;
int enter_editmode;
int state, cap_end, cap_tri;
unsigned int layer;
cap_end = RNA_enum_get(op->ptr, "fill_type");
cap_tri = (cap_end == 2);
ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer, NULL);
make_prim_init(C, "Circle", &dia, mat, &state, loc, rot, layer);
obedit = CTX_data_edit_object(C);
me = obedit->data;
em = me->edit_btmesh;
if (!EDBM_op_call_and_selectf(em, op, "vertout",
"create_circle segments=%i diameter=%f cap_ends=%b cap_tris=%b mat=%m4",
RNA_int_get(op->ptr, "vertices"), RNA_float_get(op->ptr, "radius"),
cap_end, cap_tri, mat))
{
return OPERATOR_CANCELLED;
}
make_prim_finish(C, &state, enter_editmode);
return OPERATOR_FINISHED;
}
void MESH_OT_primitive_circle_add(wmOperatorType *ot)
{
PropertyRNA *prop;
/* identifiers */
ot->name = "Add Circle";
ot->description = "Construct a circle mesh";
ot->idname = "MESH_OT_primitive_circle_add";
/* api callbacks */
ot->invoke = ED_object_add_generic_invoke;
ot->exec = add_primitive_circle_exec;
ot->poll = ED_operator_scene_editable;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
/* props */
RNA_def_int(ot->srna, "vertices", 32, 3, INT_MAX, "Vertices", "", 3, 500);
prop = RNA_def_float(ot->srna, "radius", 1.0f, 0.0, FLT_MAX, "Radius", "", 0.001, 100.00);
RNA_def_property_subtype(prop, PROP_DISTANCE);
RNA_def_enum(ot->srna, "fill_type", fill_type_items, 0, "Fill Type", "");
ED_object_add_generic_props(ot, TRUE);
}
static int add_primitive_cylinder_exec(bContext *C, wmOperator *op)
{
Object *obedit;
Mesh *me;
BMEditMesh *em;
float loc[3], rot[3], mat[4][4], dia;
int enter_editmode;
int state, cap_end, cap_tri;
unsigned int layer;
cap_end = RNA_enum_get(op->ptr, "end_fill_type");
cap_tri = (cap_end == 2);
ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer, NULL);
make_prim_init(C, "Cylinder", &dia, mat, &state, loc, rot, layer);
obedit = CTX_data_edit_object(C);
me = obedit->data;
em = me->edit_btmesh;
if (!EDBM_op_call_and_selectf(
em, op, "vertout",
"create_cone segments=%i diameter1=%f diameter2=%f cap_ends=%b cap_tris=%b depth=%f mat=%m4",
RNA_int_get(op->ptr, "vertices"),
RNA_float_get(op->ptr, "radius"),
RNA_float_get(op->ptr, "radius"),
cap_end, cap_tri,
RNA_float_get(op->ptr, "depth"), mat))
{
return OPERATOR_CANCELLED;
}
make_prim_finish(C, &state, enter_editmode);
return OPERATOR_FINISHED;
}
void MESH_OT_primitive_cylinder_add(wmOperatorType *ot)
{
PropertyRNA *prop;
/* identifiers */
ot->name = "Add Cylinder";
ot->description = "Construct a cylinder mesh";
ot->idname = "MESH_OT_primitive_cylinder_add";
/* api callbacks */
ot->invoke = ED_object_add_generic_invoke;
ot->exec = add_primitive_cylinder_exec;
ot->poll = ED_operator_scene_editable;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
/* props */
RNA_def_int(ot->srna, "vertices", 32, 3, INT_MAX, "Vertices", "", 3, 500);
prop = RNA_def_float(ot->srna, "radius", 1.0f, 0.0, FLT_MAX, "Radius", "", 0.001, 100.00);
RNA_def_property_subtype(prop, PROP_DISTANCE);
prop = RNA_def_float(ot->srna, "depth", 2.0f, 0.0, FLT_MAX, "Depth", "", 0.001, 100.00);
RNA_def_property_subtype(prop, PROP_DISTANCE);
RNA_def_enum(ot->srna, "end_fill_type", fill_type_items, 1, "Cap Fill Type", "");
ED_object_add_generic_props(ot, TRUE);
}
static int add_primitive_cone_exec(bContext *C, wmOperator *op)
{
Object *obedit;
Mesh *me;
BMEditMesh *em;
float loc[3], rot[3], mat[4][4], dia;
int enter_editmode;
int state, cap_end, cap_tri;
unsigned int layer;
cap_end = RNA_enum_get(op->ptr, "end_fill_type");
cap_tri = (cap_end == 2);
ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer, NULL);
make_prim_init(C, "Cone", &dia, mat, &state, loc, rot, layer);
obedit = CTX_data_edit_object(C);
me = obedit->data;
em = me->edit_btmesh;
if (!EDBM_op_call_and_selectf(
em, op, "vertout",
"create_cone segments=%i diameter1=%f diameter2=%f cap_ends=%b cap_tris=%b depth=%f mat=%m4",
RNA_int_get(op->ptr, "vertices"), RNA_float_get(op->ptr, "radius1"),
RNA_float_get(op->ptr, "radius2"), cap_end, cap_tri, RNA_float_get(op->ptr, "depth"), mat))
{
return OPERATOR_CANCELLED;
}
make_prim_finish(C, &state, enter_editmode);
return OPERATOR_FINISHED;
}
void MESH_OT_primitive_cone_add(wmOperatorType *ot)
{
PropertyRNA *prop;
/* identifiers */
ot->name = "Add Cone";
ot->description = "Construct a conic mesh";
ot->idname = "MESH_OT_primitive_cone_add";
/* api callbacks */
ot->invoke = ED_object_add_generic_invoke;
ot->exec = add_primitive_cone_exec;
ot->poll = ED_operator_scene_editable;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
/* props */
RNA_def_int(ot->srna, "vertices", 32, 3, INT_MAX, "Vertices", "", 3, 500);
prop = RNA_def_float(ot->srna, "radius1", 1.0f, 0.0, FLT_MAX, "Radius 1", "", 0.001, 100.00);
RNA_def_property_subtype(prop, PROP_DISTANCE);
prop = RNA_def_float(ot->srna, "radius2", 0.0f, 0.0, FLT_MAX, "Radius 2", "", 0.001, 100.00);
RNA_def_property_subtype(prop, PROP_DISTANCE);
prop = RNA_def_float(ot->srna, "depth", 2.0f, 0.0, FLT_MAX, "Depth", "", 0.001, 100.00);
RNA_def_property_subtype(prop, PROP_DISTANCE);
RNA_def_enum(ot->srna, "end_fill_type", fill_type_items, 1, "Base Fill Type", "");
ED_object_add_generic_props(ot, TRUE);
}
static int add_primitive_grid_exec(bContext *C, wmOperator *op)
{
Object *obedit;
Mesh *me;
BMEditMesh *em;
float loc[3], rot[3], mat[4][4], dia = 1.0f;
int enter_editmode;
int state;
unsigned int layer;
ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer, NULL);
make_prim_init(C, "Grid", &dia, mat, &state, loc, rot, layer);
obedit = CTX_data_edit_object(C);
me = obedit->data;
em = me->edit_btmesh;
if (!EDBM_op_call_and_selectf(em, op, "vertout",
"create_grid xsegments=%i ysegments=%i size=%f mat=%m4",
RNA_int_get(op->ptr, "x_subdivisions"),
RNA_int_get(op->ptr, "y_subdivisions"),
RNA_float_get(op->ptr, "size") * dia, mat))
{
return OPERATOR_CANCELLED;
}
make_prim_finish(C, &state, enter_editmode);
return OPERATOR_FINISHED;
}
void MESH_OT_primitive_grid_add(wmOperatorType *ot)
{
PropertyRNA *prop;
/* identifiers */
ot->name = "Add Grid";
ot->description = "Construct a grid mesh";
ot->idname = "MESH_OT_primitive_grid_add";
/* api callbacks */
ot->invoke = ED_object_add_generic_invoke;
ot->exec = add_primitive_grid_exec;
ot->poll = ED_operator_scene_editable;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
/* props */
RNA_def_int(ot->srna, "x_subdivisions", 10, 3, INT_MAX, "X Subdivisions", "", 3, 1000);
RNA_def_int(ot->srna, "y_subdivisions", 10, 3, INT_MAX, "Y Subdivisions", "", 3, 1000);
prop = RNA_def_float(ot->srna, "size", 1.0f, 0.0, FLT_MAX, "Size", "", 0.001, FLT_MAX);
RNA_def_property_subtype(prop, PROP_DISTANCE);
ED_object_add_generic_props(ot, TRUE);
}
static int add_primitive_monkey_exec(bContext *C, wmOperator *op)
{
Object *obedit;
Mesh *me;
BMEditMesh *em;
float loc[3], rot[3], mat[4][4], dia;
int enter_editmode;
int state, view_aligned;
unsigned int layer;
ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer, &view_aligned);
if (!view_aligned)
rot[0] += M_PI / 2.0f;
make_prim_init(C, "Monkey", &dia, mat, &state, loc, rot, layer);
obedit = CTX_data_edit_object(C);
me = obedit->data;
em = me->edit_btmesh;
if (!EDBM_op_call_and_selectf(em, op, "vertout", "create_monkey mat=%m4", mat)) {
return OPERATOR_CANCELLED;
}
make_prim_finish(C, &state, enter_editmode);
return OPERATOR_FINISHED;
}
void MESH_OT_primitive_monkey_add(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Add Monkey";
ot->description = "Construct a Suzanne mesh";
ot->idname = "MESH_OT_primitive_monkey_add";
/* api callbacks */
ot->invoke = ED_object_add_generic_invoke;
ot->exec = add_primitive_monkey_exec;
ot->poll = ED_operator_scene_editable;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
ED_object_add_generic_props(ot, TRUE);
}
static int add_primitive_uvsphere_exec(bContext *C, wmOperator *op)
{
Object *obedit;
Mesh *me;
BMEditMesh *em;
float loc[3], rot[3], mat[4][4], dia;
int enter_editmode;
int state;
unsigned int layer;
ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer, NULL);
make_prim_init(C, "Sphere", &dia, mat, &state, loc, rot, layer);
obedit = CTX_data_edit_object(C);
me = obedit->data;
em = me->edit_btmesh;
if (!EDBM_op_call_and_selectf(em, op, "vertout",
"create_uvsphere segments=%i revolutions=%i diameter=%f mat=%m4",
RNA_int_get(op->ptr, "segments"), RNA_int_get(op->ptr, "ring_count"),
RNA_float_get(op->ptr, "size"), mat))
{
return OPERATOR_CANCELLED;
}
make_prim_finish(C, &state, enter_editmode);
return OPERATOR_FINISHED;
}
void MESH_OT_primitive_uv_sphere_add(wmOperatorType *ot)
{
PropertyRNA *prop;
/* identifiers */
ot->name = "Add UV Sphere";
ot->description = "Construct a UV sphere mesh";
ot->idname = "MESH_OT_primitive_uv_sphere_add";
/* api callbacks */
ot->invoke = ED_object_add_generic_invoke;
ot->exec = add_primitive_uvsphere_exec;
ot->poll = ED_operator_scene_editable;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
/* props */
RNA_def_int(ot->srna, "segments", 32, 3, INT_MAX, "Segments", "", 3, 500);
RNA_def_int(ot->srna, "ring_count", 16, 3, INT_MAX, "Rings", "", 3, 500);
prop = RNA_def_float(ot->srna, "size", 1.0f, 0.0, FLT_MAX, "Size", "", 0.001, 100.00);
RNA_def_property_subtype(prop, PROP_DISTANCE);
ED_object_add_generic_props(ot, TRUE);
}
static int add_primitive_icosphere_exec(bContext *C, wmOperator *op)
{
Object *obedit;
Mesh *me;
BMEditMesh *em;
float loc[3], rot[3], mat[4][4], dia;
int enter_editmode;
int state;
unsigned int layer;
ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer, NULL);
make_prim_init(C, "Icosphere", &dia, mat, &state, loc, rot, layer);
obedit = CTX_data_edit_object(C);
me = obedit->data;
em = me->edit_btmesh;
if (!EDBM_op_call_and_selectf(
em, op, "vertout",
"create_icosphere subdivisions=%i diameter=%f mat=%m4",
RNA_int_get(op->ptr, "subdivisions"),
RNA_float_get(op->ptr, "size"), mat))
{
return OPERATOR_CANCELLED;
}
make_prim_finish(C, &state, enter_editmode);
return OPERATOR_FINISHED;
}
void MESH_OT_primitive_ico_sphere_add(wmOperatorType *ot)
{
PropertyRNA *prop;
/* identifiers */
ot->name = "Add Ico Sphere";
ot->description = "Construct an Icosphere mesh";
ot->idname = "MESH_OT_primitive_ico_sphere_add";
/* api callbacks */
ot->invoke = ED_object_add_generic_invoke;
ot->exec = add_primitive_icosphere_exec;
ot->poll = ED_operator_scene_editable;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
/* props */
RNA_def_int(ot->srna, "subdivisions", 2, 1, INT_MAX, "Subdivisions", "", 1, 8);
prop = RNA_def_float(ot->srna, "size", 1.0f, 0.0f, FLT_MAX, "Size", "", 0.001f, 100.00);
RNA_def_property_subtype(prop, PROP_DISTANCE);
ED_object_add_generic_props(ot, TRUE);
}
|