c2934b916d0313a30004bac3854e2e57f795b431
[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_group.h"
75 #include "BKE_lamp.h"
76 #include "BKE_lattice.h"
77 #include "BKE_layer.h"
78 #include "BKE_library.h"
79 #include "BKE_library_query.h"
80 #include "BKE_library_remap.h"
81 #include "BKE_key.h"
82 #include "BKE_main.h"
83 #include "BKE_material.h"
84 #include "BKE_mball.h"
85 #include "BKE_mesh.h"
86 #include "BKE_nla.h"
87 #include "BKE_object.h"
88 #include "BKE_particle.h"
89 #include "BKE_report.h"
90 #include "BKE_scene.h"
91 #include "BKE_screen.h"
92 #include "BKE_speaker.h"
93
94 #include "DEG_depsgraph.h"
95 #include "DEG_depsgraph_build.h"
96
97 #include "RNA_access.h"
98 #include "RNA_define.h"
99 #include "RNA_enum_types.h"
100
101 #include "WM_api.h"
102 #include "WM_types.h"
103
104 #include "ED_armature.h"
105 #include "ED_curve.h"
106 #include "ED_mball.h"
107 #include "ED_mesh.h"
108 #include "ED_node.h"
109 #include "ED_object.h"
110 #include "ED_physics.h"
111 #include "ED_render.h"
112 #include "ED_screen.h"
113 #include "ED_transform.h"
114 #include "ED_view3d.h"
115
116 #include "UI_resources.h"
117
118 #include "object_intern.h"
119
120 /* this is an exact copy of the define in rna_lamp.c
121  * kept here because of linking order.
122  * Icons are only defined here */
123 const EnumPropertyItem rna_enum_lamp_type_items[] = {
124         {LA_LOCAL, "POINT", ICON_LAMP_POINT, "Point", "Omnidirectional point light source"},
125         {LA_SUN, "SUN", ICON_LAMP_SUN, "Sun", "Constant direction parallel ray light source"},
126         {LA_SPOT, "SPOT", ICON_LAMP_SPOT, "Spot", "Directional cone light source"},
127         {LA_HEMI, "HEMI", ICON_LAMP_HEMI, "Hemi", "180 degree constant light source"},
128         {LA_AREA, "AREA", ICON_LAMP_AREA, "Area", "Directional area light source"},
129         {0, NULL, 0, NULL, NULL}
130 };
131
132 /* copy from rna_object_force.c */
133 static const EnumPropertyItem field_type_items[] = {
134         {PFIELD_FORCE, "FORCE", ICON_FORCE_FORCE, "Force", ""},
135         {PFIELD_WIND, "WIND", ICON_FORCE_WIND, "Wind", ""},
136         {PFIELD_VORTEX, "VORTEX", ICON_FORCE_VORTEX, "Vortex", ""},
137         {PFIELD_MAGNET, "MAGNET", ICON_FORCE_MAGNETIC, "Magnetic", ""},
138         {PFIELD_HARMONIC, "HARMONIC", ICON_FORCE_HARMONIC, "Harmonic", ""},
139         {PFIELD_CHARGE, "CHARGE", ICON_FORCE_CHARGE, "Charge", ""},
140         {PFIELD_LENNARDJ, "LENNARDJ", ICON_FORCE_LENNARDJONES, "Lennard-Jones", ""},
141         {PFIELD_TEXTURE, "TEXTURE", ICON_FORCE_TEXTURE, "Texture", ""},
142         {PFIELD_GUIDE, "GUIDE", ICON_FORCE_CURVE, "Curve Guide", ""},
143         {PFIELD_BOID, "BOID", ICON_FORCE_BOID, "Boid", ""},
144         {PFIELD_TURBULENCE, "TURBULENCE", ICON_FORCE_TURBULENCE, "Turbulence", ""},
145         {PFIELD_DRAG, "DRAG", ICON_FORCE_DRAG, "Drag", ""},
146         {PFIELD_SMOKEFLOW, "SMOKE", ICON_FORCE_SMOKEFLOW, "Smoke Flow", ""},
147         {0, NULL, 0, NULL, NULL}
148 };
149
150 static EnumPropertyItem lightprobe_type_items[] = {
151         {LIGHTPROBE_TYPE_CUBE, "SPHERE", ICON_MESH_UVSPHERE, "Reflection Cubemap",
152      "Reflection probe with spherical or cubic attenuation"},
153         {LIGHTPROBE_TYPE_PLANAR, "PLANAR", ICON_MESH_PLANE, "Reflection Plane",
154      "Planar reflection probe"},
155         {LIGHTPROBE_TYPE_GRID, "GRID", ICON_MESH_GRID, "Irradiance Volume",
156      "Irradiance probe to capture diffuse indirect lighting"},
157         {0, NULL, 0, NULL, NULL}
158 };
159
160 /************************** Exported *****************************/
161
162 void ED_object_location_from_view(bContext *C, float loc[3])
163 {
164         View3D *v3d = CTX_wm_view3d(C);
165         Scene *scene = CTX_data_scene(C);
166         const float *cursor;
167
168         cursor = ED_view3d_cursor3d_get(scene, v3d);
169
170         copy_v3_v3(loc, cursor);
171 }
172
173 void ED_object_rotation_from_quat(float rot[3], const float viewquat[4], const char align_axis)
174 {
175         BLI_assert(align_axis >= 'X' && align_axis <= 'Z');
176
177         switch (align_axis) {
178                 case 'X':
179                 {
180                         /* Same as 'rv3d->viewinv[1]' */
181                         float axis_y[4] = {0.0f, 1.0f, 0.0f};
182                         float quat_y[4], quat[4];
183                         axis_angle_to_quat(quat_y, axis_y, M_PI_2);
184                         mul_qt_qtqt(quat, viewquat, quat_y);
185                         quat_to_eul(rot, quat);
186                         break;
187                 }
188                 case 'Y':
189                 {
190                         quat_to_eul(rot, viewquat);
191                         rot[0] -= (float)M_PI_2;
192                         break;
193                 }
194                 case 'Z':
195                 {
196                         quat_to_eul(rot, viewquat);
197                         break;
198                 }
199         }
200 }
201
202 void ED_object_rotation_from_view(bContext *C, float rot[3], const char align_axis)
203 {
204         RegionView3D *rv3d = CTX_wm_region_view3d(C);
205         BLI_assert(align_axis >= 'X' && align_axis <= 'Z');
206         if (rv3d) {
207                 float viewquat[4];
208                 copy_qt_qt(viewquat, rv3d->viewquat);
209                 viewquat[0] *= -1.0f;
210                 ED_object_rotation_from_quat(rot, viewquat, align_axis);
211         }
212         else {
213                 zero_v3(rot);
214         }
215 }
216
217 void ED_object_base_init_transform(bContext *C, Base *base, const float loc[3], const float rot[3])
218 {
219         Object *ob = base->object;
220         Scene *scene = CTX_data_scene(C);
221         Depsgraph *depsgraph = CTX_data_depsgraph(C);
222
223         if (!scene) return;
224
225         if (loc)
226                 copy_v3_v3(ob->loc, loc);
227
228         if (rot)
229                 copy_v3_v3(ob->rot, rot);
230
231         BKE_object_where_is_calc(depsgraph, scene, ob);
232 }
233
234 /* Uses context to figure out transform for primitive.
235  * Returns standard diameter. */
236 float ED_object_new_primitive_matrix(
237         bContext *C, Object *obedit,
238         const float loc[3], const float rot[3], float primmat[4][4])
239 {
240         Scene *scene = CTX_data_scene(C);
241         View3D *v3d = CTX_wm_view3d(C);
242         float mat[3][3], rmat[3][3], cmat[3][3], imat[3][3];
243
244         unit_m4(primmat);
245
246         eul_to_mat3(rmat, rot);
247         invert_m3(rmat);
248
249         /* inverse transform for initial rotation and object */
250         copy_m3_m4(mat, obedit->obmat);
251         mul_m3_m3m3(cmat, rmat, mat);
252         invert_m3_m3(imat, cmat);
253         copy_m4_m3(primmat, imat);
254
255         /* center */
256         copy_v3_v3(primmat[3], loc);
257         sub_v3_v3v3(primmat[3], primmat[3], obedit->obmat[3]);
258         invert_m3_m3(imat, mat);
259         mul_m3_v3(imat, primmat[3]);
260
261         {
262                 const float dia = v3d ? ED_view3d_grid_scale(scene, v3d, NULL) : ED_scene_grid_scale(scene, NULL);
263                 return dia;
264         }
265
266         // return 1.0f;
267 }
268
269 /********************* Add Object Operator ********************/
270
271 static void view_align_update(struct Main *UNUSED(main), struct Scene *UNUSED(scene), struct PointerRNA *ptr)
272 {
273         RNA_struct_idprops_unset(ptr, "rotation");
274 }
275
276 void ED_object_add_unit_props(wmOperatorType *ot)
277 {
278         RNA_def_float_distance(ot->srna, "radius", 1.0f, 0.0, OBJECT_ADD_SIZE_MAXF, "Radius", "", 0.001, 100.00);
279 }
280
281 void ED_object_add_generic_props(wmOperatorType *ot, bool do_editmode)
282 {
283         PropertyRNA *prop;
284
285         /* note: this property gets hidden for add-camera operator */
286         prop = RNA_def_boolean(ot->srna, "view_align", 0, "Align to View", "Align the new object to the view");
287         RNA_def_property_update_runtime(prop, view_align_update);
288
289         if (do_editmode) {
290                 prop = RNA_def_boolean(ot->srna, "enter_editmode", 0, "Enter Editmode",
291                                        "Enter editmode when adding this object");
292                 RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
293         }
294
295         prop = RNA_def_float_vector_xyz(ot->srna, "location", 3, NULL, -OBJECT_ADD_SIZE_MAXF, OBJECT_ADD_SIZE_MAXF,
296                                         "Location", "Location for the newly added object", -1000.0f, 1000.0f);
297         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
298         prop = RNA_def_float_rotation(ot->srna, "rotation", 3, NULL, -OBJECT_ADD_SIZE_MAXF, OBJECT_ADD_SIZE_MAXF,
299                                       "Rotation", "Rotation for the newly added object",
300                                       DEG2RADF(-360.0f), DEG2RADF(360.0f));
301         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
302
303         prop = RNA_def_boolean_layer_member(ot->srna, "layers", 20, NULL, "Layer", "");
304         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
305 }
306
307 void ED_object_add_mesh_props(wmOperatorType *ot)
308 {
309         RNA_def_boolean(ot->srna, "calc_uvs", false, "Generate UVs", "Generate a default UV map");
310 }
311
312 bool ED_object_add_generic_get_opts(bContext *C, wmOperator *op, const char view_align_axis,
313                                     float loc[3], float rot[3],
314                                     bool *enter_editmode, unsigned int *layer, bool *is_view_aligned)
315 {
316         View3D *v3d = CTX_wm_view3d(C);
317         unsigned int _layer;
318         PropertyRNA *prop;
319
320         /* Switch to Edit mode? optional prop */
321         if ((prop = RNA_struct_find_property(op->ptr, "enter_editmode"))) {
322                 bool _enter_editmode;
323                 if (!enter_editmode)
324                         enter_editmode = &_enter_editmode;
325
326                 if (RNA_property_is_set(op->ptr, prop) && enter_editmode)
327                         *enter_editmode = RNA_property_boolean_get(op->ptr, prop);
328                 else {
329                         *enter_editmode = (U.flag & USER_ADD_EDITMODE) != 0;
330                         RNA_property_boolean_set(op->ptr, prop, *enter_editmode);
331                 }
332         }
333
334         /* Get layers! */
335         {
336                 int a, layer_values[20];
337                 if (!layer)
338                         layer = &_layer;
339
340                 prop = RNA_struct_find_property(op->ptr, "layers");
341                 if (RNA_property_is_set(op->ptr, prop)) {
342                         RNA_property_boolean_get_array(op->ptr, prop, layer_values);
343                         *layer = 0;
344                         for (a = 0; a < 20; a++) {
345                                 if (layer_values[a])
346                                         *layer |= (1 << a);
347                         }
348                 }
349                 else {
350                         Scene *scene = CTX_data_scene(C);
351                         *layer = BKE_screen_view3d_layer_active_ex(v3d, scene, false);
352                         for (a = 0; a < 20; a++) {
353                                 layer_values[a] = (*layer & (1 << a)) != 0;
354                         }
355                         RNA_property_boolean_set_array(op->ptr, prop, layer_values);
356                 }
357
358                 /* in local view we additionally add local view layers,
359                  * not part of operator properties */
360                 if (v3d && v3d->localvd)
361                         *layer |= v3d->lay;
362         }
363
364         /* Location! */
365         {
366                 float _loc[3];
367                 if (!loc)
368                         loc = _loc;
369
370                 if (RNA_struct_property_is_set(op->ptr, "location")) {
371                         RNA_float_get_array(op->ptr, "location", loc);
372                 }
373                 else {
374                         ED_object_location_from_view(C, loc);
375                         RNA_float_set_array(op->ptr, "location", loc);
376                 }
377         }
378
379         /* Rotation! */
380         {
381                 bool _is_view_aligned;
382                 float _rot[3];
383                 if (!is_view_aligned)
384                         is_view_aligned = &_is_view_aligned;
385                 if (!rot)
386                         rot = _rot;
387
388                 if (RNA_struct_property_is_set(op->ptr, "rotation"))
389                         *is_view_aligned = false;
390                 else if (RNA_struct_property_is_set(op->ptr, "view_align"))
391                         *is_view_aligned = RNA_boolean_get(op->ptr, "view_align");
392                 else {
393                         *is_view_aligned = (U.flag & USER_ADD_VIEWALIGNED) != 0;
394                         RNA_boolean_set(op->ptr, "view_align", *is_view_aligned);
395                 }
396
397                 if (*is_view_aligned) {
398                         ED_object_rotation_from_view(C, rot, view_align_axis);
399                         RNA_float_set_array(op->ptr, "rotation", rot);
400                 }
401                 else
402                         RNA_float_get_array(op->ptr, "rotation", rot);
403         }
404
405         if (layer && *layer == 0) {
406                 BKE_report(op->reports, RPT_ERROR, "Property 'layer' has no values set");
407                 return false;
408         }
409
410         return true;
411 }
412
413 /* For object add primitive operators.
414  * Do not call undo push in this function (users of this function have to). */
415 Object *ED_object_add_type(
416         bContext *C,
417         int type, const char *name,
418         const float loc[3], const float rot[3],
419         bool enter_editmode, unsigned int UNUSED(layer))
420 {
421         Main *bmain = CTX_data_main(C);
422         Scene *scene = CTX_data_scene(C);
423         ViewLayer *view_layer = CTX_data_view_layer(C);
424         Object *ob;
425
426         /* for as long scene has editmode... */
427         if (CTX_data_edit_object(C)) 
428                 ED_object_editmode_exit(C, EM_FREEDATA | EM_WAITCURSOR | EM_DO_UNDO);  /* freedata, and undo */
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 Group Instance Operator ********************/
1051
1052 static int group_instance_add_exec(bContext *C, wmOperator *op)
1053 {
1054         Group *group;
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                 group = (Group *)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                 group = BLI_findlink(&CTX_data_main(C)->group, RNA_enum_get(op->ptr, "group"));
1076
1077         if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, NULL, &layer, NULL))
1078                 return OPERATOR_CANCELLED;
1079
1080         if (group) {
1081                 Main *bmain = CTX_data_main(C);
1082                 Scene *scene = CTX_data_scene(C);
1083                 Object *ob = ED_object_add_type(C, OB_EMPTY, group->id.name + 2, loc, rot, false, layer);
1084                 ob->dup_group = group;
1085                 ob->transflag |= OB_DUPLIGROUP;
1086                 id_us_plus(&group->id);
1087
1088                 /* works without this except if you try render right after, see: 22027 */
1089                 DEG_relations_tag_update(bmain);
1090                 DEG_id_tag_update(&group->id, 0);
1091
1092                 WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
1093
1094                 return OPERATOR_FINISHED;
1095         }
1096
1097         return OPERATOR_CANCELLED;
1098 }
1099
1100 /* only used as menu */
1101 void OBJECT_OT_group_instance_add(wmOperatorType *ot)
1102 {
1103         PropertyRNA *prop;
1104
1105         /* identifiers */
1106         ot->name = "Add Group Instance";
1107         ot->description = "Add a dupligroup instance";
1108         ot->idname = "OBJECT_OT_group_instance_add";
1109
1110         /* api callbacks */
1111         ot->invoke = WM_enum_search_invoke;
1112         ot->exec = group_instance_add_exec;
1113         ot->poll = ED_operator_objectmode;
1114
1115         /* flags */
1116         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1117
1118         /* properties */
1119         RNA_def_string(ot->srna, "name", "Group", MAX_ID_NAME - 2, "Name", "Group name to add");
1120         prop = RNA_def_enum(ot->srna, "group", DummyRNA_NULL_items, 0, "Group", "");
1121         RNA_def_enum_funcs(prop, RNA_group_itemf);
1122         RNA_def_property_flag(prop, PROP_ENUM_NO_TRANSLATE);
1123         ot->prop = prop;
1124         ED_object_add_generic_props(ot, false);
1125 }
1126
1127 /********************* Add Speaker Operator ********************/
1128
1129 static int object_speaker_add_exec(bContext *C, wmOperator *op)
1130 {
1131         Object *ob;
1132         unsigned int layer;
1133         float loc[3], rot[3];
1134         Scene *scene = CTX_data_scene(C);
1135
1136         if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, NULL, &layer, NULL))
1137                 return OPERATOR_CANCELLED;
1138
1139         ob = ED_object_add_type(C, OB_SPEAKER, NULL, loc, rot, false, layer);
1140
1141         /* to make it easier to start using this immediately in NLA, a default sound clip is created
1142          * ready to be moved around to retime the sound and/or make new sound clips
1143          */
1144         {
1145                 /* create new data for NLA hierarchy */
1146                 AnimData *adt = BKE_animdata_add_id(&ob->id);
1147                 NlaTrack *nlt = BKE_nlatrack_add(adt, NULL);
1148                 NlaStrip *strip = BKE_nla_add_soundstrip(scene, ob->data);
1149                 strip->start = CFRA;
1150                 strip->end += strip->start;
1151
1152                 /* hook them up */
1153                 BKE_nlatrack_add_strip(nlt, strip);
1154
1155                 /* auto-name the strip, and give the track an interesting name  */
1156                 BLI_strncpy(nlt->name, DATA_("SoundTrack"), sizeof(nlt->name));
1157                 BKE_nlastrip_validate_name(adt, strip);
1158
1159                 WM_event_add_notifier(C, NC_ANIMATION | ND_NLA | NA_EDITED, NULL);
1160         }
1161
1162         return OPERATOR_FINISHED;
1163 }
1164
1165 void OBJECT_OT_speaker_add(wmOperatorType *ot)
1166 {
1167         /* identifiers */
1168         ot->name = "Add Speaker";
1169         ot->description = "Add a speaker object to the scene";
1170         ot->idname = "OBJECT_OT_speaker_add";
1171
1172         /* api callbacks */
1173         ot->exec = object_speaker_add_exec;
1174         ot->poll = ED_operator_objectmode;
1175
1176         /* flags */
1177         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1178
1179         ED_object_add_generic_props(ot, true);
1180 }
1181
1182 /**************************** Delete Object *************************/
1183
1184 /* remove base from a specific scene */
1185 /* note: now unlinks constraints as well */
1186 void ED_object_base_free_and_unlink(Main *bmain, Scene *scene, Object *ob)
1187 {
1188         if (BKE_library_ID_is_indirectly_used(bmain, ob) &&
1189             ID_REAL_USERS(ob) <= 1 && ID_EXTRA_USERS(ob) == 0)
1190         {
1191                 /* We cannot delete indirectly used object... */
1192                 printf("WARNING, undeletable object '%s', should have been catched before reaching this function!",
1193                        ob->id.name + 2);
1194                 return;
1195         }
1196
1197         DEG_id_tag_update_ex(bmain, &ob->id, DEG_TAG_BASE_FLAGS_UPDATE);
1198
1199         BKE_collections_object_remove(bmain, &scene->id, ob, true);
1200 }
1201
1202 static int object_delete_exec(bContext *C, wmOperator *op)
1203 {
1204         Main *bmain = CTX_data_main(C);
1205         Scene *scene = CTX_data_scene(C);
1206         wmWindowManager *wm = CTX_wm_manager(C);
1207         wmWindow *win;
1208         const bool use_global = RNA_boolean_get(op->ptr, "use_global");
1209         bool changed = false;
1210
1211         if (CTX_data_edit_object(C)) 
1212                 return OPERATOR_CANCELLED;
1213
1214         CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
1215         {
1216                 const bool is_indirectly_used = BKE_library_ID_is_indirectly_used(bmain, ob);
1217                 if (ob->id.tag & LIB_TAG_INDIRECT) {
1218                         /* Can this case ever happen? */
1219                         BKE_reportf(op->reports, RPT_WARNING, "Cannot delete indirectly linked object '%s'", ob->id.name + 2);
1220                         continue;
1221                 }
1222                 else if (is_indirectly_used && ID_REAL_USERS(ob) <= 1 && ID_EXTRA_USERS(ob) == 0) {
1223                         BKE_reportf(op->reports, RPT_WARNING,
1224                                 "Cannot delete object '%s' from scene '%s', indirectly used objects need at least one user",
1225                                 ob->id.name + 2, scene->id.name + 2);
1226                         continue;
1227                 }
1228
1229                 /* This is sort of a quick hack to address T51243 - Proper thing to do here would be to nuke most of all this
1230                  * custom scene/object/base handling, and use generic lib remap/query for that.
1231                  * But this is for later (aka 2.8, once layers & co are settled and working).
1232                  */
1233                 if (use_global && ob->id.lib == NULL) {
1234                         /* We want to nuke the object, let's nuke it the easy way (not for linked data though)... */
1235                         BKE_libblock_delete(bmain, &ob->id);
1236                         changed = true;
1237                         continue;
1238                 }
1239
1240                 /* remove from Grease Pencil parent */
1241                 /* XXX This is likely not correct? Will also remove parent from grease pencil from other scenes,
1242                  *     even when use_global is false... */
1243                 for (bGPdata *gpd = bmain->gpencil.first; gpd; gpd = gpd->id.next) {
1244                         for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
1245                                 if (gpl->parent != NULL) {
1246                                         if (gpl->parent == ob) {
1247                                                 gpl->parent = NULL;
1248                                         }
1249                                 }
1250                         }
1251                 }
1252
1253                 /* remove from current scene only */
1254                 ED_object_base_free_and_unlink(bmain, scene, ob);
1255                 changed = true;
1256
1257                 if (use_global) {
1258                         Scene *scene_iter;
1259                         for (scene_iter = bmain->scene.first; scene_iter; scene_iter = scene_iter->id.next) {
1260                                 if (scene_iter != scene && !ID_IS_LINKED(scene_iter)) {
1261                                         if (is_indirectly_used && ID_REAL_USERS(ob) <= 1 && ID_EXTRA_USERS(ob) == 0) {
1262                                                 BKE_reportf(op->reports, RPT_WARNING,
1263                                                             "Cannot delete object '%s' from scene '%s', indirectly used objects need at least one user",
1264                                                             ob->id.name + 2, scene_iter->id.name + 2);
1265                                                 break;
1266                                         }
1267                                         ED_object_base_free_and_unlink(bmain, scene_iter, ob);
1268                                 }
1269                         }
1270                 }
1271                 /* end global */
1272         }
1273         CTX_DATA_END;
1274
1275         if (!changed)
1276                 return OPERATOR_CANCELLED;
1277
1278         /* delete has to handle all open scenes */
1279         BKE_main_id_tag_listbase(&bmain->scene, LIB_TAG_DOIT, true);
1280         for (win = wm->windows.first; win; win = win->next) {
1281                 scene = WM_window_get_active_scene(win);
1282
1283                 if (scene->id.tag & LIB_TAG_DOIT) {
1284                         scene->id.tag &= ~LIB_TAG_DOIT;
1285                         
1286                         DEG_relations_tag_update(bmain);
1287
1288                         WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
1289                         WM_event_add_notifier(C, NC_SCENE | ND_LAYER_CONTENT, scene);
1290                 }
1291         }
1292
1293         return OPERATOR_FINISHED;
1294 }
1295
1296 void OBJECT_OT_delete(wmOperatorType *ot)
1297 {
1298         /* identifiers */
1299         ot->name = "Delete";
1300         ot->description = "Delete selected objects";
1301         ot->idname = "OBJECT_OT_delete";
1302
1303         /* api callbacks */
1304         ot->invoke = WM_operator_confirm;
1305         ot->exec = object_delete_exec;
1306         ot->poll = ED_operator_objectmode;
1307
1308         /* flags */
1309         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1310
1311         RNA_def_boolean(ot->srna, "use_global", 0, "Delete Globally", "Remove object from all scenes");
1312 }
1313
1314 /**************************** Copy Utilities ******************************/
1315
1316 /* after copying objects, copied data should get new pointers */
1317 static void copy_object_set_idnew(bContext *C)
1318 {
1319         Main *bmain = CTX_data_main(C);
1320
1321         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1322         {
1323                 BKE_libblock_relink_to_newid(&ob->id);
1324         }
1325         CTX_DATA_END;
1326
1327         BKE_main_id_clear_newpoins(bmain);
1328 }
1329
1330 /********************* Make Duplicates Real ************************/
1331
1332 /**
1333  * \note regarding hashing dupli-objects when using OB_DUPLIGROUP, skip the first member of #DupliObject.persistent_id
1334  * since its a unique index and we only want to know if the group objects are from the same dupli-group instance.
1335  */
1336 static unsigned int dupliobject_group_hash(const void *ptr)
1337 {
1338         const DupliObject *dob = ptr;
1339         unsigned int hash = BLI_ghashutil_ptrhash(dob->ob);
1340         unsigned int i;
1341         for (i = 1; (i < MAX_DUPLI_RECUR) && dob->persistent_id[i] != INT_MAX; i++) {
1342                 hash ^= (dob->persistent_id[i] ^ i);
1343         }
1344         return hash;
1345 }
1346
1347 /**
1348  * \note regarding hashing dupli-objects when NOT using OB_DUPLIGROUP, include the first member of #DupliObject.persistent_id
1349  * since its the index of the vertex/face the object is instantiated on and we want to identify objects on the same vertex/face.
1350  */
1351 static unsigned int dupliobject_hash(const void *ptr)
1352 {
1353         const DupliObject *dob = ptr;
1354         unsigned int hash = BLI_ghashutil_ptrhash(dob->ob);
1355         hash ^= (dob->persistent_id[0] ^ 0);
1356         return hash;
1357 }
1358
1359 /* Compare function that matches dupliobject_group_hash */
1360 static bool dupliobject_group_cmp(const void *a_, const void *b_)
1361 {
1362         const DupliObject *a = a_;
1363         const DupliObject *b = b_;
1364         unsigned int i;
1365
1366         if (a->ob != b->ob) {
1367                 return true;
1368         }
1369
1370         for (i = 1; (i < MAX_DUPLI_RECUR); i++) {
1371                 if (a->persistent_id[i] != b->persistent_id[i]) {
1372                         return true;
1373                 }
1374                 else if (a->persistent_id[i] == INT_MAX) {
1375                         break;
1376                 }
1377         }
1378
1379         /* matching */
1380         return false;
1381 }
1382
1383 /* Compare function that matches dupliobject_hash */
1384 static bool dupliobject_cmp(const void *a_, const void *b_)
1385 {
1386         const DupliObject *a = a_;
1387         const DupliObject *b = b_;
1388
1389         if (a->ob != b->ob) {
1390                 return true;
1391         }
1392
1393         if (a->persistent_id[0] != b->persistent_id[0]) {
1394                 return true;
1395         }
1396
1397         /* matching */
1398         return false;
1399 }
1400
1401 static void make_object_duplilist_real(bContext *C, Scene *scene, Base *base,
1402                                        const bool use_base_parent,
1403                                        const bool use_hierarchy)
1404 {
1405         Main *bmain = CTX_data_main(C);
1406         ViewLayer *view_layer = CTX_data_view_layer(C);
1407         Depsgraph *depsgraph = CTX_data_depsgraph(C);
1408         ListBase *lb_duplis;
1409         DupliObject *dob;
1410         GHash *dupli_gh, *parent_gh = NULL;
1411
1412         if (!(base->object->transflag & OB_DUPLI)) {
1413                 return;
1414         }
1415
1416         lb_duplis = object_duplilist(depsgraph, scene, base->object);
1417
1418         dupli_gh = BLI_ghash_ptr_new(__func__);
1419         if (use_hierarchy) {
1420                 if (base->object->transflag & OB_DUPLIGROUP) {
1421                         parent_gh = BLI_ghash_new(dupliobject_group_hash, dupliobject_group_cmp, __func__);
1422                 }
1423                 else {
1424                         parent_gh = BLI_ghash_new(dupliobject_hash, dupliobject_cmp, __func__);
1425                 }
1426         }
1427
1428         for (dob = lb_duplis->first; dob; dob = dob->next) {
1429                 Object *ob_src = dob->ob;
1430                 Object *ob_dst = ID_NEW_SET(dob->ob, BKE_object_copy(bmain, ob_src));
1431                 Base *base_dst;
1432
1433                 /* font duplis can have a totcol without material, we get them from parent
1434                  * should be implemented better...
1435                  */
1436                 if (ob_dst->mat == NULL) {
1437                         ob_dst->totcol = 0;
1438                 }
1439
1440                 BKE_collection_object_add_from(scene, base->object, ob_dst);
1441                 base_dst = BKE_view_layer_base_find(view_layer, ob_dst);
1442                 BLI_assert(base_dst != NULL);
1443
1444                 BKE_scene_object_base_flag_sync_from_base(base_dst);
1445
1446                 /* make sure apply works */
1447                 BKE_animdata_free(&ob_dst->id, true);
1448                 ob_dst->adt = NULL;
1449
1450                 /* Proxies are not to be copied. */
1451                 ob_dst->proxy_from = NULL;
1452                 ob_dst->proxy_group = NULL;
1453                 ob_dst->proxy = NULL;
1454
1455                 ob_dst->parent = NULL;
1456                 BKE_constraints_free(&ob_dst->constraints);
1457                 ob_dst->curve_cache = NULL;
1458                 ob_dst->transflag &= ~OB_DUPLI;
1459
1460                 copy_m4_m4(ob_dst->obmat, dob->mat);
1461                 BKE_object_apply_mat4(ob_dst, ob_dst->obmat, false, false);
1462
1463                 BLI_ghash_insert(dupli_gh, dob, ob_dst);
1464                 if (parent_gh) {
1465                         void **val;
1466                         /* Due to nature of hash/comparison of this ghash, a lot of duplis may be considered as 'the same',
1467                          * this avoids trying to insert same key several time and raise asserts in debug builds... */
1468                         if (!BLI_ghash_ensure_p(parent_gh, dob, &val)) {
1469                                 *val = ob_dst;
1470                         }
1471                 }
1472         }
1473
1474         for (dob = lb_duplis->first; dob; dob = dob->next) {
1475                 Object *ob_src = dob->ob;
1476                 Object *ob_dst = BLI_ghash_lookup(dupli_gh, dob);
1477
1478                 /* Remap new object to itself, and clear again newid pointer of orig object. */
1479                 BKE_libblock_relink_to_newid(&ob_dst->id);
1480
1481                 DEG_id_tag_update(&ob_dst->id, OB_RECALC_DATA);
1482
1483                 if (use_hierarchy) {
1484                         /* original parents */
1485                         Object *ob_src_par = ob_src->parent;
1486                         Object *ob_dst_par = NULL;
1487
1488                         /* find parent that was also made real */
1489                         if (ob_src_par) {
1490                                 /* OK to keep most of the members uninitialized,
1491                                  * they won't be read, this is simply for a hash lookup. */
1492                                 DupliObject dob_key;
1493                                 dob_key.ob = ob_src_par;
1494                                 if (base->object->transflag & OB_DUPLIGROUP) {
1495                                         memcpy(&dob_key.persistent_id[1],
1496                                                &dob->persistent_id[1],
1497                                                sizeof(dob->persistent_id[1]) * (MAX_DUPLI_RECUR - 1));
1498                                 }
1499                                 else {
1500                                         dob_key.persistent_id[0] = dob->persistent_id[0];
1501                                 }
1502                                 ob_dst_par = BLI_ghash_lookup(parent_gh, &dob_key);
1503                         }
1504
1505                         if (ob_dst_par) {
1506                                 /* allow for all possible parent types */
1507                                 ob_dst->partype = ob_src->partype;
1508                                 BLI_strncpy(ob_dst->parsubstr, ob_src->parsubstr, sizeof(ob_dst->parsubstr));
1509                                 ob_dst->par1 = ob_src->par1;
1510                                 ob_dst->par2 = ob_src->par2;
1511                                 ob_dst->par3 = ob_src->par3;
1512
1513                                 copy_m4_m4(ob_dst->parentinv, ob_src->parentinv);
1514
1515                                 ob_dst->parent = ob_dst_par;
1516                         }
1517                         else if (use_base_parent) {
1518                                 ob_dst->parent = base->object;
1519                                 ob_dst->partype = PAROBJECT;
1520                         }
1521                 }
1522                 else if (use_base_parent) {
1523                         /* since we are ignoring the internal hierarchy - parent all to the
1524                          * base object */
1525                         ob_dst->parent = base->object;
1526                         ob_dst->partype = PAROBJECT;
1527                 }
1528
1529                 if (ob_dst->parent) {
1530                         /* note, this may be the parent of other objects, but it should
1531                          * still work out ok */
1532                         BKE_object_apply_mat4(ob_dst, dob->mat, false, true);
1533
1534                         /* to set ob_dst->orig and in case theres any other discrepicies */
1535                         DEG_id_tag_update(&ob_dst->id, OB_RECALC_OB);
1536                 }
1537         }
1538
1539         if (base->object->transflag & OB_DUPLIGROUP && base->object->dup_group) {
1540                 for (Object *ob = bmain->object.first; ob; ob = ob->id.next) {
1541                         if (ob->proxy_group == base->object) {
1542                                 ob->proxy = NULL;
1543                                 ob->proxy_from = NULL;
1544                                 DEG_id_tag_update(&ob->id, OB_RECALC_OB);
1545                         }
1546                 }
1547         }
1548
1549         BLI_ghash_free(dupli_gh, NULL, NULL);
1550         if (parent_gh) {
1551                 BLI_ghash_free(parent_gh, NULL, NULL);
1552         }
1553
1554         free_object_duplilist(lb_duplis);
1555
1556         BKE_main_id_clear_newpoins(bmain);
1557
1558         base->object->transflag &= ~OB_DUPLI;
1559 }
1560
1561 static int object_duplicates_make_real_exec(bContext *C, wmOperator *op)
1562 {
1563         Main *bmain = CTX_data_main(C);
1564         Scene *scene = CTX_data_scene(C);
1565
1566         const bool use_base_parent = RNA_boolean_get(op->ptr, "use_base_parent");
1567         const bool use_hierarchy = RNA_boolean_get(op->ptr, "use_hierarchy");
1568
1569         BKE_main_id_clear_newpoins(bmain);
1570
1571         CTX_DATA_BEGIN (C, Base *, base, selected_editable_bases)
1572         {
1573                 make_object_duplilist_real(C, scene, base, use_base_parent, use_hierarchy);
1574
1575                 /* dependencies were changed */
1576                 WM_event_add_notifier(C, NC_OBJECT | ND_PARENT, base->object);
1577         }
1578         CTX_DATA_END;
1579
1580         DEG_relations_tag_update(bmain);
1581         WM_event_add_notifier(C, NC_SCENE, scene);
1582         WM_main_add_notifier(NC_OBJECT | ND_DRAW, NULL);
1583
1584         return OPERATOR_FINISHED;
1585 }
1586
1587 void OBJECT_OT_duplicates_make_real(wmOperatorType *ot)
1588 {
1589         /* identifiers */
1590         ot->name = "Make Duplicates Real";
1591         ot->description = "Make dupli objects attached to this object real";
1592         ot->idname = "OBJECT_OT_duplicates_make_real";
1593
1594         /* api callbacks */
1595         ot->exec = object_duplicates_make_real_exec;
1596
1597         ot->poll = ED_operator_objectmode;
1598
1599         /* flags */
1600         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1601
1602         RNA_def_boolean(ot->srna, "use_base_parent", 0, "Parent", "Parent newly created objects to the original duplicator");
1603         RNA_def_boolean(ot->srna, "use_hierarchy", 0, "Keep Hierarchy", "Maintain parent child relationships");
1604 }
1605
1606 /**************************** Convert **************************/
1607
1608 static const EnumPropertyItem convert_target_items[] = {
1609         {OB_CURVE, "CURVE", ICON_OUTLINER_OB_CURVE, "Curve from Mesh/Text", ""},
1610         {OB_MESH, "MESH", ICON_OUTLINER_OB_MESH, "Mesh from Curve/Meta/Surf/Text", ""},
1611         {0, NULL, 0, NULL, NULL}
1612 };
1613
1614 static void convert_ensure_curve_cache(Depsgraph *depsgraph, Scene *scene, Object *ob)
1615 {
1616         if (ob->curve_cache == NULL) {
1617                 /* Force creation. This is normally not needed but on operator
1618                  * redo we might end up with an object which isn't evaluated yet.
1619                  */
1620                 if (ELEM(ob->type, OB_SURF, OB_CURVE, OB_FONT)) {
1621                         BKE_displist_make_curveTypes(depsgraph, scene, ob, false);
1622                 }
1623                 else if (ob->type == OB_MBALL) {
1624                         BKE_displist_make_mball(depsgraph, scene, ob);
1625                 }
1626         }
1627 }
1628
1629 static void curvetomesh(Depsgraph *depsgraph, Scene *scene, Object *ob)
1630 {
1631         convert_ensure_curve_cache(depsgraph, scene, ob);
1632         BKE_mesh_from_nurbs(ob); /* also does users */
1633
1634         if (ob->type == OB_MESH) {
1635                 BKE_object_free_modifiers(ob, 0);
1636         }
1637 }
1638
1639 static int convert_poll(bContext *C)
1640 {
1641         Scene *scene = CTX_data_scene(C);
1642         Base *base_act = CTX_data_active_base(C);
1643         Object *obact = base_act ? base_act->object : NULL;
1644
1645         return (!ID_IS_LINKED(scene) && obact && (BKE_object_is_in_editmode(obact) == false) &&
1646                 (base_act->flag & BASE_SELECTED) && !ID_IS_LINKED(obact));
1647 }
1648
1649 /* Helper for convert_exec */
1650 static Base *duplibase_for_convert(Main *bmain, Scene *scene, ViewLayer *view_layer, Base *base, Object *ob)
1651 {
1652         Object *obn;
1653         Base *basen;
1654
1655         if (ob == NULL) {
1656                 ob = base->object;
1657         }
1658
1659         obn = BKE_object_copy(bmain, ob);
1660         DEG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1661         BKE_collection_object_add_from(scene, ob, obn);
1662
1663         basen = BKE_view_layer_base_find(view_layer, obn);
1664         ED_object_base_select(basen, BA_SELECT);
1665         ED_object_base_select(basen, BA_DESELECT);
1666         return basen;
1667 }
1668
1669 static int convert_exec(bContext *C, wmOperator *op)
1670 {
1671         Main *bmain = CTX_data_main(C);
1672         Depsgraph *depsgraph = CTX_data_depsgraph(C);
1673         Scene *scene = CTX_data_scene(C);
1674         ViewLayer *view_layer = CTX_data_view_layer(C);
1675         Base *basen = NULL, *basact = NULL;
1676         Object *ob1, *newob, *obact = CTX_data_active_object(C);
1677         DerivedMesh *dm;
1678         Curve *cu;
1679         Nurb *nu;
1680         MetaBall *mb;
1681         Mesh *me;
1682         const short target = RNA_enum_get(op->ptr, "target");
1683         bool keep_original = RNA_boolean_get(op->ptr, "keep_original");
1684         int a, mballConverted = 0;
1685
1686         /* don't forget multiple users! */
1687
1688         {
1689                 FOREACH_SCENE_OBJECT_BEGIN(scene, ob)
1690                 {
1691                         ob->flag &= ~OB_DONE;
1692
1693                         /* flag data thats not been edited (only needed for !keep_original) */
1694                         if (ob->data) {
1695                                 ((ID *)ob->data)->tag |= LIB_TAG_DOIT;
1696                         }
1697
1698                         /* possible metaball basis is not in this scene */
1699                         if (ob->type == OB_MBALL && target == OB_MESH) {
1700                                 if (BKE_mball_is_basis(ob) == false) {
1701                                         Object *ob_basis;
1702                                         ob_basis = BKE_mball_basis_find(scene, ob);
1703                                         if (ob_basis) {
1704                                                 ob_basis->flag &= ~OB_DONE;
1705                                         }
1706                                 }
1707                         }
1708                 }
1709                 FOREACH_SCENE_OBJECT_END;
1710         }
1711
1712         ListBase selected_editable_bases = CTX_data_collection_get(C, "selected_editable_bases");
1713
1714         /* Ensure we get all meshes calculated with a sufficient data-mask,
1715          * needed since re-evaluating single modifiers causes bugs if they depend
1716          * on other objects data masks too, see: T50950. */
1717         {
1718                 for (CollectionPointerLink *link = selected_editable_bases.first; link; link = link->next) {
1719                         Base *base = link->ptr.data;
1720                         Object *ob = base->object;
1721
1722                         /* The way object type conversion works currently (enforcing conversion of *all* objetcs using converted
1723                          * obdata, even some un-selected/hidden/inother scene ones, sounds totally bad to me.
1724                          * However, changing this is more design than bugfix, not to mention convoluted code below,
1725                          * so that will be for later.
1726                          * But at the very least, do not do that with linked IDs! */
1727                         if ((ID_IS_LINKED(ob) || (ob->data && ID_IS_LINKED(ob->data))) && !keep_original) {
1728                                 keep_original = true;
1729                                 BKE_reportf(op->reports, RPT_INFO,
1730                                             "Converting some linked object/object data, enforcing 'Keep Original' option to True");
1731                         }
1732
1733                         DEG_id_tag_update(&base->object->id, OB_RECALC_DATA);
1734                 }
1735
1736                 uint64_t customdata_mask_prev = scene->customdata_mask;
1737                 scene->customdata_mask |= CD_MASK_MESH;
1738                 BKE_scene_graph_update_tagged(depsgraph, bmain);
1739                 scene->customdata_mask = customdata_mask_prev;
1740         }
1741
1742         for (CollectionPointerLink *link = selected_editable_bases.first; link; link = link->next) {
1743                 Base *base = link->ptr.data;
1744                 Object *ob = base->object;
1745
1746                 if (ob->flag & OB_DONE || !IS_TAGGED(ob->data)) {
1747                         if (ob->type != target) {
1748                                 base->flag &= ~SELECT;
1749                                 ob->flag &= ~SELECT;
1750                         }
1751
1752                         /* obdata already modified */
1753                         if (!IS_TAGGED(ob->data)) {
1754                                 /* When 2 objects with linked data are selected, converting both
1755                                  * would keep modifiers on all but the converted object [#26003] */
1756                                 if (ob->type == OB_MESH) {
1757                                         BKE_object_free_modifiers(ob, 0);  /* after derivedmesh calls! */
1758                                 }
1759                         }
1760                 }
1761                 else if (ob->type == OB_MESH && target == OB_CURVE) {
1762                         ob->flag |= OB_DONE;
1763
1764                         if (keep_original) {
1765                                 basen = duplibase_for_convert(bmain, scene, view_layer, base, NULL);
1766                                 newob = basen->object;
1767
1768                                 /* decrement original mesh's usage count  */
1769                                 me = newob->data;
1770                                 id_us_min(&me->id);
1771
1772                                 /* make a new copy of the mesh */
1773                                 newob->data = BKE_mesh_copy(bmain, me);
1774                         }
1775                         else {
1776                                 newob = ob;
1777                         }
1778
1779                         BKE_mesh_to_curve(depsgraph, scene, newob);
1780
1781                         if (newob->type == OB_CURVE) {
1782                                 BKE_object_free_modifiers(newob, 0);   /* after derivedmesh calls! */
1783                                 ED_rigidbody_object_remove(bmain, scene, newob);
1784                         }
1785                 }
1786                 else if (ob->type == OB_MESH) {
1787                         ob->flag |= OB_DONE;
1788
1789                         if (keep_original) {
1790                                 basen = duplibase_for_convert(bmain, scene, view_layer, base, NULL);
1791                                 newob = basen->object;
1792
1793                                 /* decrement original mesh's usage count  */
1794                                 me = newob->data;
1795                                 id_us_min(&me->id);
1796
1797                                 /* make a new copy of the mesh */
1798                                 newob->data = BKE_mesh_copy(bmain, me);
1799                         }
1800                         else {
1801                                 newob = ob;
1802                                 DEG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1803                         }
1804
1805                         /* make new mesh data from the original copy */
1806                         /* note: get the mesh from the original, not from the copy in some
1807                          * cases this doesnt give correct results (when MDEF is used for eg)
1808                          */
1809                         dm = mesh_get_derived_final(depsgraph, scene, newob, CD_MASK_MESH);
1810
1811                         DM_to_mesh(dm, newob->data, newob, CD_MASK_MESH, true);
1812
1813                         /* re-tessellation is called by DM_to_mesh */
1814
1815                         BKE_object_free_modifiers(newob, 0);   /* after derivedmesh calls! */
1816                 }
1817                 else if (ob->type == OB_FONT) {
1818                         ob->flag |= OB_DONE;
1819
1820                         if (keep_original) {
1821                                 basen = duplibase_for_convert(bmain, scene, view_layer, base, NULL);
1822                                 newob = basen->object;
1823
1824                                 /* decrement original curve's usage count  */
1825                                 id_us_min(&((Curve *)newob->data)->id);
1826
1827                                 /* make a new copy of the curve */
1828                                 newob->data = BKE_curve_copy(bmain, ob->data);
1829                         }
1830                         else {
1831                                 newob = ob;
1832                         }
1833
1834                         cu = newob->data;
1835
1836                         /* TODO(sergey): Ideally DAG will create nurbs list for a curve data
1837                          *               datablock, but for until we've got granular update
1838                          *               lets take care by selves.
1839                          */
1840                         BKE_vfont_to_curve(bmain, newob, FO_EDIT);
1841
1842                         newob->type = OB_CURVE;
1843                         cu->type = OB_CURVE;
1844
1845                         if (cu->vfont) {
1846                                 id_us_min(&cu->vfont->id);
1847                                 cu->vfont = NULL;
1848                         }
1849                         if (cu->vfontb) {
1850                                 id_us_min(&cu->vfontb->id);
1851                                 cu->vfontb = NULL;
1852                         }
1853                         if (cu->vfonti) {
1854                                 id_us_min(&cu->vfonti->id);
1855                                 cu->vfonti = NULL;
1856                         }
1857                         if (cu->vfontbi) {
1858                                 id_us_min(&cu->vfontbi->id);
1859                                 cu->vfontbi = NULL;
1860                         }
1861
1862                         if (!keep_original) {
1863                                 /* other users */
1864                                 if (cu->id.us > 1) {
1865                                         for (ob1 = bmain->object.first; ob1; ob1 = ob1->id.next) {
1866                                                 if (ob1->data == ob->data) {
1867                                                         ob1->type = OB_CURVE;
1868                                                         DEG_id_tag_update(&ob1->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1869                                                 }
1870                                         }
1871                                 }
1872                         }
1873
1874                         for (nu = cu->nurb.first; nu; nu = nu->next)
1875                                 nu->charidx = 0;
1876
1877                         cu->flag &= ~CU_3D;
1878                         BKE_curve_curve_dimension_update(cu);
1879
1880                         if (target == OB_MESH) {
1881                                 curvetomesh(depsgraph, scene, newob);
1882
1883                                 /* meshes doesn't use displist */
1884                                 BKE_object_free_curve_cache(newob);
1885                         }
1886                 }
1887                 else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
1888                         ob->flag |= OB_DONE;
1889
1890                         if (target == OB_MESH) {
1891                                 if (keep_original) {
1892                                         basen = duplibase_for_convert(bmain, scene, view_layer, base, NULL);
1893                                         newob = basen->object;
1894
1895                                         /* decrement original curve's usage count  */
1896                                         id_us_min(&((Curve *)newob->data)->id);
1897
1898                                         /* make a new copy of the curve */
1899                                         newob->data = BKE_curve_copy(bmain, ob->data);
1900                                 }
1901                                 else {
1902                                         newob = ob;
1903                                 }
1904
1905                                 curvetomesh(depsgraph, scene, newob);
1906
1907                                 /* meshes doesn't use displist */
1908                                 BKE_object_free_curve_cache(newob);
1909                         }
1910                 }
1911                 else if (ob->type == OB_MBALL && target == OB_MESH) {
1912                         Object *baseob;
1913
1914                         base->flag &= ~BASE_SELECTED;
1915                         ob->base_flag &= ~BASE_SELECTED;
1916
1917                         baseob = BKE_mball_basis_find(scene, ob);
1918
1919                         if (ob != baseob) {
1920                                 /* if motherball is converting it would be marked as done later */
1921                                 ob->flag |= OB_DONE;
1922                         }
1923
1924                         if (!(baseob->flag & OB_DONE)) {
1925                                 baseob->flag |= OB_DONE;
1926
1927                                 basen = duplibase_for_convert(bmain, scene, view_layer, base, baseob);
1928                                 newob = basen->object;
1929
1930                                 mb = newob->data;
1931                                 id_us_min(&mb->id);
1932
1933                                 newob->data = BKE_mesh_add(bmain, "Mesh");
1934                                 newob->type = OB_MESH;
1935
1936                                 me = newob->data;
1937                                 me->totcol = mb->totcol;
1938                                 if (newob->totcol) {
1939                                         me->mat = MEM_dupallocN(mb->mat);
1940                                         for (a = 0; a < newob->totcol; a++) id_us_plus((ID *)me->mat[a]);
1941                                 }
1942
1943                                 convert_ensure_curve_cache(depsgraph, scene, baseob);
1944                                 BKE_mesh_from_metaball(&baseob->curve_cache->disp, newob->data);
1945
1946                                 if (obact->type == OB_MBALL) {
1947                                         basact = basen;
1948                                 }
1949
1950                                 mballConverted = 1;
1951                         }
1952                 }
1953                 else {
1954                         continue;
1955                 }
1956
1957                 /* tag obdata if it was been changed */
1958
1959                 /* If the original object is active then make this object active */
1960                 if (basen) {
1961                         if (ob == obact) {
1962                                 /* store new active base to update BASACT */
1963                                 basact = basen;
1964                         }
1965
1966                         basen = NULL;
1967                 }
1968
1969                 if (!keep_original && (ob->flag & OB_DONE)) {
1970                         DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
1971                         ((ID *)ob->data)->tag &= ~LIB_TAG_DOIT; /* flag not to convert this datablock again */
1972                 }
1973         }
1974         BLI_freelistN(&selected_editable_bases);
1975
1976         if (!keep_original) {
1977                 if (mballConverted) {
1978                         FOREACH_SCENE_OBJECT_BEGIN(scene, ob_mball)
1979                         {
1980                                 if (ob_mball->type == OB_MBALL) {
1981                                         if (ob_mball->flag & OB_DONE) {
1982                                                 Object *ob_basis = NULL;
1983                                                 if (BKE_mball_is_basis(ob_mball) ||
1984                                                     ((ob_basis = BKE_mball_basis_find(scene, ob_mball)) && (ob_basis->flag & OB_DONE)))
1985                                                 {
1986                                                         ED_object_base_free_and_unlink(bmain, scene, ob_mball);
1987                                                 }
1988                                         }
1989                                 }
1990                         }
1991                         FOREACH_SCENE_OBJECT_END;
1992                 }
1993
1994                 /* delete object should renew depsgraph */
1995                 DEG_relations_tag_update(bmain);
1996         }
1997
1998 // XXX  ED_object_editmode_enter(C, 0);
1999 // XXX  exit_editmode(C, EM_FREEDATA|EM_WAITCURSOR); /* freedata, but no undo */
2000
2001         if (basact) {
2002                 /* active base was changed */
2003                 ED_object_base_activate(C, basact);
2004                 BASACT(view_layer) = basact;
2005         }
2006         else if (BASACT(view_layer)->object->flag & OB_DONE) {
2007                 WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, BASACT(view_layer)->object);
2008                 WM_event_add_notifier(C, NC_OBJECT | ND_DATA, BASACT(view_layer)->object);
2009         }
2010
2011         DEG_relations_tag_update(bmain);
2012         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, scene);
2013         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
2014
2015         return OPERATOR_FINISHED;
2016 }
2017
2018
2019 void OBJECT_OT_convert(wmOperatorType *ot)
2020 {
2021         /* identifiers */
2022         ot->name = "Convert to";
2023         ot->description = "Convert selected objects to another type";
2024         ot->idname = "OBJECT_OT_convert";
2025
2026         /* api callbacks */
2027         ot->invoke = WM_menu_invoke;
2028         ot->exec = convert_exec;
2029         ot->poll = convert_poll;
2030
2031         /* flags */
2032         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2033
2034         /* properties */
2035         ot->prop = RNA_def_enum(ot->srna, "target", convert_target_items, OB_MESH, "Target", "Type of object to convert to");
2036         RNA_def_boolean(ot->srna, "keep_original", 0, "Keep Original", "Keep original objects instead of replacing them");
2037 }
2038
2039 /**************************** Duplicate ************************/
2040
2041 /* 
2042  * dupflag: a flag made from constants declared in DNA_userdef_types.h
2043  * The flag tells adduplicate() whether to copy data linked to the object, or to reference the existing data.
2044  * U.dupflag for default operations or you can construct a flag as python does
2045  * if the dupflag is 0 then no data will be copied (linked duplicate) */
2046
2047 /* used below, assumes id.new is correct */
2048 /* leaves selection of base/object unaltered */
2049 /* Does set ID->newid pointers. */
2050 static Base *object_add_duplicate_internal(Main *bmain, Scene *scene, ViewLayer *view_layer, Object *ob, int dupflag)
2051 {
2052 #define ID_NEW_REMAP_US(a)      if (      (a)->id.newid) { (a) = (void *)(a)->id.newid;       (a)->id.us++; }
2053 #define ID_NEW_REMAP_US2(a)     if (((ID *)a)->newid)    { (a) = ((ID  *)a)->newid;     ((ID *)a)->us++;    }
2054
2055         Base *base, *basen = NULL;
2056         Material ***matarar;
2057         Object *obn;
2058         ID *id;
2059         int a, didit;
2060
2061         if (ob->mode & OB_MODE_POSE) {
2062                 ; /* nothing? */
2063         }
2064         else {
2065                 obn = ID_NEW_SET(ob, BKE_object_copy(bmain, ob));
2066                 DEG_id_tag_update(&obn->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
2067
2068                 base = BKE_view_layer_base_find(view_layer, ob);
2069                 if ((base != NULL) && (base->flag & BASE_VISIBLED)) {
2070                         BKE_collection_object_add_from(scene, ob, obn);
2071                 }
2072                 else {
2073                         LayerCollection *layer_collection = BKE_layer_collection_get_active_ensure(scene, view_layer);
2074                         BKE_collection_object_add(&scene->id, layer_collection->scene_collection, obn);
2075                 }
2076                 basen = BKE_view_layer_base_find(view_layer, obn);
2077
2078                 /* 1) duplis should end up in same group as the original
2079                  * 2) Rigid Body sim participants MUST always be part of a group...
2080                  */
2081                 // XXX: is 2) really a good measure here?
2082                 if ((ob->flag & OB_FROMGROUP) != 0 || ob->rigidbody_object || ob->rigidbody_constraint) {
2083                         Group *group;
2084                         for (group = bmain->group.first; group; group = group->id.next) {
2085                                 if (BKE_group_object_exists(group, ob))
2086                                         BKE_group_object_add(group, obn);
2087                         }
2088                 }
2089
2090                 /* duplicates using userflags */
2091                 if (dupflag & USER_DUP_ACT) {
2092                         BKE_animdata_copy_id_action(&obn->id, true);
2093                 }
2094
2095                 if (dupflag & USER_DUP_MAT) {
2096                         for (a = 0; a < obn->totcol; a++) {
2097                                 id = (ID *)obn->mat[a];
2098                                 if (id) {
2099                                         ID_NEW_REMAP_US(obn->mat[a])
2100                                         else {
2101                                                 obn->mat[a] = ID_NEW_SET(obn->mat[a], BKE_material_copy(bmain, obn->mat[a]));
2102                                         }
2103                                         id_us_min(id);
2104
2105                                         if (dupflag & USER_DUP_ACT) {
2106                                                 BKE_animdata_copy_id_action(&obn->mat[a]->id, true);
2107                                         }
2108                                 }
2109                         }
2110                 }
2111                 if (dupflag & USER_DUP_PSYS) {
2112                         ParticleSystem *psys;
2113                         for (psys = obn->particlesystem.first; psys; psys = psys->next) {
2114                                 id = (ID *) psys->part;
2115                                 if (id) {
2116                                         ID_NEW_REMAP_US(psys->part)
2117                                         else {
2118                                                 psys->part = ID_NEW_SET(psys->part, BKE_particlesettings_copy(bmain, psys->part));
2119                                         }
2120
2121                                         if (dupflag & USER_DUP_ACT) {
2122                                                 BKE_animdata_copy_id_action(&psys->part->id, true);
2123                                         }
2124
2125                                         id_us_min(id);
2126                                 }
2127                         }
2128                 }
2129
2130                 id = obn->data;
2131                 didit = 0;
2132
2133                 switch (obn->type) {
2134                         case OB_MESH:
2135                                 if (dupflag & USER_DUP_MESH) {
2136                                         ID_NEW_REMAP_US2(obn->data)
2137                                         else {
2138                                                 obn->data = ID_NEW_SET(obn->data, BKE_mesh_copy(bmain, obn->data));
2139                                                 didit = 1;
2140                                         }
2141                                         id_us_min(id);
2142                                 }
2143                                 break;
2144                         case OB_CURVE:
2145                                 if (dupflag & USER_DUP_CURVE) {
2146                                         ID_NEW_REMAP_US2(obn->data)
2147                                         else {
2148                                                 obn->data = ID_NEW_SET(obn->data, BKE_curve_copy(bmain, obn->data));
2149                                                 didit = 1;
2150                                         }
2151                                         id_us_min(id);
2152                                 }
2153                                 break;
2154                         case OB_SURF:
2155                                 if (dupflag & USER_DUP_SURF) {
2156                                         ID_NEW_REMAP_US2(obn->data)
2157                                         else {
2158                                                 obn->data = ID_NEW_SET(obn->data, BKE_curve_copy(bmain, obn->data));
2159                                                 didit = 1;
2160                                         }
2161                                         id_us_min(id);
2162                                 }
2163                                 break;
2164                         case OB_FONT:
2165                                 if (dupflag & USER_DUP_FONT) {
2166                                         ID_NEW_REMAP_US2(obn->data)
2167                                         else {
2168                                                 obn->data = ID_NEW_SET(obn->data, BKE_curve_copy(bmain, obn->data));
2169                                                 didit = 1;
2170                                         }
2171                                         id_us_min(id);
2172                                 }
2173                                 break;
2174                         case OB_MBALL:
2175                                 if (dupflag & USER_DUP_MBALL) {
2176                                         ID_NEW_REMAP_US2(obn->data)
2177                                         else {
2178                                                 obn->data = ID_NEW_SET(obn->data, BKE_mball_copy(bmain, obn->data));
2179                                                 didit = 1;
2180                                         }
2181                                         id_us_min(id);
2182                                 }
2183                                 break;
2184                         case OB_LAMP:
2185                                 if (dupflag & USER_DUP_LAMP) {
2186                                         ID_NEW_REMAP_US2(obn->data)
2187                                         else {
2188                                                 obn->data = ID_NEW_SET(obn->data, BKE_lamp_copy(bmain, obn->data));
2189                                                 didit = 1;
2190                                         }
2191                                         id_us_min(id);
2192                                 }
2193                                 break;
2194                         case OB_ARMATURE:
2195                                 DEG_id_tag_update(&obn->id, OB_RECALC_DATA);
2196                                 if (obn->pose)
2197                                         BKE_pose_tag_recalc(bmain, obn->pose);
2198                                 if (dupflag & USER_DUP_ARM) {
2199                                         ID_NEW_REMAP_US2(obn->data)
2200                                         else {
2201                                                 obn->data = ID_NEW_SET(obn->data, BKE_armature_copy(bmain, obn->data));
2202                                                 BKE_pose_rebuild(obn, obn->data);
2203                                                 didit = 1;
2204                                         }
2205                                         id_us_min(id);
2206                                 }
2207                                 break;
2208                         case OB_LATTICE:
2209                                 if (dupflag != 0) {
2210                                         ID_NEW_REMAP_US2(obn->data)
2211                                         else {
2212                                                 obn->data = ID_NEW_SET(obn->data, BKE_lattice_copy(bmain, obn->data));
2213                                                 didit = 1;
2214                                         }
2215                                         id_us_min(id);
2216                                 }
2217                                 break;
2218                         case OB_CAMERA:
2219                                 if (dupflag != 0) {
2220                                         ID_NEW_REMAP_US2(obn->data)
2221                                         else {
2222                                                 obn->data = ID_NEW_SET(obn->data, BKE_camera_copy(bmain, obn->data));
2223                                                 didit = 1;
2224                                         }
2225                                         id_us_min(id);
2226                                 }
2227                                 break;
2228                         case OB_SPEAKER:
2229                                 if (dupflag != 0) {
2230                                         ID_NEW_REMAP_US2(obn->data)
2231                                         else {
2232                                                 obn->data = ID_NEW_SET(obn->data, BKE_speaker_copy(bmain, obn->data));
2233                                                 didit = 1;
2234                                         }
2235                                         id_us_min(id);
2236                                 }
2237                                 break;
2238                 }
2239
2240                 /* check if obdata is copied */
2241                 if (didit) {
2242                         Key *key = BKE_key_from_object(obn);
2243
2244                         Key *oldkey = BKE_key_from_object(ob);
2245                         if (oldkey != NULL) {
2246                                 ID_NEW_SET(oldkey, key);
2247                         }
2248
2249                         if (dupflag & USER_DUP_ACT) {
2250                                 BKE_animdata_copy_id_action((ID *)obn->data, true);
2251                                 if (key) {
2252                                         BKE_animdata_copy_id_action((ID *)key, true);
2253                                 }
2254                         }
2255
2256                         if (dupflag & USER_DUP_MAT) {
2257                                 matarar = give_matarar(obn);
2258                                 if (matarar) {
2259                                         for (a = 0; a < obn->totcol; a++) {
2260                                                 id = (ID *)(*matarar)[a];
2261                                                 if (id) {
2262                                                         ID_NEW_REMAP_US((*matarar)[a])
2263                                                         else {
2264                                                                 (*matarar)[a] = ID_NEW_SET((*matarar)[a], BKE_material_copy(bmain, (*matarar)[a]));
2265                                                         }
2266                                                         id_us_min(id);
2267                                                 }
2268                                         }
2269                                 }
2270                         }
2271                 }
2272         }
2273         return basen;
2274
2275 #undef ID_NEW_REMAP_US
2276 #undef ID_NEW_REMAP_US2
2277 }
2278
2279 /* single object duplicate, if dupflag==0, fully linked, else it uses the flags given */
2280 /* leaves selection of base/object unaltered.
2281  * note: don't call this within a loop since clear_* funcs loop over the entire database.
2282  * note: caller must do DAG_relations_tag_update(bmain);
2283  *       this is not done automatic since we may duplicate many objects in a batch */
2284 Base *ED_object_add_duplicate(Main *bmain, Scene *scene, ViewLayer *view_layer, Base *base, int dupflag)
2285 {
2286         Base *basen;
2287         Object *ob;
2288
2289         basen = object_add_duplicate_internal(bmain, scene, view_layer, base->object, dupflag);
2290         if (basen == NULL) {
2291                 return NULL;
2292         }
2293
2294         ob = basen->object;
2295
2296         /* link own references to the newly duplicated data [#26816] */
2297         BKE_libblock_relink_to_newid(&ob->id);
2298
2299         /* DAG_relations_tag_update(bmain); */ /* caller must do */
2300
2301         if (ob->data != NULL) {
2302                 DEG_id_tag_update_ex(bmain, (ID *)ob->data, DEG_TAG_EDITORS_UPDATE);
2303         }
2304
2305         BKE_main_id_clear_newpoins(bmain);
2306
2307         return basen;
2308 }
2309
2310 /* contextual operator dupli */
2311 static int duplicate_exec(bContext *C, wmOperator *op)
2312 {
2313         Main *bmain = CTX_data_main(C);
2314         Scene *scene = CTX_data_scene(C);
2315         ViewLayer *view_layer = CTX_data_view_layer(C);
2316         const bool linked = RNA_boolean_get(op->ptr, "linked");
2317         int dupflag = (linked) ? 0 : U.dupflag;
2318
2319         CTX_DATA_BEGIN (C, Base *, base, selected_bases)
2320         {
2321                 Base *basen = object_add_duplicate_internal(bmain, scene, view_layer, base->object, dupflag);
2322
2323                 /* note that this is safe to do with this context iterator,
2324                  * the list is made in advance */
2325                 ED_object_base_select(base, BA_DESELECT);
2326                 ED_object_base_select(basen, BA_SELECT);
2327
2328                 if (basen == NULL) {
2329                         continue;
2330                 }
2331
2332                 /* new object becomes active */
2333                 if (BASACT(view_layer) == base)
2334                         ED_object_base_activate(C, basen);
2335
2336                 if (basen->object->data) {
2337                         DEG_id_tag_update(basen->object->data, 0);
2338                 }
2339         }
2340         CTX_DATA_END;
2341
2342         copy_object_set_idnew(C);
2343
2344         BKE_main_id_clear_newpoins(bmain);
2345
2346         DEG_relations_tag_update(bmain);
2347         /* TODO(sergey): Use proper flag for tagging here. */
2348         DEG_id_tag_update(&CTX_data_scene(C)->id, 0);
2349
2350         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
2351
2352         return OPERATOR_FINISHED;
2353 }
2354
2355 void OBJECT_OT_duplicate(wmOperatorType *ot)
2356 {
2357         PropertyRNA *prop;
2358
2359         /* identifiers */
2360         ot->name = "Duplicate Objects";
2361         ot->description = "Duplicate selected objects";
2362         ot->idname = "OBJECT_OT_duplicate";
2363
2364         /* api callbacks */
2365         ot->exec = duplicate_exec;
2366         ot->poll = ED_operator_objectmode;
2367
2368         /* flags */
2369         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2370
2371         /* to give to transform */
2372         RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data");
2373         prop = RNA_def_enum(ot->srna, "mode", rna_enum_transform_mode_types, TFM_TRANSLATION, "Mode", "");
2374         RNA_def_property_flag(prop, PROP_HIDDEN);
2375 }
2376
2377 /* **************** add named object, for dragdrop ************* */
2378
2379 static int add_named_exec(bContext *C, wmOperator *op)
2380 {
2381         wmWindow *win = CTX_wm_window(C);
2382         const wmEvent *event = win ? win->eventstate : NULL;
2383         Main *bmain = CTX_data_main(C);
2384         Scene *scene = CTX_data_scene(C);
2385         ViewLayer *view_layer = CTX_data_view_layer(C);
2386         Base *basen;
2387         Object *ob;
2388         const bool linked = RNA_boolean_get(op->ptr, "linked");
2389         int dupflag = (linked) ? 0 : U.dupflag;
2390         char name[MAX_ID_NAME - 2];
2391
2392         /* find object, create fake base */
2393         RNA_string_get(op->ptr, "name", name);
2394         ob = (Object *)BKE_libblock_find_name(ID_OB, name);
2395
2396         if (ob == NULL) {
2397                 BKE_report(op->reports, RPT_ERROR, "Object not found");
2398                 return OPERATOR_CANCELLED;
2399         }
2400
2401         /* prepare dupli */
2402         basen = object_add_duplicate_internal(bmain, scene, view_layer, ob, dupflag);
2403
2404         if (basen == NULL) {
2405                 BKE_report(op->reports, RPT_ERROR, "Object could not be duplicated");
2406                 return OPERATOR_CANCELLED;
2407         }
2408
2409         BKE_scene_object_base_flag_sync_from_object(basen);
2410         basen->object->restrictflag &= ~OB_RESTRICT_VIEW;
2411
2412         if (event) {
2413                 ARegion *ar = CTX_wm_region(C);
2414                 const int mval[2] = {event->x - ar->winrct.xmin,
2415                                      event->y - ar->winrct.ymin};
2416                 ED_object_location_from_view(C, basen->object->loc);
2417                 ED_view3d_cursor3d_position(C, basen->object->loc, mval);
2418         }
2419         
2420         ED_object_base_select(basen, BA_SELECT);
2421         ED_object_base_activate(C, basen);
2422
2423         copy_object_set_idnew(C);
2424
2425         BKE_main_id_clear_newpoins(bmain);
2426
2427         /* TODO(sergey): Only update relations for the current scene. */
2428         DEG_relations_tag_update(bmain);
2429
2430         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
2431         WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
2432
2433         return OPERATOR_FINISHED;
2434 }
2435
2436 void OBJECT_OT_add_named(wmOperatorType *ot)
2437 {
2438         /* identifiers */
2439         ot->name = "Add Named Object";
2440         ot->description = "Add named object";
2441         ot->idname = "OBJECT_OT_add_named";
2442
2443         /* api callbacks */
2444         ot->exec = add_named_exec;
2445         ot->poll = ED_operator_objectmode;
2446
2447         /* flags */
2448         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2449
2450         RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data");
2451         RNA_def_string(ot->srna, "name", NULL, MAX_ID_NAME - 2, "Name", "Object name to add");
2452 }
2453
2454 /**************************** Join *************************/
2455
2456 static int join_poll(bContext *C)
2457 {
2458         Object *ob = CTX_data_active_object(C);
2459
2460         if (!ob || ID_IS_LINKED(ob)) return 0;
2461
2462         if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_ARMATURE))
2463                 return ED_operator_screenactive(C);
2464         else
2465                 return 0;
2466 }
2467
2468 static int join_exec(bContext *C, wmOperator *op)
2469 {
2470         Object *ob = CTX_data_active_object(C);
2471
2472         if (ob->mode & OB_MODE_EDIT) {
2473                 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in edit mode");
2474                 return OPERATOR_CANCELLED;
2475         }
2476         else if (BKE_object_obdata_is_libdata(ob)) {
2477                 BKE_report(op->reports, RPT_ERROR, "Cannot edit external libdata");
2478                 return OPERATOR_CANCELLED;
2479         }
2480
2481         if (ob->type == OB_MESH)
2482                 return join_mesh_exec(C, op);
2483         else if (ELEM(ob->type, OB_CURVE, OB_SURF))
2484                 return join_curve_exec(C, op);
2485         else if (ob->type == OB_ARMATURE)
2486                 return join_armature_exec(C, op);
2487
2488         return OPERATOR_CANCELLED;
2489 }
2490
2491 void OBJECT_OT_join(wmOperatorType *ot)
2492 {
2493         /* identifiers */
2494         ot->name = "Join";
2495         ot->description = "Join selected objects into active object";
2496         ot->idname = "OBJECT_OT_join";
2497
2498         /* api callbacks */
2499         ot->exec = join_exec;
2500         ot->poll = join_poll;
2501
2502         /* flags */
2503         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2504 }
2505
2506 /**************************** Join as Shape Key*************************/
2507
2508 static int join_shapes_poll(bContext *C)
2509 {
2510         Object *ob = CTX_data_active_object(C);
2511
2512         if (!ob || ID_IS_LINKED(ob)) return 0;
2513
2514         /* only meshes supported at the moment */
2515         if (ob->type == OB_MESH)
2516                 return ED_operator_screenactive(C);
2517         else
2518                 return 0;
2519 }
2520
2521 static int join_shapes_exec(bContext *C, wmOperator *op)
2522 {
2523         Object *ob = CTX_data_active_object(C);
2524
2525         if (ob->mode & OB_MODE_EDIT) {
2526                 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in edit mode");
2527                 return OPERATOR_CANCELLED;
2528         }
2529         else if (BKE_object_obdata_is_libdata(ob)) {
2530                 BKE_report(op->reports, RPT_ERROR, "Cannot edit external libdata");
2531                 return OPERATOR_CANCELLED;
2532         }
2533
2534         if (ob->type == OB_MESH)
2535                 return join_mesh_shapes_exec(C, op);
2536
2537         return OPERATOR_CANCELLED;
2538 }
2539
2540 void OBJECT_OT_join_shapes(wmOperatorType *ot)
2541 {
2542         /* identifiers */
2543         ot->name = "Join as Shapes";
2544         ot->description = "Merge selected objects to shapes of active object";
2545         ot->idname = "OBJECT_OT_join_shapes";
2546
2547         /* api callbacks */
2548         ot->exec = join_shapes_exec;
2549         ot->poll = join_shapes_poll;
2550
2551         /* flags */
2552         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2553 }