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