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