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