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