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