Moved function declarations from BKE_DerivedMesh.h to BKE_mesh_runtime.h
[blender.git] / source / blender / editors / object / object_add.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation, 2002-2008 full recode
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/object/object_add.c
27  *  \ingroup edobj
28  */
29
30
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "MEM_guardedalloc.h"
35
36 #include "DNA_anim_types.h"
37 #include "DNA_camera_types.h"
38 #include "DNA_curve_types.h"
39 #include "DNA_group_types.h"
40 #include "DNA_lamp_types.h"
41 #include "DNA_key_types.h"
42 #include "DNA_material_types.h"
43 #include "DNA_mesh_types.h"
44 #include "DNA_meta_types.h"
45 #include "DNA_object_fluidsim_types.h"
46 #include "DNA_object_force_types.h"
47 #include "DNA_object_types.h"
48 #include "DNA_lightprobe_types.h"
49 #include "DNA_scene_types.h"
50 #include "DNA_vfont_types.h"
51 #include "DNA_gpencil_types.h"
52
53 #include "BLI_utildefines.h"
54 #include "BLI_ghash.h"
55 #include "BLI_listbase.h"
56 #include "BLI_math.h"
57 #include "BLI_string.h"
58
59 #include "BLT_translation.h"
60
61 #include "BKE_action.h"
62 #include "BKE_anim.h"
63 #include "BKE_animsys.h"
64 #include "BKE_armature.h"
65 #include "BKE_camera.h"
66 #include "BKE_collection.h"
67 #include "BKE_context.h"
68 #include "BKE_constraint.h"
69 #include "BKE_curve.h"
70 #include "BKE_DerivedMesh.h"
71 #include "BKE_displist.h"
72 #include "BKE_effect.h"
73 #include "BKE_font.h"
74 #include "BKE_lamp.h"
75 #include "BKE_lattice.h"
76 #include "BKE_layer.h"
77 #include "BKE_library.h"
78 #include "BKE_library_query.h"
79 #include "BKE_library_remap.h"
80 #include "BKE_key.h"
81 #include "BKE_main.h"
82 #include "BKE_material.h"
83 #include "BKE_mball.h"
84 #include "BKE_mesh.h"
85 #include "BKE_mesh_runtime.h"
86 #include "BKE_nla.h"
87 #include "BKE_object.h"
88 #include "BKE_particle.h"
89 #include "BKE_report.h"
90 #include "BKE_scene.h"
91 #include "BKE_screen.h"
92 #include "BKE_speaker.h"
93
94 #include "DEG_depsgraph.h"
95 #include "DEG_depsgraph_build.h"
96
97 #include "RNA_access.h"
98 #include "RNA_define.h"
99 #include "RNA_enum_types.h"
100
101 #include "WM_api.h"
102 #include "WM_types.h"
103
104 #include "ED_armature.h"
105 #include "ED_curve.h"
106 #include "ED_mball.h"
107 #include "ED_mesh.h"
108 #include "ED_node.h"
109 #include "ED_object.h"
110 #include "ED_physics.h"
111 #include "ED_render.h"
112 #include "ED_screen.h"
113 #include "ED_transform.h"
114 #include "ED_view3d.h"
115
116 #include "UI_resources.h"
117
118 #include "object_intern.h"
119
120 /* this is an exact copy of the define in rna_lamp.c
121  * kept here because of linking order.
122  * Icons are only defined here */
123 const EnumPropertyItem rna_enum_lamp_type_items[] = {
124         {LA_LOCAL, "POINT", ICON_LAMP_POINT, "Point", "Omnidirectional point light source"},
125         {LA_SUN, "SUN", ICON_LAMP_SUN, "Sun", "Constant direction parallel ray light source"},
126         {LA_SPOT, "SPOT", ICON_LAMP_SPOT, "Spot", "Directional cone light source"},
127         {LA_HEMI, "HEMI", ICON_LAMP_HEMI, "Hemi", "180 degree constant light source"},
128         {LA_AREA, "AREA", ICON_LAMP_AREA, "Area", "Directional area light source"},
129         {0, NULL, 0, NULL, NULL}
130 };
131
132 /* copy from rna_object_force.c */
133 static const EnumPropertyItem field_type_items[] = {
134         {PFIELD_FORCE, "FORCE", ICON_FORCE_FORCE, "Force", ""},
135         {PFIELD_WIND, "WIND", ICON_FORCE_WIND, "Wind", ""},
136         {PFIELD_VORTEX, "VORTEX", ICON_FORCE_VORTEX, "Vortex", ""},
137         {PFIELD_MAGNET, "MAGNET", ICON_FORCE_MAGNETIC, "Magnetic", ""},
138         {PFIELD_HARMONIC, "HARMONIC", ICON_FORCE_HARMONIC, "Harmonic", ""},
139         {PFIELD_CHARGE, "CHARGE", ICON_FORCE_CHARGE, "Charge", ""},
140         {PFIELD_LENNARDJ, "LENNARDJ", ICON_FORCE_LENNARDJONES, "Lennard-Jones", ""},
141         {PFIELD_TEXTURE, "TEXTURE", ICON_FORCE_TEXTURE, "Texture", ""},
142         {PFIELD_GUIDE, "GUIDE", ICON_FORCE_CURVE, "Curve Guide", ""},
143         {PFIELD_BOID, "BOID", ICON_FORCE_BOID, "Boid", ""},
144         {PFIELD_TURBULENCE, "TURBULENCE", ICON_FORCE_TURBULENCE, "Turbulence", ""},
145         {PFIELD_DRAG, "DRAG", ICON_FORCE_DRAG, "Drag", ""},
146         {PFIELD_SMOKEFLOW, "SMOKE", ICON_FORCE_SMOKEFLOW, "Smoke Flow", ""},
147         {0, NULL, 0, NULL, NULL}
148 };
149
150 static EnumPropertyItem lightprobe_type_items[] = {
151         {LIGHTPROBE_TYPE_CUBE, "CUBEMAP", ICON_LIGHTPROBE_CUBEMAP, "Reflection Cubemap",
152      "Reflection probe with spherical or cubic attenuation"},
153         {LIGHTPROBE_TYPE_PLANAR, "PLANAR", ICON_LIGHTPROBE_PLANAR, "Reflection Plane",
154      "Planar reflection probe"},
155         {LIGHTPROBE_TYPE_GRID, "GRID", ICON_LIGHTPROBE_GRID, "Irradiance Volume",
156      "Irradiance probe to capture diffuse indirect lighting"},
157         {0, NULL, 0, NULL, NULL}
158 };
159
160 /************************** Exported *****************************/
161
162 void ED_object_location_from_view(bContext *C, float loc[3])
163 {
164         View3D *v3d = CTX_wm_view3d(C);
165         Scene *scene = CTX_data_scene(C);
166         const float *cursor;
167
168         cursor = ED_view3d_cursor3d_get(scene, v3d)->location;
169
170         copy_v3_v3(loc, cursor);
171 }
172
173 void ED_object_rotation_from_quat(float rot[3], const float viewquat[4], const char align_axis)
174 {
175         BLI_assert(align_axis >= 'X' && align_axis <= 'Z');
176
177         switch (align_axis) {
178                 case 'X':
179                 {
180                         /* Same as 'rv3d->viewinv[1]' */
181                         float axis_y[4] = {0.0f, 1.0f, 0.0f};
182                         float quat_y[4], quat[4];
183                         axis_angle_to_quat(quat_y, axis_y, M_PI_2);
184                         mul_qt_qtqt(quat, viewquat, quat_y);
185                         quat_to_eul(rot, quat);
186                         break;
187                 }
188                 case 'Y':
189                 {
190                         quat_to_eul(rot, viewquat);
191                         rot[0] -= (float)M_PI_2;
192                         break;
193                 }
194                 case 'Z':
195                 {
196                         quat_to_eul(rot, viewquat);
197                         break;
198                 }
199         }
200 }
201
202 void ED_object_rotation_from_view(bContext *C, float rot[3], const char align_axis)
203 {
204         RegionView3D *rv3d = CTX_wm_region_view3d(C);
205         BLI_assert(align_axis >= 'X' && align_axis <= 'Z');
206         if (rv3d) {
207                 float viewquat[4];
208                 copy_qt_qt(viewquat, rv3d->viewquat);
209                 viewquat[0] *= -1.0f;
210                 ED_object_rotation_from_quat(rot, viewquat, align_axis);
211         }
212         else {
213                 zero_v3(rot);
214         }
215 }
216
217 void ED_object_base_init_transform(bContext *C, Base *base, const float loc[3], const float rot[3])
218 {
219         Object *ob = base->object;
220         Scene *scene = CTX_data_scene(C);
221         Depsgraph *depsgraph = CTX_data_depsgraph(C);
222
223         if (!scene) return;
224
225         if (loc)
226                 copy_v3_v3(ob->loc, loc);
227
228         if (rot)
229                 copy_v3_v3(ob->rot, rot);
230
231         BKE_object_where_is_calc(depsgraph, scene, ob);
232 }
233
234 /* Uses context to figure out transform for primitive.
235  * Returns standard diameter. */
236 float ED_object_new_primitive_matrix(
237         bContext *C, Object *obedit,
238         const float loc[3], const float rot[3], float primmat[4][4])
239 {
240         Scene *scene = CTX_data_scene(C);
241         View3D *v3d = CTX_wm_view3d(C);
242         float mat[3][3], rmat[3][3], cmat[3][3], imat[3][3];
243
244         unit_m4(primmat);
245
246         eul_to_mat3(rmat, rot);
247         invert_m3(rmat);
248
249         /* inverse transform for initial rotation and object */
250         copy_m3_m4(mat, obedit->obmat);
251         mul_m3_m3m3(cmat, rmat, mat);
252         invert_m3_m3(imat, cmat);
253         copy_m4_m3(primmat, imat);
254
255         /* center */
256         copy_v3_v3(primmat[3], loc);
257         sub_v3_v3v3(primmat[3], primmat[3], obedit->obmat[3]);
258         invert_m3_m3(imat, mat);
259         mul_m3_v3(imat, primmat[3]);
260
261         {
262                 const float dia = v3d ? ED_view3d_grid_scale(scene, v3d, NULL) : ED_scene_grid_scale(scene, NULL);
263                 return dia;
264         }
265
266         // return 1.0f;
267 }
268
269 /********************* Add Object Operator ********************/
270
271 static void view_align_update(struct Main *UNUSED(main), struct Scene *UNUSED(scene), struct PointerRNA *ptr)
272 {
273         RNA_struct_idprops_unset(ptr, "rotation");
274 }
275
276 void ED_object_add_unit_props(wmOperatorType *ot)
277 {
278         RNA_def_float_distance(ot->srna, "radius", 1.0f, 0.0, OBJECT_ADD_SIZE_MAXF, "Radius", "", 0.001, 100.00);
279 }
280
281 void ED_object_add_generic_props(wmOperatorType *ot, bool do_editmode)
282 {
283         PropertyRNA *prop;
284
285         /* note: this property gets hidden for add-camera operator */
286         prop = RNA_def_boolean(ot->srna, "view_align", 0, "Align to View", "Align the new object to the view");
287         RNA_def_property_update_runtime(prop, view_align_update);
288
289         if (do_editmode) {
290                 prop = RNA_def_boolean(ot->srna, "enter_editmode", 0, "Enter Editmode",
291                                        "Enter editmode when adding this object");
292                 RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
293         }
294
295         prop = RNA_def_float_vector_xyz(ot->srna, "location", 3, NULL, -OBJECT_ADD_SIZE_MAXF, OBJECT_ADD_SIZE_MAXF,
296                                         "Location", "Location for the newly added object", -1000.0f, 1000.0f);
297         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
298         prop = RNA_def_float_rotation(ot->srna, "rotation", 3, NULL, -OBJECT_ADD_SIZE_MAXF, OBJECT_ADD_SIZE_MAXF,
299                                       "Rotation", "Rotation for the newly added object",
300                                       DEG2RADF(-360.0f), DEG2RADF(360.0f));
301         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
302
303         prop = RNA_def_boolean_layer_member(ot->srna, "layers", 20, NULL, "Layer", "");
304         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
305 }
306
307 void ED_object_add_mesh_props(wmOperatorType *ot)
308 {
309         RNA_def_boolean(ot->srna, "calc_uvs", true, "Generate UVs", "Generate a default UV map");
310 }
311
312 bool ED_object_add_generic_get_opts(bContext *C, wmOperator *op, const char view_align_axis,
313                                     float loc[3], float rot[3],
314                                     bool *enter_editmode, unsigned int *layer, bool *is_view_aligned)
315 {
316         View3D *v3d = CTX_wm_view3d(C);
317         unsigned int _layer;
318         PropertyRNA *prop;
319
320         /* Switch to Edit mode? optional prop */
321         if ((prop = RNA_struct_find_property(op->ptr, "enter_editmode"))) {
322                 bool _enter_editmode;
323                 if (!enter_editmode)
324                         enter_editmode = &_enter_editmode;
325
326                 if (RNA_property_is_set(op->ptr, prop) && enter_editmode)
327                         *enter_editmode = RNA_property_boolean_get(op->ptr, prop);
328                 else {
329                         *enter_editmode = (U.flag & USER_ADD_EDITMODE) != 0;
330                         RNA_property_boolean_set(op->ptr, prop, *enter_editmode);
331                 }
332         }
333
334         /* Get layers! */
335         {
336                 int a, layer_values[20];
337                 if (!layer)
338                         layer = &_layer;
339
340                 prop = RNA_struct_find_property(op->ptr, "layers");
341                 if (RNA_property_is_set(op->ptr, prop)) {
342                         RNA_property_boolean_get_array(op->ptr, prop, layer_values);
343                         *layer = 0;
344                         for (a = 0; a < 20; a++) {
345                                 if (layer_values[a])
346                                         *layer |= (1 << a);
347                         }
348                 }
349                 else {
350                         Scene *scene = CTX_data_scene(C);
351                         *layer = BKE_screen_view3d_layer_active_ex(v3d, scene, false);
352                         for (a = 0; a < 20; a++) {
353                                 layer_values[a] = (*layer & (1 << a)) != 0;
354                         }
355                         RNA_property_boolean_set_array(op->ptr, prop, layer_values);
356                 }
357
358                 /* in local view we additionally add local view layers,
359                  * not part of operator properties */
360                 if (v3d && v3d->localvd)
361                         *layer |= v3d->lay;
362         }
363
364         /* Location! */
365         {
366                 float _loc[3];
367                 if (!loc)
368                         loc = _loc;
369
370                 if (RNA_struct_property_is_set(op->ptr, "location")) {
371                         RNA_float_get_array(op->ptr, "location", loc);
372                 }
373                 else {
374                         ED_object_location_from_view(C, loc);
375                         RNA_float_set_array(op->ptr, "location", loc);
376                 }
377         }
378
379         /* Rotation! */
380         {
381                 bool _is_view_aligned;
382                 float _rot[3];
383                 if (!is_view_aligned)
384                         is_view_aligned = &_is_view_aligned;
385                 if (!rot)
386                         rot = _rot;
387
388                 if (RNA_struct_property_is_set(op->ptr, "rotation"))
389                         *is_view_aligned = false;
390                 else if (RNA_struct_property_is_set(op->ptr, "view_align"))
391                         *is_view_aligned = RNA_boolean_get(op->ptr, "view_align");
392                 else {
393                         *is_view_aligned = (U.flag & USER_ADD_VIEWALIGNED) != 0;
394                         RNA_boolean_set(op->ptr, "view_align", *is_view_aligned);
395                 }
396
397                 if (*is_view_aligned) {
398                         ED_object_rotation_from_view(C, rot, view_align_axis);
399                         RNA_float_set_array(op->ptr, "rotation", rot);
400                 }
401                 else
402                         RNA_float_get_array(op->ptr, "rotation", rot);
403         }
404
405         if (layer && *layer == 0) {
406                 BKE_report(op->reports, RPT_ERROR, "Property 'layer' has no values set");
407                 return false;
408         }
409
410         return true;
411 }
412
413 /* For object add primitive operators.
414  * Do not call undo push in this function (users of this function have to). */
415 Object *ED_object_add_type(
416         bContext *C,
417         int type, const char *name,
418         const float loc[3], const float rot[3],
419         bool enter_editmode, unsigned int UNUSED(layer))
420 {
421         Main *bmain = CTX_data_main(C);
422         Scene *scene = CTX_data_scene(C);
423         ViewLayer *view_layer = CTX_data_view_layer(C);
424         Object *ob;
425
426         /* for as long scene has editmode... */
427         if (CTX_data_edit_object(C)) {
428                 ED_object_editmode_exit(C, EM_FREEDATA | EM_WAITCURSOR);
429         }
430
431         /* deselects all, sets scene->basact */
432         ob = BKE_object_add(bmain, scene, view_layer, type, name);
433         /* editor level activate, notifiers */
434         ED_object_base_activate(C, view_layer->basact);
435
436         /* more editor stuff */
437         ED_object_base_init_transform(C, view_layer->basact, loc, rot);
438
439         /* TODO(sergey): This is weird to manually tag objects for update, better to
440          * use DEG_id_tag_update here perhaps.
441          */
442         DEG_id_type_tag(bmain, ID_OB);
443         DEG_relations_tag_update(bmain);
444         if (ob->data != NULL) {
445                 DEG_id_tag_update_ex(bmain, (ID *)ob->data, DEG_TAG_EDITORS_UPDATE);
446         }
447
448         if (enter_editmode)
449                 ED_object_editmode_enter(C, EM_IGNORE_LAYER);
450
451         WM_event_add_notifier(C, NC_SCENE | ND_LAYER_CONTENT, scene);
452
453         /* TODO(sergey): Use proper flag for tagging here. */
454         DEG_id_tag_update(&scene->id, 0);
455
456         return ob;
457 }
458
459 /* for object add operator */
460 static int object_add_exec(bContext *C, wmOperator *op)
461 {
462         Object *ob;
463         bool enter_editmode;
464         unsigned int layer;
465         float loc[3], rot[3], radius;
466
467         WM_operator_view3d_unit_defaults(C, op);
468         if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, &enter_editmode, &layer, NULL))
469                 return OPERATOR_CANCELLED;
470
471         radius = RNA_float_get(op->ptr, "radius");
472         ob = ED_object_add_type(C, RNA_enum_get(op->ptr, "type"), NULL, loc, rot, enter_editmode, layer);
473
474         if (ob->type == OB_LATTICE) {
475                 /* lattice is a special case!
476                  * we never want to scale the obdata since that is the rest-state */
477                 copy_v3_fl(ob->size, radius);
478         }
479         else {
480                 BKE_object_obdata_size_init(ob, radius);
481         }
482
483         return OPERATOR_FINISHED;
484 }
485
486 void OBJECT_OT_add(wmOperatorType *ot)
487 {
488         /* identifiers */
489         ot->name = "Add Object";
490         ot->description = "Add an object to the scene";
491         ot->idname = "OBJECT_OT_add";
492
493         /* api callbacks */
494         ot->exec = object_add_exec;
495         ot->poll = ED_operator_objectmode;
496
497         /* flags */
498         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
499
500         /* properties */
501         ED_object_add_unit_props(ot);
502         RNA_def_enum(ot->srna, "type", rna_enum_object_type_items, 0, "Type", "");
503
504         ED_object_add_generic_props(ot, true);
505 }
506
507 /********************** Add Probe Operator **********************/
508
509 /* for object add operator */
510 static int lightprobe_add_exec(bContext *C, wmOperator *op)
511 {
512         Object *ob;
513         LightProbe *probe;
514         int type;
515         bool enter_editmode;
516         unsigned int layer;
517         float loc[3], rot[3];
518         float radius;
519
520         WM_operator_view3d_unit_defaults(C, op);
521         if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, &enter_editmode, &layer, NULL))
522                 return OPERATOR_CANCELLED;
523
524         type = RNA_enum_get(op->ptr, "type");
525         radius = RNA_float_get(op->ptr, "radius");
526
527         const char *name = CTX_DATA_(BLT_I18NCONTEXT_ID_OBJECT, "Light Probe");
528         ob = ED_object_add_type(C, OB_LIGHTPROBE, name, loc, rot, false, layer);
529         BKE_object_obdata_size_init(ob, radius);
530
531         probe = (LightProbe *)ob->data;
532         probe->type = type;
533
534         switch (type) {
535                 case LIGHTPROBE_TYPE_GRID:
536                         probe->distinf = 0.3f;
537                         probe->falloff = 1.0f;
538                         probe->clipsta = 0.01f;
539                         break;
540                 case LIGHTPROBE_TYPE_PLANAR:
541                         probe->distinf = 0.1f;
542                         probe->falloff = 0.5f;
543                         probe->clipsta = 0.001f;
544                         ob->empty_drawsize = 0.5f;
545                         break;
546                 case LIGHTPROBE_TYPE_CUBE:
547                         probe->attenuation_type = LIGHTPROBE_SHAPE_ELIPSOID;
548                         break;
549                 default:
550                         BLI_assert(!"Lightprobe type not configured.");
551                         break;
552         }
553
554         DEG_relations_tag_update(CTX_data_main(C));
555
556         return OPERATOR_FINISHED;
557 }
558
559 void OBJECT_OT_lightprobe_add(wmOperatorType *ot)
560 {
561         /* identifiers */
562         ot->name = "Add Light Probe";
563         ot->description = "Add a light probe object";
564         ot->idname = "OBJECT_OT_lightprobe_add";
565
566         /* api callbacks */
567         ot->exec = lightprobe_add_exec;
568         ot->poll = ED_operator_objectmode;
569
570         /* flags */
571         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
572
573         /* properties */
574         ot->prop = RNA_def_enum(ot->srna, "type", lightprobe_type_items, 0, "Type", "");
575
576         ED_object_add_unit_props(ot);
577         ED_object_add_generic_props(ot, true);
578 }
579
580 /********************* Add Effector Operator ********************/
581
582 /* for object add operator */
583 static int effector_add_exec(bContext *C, wmOperator *op)
584 {
585         Object *ob;
586         int type;
587         bool enter_editmode;
588         unsigned int layer;
589         float loc[3], rot[3];
590         float mat[4][4];
591         float dia;
592
593         WM_operator_view3d_unit_defaults(C, op);
594         if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, &enter_editmode, &layer, NULL))
595                 return OPERATOR_CANCELLED;
596
597         type = RNA_enum_get(op->ptr, "type");
598         dia = RNA_float_get(op->ptr, "radius");
599
600         if (type == PFIELD_GUIDE) {
601                 Curve *cu;
602                 const char *name = CTX_DATA_(BLT_I18NCONTEXT_ID_OBJECT, "CurveGuide");
603                 ob = ED_object_add_type(C, OB_CURVE, name, loc, rot, false, layer);
604
605                 cu = ob->data;
606                 cu->flag |= CU_PATH | CU_3D;
607                 ED_object_editmode_enter(C, 0);
608                 ED_object_new_primitive_matrix(C, ob, loc, rot, mat);
609                 BLI_addtail(&cu->editnurb->nurbs, ED_curve_add_nurbs_primitive(C, ob, mat, CU_NURBS | CU_PRIM_PATH, dia));
610                 if (!enter_editmode)
611                         ED_object_editmode_exit(C, EM_FREEDATA);
612         }
613         else {
614                 const char *name = CTX_DATA_(BLT_I18NCONTEXT_ID_OBJECT, "Field");
615                 ob = ED_object_add_type(C, OB_EMPTY, name, loc, rot, false, layer);
616                 BKE_object_obdata_size_init(ob, dia);
617                 if (ELEM(type, PFIELD_WIND, PFIELD_VORTEX))
618                         ob->empty_drawtype = OB_SINGLE_ARROW;
619         }
620
621         ob->pd = object_add_collision_fields(type);
622
623         DEG_relations_tag_update(CTX_data_main(C));
624
625         return OPERATOR_FINISHED;
626 }
627
628 void OBJECT_OT_effector_add(wmOperatorType *ot)
629 {
630         /* identifiers */
631         ot->name = "Add Effector";
632         ot->description = "Add an empty object with a physics effector to the scene";
633         ot->idname = "OBJECT_OT_effector_add";
634
635         /* api callbacks */
636         ot->exec = effector_add_exec;
637         ot->poll = ED_operator_objectmode;
638
639         /* flags */
640         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
641
642         /* properties */
643         ot->prop = RNA_def_enum(ot->srna, "type", field_type_items, 0, "Type", "");
644
645         ED_object_add_unit_props(ot);
646         ED_object_add_generic_props(ot, true);
647 }
648
649 /********************* Add Camera Operator ********************/
650
651 static int object_camera_add_exec(bContext *C, wmOperator *op)
652 {
653         View3D *v3d = CTX_wm_view3d(C);
654         Scene *scene = CTX_data_scene(C);
655         Object *ob;
656         Camera *cam;
657         bool enter_editmode;
658         unsigned int layer;
659         float loc[3], rot[3];
660
661         /* force view align for cameras */
662         RNA_boolean_set(op->ptr, "view_align", true);
663
664         if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, &enter_editmode, &layer, NULL))
665                 return OPERATOR_CANCELLED;
666
667         ob = ED_object_add_type(C, OB_CAMERA, NULL, loc, rot, false, layer);
668
669         if (v3d) {
670                 if (v3d->camera == NULL)
671                         v3d->camera = ob;
672                 if (v3d->scenelock && scene->camera == NULL) {
673                         scene->camera = ob;
674                 }
675         }
676
677         cam = ob->data;
678         cam->drawsize = v3d ? ED_view3d_grid_scale(scene, v3d, NULL) : ED_scene_grid_scale(scene, NULL);
679
680         return OPERATOR_FINISHED;
681 }
682
683 void OBJECT_OT_camera_add(wmOperatorType *ot)
684 {
685         PropertyRNA *prop;
686
687         /* identifiers */
688         ot->name = "Add Camera";
689         ot->description = "Add a camera object to the scene";
690         ot->idname = "OBJECT_OT_camera_add";
691
692         /* api callbacks */
693         ot->exec = object_camera_add_exec;
694         ot->poll = ED_operator_objectmode;
695
696         /* flags */
697         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
698
699         ED_object_add_generic_props(ot, true);
700
701         /* hide this for cameras, default */
702         prop = RNA_struct_type_find_property(ot->srna, "view_align");
703         RNA_def_property_flag(prop, PROP_HIDDEN);
704 }
705
706
707 /********************* Add Metaball Operator ********************/
708
709 static int object_metaball_add_exec(bContext *C, wmOperator *op)
710 {
711         Object *obedit = CTX_data_edit_object(C);
712         bool newob = false;
713         bool enter_editmode;
714         unsigned int layer;
715         float loc[3], rot[3];
716         float mat[4][4];
717         float dia;
718
719         WM_operator_view3d_unit_defaults(C, op);
720         if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, &enter_editmode, &layer, NULL))
721                 return OPERATOR_CANCELLED;
722
723         if (obedit == NULL || obedit->type != OB_MBALL) {
724                 obedit = ED_object_add_type(C, OB_MBALL, NULL, loc, rot, true, layer);
725                 newob = true;
726         }
727         else {
728                 DEG_id_tag_update(&obedit->id, OB_RECALC_DATA);
729         }
730
731         ED_object_new_primitive_matrix(C, obedit, loc, rot, mat);
732         dia = RNA_float_get(op->ptr, "radius");
733
734         ED_mball_add_primitive(C, obedit, mat, dia, RNA_enum_get(op->ptr, "type"));
735
736         /* userdef */
737         if (newob && !enter_editmode) {
738                 ED_object_editmode_exit(C, EM_FREEDATA);
739         }
740
741         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, obedit);
742
743         return OPERATOR_FINISHED;
744 }
745
746 void OBJECT_OT_metaball_add(wmOperatorType *ot)
747 {
748         /* identifiers */
749         ot->name = "Add Metaball";
750         ot->description = "Add an metaball object to the scene";
751         ot->idname = "OBJECT_OT_metaball_add";
752
753         /* api callbacks */
754         ot->invoke = WM_menu_invoke;
755         ot->exec = object_metaball_add_exec;
756         ot->poll = ED_operator_scene_editable;
757
758         /* flags */
759         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
760
761         ot->prop = RNA_def_enum(ot->srna, "type", rna_enum_metaelem_type_items, 0, "Primitive", "");
762
763         ED_object_add_unit_props(ot);
764         ED_object_add_generic_props(ot, true);
765 }
766
767 /********************* Add Text Operator ********************/
768
769 static int object_add_text_exec(bContext *C, wmOperator *op)
770 {
771         Object *obedit = CTX_data_edit_object(C);
772         bool enter_editmode;
773         unsigned int layer;
774         float loc[3], rot[3];
775
776         WM_operator_view3d_unit_defaults(C, op);
777         if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, &enter_editmode, &layer, NULL))
778                 return OPERATOR_CANCELLED;
779
780         if (obedit && obedit->type == OB_FONT)
781                 return OPERATOR_CANCELLED;
782
783         obedit = ED_object_add_type(C, OB_FONT, NULL, loc, rot, enter_editmode, layer);
784         BKE_object_obdata_size_init(obedit, RNA_float_get(op->ptr, "radius"));
785
786         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, obedit);
787
788         return OPERATOR_FINISHED;
789 }
790
791 void OBJECT_OT_text_add(wmOperatorType *ot)
792 {
793         /* identifiers */
794         ot->name = "Add Text";
795         ot->description = "Add a text object to the scene";
796         ot->idname = "OBJECT_OT_text_add";
797
798         /* api callbacks */
799         ot->exec = object_add_text_exec;
800         ot->poll = ED_operator_objectmode;
801
802         /* flags */
803         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
804
805         /* properties */
806         ED_object_add_unit_props(ot);
807         ED_object_add_generic_props(ot, true);
808 }
809
810 /********************* Add Armature Operator ********************/
811
812 static int object_armature_add_exec(bContext *C, wmOperator *op)
813 {
814         Object *obedit = CTX_data_edit_object(C);
815         RegionView3D *rv3d = CTX_wm_region_view3d(C);
816         bool newob = false;
817         bool enter_editmode;
818         unsigned int layer;
819         float loc[3], rot[3], dia;
820         bool view_aligned = rv3d && (U.flag & USER_ADD_VIEWALIGNED);
821
822         WM_operator_view3d_unit_defaults(C, op);
823         if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, &enter_editmode, &layer, NULL))
824                 return OPERATOR_CANCELLED;
825
826         if ((obedit == NULL) || (obedit->type != OB_ARMATURE)) {
827                 obedit = ED_object_add_type(C, OB_ARMATURE, NULL, loc, rot, true, layer);
828                 ED_object_editmode_enter(C, 0);
829                 newob = true;
830         }
831         else {
832                 DEG_id_tag_update(&obedit->id, OB_RECALC_DATA);
833         }
834
835         if (obedit == NULL) {
836                 BKE_report(op->reports, RPT_ERROR, "Cannot create editmode armature");
837                 return OPERATOR_CANCELLED;
838         }
839
840         dia = RNA_float_get(op->ptr, "radius");
841         ED_armature_ebone_add_primitive(obedit, dia, view_aligned);
842
843         /* userdef */
844         if (newob && !enter_editmode)
845                 ED_object_editmode_exit(C, EM_FREEDATA);
846
847         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, obedit);
848
849         return OPERATOR_FINISHED;
850 }
851
852 void OBJECT_OT_armature_add(wmOperatorType *ot)
853 {
854         /* identifiers */
855         ot->name = "Add Armature";
856         ot->description = "Add an armature object to the scene";
857         ot->idname = "OBJECT_OT_armature_add";
858
859         /* api callbacks */
860         ot->exec = object_armature_add_exec;
861         ot->poll = ED_operator_objectmode;
862
863         /* flags */
864         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
865
866         /* properties */
867         ED_object_add_unit_props(ot);
868         ED_object_add_generic_props(ot, true);
869 }
870
871 /********************* Add Empty Operator ********************/
872
873 static int object_empty_add_exec(bContext *C, wmOperator *op)
874 {
875         Object *ob;
876         int type = RNA_enum_get(op->ptr, "type");
877         unsigned int layer;
878         float loc[3], rot[3];
879
880         WM_operator_view3d_unit_defaults(C, op);
881         if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, NULL, &layer, NULL))
882                 return OPERATOR_CANCELLED;
883
884         ob = ED_object_add_type(C, OB_EMPTY, NULL, loc, rot, false, layer);
885
886         BKE_object_empty_draw_type_set(ob, type);
887         BKE_object_obdata_size_init(ob, RNA_float_get(op->ptr, "radius"));
888
889         return OPERATOR_FINISHED;
890 }
891
892 void OBJECT_OT_empty_add(wmOperatorType *ot)
893 {
894         /* identifiers */
895         ot->name = "Add Empty";
896         ot->description = "Add an empty object to the scene";
897         ot->idname = "OBJECT_OT_empty_add";
898
899         /* api callbacks */
900         ot->invoke = WM_menu_invoke;
901         ot->exec = object_empty_add_exec;
902         ot->poll = ED_operator_objectmode;
903
904         /* flags */
905         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
906
907         /* properties */
908         ot->prop = RNA_def_enum(ot->srna, "type", rna_enum_object_empty_drawtype_items, 0, "Type", "");
909
910         ED_object_add_unit_props(ot);
911         ED_object_add_generic_props(ot, false);
912 }
913
914 static int empty_drop_named_image_invoke(bContext *C, wmOperator *op, const wmEvent *event)
915 {
916         Scene *scene = CTX_data_scene(C);
917
918         Base *base = NULL;
919         Image *ima = NULL;
920         Object *ob = NULL;
921
922         ima = (Image *)WM_operator_drop_load_path(C, op, ID_IM);
923         if (!ima) {
924                 return OPERATOR_CANCELLED;
925         }
926         /* handled below */
927         id_us_min((ID *)ima);
928
929         base = ED_view3d_give_base_under_cursor(C, event->mval);
930
931         /* if empty under cursor, then set object */
932         if (base && base->object->type == OB_EMPTY) {
933                 ob = base->object;
934                 WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
935         }
936         else {
937                 /* add new empty */
938                 unsigned int layer;
939                 float rot[3];
940
941                 if (!ED_object_add_generic_get_opts(C, op, 'Z', NULL, rot, NULL, &layer, NULL))
942                         return OPERATOR_CANCELLED;
943
944                 ob = ED_object_add_type(C, OB_EMPTY, NULL, NULL, rot, false, layer);
945
946                 /* add under the mouse */
947                 ED_object_location_from_view(C, ob->loc);
948                 ED_view3d_cursor3d_position(C, ob->loc, event->mval);
949         }
950
951         BKE_object_empty_draw_type_set(ob, OB_EMPTY_IMAGE);
952
953         id_us_min(ob->data);
954         ob->data = ima;
955         id_us_plus(ob->data);
956
957         return OPERATOR_FINISHED;
958 }
959
960 void OBJECT_OT_drop_named_image(wmOperatorType *ot)
961 {
962         PropertyRNA *prop;
963
964         /* identifiers */
965         ot->name = "Add Empty Image/Drop Image To Empty";
966         ot->description = "Add an empty image type to scene with data";
967         ot->idname = "OBJECT_OT_drop_named_image";
968
969         /* api callbacks */
970         ot->invoke = empty_drop_named_image_invoke;
971         ot->poll = ED_operator_objectmode;
972
973         /* flags */
974         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
975
976         /* properties */
977         prop = RNA_def_string(ot->srna, "filepath", NULL, FILE_MAX, "Filepath", "Path to image file");
978         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
979         RNA_def_boolean(ot->srna, "relative_path", true, "Relative Path", "Select the file relative to the blend file");
980         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
981         prop = RNA_def_string(ot->srna, "name", NULL, MAX_ID_NAME - 2, "Name", "Image name to assign");
982         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
983         ED_object_add_generic_props(ot, false);
984 }
985
986 /********************* Add Lamp Operator ********************/
987
988 static const char *get_lamp_defname(int type)
989 {
990         switch (type) {
991                 case LA_LOCAL: return CTX_DATA_(BLT_I18NCONTEXT_ID_LAMP, "Point");
992                 case LA_SUN: return CTX_DATA_(BLT_I18NCONTEXT_ID_LAMP, "Sun");
993                 case LA_SPOT: return CTX_DATA_(BLT_I18NCONTEXT_ID_LAMP, "Spot");
994                 case LA_HEMI: return CTX_DATA_(BLT_I18NCONTEXT_ID_LAMP, "Hemi");
995                 case LA_AREA: return CTX_DATA_(BLT_I18NCONTEXT_ID_LAMP, "Area");
996                 default:
997                         return CTX_DATA_(BLT_I18NCONTEXT_ID_LAMP, "Lamp");
998         }
999 }
1000
1001 static int object_lamp_add_exec(bContext *C, wmOperator *op)
1002 {
1003         Scene *scene = CTX_data_scene(C);
1004         Object *ob;
1005         Lamp *la;
1006         int type = RNA_enum_get(op->ptr, "type");
1007         unsigned int layer;
1008         float loc[3], rot[3];
1009
1010         WM_operator_view3d_unit_defaults(C, op);
1011         if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, NULL, &layer, NULL))
1012                 return OPERATOR_CANCELLED;
1013
1014         ob = ED_object_add_type(C, OB_LAMP, get_lamp_defname(type), loc, rot, false, layer);
1015         BKE_object_obdata_size_init(ob, RNA_float_get(op->ptr, "radius"));
1016
1017         la = (Lamp *)ob->data;
1018         la->type = type;
1019
1020         if (BKE_scene_uses_cycles(scene)) {
1021                 ED_node_shader_default(C, &la->id);
1022                 la->use_nodes = true;
1023         }
1024
1025         return OPERATOR_FINISHED;
1026 }
1027
1028 void OBJECT_OT_lamp_add(wmOperatorType *ot)
1029 {
1030         /* identifiers */
1031         ot->name = "Add Lamp";
1032         ot->description = "Add a lamp object to the scene";
1033         ot->idname = "OBJECT_OT_lamp_add";
1034
1035         /* api callbacks */
1036         ot->invoke = WM_menu_invoke;
1037         ot->exec = object_lamp_add_exec;
1038         ot->poll = ED_operator_objectmode;
1039
1040         /* flags */
1041         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1042
1043         /* properties */
1044         ot->prop = RNA_def_enum(ot->srna, "type", rna_enum_lamp_type_items, 0, "Type", "");
1045         RNA_def_property_translation_context(ot->prop, BLT_I18NCONTEXT_ID_LAMP);
1046
1047         ED_object_add_unit_props(ot);
1048         ED_object_add_generic_props(ot, false);
1049 }
1050
1051 /********************* Add Collection Instance Operator ********************/
1052
1053 static int collection_instance_add_exec(bContext *C, wmOperator *op)
1054 {
1055         Main *bmain = CTX_data_main(C);
1056         Collection *collection;
1057         unsigned int layer;
1058         float loc[3], rot[3];
1059
1060         if (RNA_struct_property_is_set(op->ptr, "name")) {
1061                 char name[MAX_ID_NAME - 2];
1062
1063                 RNA_string_get(op->ptr, "name", name);
1064                 collection = (Collection *)BKE_libblock_find_name(bmain, ID_GR, name);
1065
1066                 if (0 == RNA_struct_property_is_set(op->ptr, "location")) {
1067                         const wmEvent *event = CTX_wm_window(C)->eventstate;
1068                         ARegion *ar = CTX_wm_region(C);
1069                         const int mval[2] = {event->x - ar->winrct.xmin,
1070                                              event->y - ar->winrct.ymin};
1071                         ED_object_location_from_view(C, loc);
1072                         ED_view3d_cursor3d_position(C, loc, mval);
1073                         RNA_float_set_array(op->ptr, "location", loc);
1074                 }
1075         }
1076         else
1077                 collection = BLI_findlink(&CTX_data_main(C)->collection, RNA_enum_get(op->ptr, "collection"));
1078
1079         if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, NULL, &layer, NULL))
1080                 return OPERATOR_CANCELLED;
1081
1082         if (collection) {
1083                 Scene *scene = CTX_data_scene(C);
1084                 ViewLayer *view_layer = CTX_data_view_layer(C);
1085
1086                 /* Avoid dependency cycles. */
1087                 LayerCollection *active_lc = BKE_layer_collection_get_active(view_layer);
1088                 while (BKE_collection_find_cycle(active_lc->collection, collection)) {
1089                         active_lc = BKE_layer_collection_activate_parent(view_layer, active_lc);
1090                 }
1091
1092                 Object *ob = ED_object_add_type(C, OB_EMPTY, collection->id.name + 2, loc, rot, false, layer);
1093                 ob->dup_group = collection;
1094                 ob->transflag |= OB_DUPLICOLLECTION;
1095                 id_us_plus(&collection->id);
1096
1097                 /* works without this except if you try render right after, see: 22027 */
1098                 DEG_relations_tag_update(bmain);
1099                 DEG_id_tag_update(&collection->id, 0);
1100
1101                 WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
1102
1103                 return OPERATOR_FINISHED;
1104         }
1105
1106         return OPERATOR_CANCELLED;
1107 }
1108
1109 /* only used as menu */
1110 void OBJECT_OT_collection_instance_add(wmOperatorType *ot)
1111 {
1112         PropertyRNA *prop;
1113
1114         /* identifiers */
1115         ot->name = "Add Collection Instance";
1116         ot->description = "Add a collection instance";
1117         ot->idname = "OBJECT_OT_collection_instance_add";
1118
1119         /* api callbacks */
1120         ot->invoke = WM_enum_search_invoke;
1121         ot->exec = collection_instance_add_exec;
1122         ot->poll = ED_operator_objectmode;
1123
1124         /* flags */
1125         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1126
1127         /* properties */
1128         RNA_def_string(ot->srna, "name", "Collection", MAX_ID_NAME - 2, "Name", "Collection name to add");
1129         prop = RNA_def_enum(ot->srna, "collection", DummyRNA_NULL_items, 0, "Collection", "");
1130         RNA_def_enum_funcs(prop, RNA_collection_itemf);
1131         RNA_def_property_flag(prop, PROP_ENUM_NO_TRANSLATE);
1132         ot->prop = prop;
1133         ED_object_add_generic_props(ot, false);
1134 }
1135
1136 /********************* Add Speaker Operator ********************/
1137
1138 static int object_speaker_add_exec(bContext *C, wmOperator *op)
1139 {
1140         Object *ob;
1141         unsigned int layer;
1142         float loc[3], rot[3];
1143         Scene *scene = CTX_data_scene(C);
1144
1145         if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, NULL, &layer, NULL))
1146                 return OPERATOR_CANCELLED;
1147
1148         ob = ED_object_add_type(C, OB_SPEAKER, NULL, loc, rot, false, layer);
1149
1150         /* to make it easier to start using this immediately in NLA, a default sound clip is created
1151          * ready to be moved around to retime the sound and/or make new sound clips
1152          */
1153         {
1154                 /* create new data for NLA hierarchy */
1155                 AnimData *adt = BKE_animdata_add_id(&ob->id);
1156                 NlaTrack *nlt = BKE_nlatrack_add(adt, NULL);
1157                 NlaStrip *strip = BKE_nla_add_soundstrip(scene, ob->data);
1158                 strip->start = CFRA;
1159                 strip->end += strip->start;
1160
1161                 /* hook them up */
1162                 BKE_nlatrack_add_strip(nlt, strip);
1163
1164                 /* auto-name the strip, and give the track an interesting name  */
1165                 BLI_strncpy(nlt->name, DATA_("SoundTrack"), sizeof(nlt->name));
1166                 BKE_nlastrip_validate_name(adt, strip);
1167
1168                 WM_event_add_notifier(C, NC_ANIMATION | ND_NLA | NA_EDITED, NULL);
1169         }
1170
1171         return OPERATOR_FINISHED;
1172 }
1173
1174 void OBJECT_OT_speaker_add(wmOperatorType *ot)
1175 {
1176         /* identifiers */
1177         ot->name = "Add Speaker";
1178         ot->description = "Add a speaker object to the scene";
1179         ot->idname = "OBJECT_OT_speaker_add";
1180
1181         /* api callbacks */
1182         ot->exec = object_speaker_add_exec;
1183         ot->poll = ED_operator_objectmode;
1184
1185         /* flags */
1186         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1187
1188         ED_object_add_generic_props(ot, true);
1189 }
1190
1191 /**************************** Delete Object *************************/
1192
1193 /* remove base from a specific scene */
1194 /* note: now unlinks constraints as well */
1195 void ED_object_base_free_and_unlink(Main *bmain, Scene *scene, Object *ob)
1196 {
1197         if (BKE_library_ID_is_indirectly_used(bmain, ob) &&
1198             ID_REAL_USERS(ob) <= 1 && ID_EXTRA_USERS(ob) == 0)
1199         {
1200                 /* We cannot delete indirectly used object... */
1201                 printf("WARNING, undeletable object '%s', should have been catched before reaching this function!",
1202                        ob->id.name + 2);
1203                 return;
1204         }
1205
1206         DEG_id_tag_update_ex(bmain, &ob->id, DEG_TAG_BASE_FLAGS_UPDATE);
1207
1208         BKE_scene_collections_object_remove(bmain, scene, ob, true);
1209 }
1210
1211 static int object_delete_exec(bContext *C, wmOperator *op)
1212 {
1213         Main *bmain = CTX_data_main(C);
1214         Scene *scene = CTX_data_scene(C);
1215         wmWindowManager *wm = CTX_wm_manager(C);
1216         wmWindow *win;
1217         const bool use_global = RNA_boolean_get(op->ptr, "use_global");
1218         bool changed = false;
1219
1220         if (CTX_data_edit_object(C))
1221                 return OPERATOR_CANCELLED;
1222
1223         CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
1224         {
1225                 const bool is_indirectly_used = BKE_library_ID_is_indirectly_used(bmain, ob);
1226                 if (ob->id.tag & LIB_TAG_INDIRECT) {
1227                         /* Can this case ever happen? */
1228                         BKE_reportf(op->reports, RPT_WARNING, "Cannot delete indirectly linked object '%s'", ob->id.name + 2);
1229                         continue;
1230                 }
1231                 else if (is_indirectly_used && ID_REAL_USERS(ob) <= 1 && ID_EXTRA_USERS(ob) == 0) {
1232                         BKE_reportf(op->reports, RPT_WARNING,
1233                                 "Cannot delete object '%s' from scene '%s', indirectly used objects need at least one user",
1234                                 ob->id.name + 2, scene->id.name + 2);
1235                         continue;
1236                 }
1237
1238                 /* This is sort of a quick hack to address T51243 - Proper thing to do here would be to nuke most of all this
1239                  * custom scene/object/base handling, and use generic lib remap/query for that.
1240                  * But this is for later (aka 2.8, once layers & co are settled and working).
1241                  */
1242                 if (use_global && ob->id.lib == NULL) {
1243                         /* We want to nuke the object, let's nuke it the easy way (not for linked data though)... */
1244                         BKE_libblock_delete(bmain, &ob->id);
1245                         changed = true;
1246                         continue;
1247                 }
1248
1249                 /* remove from Grease Pencil parent */
1250                 /* XXX This is likely not correct? Will also remove parent from grease pencil from other scenes,
1251                  *     even when use_global is false... */
1252                 for (bGPdata *gpd = bmain->gpencil.first; gpd; gpd = gpd->id.next) {
1253                         for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
1254                                 if (gpl->parent != NULL) {
1255                                         if (gpl->parent == ob) {
1256                                                 gpl->parent = NULL;
1257                                         }
1258                                 }
1259                         }
1260                 }
1261
1262                 /* remove from current scene only */
1263                 ED_object_base_free_and_unlink(bmain, scene, ob);
1264                 changed = true;
1265
1266                 if (use_global) {
1267                         Scene *scene_iter;
1268                         for (scene_iter = bmain->scene.first; scene_iter; scene_iter = scene_iter->id.next) {
1269                                 if (scene_iter != scene && !ID_IS_LINKED(scene_iter)) {
1270                                         if (is_indirectly_used && ID_REAL_USERS(ob) <= 1 && ID_EXTRA_USERS(ob) == 0) {
1271                                                 BKE_reportf(op->reports, RPT_WARNING,
1272                                                             "Cannot delete object '%s' from scene '%s', indirectly used objects need at least one user",
1273                                                             ob->id.name + 2, scene_iter->id.name + 2);
1274                                                 break;
1275                                         }
1276                                         ED_object_base_free_and_unlink(bmain, scene_iter, ob);
1277                                 }
1278                         }
1279                 }
1280                 /* end global */
1281         }
1282         CTX_DATA_END;
1283
1284         if (!changed)
1285                 return OPERATOR_CANCELLED;
1286
1287         /* delete has to handle all open scenes */
1288         BKE_main_id_tag_listbase(&bmain->scene, LIB_TAG_DOIT, true);
1289         for (win = wm->windows.first; win; win = win->next) {
1290                 scene = WM_window_get_active_scene(win);
1291
1292                 if (scene->id.tag & LIB_TAG_DOIT) {
1293                         scene->id.tag &= ~LIB_TAG_DOIT;
1294
1295                         DEG_relations_tag_update(bmain);
1296
1297                         WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
1298                         WM_event_add_notifier(C, NC_SCENE | ND_LAYER_CONTENT, scene);
1299                 }
1300         }
1301
1302         return OPERATOR_FINISHED;
1303 }
1304
1305 void OBJECT_OT_delete(wmOperatorType *ot)
1306 {
1307         /* identifiers */
1308         ot->name = "Delete";
1309         ot->description = "Delete selected objects";
1310         ot->idname = "OBJECT_OT_delete";
1311
1312         /* api callbacks */
1313         ot->invoke = WM_operator_confirm;
1314         ot->exec = object_delete_exec;
1315         ot->poll = ED_operator_objectmode;
1316
1317         /* flags */
1318         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1319
1320         RNA_def_boolean(ot->srna, "use_global", 0, "Delete Globally", "Remove object from all scenes");
1321 }
1322
1323 /**************************** Copy Utilities ******************************/
1324
1325 /* after copying objects, copied data should get new pointers */
1326 static void copy_object_set_idnew(bContext *C)
1327 {
1328         Main *bmain = CTX_data_main(C);
1329
1330         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1331         {
1332                 BKE_libblock_relink_to_newid(&ob->id);
1333         }
1334         CTX_DATA_END;
1335
1336         BKE_main_id_clear_newpoins(bmain);
1337 }
1338
1339 /********************* Make Duplicates Real ************************/
1340
1341 /**
1342  * \note regarding hashing dupli-objects when using OB_DUPLICOLLECTION, skip the first member of #DupliObject.persistent_id
1343  * since its a unique index and we only want to know if the group objects are from the same dupli-group instance.
1344  */
1345 static unsigned int dupliobject_group_hash(const void *ptr)
1346 {
1347         const DupliObject *dob = ptr;
1348         unsigned int hash = BLI_ghashutil_ptrhash(dob->ob);
1349         unsigned int i;
1350         for (i = 1; (i < MAX_DUPLI_RECUR) && dob->persistent_id[i] != INT_MAX; i++) {
1351                 hash ^= (dob->persistent_id[i] ^ i);
1352         }
1353         return hash;
1354 }
1355
1356 /**
1357  * \note regarding hashing dupli-objects when NOT using OB_DUPLICOLLECTION, include the first member of #DupliObject.persistent_id
1358  * since its the index of the vertex/face the object is instantiated on and we want to identify objects on the same vertex/face.
1359  */
1360 static unsigned int dupliobject_hash(const void *ptr)
1361 {
1362         const DupliObject *dob = ptr;
1363         unsigned int hash = BLI_ghashutil_ptrhash(dob->ob);
1364         hash ^= (dob->persistent_id[0] ^ 0);
1365         return hash;
1366 }
1367
1368 /* Compare function that matches dupliobject_group_hash */
1369 static bool dupliobject_group_cmp(const void *a_, const void *b_)
1370 {
1371         const DupliObject *a = a_;
1372         const DupliObject *b = b_;
1373         unsigned int i;
1374
1375         if (a->ob != b->ob) {
1376                 return true;
1377         }
1378
1379         for (i = 1; (i < MAX_DUPLI_RECUR); i++) {
1380                 if (a->persistent_id[i] != b->persistent_id[i]) {
1381                         return true;
1382                 }
1383                 else if (a->persistent_id[i] == INT_MAX) {
1384                         break;
1385                 }
1386         }
1387
1388         /* matching */
1389         return false;
1390 }
1391
1392 /* Compare function that matches dupliobject_hash */
1393 static bool dupliobject_cmp(const void *a_, const void *b_)
1394 {
1395         const DupliObject *a = a_;
1396         const DupliObject *b = b_;
1397
1398         if (a->ob != b->ob) {
1399                 return true;
1400         }
1401
1402         if (a->persistent_id[0] != b->persistent_id[0]) {
1403                 return true;
1404         }
1405
1406         /* matching */
1407         return false;
1408 }
1409
1410 static void make_object_duplilist_real(bContext *C, Scene *scene, Base *base,
1411                                        const bool use_base_parent,
1412                                        const bool use_hierarchy)
1413 {
1414         Main *bmain = CTX_data_main(C);
1415         ViewLayer *view_layer = CTX_data_view_layer(C);
1416         Depsgraph *depsgraph = CTX_data_depsgraph(C);
1417         ListBase *lb_duplis;
1418         DupliObject *dob;
1419         GHash *dupli_gh, *parent_gh = NULL;
1420
1421         if (!(base->object->transflag & OB_DUPLI)) {
1422                 return;
1423         }
1424
1425         lb_duplis = object_duplilist(depsgraph, scene, base->object);
1426
1427         dupli_gh = BLI_ghash_ptr_new(__func__);
1428         if (use_hierarchy) {
1429                 if (base->object->transflag & OB_DUPLICOLLECTION) {
1430                         parent_gh = BLI_ghash_new(dupliobject_group_hash, dupliobject_group_cmp, __func__);
1431                 }
1432                 else {
1433                         parent_gh = BLI_ghash_new(dupliobject_hash, dupliobject_cmp, __func__);
1434                 }
1435         }
1436
1437         for (dob = lb_duplis->first; dob; dob = dob->next) {
1438                 Object *ob_src = dob->ob;
1439                 Object *ob_dst = ID_NEW_SET(dob->ob, BKE_object_copy(bmain, ob_src));
1440                 Base *base_dst;
1441
1442                 /* font duplis can have a totcol without material, we get them from parent
1443                  * should be implemented better...
1444                  */
1445                 if (ob_dst->mat == NULL) {
1446                         ob_dst->totcol = 0;
1447                 }
1448
1449                 BKE_collection_object_add_from(bmain, scene, base->object, ob_dst);
1450                 base_dst = BKE_view_layer_base_find(view_layer, ob_dst);
1451                 BLI_assert(base_dst != NULL);
1452
1453                 BKE_scene_object_base_flag_sync_from_base(base_dst);
1454
1455                 /* make sure apply works */
1456                 BKE_animdata_free(&ob_dst->id, true);
1457                 ob_dst->adt = NULL;
1458
1459                 /* Proxies are not to be copied. */
1460                 ob_dst->proxy_from = NULL;
1461                 ob_dst->proxy_group = NULL;
1462                 ob_dst->proxy = NULL;
1463
1464                 ob_dst->parent = NULL;
1465                 BKE_constraints_free(&ob_dst->constraints);
1466                 ob_dst->curve_cache = NULL;
1467                 ob_dst->transflag &= ~OB_DUPLI;
1468
1469                 copy_m4_m4(ob_dst->obmat, dob->mat);
1470                 BKE_object_apply_mat4(ob_dst, ob_dst->obmat, false, false);
1471
1472                 BLI_ghash_insert(dupli_gh, dob, ob_dst);
1473                 if (parent_gh) {
1474                         void **val;
1475                         /* Due to nature of hash/comparison of this ghash, a lot of duplis may be considered as 'the same',
1476                          * this avoids trying to insert same key several time and raise asserts in debug builds... */
1477                         if (!BLI_ghash_ensure_p(parent_gh, dob, &val)) {
1478                                 *val = ob_dst;
1479                         }
1480                 }
1481         }
1482
1483         for (dob = lb_duplis->first; dob; dob = dob->next) {
1484                 Object *ob_src = dob->ob;
1485                 Object *ob_dst = BLI_ghash_lookup(dupli_gh, dob);
1486
1487                 /* Remap new object to itself, and clear again newid pointer of orig object. */
1488                 BKE_libblock_relink_to_newid(&ob_dst->id);
1489
1490                 DEG_id_tag_update(&ob_dst->id, OB_RECALC_DATA);
1491
1492                 if (use_hierarchy) {
1493                         /* original parents */
1494                         Object *ob_src_par = ob_src->parent;
1495                         Object *ob_dst_par = NULL;
1496
1497                         /* find parent that was also made real */
1498                         if (ob_src_par) {
1499                                 /* OK to keep most of the members uninitialized,
1500                                  * they won't be read, this is simply for a hash lookup. */
1501                                 DupliObject dob_key;
1502                                 dob_key.ob = ob_src_par;
1503                                 if (base->object->transflag & OB_DUPLICOLLECTION) {
1504                                         memcpy(&dob_key.persistent_id[1],
1505                                                &dob->persistent_id[1],
1506                                                sizeof(dob->persistent_id[1]) * (MAX_DUPLI_RECUR - 1));
1507                                 }
1508                                 else {
1509                                         dob_key.persistent_id[0] = dob->persistent_id[0];
1510                                 }
1511                                 ob_dst_par = BLI_ghash_lookup(parent_gh, &dob_key);
1512                         }
1513
1514                         if (ob_dst_par) {
1515                                 /* allow for all possible parent types */
1516                                 ob_dst->partype = ob_src->partype;
1517                                 BLI_strncpy(ob_dst->parsubstr, ob_src->parsubstr, sizeof(ob_dst->parsubstr));
1518                                 ob_dst->par1 = ob_src->par1;
1519                                 ob_dst->par2 = ob_src->par2;
1520                                 ob_dst->par3 = ob_src->par3;
1521
1522                                 copy_m4_m4(ob_dst->parentinv, ob_src->parentinv);
1523
1524                                 ob_dst->parent = ob_dst_par;
1525                         }
1526                         else if (use_base_parent) {
1527                                 ob_dst->parent = base->object;
1528                                 ob_dst->partype = PAROBJECT;
1529                         }
1530                 }
1531                 else if (use_base_parent) {
1532                         /* since we are ignoring the internal hierarchy - parent all to the
1533                          * base object */
1534                         ob_dst->parent = base->object;
1535                         ob_dst->partype = PAROBJECT;
1536                 }
1537
1538                 if (ob_dst->parent) {
1539                         /* note, this may be the parent of other objects, but it should
1540                          * still work out ok */
1541                         BKE_object_apply_mat4(ob_dst, dob->mat, false, true);
1542
1543                         /* to set ob_dst->orig and in case theres any other discrepicies */
1544                         DEG_id_tag_update(&ob_dst->id, OB_RECALC_OB);
1545                 }
1546         }
1547
1548         if (base->object->transflag & OB_DUPLICOLLECTION && base->object->dup_group) {
1549                 for (Object *ob = bmain->object.first; ob; ob = ob->id.next) {
1550                         if (ob->proxy_group == base->object) {
1551                                 ob->proxy = NULL;
1552                                 ob->proxy_from = NULL;
1553                                 DEG_id_tag_update(&ob->id, OB_RECALC_OB);
1554                         }
1555                 }
1556         }
1557
1558         BLI_ghash_free(dupli_gh, NULL, NULL);
1559         if (parent_gh) {
1560                 BLI_ghash_free(parent_gh, NULL, NULL);
1561         }
1562
1563         free_object_duplilist(lb_duplis);
1564
1565         BKE_main_id_clear_newpoins(bmain);
1566
1567         base->object->transflag &= ~OB_DUPLI;
1568 }
1569
1570 static int object_duplicates_make_real_exec(bContext *C, wmOperator *op)
1571 {
1572         Main *bmain = CTX_data_main(C);
1573         Scene *scene = CTX_data_scene(C);
1574
1575         const bool use_base_parent = RNA_boolean_get(op->ptr, "use_base_parent");
1576         const bool use_hierarchy = RNA_boolean_get(op->ptr, "use_hierarchy");
1577
1578         BKE_main_id_clear_newpoins(bmain);
1579
1580         CTX_DATA_BEGIN (C, Base *, base, selected_editable_bases)
1581         {
1582                 make_object_duplilist_real(C, scene, base, use_base_parent, use_hierarchy);
1583
1584                 /* dependencies were changed */
1585                 WM_event_add_notifier(C, NC_OBJECT | ND_PARENT, base->object);
1586         }
1587         CTX_DATA_END;
1588
1589         DEG_relations_tag_update(bmain);
1590         WM_event_add_notifier(C, NC_SCENE, scene);
1591         WM_main_add_notifier(NC_OBJECT | ND_DRAW, NULL);
1592
1593         return OPERATOR_FINISHED;
1594 }
1595
1596 void OBJECT_OT_duplicates_make_real(wmOperatorType *ot)
1597 {
1598         /* identifiers */
1599         ot->name = "Make Duplicates Real";
1600         ot->description = "Make dupli objects attached to this object real";
1601         ot->idname = "OBJECT_OT_duplicates_make_real";
1602
1603         /* api callbacks */
1604         ot->exec = object_duplicates_make_real_exec;
1605
1606         ot->poll = ED_operator_objectmode;
1607
1608         /* flags */
1609         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1610
1611         RNA_def_boolean(ot->srna, "use_base_parent", 0, "Parent", "Parent newly created objects to the original duplicator");
1612         RNA_def_boolean(ot->srna, "use_hierarchy", 0, "Keep Hierarchy", "Maintain parent child relationships");
1613 }
1614
1615 /**************************** Convert **************************/
1616
1617 static const EnumPropertyItem convert_target_items[] = {
1618         {OB_CURVE, "CURVE", ICON_OUTLINER_OB_CURVE, "Curve from Mesh/Text", ""},
1619         {OB_MESH, "MESH", ICON_OUTLINER_OB_MESH, "Mesh from Curve/Meta/Surf/Text", ""},
1620         {0, NULL, 0, NULL, NULL}
1621 };
1622
1623 static void convert_ensure_curve_cache(Depsgraph *depsgraph, Scene *scene, Object *ob)
1624 {
1625         if (ob->curve_cache == NULL) {
1626                 /* Force creation. This is normally not needed but on operator
1627                  * redo we might end up with an object which isn't evaluated yet.
1628                  */
1629                 if (ELEM(ob->type, OB_SURF, OB_CURVE, OB_FONT)) {
1630                         BKE_displist_make_curveTypes(depsgraph, scene, ob, false);
1631                 }
1632                 else if (ob->type == OB_MBALL) {
1633                         BKE_displist_make_mball(depsgraph, scene, ob);
1634                 }
1635         }
1636 }
1637
1638 static void curvetomesh(Depsgraph *depsgraph, Scene *scene, Object *ob)
1639 {
1640         convert_ensure_curve_cache(depsgraph, scene, ob);
1641         BKE_mesh_from_nurbs(ob); /* also does users */
1642
1643         if (ob->type == OB_MESH) {
1644                 BKE_object_free_modifiers(ob, 0);
1645         }
1646 }
1647
1648 static int convert_poll(bContext *C)
1649 {
1650         Scene *scene = CTX_data_scene(C);
1651         Base *base_act = CTX_data_active_base(C);
1652         Object *obact = base_act ? base_act->object : NULL;
1653
1654         return (!ID_IS_LINKED(scene) && obact && (BKE_object_is_in_editmode(obact) == false) &&
1655                 (base_act->flag & BASE_SELECTED) && !ID_IS_LINKED(obact));
1656 }
1657
1658 /* Helper for convert_exec */
1659 static Base *duplibase_for_convert(Main *bmain, Scene *scene, ViewLayer *view_layer, Base *base, Object *ob)
1660 {
1661         Object *obn;
1662         Base *basen;
1663
1664         if (ob == NULL) {
1665                 ob = base->object;
1666         }
1667
1668         obn = BKE_object_copy(bmain, ob);
1669         DEG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1670         BKE_collection_object_add_from(bmain, scene, ob, obn);
1671
1672         basen = BKE_view_layer_base_find(view_layer, obn);
1673         ED_object_base_select(basen, BA_SELECT);
1674         ED_object_base_select(basen, BA_DESELECT);
1675         return basen;
1676 }
1677
1678 static int convert_exec(bContext *C, wmOperator *op)
1679 {
1680         Main *bmain = CTX_data_main(C);
1681         Depsgraph *depsgraph = CTX_data_depsgraph(C);
1682         Scene *scene = CTX_data_scene(C);
1683         ViewLayer *view_layer = CTX_data_view_layer(C);
1684         Base *basen = NULL, *basact = NULL;
1685         Object *ob1, *newob, *obact = CTX_data_active_object(C);
1686         DerivedMesh *dm;
1687         Curve *cu;
1688         Nurb *nu;
1689         MetaBall *mb;
1690         Mesh *me;
1691         const short target = RNA_enum_get(op->ptr, "target");
1692         bool keep_original = RNA_boolean_get(op->ptr, "keep_original");
1693         int a, mballConverted = 0;
1694
1695         /* don't forget multiple users! */
1696
1697         {
1698                 FOREACH_SCENE_OBJECT_BEGIN(scene, ob)
1699                 {
1700                         ob->flag &= ~OB_DONE;
1701
1702                         /* flag data thats not been edited (only needed for !keep_original) */
1703                         if (ob->data) {
1704                                 ((ID *)ob->data)->tag |= LIB_TAG_DOIT;
1705                         }
1706
1707                         /* possible metaball basis is not in this scene */
1708                         if (ob->type == OB_MBALL && target == OB_MESH) {
1709                                 if (BKE_mball_is_basis(ob) == false) {
1710                                         Object *ob_basis;
1711                                         ob_basis = BKE_mball_basis_find(scene, ob);
1712                                         if (ob_basis) {
1713                                                 ob_basis->flag &= ~OB_DONE;
1714                                         }
1715                                 }
1716                         }
1717                 }
1718                 FOREACH_SCENE_OBJECT_END;
1719         }
1720
1721         ListBase selected_editable_bases = CTX_data_collection_get(C, "selected_editable_bases");
1722
1723         /* Ensure we get all meshes calculated with a sufficient data-mask,
1724          * needed since re-evaluating single modifiers causes bugs if they depend
1725          * on other objects data masks too, see: T50950. */
1726         {
1727                 for (CollectionPointerLink *link = selected_editable_bases.first; link; link = link->next) {
1728                         Base *base = link->ptr.data;
1729                         Object *ob = base->object;
1730
1731                         /* The way object type conversion works currently (enforcing conversion of *all* objetcs using converted
1732                          * obdata, even some un-selected/hidden/inother scene ones, sounds totally bad to me.
1733                          * However, changing this is more design than bugfix, not to mention convoluted code below,
1734                          * so that will be for later.
1735                          * But at the very least, do not do that with linked IDs! */
1736                         if ((ID_IS_LINKED(ob) || (ob->data && ID_IS_LINKED(ob->data))) && !keep_original) {
1737                                 keep_original = true;
1738                                 BKE_reportf(op->reports, RPT_INFO,
1739                                             "Converting some linked object/object data, enforcing 'Keep Original' option to True");
1740                         }
1741
1742                         DEG_id_tag_update(&base->object->id, OB_RECALC_DATA);
1743                 }
1744
1745                 uint64_t customdata_mask_prev = scene->customdata_mask;
1746                 scene->customdata_mask |= CD_MASK_MESH;
1747                 BKE_scene_graph_update_tagged(depsgraph, bmain);
1748                 scene->customdata_mask = customdata_mask_prev;
1749         }
1750
1751         for (CollectionPointerLink *link = selected_editable_bases.first; link; link = link->next) {
1752                 Base *base = link->ptr.data;
1753                 Object *ob = base->object;
1754
1755                 if (ob->flag & OB_DONE || !IS_TAGGED(ob->data)) {
1756                         if (ob->type != target) {
1757                                 base->flag &= ~SELECT;
1758                                 ob->flag &= ~SELECT;
1759                         }
1760
1761                         /* obdata already modified */
1762                         if (!IS_TAGGED(ob->data)) {
1763                                 /* When 2 objects with linked data are selected, converting both
1764                                  * would keep modifiers on all but the converted object [#26003] */
1765                                 if (ob->type == OB_MESH) {
1766                                         BKE_object_free_modifiers(ob, 0);  /* after derivedmesh calls! */
1767                                 }
1768                         }
1769                 }
1770                 else if (ob->type == OB_MESH && target == OB_CURVE) {
1771                         ob->flag |= OB_DONE;
1772
1773                         if (keep_original) {
1774                                 basen = duplibase_for_convert(bmain, scene, view_layer, base, NULL);
1775                                 newob = basen->object;
1776
1777                                 /* decrement original mesh's usage count  */
1778                                 me = newob->data;
1779                                 id_us_min(&me->id);
1780
1781                                 /* make a new copy of the mesh */
1782                                 newob->data = BKE_mesh_copy(bmain, me);
1783                         }
1784                         else {
1785                                 newob = ob;
1786                         }
1787
1788                         BKE_mesh_to_curve(depsgraph, scene, newob);
1789
1790                         if (newob->type == OB_CURVE) {
1791                                 BKE_object_free_modifiers(newob, 0);   /* after derivedmesh calls! */
1792                                 ED_rigidbody_object_remove(bmain, scene, newob);
1793                         }
1794                 }
1795                 else if (ob->type == OB_MESH) {
1796                         ob->flag |= OB_DONE;
1797
1798                         if (keep_original) {
1799                                 basen = duplibase_for_convert(bmain, scene, view_layer, base, NULL);
1800                                 newob = basen->object;
1801
1802                                 /* decrement original mesh's usage count  */
1803                                 me = newob->data;
1804                                 id_us_min(&me->id);
1805
1806                                 /* make a new copy of the mesh */
1807                                 newob->data = BKE_mesh_copy(bmain, me);
1808                         }
1809                         else {
1810                                 newob = ob;
1811                                 DEG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1812                         }
1813
1814                         /* make new mesh data from the original copy */
1815                         /* note: get the mesh from the original, not from the copy in some
1816                          * cases this doesnt give correct results (when MDEF is used for eg)
1817                          */
1818                         dm = mesh_get_derived_final(depsgraph, scene, newob, CD_MASK_MESH);
1819
1820                         DM_to_mesh(dm, newob->data, newob, CD_MASK_MESH, true);
1821
1822                         /* re-tessellation is called by DM_to_mesh */
1823
1824                         BKE_object_free_modifiers(newob, 0);   /* after derivedmesh calls! */
1825                 }
1826                 else if (ob->type == OB_FONT) {
1827                         ob->flag |= OB_DONE;
1828
1829                         if (keep_original) {
1830                                 basen = duplibase_for_convert(bmain, scene, view_layer, base, NULL);
1831                                 newob = basen->object;
1832
1833                                 /* decrement original curve's usage count  */
1834                                 id_us_min(&((Curve *)newob->data)->id);
1835
1836                                 /* make a new copy of the curve */
1837                                 newob->data = BKE_curve_copy(bmain, ob->data);
1838                         }
1839                         else {
1840                                 newob = ob;
1841                         }
1842
1843                         cu = newob->data;
1844
1845                         /* TODO(sergey): Ideally DAG will create nurbs list for a curve data
1846                          *               datablock, but for until we've got granular update
1847                          *               lets take care by selves.
1848                          */
1849                         BKE_vfont_to_curve(bmain, newob, FO_EDIT);
1850
1851                         newob->type = OB_CURVE;
1852                         cu->type = OB_CURVE;
1853
1854                         if (cu->vfont) {
1855                                 id_us_min(&cu->vfont->id);
1856                                 cu->vfont = NULL;
1857                         }
1858                         if (cu->vfontb) {
1859                                 id_us_min(&cu->vfontb->id);
1860                                 cu->vfontb = NULL;
1861                         }
1862                         if (cu->vfonti) {
1863                                 id_us_min(&cu->vfonti->id);
1864                                 cu->vfonti = NULL;
1865                         }
1866                         if (cu->vfontbi) {
1867                                 id_us_min(&cu->vfontbi->id);
1868                                 cu->vfontbi = NULL;
1869                         }
1870
1871                         if (!keep_original) {
1872                                 /* other users */
1873                                 if (cu->id.us > 1) {
1874                                         for (ob1 = bmain->object.first; ob1; ob1 = ob1->id.next) {
1875                                                 if (ob1->data == ob->data) {
1876                                                         ob1->type = OB_CURVE;
1877                                                         DEG_id_tag_update(&ob1->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1878                                                 }
1879                                         }
1880                                 }
1881                         }
1882
1883                         for (nu = cu->nurb.first; nu; nu = nu->next)
1884                                 nu->charidx = 0;
1885
1886                         cu->flag &= ~CU_3D;
1887                         BKE_curve_curve_dimension_update(cu);
1888
1889                         if (target == OB_MESH) {
1890                                 curvetomesh(depsgraph, scene, newob);
1891
1892                                 /* meshes doesn't use displist */
1893                                 BKE_object_free_curve_cache(newob);
1894                         }
1895                 }
1896                 else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
1897                         ob->flag |= OB_DONE;
1898
1899                         if (target == OB_MESH) {
1900                                 if (keep_original) {
1901                                         basen = duplibase_for_convert(bmain, scene, view_layer, base, NULL);
1902                                         newob = basen->object;
1903
1904                                         /* decrement original curve's usage count  */
1905                                         id_us_min(&((Curve *)newob->data)->id);
1906
1907                                         /* make a new copy of the curve */
1908                                         newob->data = BKE_curve_copy(bmain, ob->data);
1909                                 }
1910                                 else {
1911                                         newob = ob;
1912                                 }
1913
1914                                 curvetomesh(depsgraph, scene, newob);
1915
1916                                 /* meshes doesn't use displist */
1917                                 BKE_object_free_curve_cache(newob);
1918                         }
1919                 }
1920                 else if (ob->type == OB_MBALL && target == OB_MESH) {
1921                         Object *baseob;
1922
1923                         base->flag &= ~BASE_SELECTED;
1924                         ob->base_flag &= ~BASE_SELECTED;
1925
1926                         baseob = BKE_mball_basis_find(scene, ob);
1927
1928                         if (ob != baseob) {
1929                                 /* if motherball is converting it would be marked as done later */
1930                                 ob->flag |= OB_DONE;
1931                         }
1932
1933                         if (!(baseob->flag & OB_DONE)) {
1934                                 baseob->flag |= OB_DONE;
1935
1936                                 basen = duplibase_for_convert(bmain, scene, view_layer, base, baseob);
1937                                 newob = basen->object;
1938
1939                                 mb = newob->data;
1940                                 id_us_min(&mb->id);
1941
1942                                 newob->data = BKE_mesh_add(bmain, "Mesh");
1943                                 newob->type = OB_MESH;
1944
1945                                 me = newob->data;
1946                                 me->totcol = mb->totcol;
1947                                 if (newob->totcol) {
1948                                         me->mat = MEM_dupallocN(mb->mat);
1949                                         for (a = 0; a < newob->totcol; a++) id_us_plus((ID *)me->mat[a]);
1950                                 }
1951
1952                                 convert_ensure_curve_cache(depsgraph, scene, baseob);
1953                                 BKE_mesh_from_metaball(&baseob->curve_cache->disp, newob->data);
1954
1955                                 if (obact->type == OB_MBALL) {
1956                                         basact = basen;
1957                                 }
1958
1959                                 mballConverted = 1;
1960                         }
1961                 }
1962                 else {
1963                         continue;
1964                 }
1965
1966                 /* tag obdata if it was been changed */
1967
1968                 /* If the original object is active then make this object active */
1969                 if (basen) {
1970                         if (ob == obact) {
1971                                 /* store new active base to update BASACT */
1972                                 basact = basen;
1973                         }
1974
1975                         basen = NULL;
1976                 }
1977
1978                 if (!keep_original && (ob->flag & OB_DONE)) {
1979                         DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
1980                         ((ID *)ob->data)->tag &= ~LIB_TAG_DOIT; /* flag not to convert this datablock again */
1981                 }
1982         }
1983         BLI_freelistN(&selected_editable_bases);
1984
1985         if (!keep_original) {
1986                 if (mballConverted) {
1987                         FOREACH_SCENE_OBJECT_BEGIN(scene, ob_mball)
1988                         {
1989                                 if (ob_mball->type == OB_MBALL) {
1990                                         if (ob_mball->flag & OB_DONE) {
1991                                                 Object *ob_basis = NULL;
1992                                                 if (BKE_mball_is_basis(ob_mball) ||
1993                                                     ((ob_basis = BKE_mball_basis_find(scene, ob_mball)) && (ob_basis->flag & OB_DONE)))
1994                                                 {
1995                                                         ED_object_base_free_and_unlink(bmain, scene, ob_mball);
1996                                                 }
1997                                         }
1998                                 }
1999                         }
2000                         FOREACH_SCENE_OBJECT_END;
2001                 }
2002
2003                 /* delete object should renew depsgraph */
2004                 DEG_relations_tag_update(bmain);
2005         }
2006
2007 // XXX  ED_object_editmode_enter(C, 0);
2008 // XXX  exit_editmode(C, EM_FREEDATA|EM_WAITCURSOR); /* freedata, but no undo */
2009
2010         if (basact) {
2011                 /* active base was changed */
2012                 ED_object_base_activate(C, basact);
2013                 BASACT(view_layer) = basact;
2014         }
2015         else if (BASACT(view_layer)->object->flag & OB_DONE) {
2016                 WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, BASACT(view_layer)->object);
2017                 WM_event_add_notifier(C, NC_OBJECT | ND_DATA, BASACT(view_layer)->object);
2018         }
2019
2020         DEG_relations_tag_update(bmain);
2021         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, scene);
2022         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
2023
2024         return OPERATOR_FINISHED;
2025 }
2026
2027
2028 void OBJECT_OT_convert(wmOperatorType *ot)
2029 {
2030         /* identifiers */
2031         ot->name = "Convert to";
2032         ot->description = "Convert selected objects to another type";
2033         ot->idname = "OBJECT_OT_convert";
2034
2035         /* api callbacks */
2036         ot->invoke = WM_menu_invoke;
2037         ot->exec = convert_exec;
2038         ot->poll = convert_poll;
2039
2040         /* flags */
2041         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2042
2043         /* properties */
2044         ot->prop = RNA_def_enum(ot->srna, "target", convert_target_items, OB_MESH, "Target", "Type of object to convert to");
2045         RNA_def_boolean(ot->srna, "keep_original", 0, "Keep Original", "Keep original objects instead of replacing them");
2046 }
2047
2048 /**************************** Duplicate ************************/
2049
2050 /*
2051  * dupflag: a flag made from constants declared in DNA_userdef_types.h
2052  * The flag tells adduplicate() whether to copy data linked to the object, or to reference the existing data.
2053  * U.dupflag for default operations or you can construct a flag as python does
2054  * if the dupflag is 0 then no data will be copied (linked duplicate) */
2055
2056 /* used below, assumes id.new is correct */
2057 /* leaves selection of base/object unaltered */
2058 /* Does set ID->newid pointers. */
2059 static Base *object_add_duplicate_internal(Main *bmain, Scene *scene, ViewLayer *view_layer, Object *ob, int dupflag)
2060 {
2061 #define ID_NEW_REMAP_US(a)      if (      (a)->id.newid) { (a) = (void *)(a)->id.newid;       (a)->id.us++; }
2062 #define ID_NEW_REMAP_US2(a)     if (((ID *)a)->newid)    { (a) = ((ID  *)a)->newid;     ((ID *)a)->us++;    }
2063
2064         Base *base, *basen = NULL;
2065         Material ***matarar;
2066         Object *obn;
2067         ID *id;
2068         int a, didit;
2069
2070         if (ob->mode & OB_MODE_POSE) {
2071                 ; /* nothing? */
2072         }
2073         else {
2074                 obn = ID_NEW_SET(ob, BKE_object_copy(bmain, ob));
2075                 DEG_id_tag_update(&obn->id, OB_RECALC_OB | OB_RECALC_DATA);
2076
2077                 base = BKE_view_layer_base_find(view_layer, ob);
2078                 if ((base != NULL) && (base->flag & BASE_VISIBLED)) {
2079                         BKE_collection_object_add_from(bmain, scene, ob, obn);
2080                 }
2081                 else {
2082                         LayerCollection *layer_collection = BKE_layer_collection_get_active(view_layer);
2083                         BKE_collection_object_add(bmain, layer_collection->collection, obn);
2084                 }
2085                 basen = BKE_view_layer_base_find(view_layer, obn);
2086
2087                 /* 1) duplis should end up in same collection as the original
2088                  * 2) Rigid Body sim participants MUST always be part of a collection...
2089                  */
2090                 // XXX: is 2) really a good measure here?
2091                 if (ob->rigidbody_object || ob->rigidbody_constraint) {
2092                         Collection *collection;
2093                         for (collection = bmain->collection.first; collection; collection = collection->id.next) {
2094                                 if (BKE_collection_has_object(collection, ob))
2095                                         BKE_collection_object_add(bmain, collection, obn);
2096                         }
2097                 }
2098
2099                 /* duplicates using userflags */
2100                 if (dupflag & USER_DUP_ACT) {
2101                         BKE_animdata_copy_id_action(&obn->id, true);
2102                 }
2103
2104                 if (dupflag & USER_DUP_MAT) {
2105                         for (a = 0; a < obn->totcol; a++) {
2106                                 id = (ID *)obn->mat[a];
2107                                 if (id) {
2108                                         ID_NEW_REMAP_US(obn->mat[a])
2109                                         else {
2110                                                 obn->mat[a] = ID_NEW_SET(obn->mat[a], BKE_material_copy(bmain, obn->mat[a]));
2111                                         }
2112                                         id_us_min(id);
2113
2114                                         if (dupflag & USER_DUP_ACT) {
2115                                                 BKE_animdata_copy_id_action(&obn->mat[a]->id, true);
2116                                         }
2117                                 }
2118                         }
2119                 }
2120                 if (dupflag & USER_DUP_PSYS) {
2121                         ParticleSystem *psys;
2122                         for (psys = obn->particlesystem.first; psys; psys = psys->next) {
2123                                 id = (ID *) psys->part;
2124                                 if (id) {
2125                                         ID_NEW_REMAP_US(psys->part)
2126                                         else {
2127                                                 psys->part = ID_NEW_SET(psys->part, BKE_particlesettings_copy(bmain, psys->part));
2128                                         }
2129
2130                                         if (dupflag & USER_DUP_ACT) {
2131                                                 BKE_animdata_copy_id_action(&psys->part->id, true);
2132                                         }
2133
2134                                         id_us_min(id);
2135                                 }
2136                         }
2137                 }
2138
2139                 id = obn->data;
2140                 didit = 0;
2141
2142                 switch (obn->type) {
2143                         case OB_MESH:
2144                                 if (dupflag & USER_DUP_MESH) {
2145                                         ID_NEW_REMAP_US2(obn->data)
2146                                         else {
2147                                                 obn->data = ID_NEW_SET(obn->data, BKE_mesh_copy(bmain, obn->data));
2148                                                 didit = 1;
2149                                         }
2150                                         id_us_min(id);
2151                                 }
2152                                 break;
2153                         case OB_CURVE:
2154                                 if (dupflag & USER_DUP_CURVE) {
2155                                         ID_NEW_REMAP_US2(obn->data)
2156                                         else {
2157                                                 obn->data = ID_NEW_SET(obn->data, BKE_curve_copy(bmain, obn->data));
2158                                                 didit = 1;
2159                                         }
2160                                         id_us_min(id);
2161                                 }
2162                                 break;
2163                         case OB_SURF:
2164                                 if (dupflag & USER_DUP_SURF) {
2165                                         ID_NEW_REMAP_US2(obn->data)
2166                                         else {
2167                                                 obn->data = ID_NEW_SET(obn->data, BKE_curve_copy(bmain, obn->data));
2168                                                 didit = 1;
2169                                         }
2170                                         id_us_min(id);
2171                                 }
2172                                 break;
2173                         case OB_FONT:
2174                                 if (dupflag & USER_DUP_FONT) {
2175                                         ID_NEW_REMAP_US2(obn->data)
2176                                         else {
2177                                                 obn->data = ID_NEW_SET(obn->data, BKE_curve_copy(bmain, obn->data));
2178                                                 didit = 1;
2179                                         }
2180                                         id_us_min(id);
2181                                 }
2182                                 break;
2183                         case OB_MBALL:
2184                                 if (dupflag & USER_DUP_MBALL) {
2185                                         ID_NEW_REMAP_US2(obn->data)
2186                                         else {
2187                                                 obn->data = ID_NEW_SET(obn->data, BKE_mball_copy(bmain, obn->data));
2188                                                 didit = 1;
2189                                         }
2190                                         id_us_min(id);
2191                                 }
2192                                 break;
2193                         case OB_LAMP:
2194                                 if (dupflag & USER_DUP_LAMP) {
2195                                         ID_NEW_REMAP_US2(obn->data)
2196                                         else {
2197                                                 obn->data = ID_NEW_SET(obn->data, BKE_lamp_copy(bmain, obn->data));
2198                                                 didit = 1;
2199                                         }
2200                                         id_us_min(id);
2201                                 }
2202                                 break;
2203                         case OB_ARMATURE:
2204                                 DEG_id_tag_update(&obn->id, OB_RECALC_DATA);
2205                                 if (obn->pose)
2206                                         BKE_pose_tag_recalc(bmain, obn->pose);
2207                                 if (dupflag & USER_DUP_ARM) {
2208                                         ID_NEW_REMAP_US2(obn->data)
2209                                         else {
2210                                                 obn->data = ID_NEW_SET(obn->data, BKE_armature_copy(bmain, obn->data));
2211                                                 BKE_pose_rebuild(obn, obn->data);
2212                                                 didit = 1;
2213                                         }
2214                                         id_us_min(id);
2215                                 }
2216                                 break;
2217                         case OB_LATTICE:
2218                                 if (dupflag != 0) {
2219                                         ID_NEW_REMAP_US2(obn->data)
2220                                         else {
2221                                                 obn->data = ID_NEW_SET(obn->data, BKE_lattice_copy(bmain, obn->data));
2222                                                 didit = 1;
2223                                         }
2224                                         id_us_min(id);
2225                                 }
2226                                 break;
2227                         case OB_CAMERA:
2228                                 if (dupflag != 0) {
2229                                         ID_NEW_REMAP_US2(obn->data)
2230                                         else {
2231                                                 obn->data = ID_NEW_SET(obn->data, BKE_camera_copy(bmain, obn->data));
2232                                                 didit = 1;
2233                                         }
2234                                         id_us_min(id);
2235                                 }
2236                                 break;
2237                         case OB_SPEAKER:
2238                                 if (dupflag != 0) {
2239                                         ID_NEW_REMAP_US2(obn->data)
2240                                         else {
2241                                                 obn->data = ID_NEW_SET(obn->data, BKE_speaker_copy(bmain, obn->data));
2242                                                 didit = 1;
2243                                         }
2244                                         id_us_min(id);
2245                                 }
2246                                 break;
2247                 }
2248
2249                 /* check if obdata is copied */
2250                 if (didit) {
2251                         Key *key = BKE_key_from_object(obn);
2252
2253                         Key *oldkey = BKE_key_from_object(ob);
2254                         if (oldkey != NULL) {
2255                                 ID_NEW_SET(oldkey, key);
2256                         }
2257
2258                         if (dupflag & USER_DUP_ACT) {
2259                                 BKE_animdata_copy_id_action((ID *)obn->data, true);
2260                                 if (key) {
2261                                         BKE_animdata_copy_id_action((ID *)key, true);
2262                                 }
2263                         }
2264
2265                         if (dupflag & USER_DUP_MAT) {
2266                                 matarar = give_matarar(obn);
2267                                 if (matarar) {
2268                                         for (a = 0; a < obn->totcol; a++) {
2269                                                 id = (ID *)(*matarar)[a];
2270                                                 if (id) {
2271                                                         ID_NEW_REMAP_US((*matarar)[a])
2272                                                         else {
2273                                                                 (*matarar)[a] = ID_NEW_SET((*matarar)[a], BKE_material_copy(bmain, (*matarar)[a]));
2274                                                         }
2275                                                         id_us_min(id);
2276                                                 }
2277                                         }
2278                                 }
2279                         }
2280                 }
2281         }
2282         return basen;
2283
2284 #undef ID_NEW_REMAP_US
2285 #undef ID_NEW_REMAP_US2
2286 }
2287
2288 /* single object duplicate, if dupflag==0, fully linked, else it uses the flags given */
2289 /* leaves selection of base/object unaltered.
2290  * note: don't call this within a loop since clear_* funcs loop over the entire database.
2291  * note: caller must do DAG_relations_tag_update(bmain);
2292  *       this is not done automatic since we may duplicate many objects in a batch */
2293 Base *ED_object_add_duplicate(Main *bmain, Scene *scene, ViewLayer *view_layer, Base *base, int dupflag)
2294 {
2295         Base *basen;
2296         Object *ob;
2297
2298         basen = object_add_duplicate_internal(bmain, scene, view_layer, base->object, dupflag);
2299         if (basen == NULL) {
2300                 return NULL;
2301         }
2302
2303         ob = basen->object;
2304
2305         /* link own references to the newly duplicated data [#26816] */
2306         BKE_libblock_relink_to_newid(&ob->id);
2307
2308         /* DAG_relations_tag_update(bmain); */ /* caller must do */
2309
2310         if (ob->data != NULL) {
2311                 DEG_id_tag_update_ex(bmain, (ID *)ob->data, DEG_TAG_EDITORS_UPDATE);
2312         }
2313
2314         BKE_main_id_clear_newpoins(bmain);
2315
2316         return basen;
2317 }
2318
2319 /* contextual operator dupli */
2320 static int duplicate_exec(bContext *C, wmOperator *op)
2321 {
2322         Main *bmain = CTX_data_main(C);
2323         Scene *scene = CTX_data_scene(C);
2324         ViewLayer *view_layer = CTX_data_view_layer(C);
2325         const bool linked = RNA_boolean_get(op->ptr, "linked");
2326         int dupflag = (linked) ? 0 : U.dupflag;
2327
2328         CTX_DATA_BEGIN (C, Base *, base, selected_bases)
2329         {
2330                 Base *basen = object_add_duplicate_internal(bmain, scene, view_layer, base->object, dupflag);
2331
2332                 /* note that this is safe to do with this context iterator,
2333                  * the list is made in advance */
2334                 ED_object_base_select(base, BA_DESELECT);
2335                 ED_object_base_select(basen, BA_SELECT);
2336
2337                 if (basen == NULL) {
2338                         continue;
2339                 }
2340
2341                 /* new object becomes active */
2342                 if (BASACT(view_layer) == base)
2343                         ED_object_base_activate(C, basen);
2344
2345                 if (basen->object->data) {
2346                         DEG_id_tag_update(basen->object->data, 0);
2347                 }
2348         }
2349         CTX_DATA_END;
2350
2351         copy_object_set_idnew(C);
2352
2353         BKE_main_id_clear_newpoins(bmain);
2354
2355         DEG_relations_tag_update(bmain);
2356         /* TODO(sergey): Use proper flag for tagging here. */
2357         DEG_id_tag_update(&scene->id, 0);
2358
2359         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
2360
2361         return OPERATOR_FINISHED;
2362 }
2363
2364 void OBJECT_OT_duplicate(wmOperatorType *ot)
2365 {
2366         PropertyRNA *prop;
2367
2368         /* identifiers */
2369         ot->name = "Duplicate Objects";
2370         ot->description = "Duplicate selected objects";
2371         ot->idname = "OBJECT_OT_duplicate";
2372
2373         /* api callbacks */
2374         ot->exec = duplicate_exec;
2375         ot->poll = ED_operator_objectmode;
2376
2377         /* flags */
2378         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2379
2380         /* to give to transform */
2381         RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data");
2382         prop = RNA_def_enum(ot->srna, "mode", rna_enum_transform_mode_types, TFM_TRANSLATION, "Mode", "");
2383         RNA_def_property_flag(prop, PROP_HIDDEN);
2384 }
2385
2386 /* **************** add named object, for dragdrop ************* */
2387
2388 static int add_named_exec(bContext *C, wmOperator *op)
2389 {
2390         wmWindow *win = CTX_wm_window(C);
2391         const wmEvent *event = win ? win->eventstate : NULL;
2392         Main *bmain = CTX_data_main(C);
2393         Scene *scene = CTX_data_scene(C);
2394         ViewLayer *view_layer = CTX_data_view_layer(C);
2395         Base *basen;
2396         Object *ob;
2397         const bool linked = RNA_boolean_get(op->ptr, "linked");
2398         int dupflag = (linked) ? 0 : U.dupflag;
2399         char name[MAX_ID_NAME - 2];
2400
2401         /* find object, create fake base */
2402         RNA_string_get(op->ptr, "name", name);
2403         ob = (Object *)BKE_libblock_find_name(bmain, ID_OB, name);
2404
2405         if (ob == NULL) {
2406                 BKE_report(op->reports, RPT_ERROR, "Object not found");
2407                 return OPERATOR_CANCELLED;
2408         }
2409
2410         /* prepare dupli */
2411         basen = object_add_duplicate_internal(bmain, scene, view_layer, ob, dupflag);
2412
2413         if (basen == NULL) {
2414                 BKE_report(op->reports, RPT_ERROR, "Object could not be duplicated");
2415                 return OPERATOR_CANCELLED;
2416         }
2417
2418         BKE_scene_object_base_flag_sync_from_object(basen);
2419         basen->object->restrictflag &= ~OB_RESTRICT_VIEW;
2420
2421         if (event) {
2422                 ARegion *ar = CTX_wm_region(C);
2423                 const int mval[2] = {event->x - ar->winrct.xmin,
2424                                      event->y - ar->winrct.ymin};
2425                 ED_object_location_from_view(C, basen->object->loc);
2426                 ED_view3d_cursor3d_position(C, basen->object->loc, mval);
2427         }
2428
2429         ED_object_base_select(basen, BA_SELECT);
2430         ED_object_base_activate(C, basen);
2431
2432         copy_object_set_idnew(C);
2433
2434         BKE_main_id_clear_newpoins(bmain);
2435
2436         /* TODO(sergey): Only update relations for the current scene. */
2437         DEG_relations_tag_update(bmain);
2438
2439         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
2440         WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
2441
2442         return OPERATOR_FINISHED;
2443 }
2444
2445 void OBJECT_OT_add_named(wmOperatorType *ot)
2446 {
2447         /* identifiers */
2448         ot->name = "Add Named Object";
2449         ot->description = "Add named object";
2450         ot->idname = "OBJECT_OT_add_named";
2451
2452         /* api callbacks */
2453         ot->exec = add_named_exec;
2454         ot->poll = ED_operator_objectmode;
2455
2456         /* flags */
2457         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2458
2459         RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data");
2460         RNA_def_string(ot->srna, "name", NULL, MAX_ID_NAME - 2, "Name", "Object name to add");
2461 }
2462
2463 /**************************** Join *************************/
2464
2465 static int join_poll(bContext *C)
2466 {
2467         Object *ob = CTX_data_active_object(C);
2468
2469         if (!ob || ID_IS_LINKED(ob)) return 0;
2470
2471         if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_ARMATURE))
2472                 return ED_operator_screenactive(C);
2473         else
2474                 return 0;
2475 }
2476
2477 static int join_exec(bContext *C, wmOperator *op)
2478 {
2479         Object *ob = CTX_data_active_object(C);
2480
2481         if (ob->mode & OB_MODE_EDIT) {
2482                 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in edit mode");
2483                 return OPERATOR_CANCELLED;
2484         }
2485         else if (BKE_object_obdata_is_libdata(ob)) {
2486                 BKE_report(op->reports, RPT_ERROR, "Cannot edit external libdata");
2487                 return OPERATOR_CANCELLED;
2488         }
2489
2490         if (ob->type == OB_MESH)
2491                 return join_mesh_exec(C, op);
2492         else if (ELEM(ob->type, OB_CURVE, OB_SURF))
2493                 return join_curve_exec(C, op);
2494         else if (ob->type == OB_ARMATURE)
2495                 return join_armature_exec(C, op);
2496
2497         return OPERATOR_CANCELLED;
2498 }
2499
2500 void OBJECT_OT_join(wmOperatorType *ot)
2501 {
2502         /* identifiers */
2503         ot->name = "Join";
2504         ot->description = "Join selected objects into active object";
2505         ot->idname = "OBJECT_OT_join";
2506
2507         /* api callbacks */
2508         ot->exec = join_exec;
2509         ot->poll = join_poll;
2510
2511         /* flags */
2512         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2513 }
2514
2515 /**************************** Join as Shape Key*************************/
2516
2517 static int join_shapes_poll(bContext *C)
2518 {
2519         Object *ob = CTX_data_active_object(C);
2520
2521         if (!ob || ID_IS_LINKED(ob)) return 0;
2522
2523         /* only meshes supported at the moment */
2524         if (ob->type == OB_MESH)
2525                 return ED_operator_screenactive(C);
2526         else
2527                 return 0;
2528 }
2529
2530 static int join_shapes_exec(bContext *C, wmOperator *op)
2531 {
2532         Object *ob = CTX_data_active_object(C);
2533
2534         if (ob->mode & OB_MODE_EDIT) {
2535                 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in edit mode");
2536                 return OPERATOR_CANCELLED;
2537         }
2538         else if (BKE_object_obdata_is_libdata(ob)) {
2539                 BKE_report(op->reports, RPT_ERROR, "Cannot edit external libdata");
2540                 return OPERATOR_CANCELLED;
2541         }
2542
2543         if (ob->type == OB_MESH)
2544                 return join_mesh_shapes_exec(C, op);
2545
2546         return OPERATOR_CANCELLED;
2547 }
2548
2549 void OBJECT_OT_join_shapes(wmOperatorType *ot)
2550 {
2551         /* identifiers */
2552         ot->name = "Join as Shapes";
2553         ot->description = "Merge selected objects to shapes of active object";
2554         ot->idname = "OBJECT_OT_join_shapes";
2555
2556         /* api callbacks */
2557         ot->exec = join_shapes_exec;
2558         ot->poll = join_shapes_poll;
2559
2560         /* flags */
2561         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2562 }