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