Merge branch 'master' into blender2.8
[blender.git] / source / blender / editors / object / object_add.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation, 2002-2008 full recode
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/object/object_add.c
27  *  \ingroup edobj
28  */
29
30
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "MEM_guardedalloc.h"
35
36 #include "DNA_anim_types.h"
37 #include "DNA_camera_types.h"
38 #include "DNA_curve_types.h"
39 #include "DNA_group_types.h"
40 #include "DNA_lamp_types.h"
41 #include "DNA_key_types.h"
42 #include "DNA_material_types.h"
43 #include "DNA_mesh_types.h"
44 #include "DNA_meta_types.h"
45 #include "DNA_object_fluidsim.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 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 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                         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;
1431         DupliObject *dob;
1432         GHash *dupli_gh = NULL, *parent_gh = NULL;
1433         Object *object;
1434
1435         if (!(base->object->transflag & OB_DUPLI))
1436                 return;
1437
1438         lb = object_duplilist(bmain->eval_ctx, scene, base->object);
1439
1440         if (use_hierarchy || use_base_parent) {
1441                 dupli_gh = BLI_ghash_ptr_new(__func__);
1442                 if (use_hierarchy) {
1443                         if (base->object->transflag & OB_DUPLIGROUP) {
1444                                 parent_gh = BLI_ghash_new(dupliobject_group_hash, dupliobject_group_cmp, __func__);
1445                         }
1446                         else {
1447                                 parent_gh = BLI_ghash_new(dupliobject_hash, dupliobject_cmp, __func__);
1448                         }
1449                 }
1450         }
1451
1452         for (dob = lb->first; dob; dob = dob->next) {
1453                 Base *basen;
1454                 Object *ob = ID_NEW_SET(dob->ob, BKE_object_copy(bmain, dob->ob));
1455
1456                 /* font duplis can have a totcol without material, we get them from parent
1457                  * should be implemented better...
1458                  */
1459                 if (ob->mat == NULL) ob->totcol = 0;
1460
1461                 BKE_collection_object_add_from(scene, dob->ob, ob);
1462                 basen = BKE_scene_layer_base_find(sl, ob);
1463
1464                 BKE_scene_object_base_flag_sync_from_base(basen);
1465
1466                 /* make sure apply works */
1467                 BKE_animdata_free(&ob->id, true);
1468                 ob->adt = NULL;
1469
1470                 /* Proxies are not to be copied. */
1471                 ob->proxy_from = NULL;
1472                 ob->proxy_group = NULL;
1473                 ob->proxy = NULL;
1474
1475                 ob->parent = NULL;
1476                 BKE_constraints_free(&ob->constraints);
1477                 ob->curve_cache = NULL;
1478                 ob->transflag &= ~OB_DUPLI;
1479
1480                 copy_m4_m4(ob->obmat, dob->mat);
1481                 BKE_object_apply_mat4(ob, ob->obmat, false, false);
1482
1483                 if (dupli_gh) {
1484                         BLI_ghash_insert(dupli_gh, dob, ob);
1485                 }
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;
1492                         }
1493                 }
1494
1495                 /* Remap new object to itself, and clear again newid pointer of orig object. */
1496                 BKE_libblock_relink_to_newid(&ob->id);
1497                 set_sca_new_poins_ob(ob);
1498                 BKE_id_clear_newpoin(&dob->ob->id);
1499
1500                 DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
1501         }
1502
1503         if (use_hierarchy) {
1504                 for (dob = lb->first; dob; dob = dob->next) {
1505                         /* original parents */
1506                         Object *ob_src =     dob->ob;
1507                         Object *ob_src_par = ob_src->parent;
1508
1509                         Object *ob_dst =     BLI_ghash_lookup(dupli_gh, dob);
1510                         Object *ob_dst_par = NULL;
1511
1512                         /* find parent that was also made real */
1513                         if (ob_src_par) {
1514                                 /* OK to keep most of the members uninitialized,
1515                                  * they won't be read, this is simply for a hash lookup. */
1516                                 DupliObject dob_key;
1517                                 dob_key.ob = ob_src_par;
1518                                 if (base->object->transflag & OB_DUPLIGROUP) {
1519                                         memcpy(&dob_key.persistent_id[1],
1520                                                    &dob->persistent_id[1],
1521                                                    sizeof(dob->persistent_id[1]) * (MAX_DUPLI_RECUR - 1));
1522                                 }
1523                                 else {
1524                                         dob_key.persistent_id[0] = dob->persistent_id[0];
1525                                 }
1526                                 ob_dst_par = BLI_ghash_lookup(parent_gh, &dob_key);
1527                         }
1528
1529                         if (ob_dst_par) {
1530                                 /* allow for all possible parent types */
1531                                 ob_dst->partype = ob_src->partype;
1532                                 BLI_strncpy(ob_dst->parsubstr, ob_src->parsubstr, sizeof(ob_dst->parsubstr));
1533                                 ob_dst->par1 = ob_src->par1;
1534                                 ob_dst->par2 = ob_src->par2;
1535                                 ob_dst->par3 = ob_src->par3;
1536
1537                                 copy_m4_m4(ob_dst->parentinv, ob_src->parentinv);
1538
1539                                 ob_dst->parent = ob_dst_par;
1540                         }
1541                         else if (use_base_parent) {
1542                                 ob_dst->parent = base->object;
1543                                 ob_dst->partype = PAROBJECT;
1544                         }
1545
1546                         if (ob_dst->parent) {
1547                                 /* note, this may be the parent of other objects, but it should
1548                                  * still work out ok */
1549                                 BKE_object_apply_mat4(ob_dst, dob->mat, false, true);
1550
1551                                 /* to set ob_dst->orig and in case theres any other discrepicies */
1552                                 DEG_id_tag_update(&ob_dst->id, OB_RECALC_OB);
1553                         }
1554                 }
1555         }
1556         else if (use_base_parent) {
1557                 /* since we are ignoring the internal hierarchy - parent all to the
1558                  * base object */
1559                 for (dob = lb->first; dob; dob = dob->next) {
1560                         /* original parents */
1561                         Object *ob_dst = BLI_ghash_lookup(dupli_gh, dob);
1562
1563                         ob_dst->parent = base->object;
1564                         ob_dst->partype = PAROBJECT;
1565
1566                         /* similer to the code above, see comments */
1567                         BKE_object_apply_mat4(ob_dst, dob->mat, false, true);
1568                         DEG_id_tag_update(&ob_dst->id, OB_RECALC_OB);
1569                 }
1570         }
1571
1572         if (base->object->transflag & OB_DUPLIGROUP && base->object->dup_group) {
1573                 for (object = bmain->object.first; object; object = object->id.next) {
1574                         if (object->proxy_group == base->object) {
1575                                 object->proxy = NULL;
1576                                 object->proxy_from = NULL;
1577                                 DEG_id_tag_update(&object->id, OB_RECALC_OB);
1578                         }
1579                 }
1580         }
1581
1582         if (dupli_gh)
1583                 BLI_ghash_free(dupli_gh, NULL, NULL);
1584         if (parent_gh)
1585                 BLI_ghash_free(parent_gh, NULL, NULL);
1586
1587         free_object_duplilist(lb);
1588
1589         base->object->transflag &= ~OB_DUPLI;
1590 }
1591
1592 static int object_duplicates_make_real_exec(bContext *C, wmOperator *op)
1593 {
1594         Main *bmain = CTX_data_main(C);
1595         Scene *scene = CTX_data_scene(C);
1596
1597         const bool use_base_parent = RNA_boolean_get(op->ptr, "use_base_parent");
1598         const bool use_hierarchy = RNA_boolean_get(op->ptr, "use_hierarchy");
1599
1600         BKE_main_id_clear_newpoins(bmain);
1601
1602         CTX_DATA_BEGIN (C, Base *, base, selected_editable_bases)
1603         {
1604                 make_object_duplilist_real(C, scene, base, use_base_parent, use_hierarchy);
1605
1606                 /* dependencies were changed */
1607                 WM_event_add_notifier(C, NC_OBJECT | ND_PARENT, base->object);
1608         }
1609         CTX_DATA_END;
1610
1611         DEG_relations_tag_update(bmain);
1612         WM_event_add_notifier(C, NC_SCENE, scene);
1613         WM_main_add_notifier(NC_OBJECT | ND_DRAW, NULL);
1614
1615         return OPERATOR_FINISHED;
1616 }
1617
1618 void OBJECT_OT_duplicates_make_real(wmOperatorType *ot)
1619 {
1620         /* identifiers */
1621         ot->name = "Make Duplicates Real";
1622         ot->description = "Make dupli objects attached to this object real";
1623         ot->idname = "OBJECT_OT_duplicates_make_real";
1624
1625         /* api callbacks */
1626         ot->exec = object_duplicates_make_real_exec;
1627
1628         ot->poll = ED_operator_objectmode;
1629
1630         /* flags */
1631         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1632
1633         RNA_def_boolean(ot->srna, "use_base_parent", 0, "Parent", "Parent newly created objects to the original duplicator");
1634         RNA_def_boolean(ot->srna, "use_hierarchy", 0, "Keep Hierarchy", "Maintain parent child relationships");
1635 }
1636
1637 /**************************** Convert **************************/
1638
1639 static EnumPropertyItem convert_target_items[] = {
1640         {OB_CURVE, "CURVE", ICON_OUTLINER_OB_CURVE, "Curve from Mesh/Text", ""},
1641         {OB_MESH, "MESH", ICON_OUTLINER_OB_MESH, "Mesh from Curve/Meta/Surf/Text", ""},
1642         {0, NULL, 0, NULL, NULL}
1643 };
1644
1645 static void convert_ensure_curve_cache(EvaluationContext *eval_ctx, Main *bmain, Scene *scene, Object *ob)
1646 {
1647         if (ob->curve_cache == NULL) {
1648                 /* Force creation. This is normally not needed but on operator
1649                  * redo we might end up with an object which isn't evaluated yet.
1650                  */
1651                 if (ELEM(ob->type, OB_SURF, OB_CURVE, OB_FONT)) {
1652                         BKE_displist_make_curveTypes(eval_ctx, scene, ob, false);
1653                 }
1654                 else if (ob->type == OB_MBALL) {
1655                         BKE_displist_make_mball(bmain->eval_ctx, scene, ob);
1656                 }
1657         }
1658 }
1659
1660 static void curvetomesh(EvaluationContext *eval_ctx, Main *bmain, Scene *scene, Object *ob)
1661 {
1662         convert_ensure_curve_cache(eval_ctx, bmain, scene, ob);
1663         BKE_mesh_from_nurbs(ob); /* also does users */
1664
1665         if (ob->type == OB_MESH) {
1666                 BKE_object_free_modifiers(ob);
1667
1668                 /* Game engine defaults for mesh objects */
1669                 ob->body_type = OB_BODY_TYPE_STATIC;
1670                 ob->gameflag = OB_PROP | OB_COLLISION;
1671         }
1672 }
1673
1674 static int convert_poll(bContext *C)
1675 {
1676         Object *obact = CTX_data_active_object(C);
1677         Scene *scene = CTX_data_scene(C);
1678
1679         return (!ID_IS_LINKED_DATABLOCK(scene) && obact && scene->obedit != obact &&
1680                 (obact->flag & SELECT) && !ID_IS_LINKED_DATABLOCK(obact));
1681 }
1682
1683 /* Helper for convert_exec */
1684 static Base *duplibase_for_convert(Main *bmain, Scene *scene, SceneLayer *sl, Base *base, Object *ob)
1685 {
1686         Object *obn;
1687         Base *basen;
1688
1689         if (ob == NULL) {
1690                 ob = base->object;
1691         }
1692
1693         obn = BKE_object_copy(bmain, ob);
1694         DEG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1695         BKE_collection_object_add_from(scene, ob, obn);
1696
1697         basen = BKE_scene_layer_base_find(sl, obn);
1698         ED_object_base_select(basen, BA_SELECT);
1699         ED_object_base_select(basen, BA_DESELECT);
1700         return basen;
1701 }
1702
1703 static int convert_exec(bContext *C, wmOperator *op)
1704 {
1705         Main *bmain = CTX_data_main(C);
1706         Scene *scene = CTX_data_scene(C);
1707         SceneLayer *sl = CTX_data_scene_layer(C);
1708         EvaluationContext eval_ctx;
1709         Base *basen = NULL, *basact = NULL;
1710         Object *ob, *ob1, *newob, *obact = CTX_data_active_object(C);
1711         DerivedMesh *dm;
1712         Curve *cu;
1713         Nurb *nu;
1714         MetaBall *mb;
1715         Mesh *me;
1716         const short target = RNA_enum_get(op->ptr, "target");
1717         bool keep_original = RNA_boolean_get(op->ptr, "keep_original");
1718         int a, mballConverted = 0;
1719
1720         CTX_data_eval_ctx(C, &eval_ctx);
1721
1722         /* don't forget multiple users! */
1723
1724         {
1725                 BaseLegacy *base;
1726
1727                 for (base = scene->base.first; base; base = base->next) {
1728                         ob = base->object;
1729                         ob->flag &= ~OB_DONE;
1730
1731                         /* flag data thats not been edited (only needed for !keep_original) */
1732                         if (ob->data) {
1733                                 ((ID *)ob->data)->tag |= LIB_TAG_DOIT;
1734                         }
1735
1736                         /* possible metaball basis is not in this scene */
1737                         if (ob->type == OB_MBALL && target == OB_MESH) {
1738                                 if (BKE_mball_is_basis(ob) == false) {
1739                                         Object *ob_basis;
1740                                         ob_basis = BKE_mball_basis_find(scene, ob);
1741                                         if (ob_basis) {
1742                                                 ob_basis->flag &= ~OB_DONE;
1743                                         }
1744                                 }
1745                         }
1746                 }
1747         }
1748
1749         ListBase selected_editable_bases = CTX_data_collection_get(C, "selected_editable_bases");
1750
1751         /* Ensure we get all meshes calculated with a sufficient data-mask,
1752          * needed since re-evaluating single modifiers causes bugs if they depend
1753          * on other objects data masks too, see: T50950. */
1754         {
1755                 for (CollectionPointerLink *link = selected_editable_bases.first; link; link = link->next) {
1756                         BaseLegacy *base = link->ptr.data;
1757                         ob = base->object;
1758
1759                         /* The way object type conversion works currently (enforcing conversion of *all* objetcs using converted
1760                          * obdata, even some un-selected/hidden/inother scene ones, sounds totally bad to me.
1761                          * However, changing this is more design than bugfix, not to mention convoluted code below,
1762                          * so that will be for later.
1763                          * But at the very least, do not do that with linked IDs! */
1764                         if ((ID_IS_LINKED_DATABLOCK(ob) || (ob->data && ID_IS_LINKED_DATABLOCK(ob->data))) && !keep_original) {
1765                                 keep_original = true;
1766                                 BKE_reportf(op->reports, RPT_INFO,
1767                                             "Converting some linked object/object data, enforcing 'Keep Original' option to True");
1768                         }
1769
1770                         DEG_id_tag_update(&base->object->id, OB_RECALC_DATA);
1771                 }
1772
1773                 uint64_t customdata_mask_prev = scene->customdata_mask;
1774                 scene->customdata_mask |= CD_MASK_MESH;
1775                 BKE_scene_update_tagged(bmain->eval_ctx, bmain, scene);
1776                 scene->customdata_mask = customdata_mask_prev;
1777         }
1778
1779         for (CollectionPointerLink *link = selected_editable_bases.first; link; link = link->next) {
1780                 BaseLegacy *base = link->ptr.data;
1781                 ob = base->object;
1782
1783                 if (ob->flag & OB_DONE || !IS_TAGGED(ob->data)) {
1784                         if (ob->type != target) {
1785                                 base->flag &= ~SELECT;
1786                                 ob->flag &= ~SELECT;
1787                         }
1788
1789                         /* obdata already modified */
1790                         if (!IS_TAGGED(ob->data)) {
1791                                 /* When 2 objects with linked data are selected, converting both
1792                                  * would keep modifiers on all but the converted object [#26003] */
1793                                 if (ob->type == OB_MESH) {
1794                                         BKE_object_free_modifiers(ob);  /* after derivedmesh calls! */
1795                                 }
1796                         }
1797                 }
1798                 else if (ob->type == OB_MESH && target == OB_CURVE) {
1799                         ob->flag |= OB_DONE;
1800
1801                         if (keep_original) {
1802                                 basen = duplibase_for_convert(bmain, scene, sl, base, NULL);
1803                                 newob = basen->object;
1804
1805                                 /* decrement original mesh's usage count  */
1806                                 me = newob->data;
1807                                 id_us_min(&me->id);
1808
1809                                 /* make a new copy of the mesh */
1810                                 newob->data = BKE_mesh_copy(bmain, me);
1811                         }
1812                         else {
1813                                 newob = ob;
1814                         }
1815
1816                         BKE_mesh_to_curve(&eval_ctx, scene, newob);
1817
1818                         if (newob->type == OB_CURVE) {
1819                                 BKE_object_free_modifiers(newob);   /* after derivedmesh calls! */
1820                                 ED_rigidbody_object_remove(bmain, scene, newob);
1821                         }
1822                 }
1823                 else if (ob->type == OB_MESH) {
1824                         ob->flag |= OB_DONE;
1825
1826                         if (keep_original) {
1827                                 basen = duplibase_for_convert(bmain, scene, sl, base, NULL);
1828                                 newob = basen->object;
1829
1830                                 /* decrement original mesh's usage count  */
1831                                 me = newob->data;
1832                                 id_us_min(&me->id);
1833
1834                                 /* make a new copy of the mesh */
1835                                 newob->data = BKE_mesh_copy(bmain, me);
1836                         }
1837                         else {
1838                                 newob = ob;
1839                                 DEG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1840                         }
1841
1842                         /* make new mesh data from the original copy */
1843                         /* note: get the mesh from the original, not from the copy in some
1844                          * cases this doesnt give correct results (when MDEF is used for eg)
1845                          */
1846                         dm = mesh_get_derived_final(&eval_ctx, scene, newob, CD_MASK_MESH);
1847
1848                         DM_to_mesh(dm, newob->data, newob, CD_MASK_MESH, true);
1849
1850                         /* re-tessellation is called by DM_to_mesh */
1851
1852                         BKE_object_free_modifiers(newob);   /* after derivedmesh calls! */
1853                 }
1854                 else if (ob->type == OB_FONT) {
1855                         ob->flag |= OB_DONE;
1856
1857                         if (keep_original) {
1858                                 basen = duplibase_for_convert(bmain, scene, sl, base, NULL);
1859                                 newob = basen->object;
1860
1861                                 /* decrement original curve's usage count  */
1862                                 id_us_min(&((Curve *)newob->data)->id);
1863
1864                                 /* make a new copy of the curve */
1865                                 newob->data = BKE_curve_copy(bmain, ob->data);
1866                         }
1867                         else {
1868                                 newob = ob;
1869                         }
1870
1871                         cu = newob->data;
1872
1873                         /* TODO(sergey): Ideally DAG will create nurbs list for a curve data
1874                          *               datablock, but for until we've got granular update
1875                          *               lets take care by selves.
1876                          */
1877                         BKE_vfont_to_curve(bmain, newob, FO_EDIT);
1878
1879                         newob->type = OB_CURVE;
1880                         cu->type = OB_CURVE;
1881
1882                         if (cu->vfont) {
1883                                 id_us_min(&cu->vfont->id);
1884                                 cu->vfont = NULL;
1885                         }
1886                         if (cu->vfontb) {
1887                                 id_us_min(&cu->vfontb->id);
1888                                 cu->vfontb = NULL;
1889                         }
1890                         if (cu->vfonti) {
1891                                 id_us_min(&cu->vfonti->id);
1892                                 cu->vfonti = NULL;
1893                         }
1894                         if (cu->vfontbi) {
1895                                 id_us_min(&cu->vfontbi->id);
1896                                 cu->vfontbi = NULL;
1897                         }
1898
1899                         if (!keep_original) {
1900                                 /* other users */
1901                                 if (cu->id.us > 1) {
1902                                         for (ob1 = bmain->object.first; ob1; ob1 = ob1->id.next) {
1903                                                 if (ob1->data == ob->data) {
1904                                                         ob1->type = OB_CURVE;
1905                                                         DEG_id_tag_update(&ob1->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1906                                                 }
1907                                         }
1908                                 }
1909                         }
1910
1911                         for (nu = cu->nurb.first; nu; nu = nu->next)
1912                                 nu->charidx = 0;
1913
1914                         cu->flag &= ~CU_3D;
1915                         BKE_curve_curve_dimension_update(cu);
1916
1917                         if (target == OB_MESH) {
1918                                 curvetomesh(&eval_ctx, bmain, scene, newob);
1919
1920                                 /* meshes doesn't use displist */
1921                                 BKE_object_free_curve_cache(newob);
1922                         }
1923                 }
1924                 else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
1925                         ob->flag |= OB_DONE;
1926
1927                         if (target == OB_MESH) {
1928                                 if (keep_original) {
1929                                         basen = duplibase_for_convert(bmain, scene, sl, base, NULL);
1930                                         newob = basen->object;
1931
1932                                         /* decrement original curve's usage count  */
1933                                         id_us_min(&((Curve *)newob->data)->id);
1934
1935                                         /* make a new copy of the curve */
1936                                         newob->data = BKE_curve_copy(bmain, ob->data);
1937                                 }
1938                                 else {
1939                                         newob = ob;
1940                                 }
1941
1942                                 curvetomesh(&eval_ctx, bmain, scene, newob);
1943
1944                                 /* meshes doesn't use displist */
1945                                 BKE_object_free_curve_cache(newob);
1946                         }
1947                 }
1948                 else if (ob->type == OB_MBALL && target == OB_MESH) {
1949                         Object *baseob;
1950
1951                         base->flag &= ~SELECT;
1952                         ob->flag &= ~SELECT;
1953
1954                         baseob = BKE_mball_basis_find(scene, ob);
1955
1956                         if (ob != baseob) {
1957                                 /* if motherball is converting it would be marked as done later */
1958                                 ob->flag |= OB_DONE;
1959                         }
1960
1961                         if (!(baseob->flag & OB_DONE)) {
1962                                 baseob->flag |= OB_DONE;
1963
1964                                 basen = duplibase_for_convert(bmain, scene, sl, base, baseob);
1965                                 newob = basen->object;
1966
1967                                 mb = newob->data;
1968                                 id_us_min(&mb->id);
1969
1970                                 newob->data = BKE_mesh_add(bmain, "Mesh");
1971                                 newob->type = OB_MESH;
1972
1973                                 me = newob->data;
1974                                 me->totcol = mb->totcol;
1975                                 if (newob->totcol) {
1976                                         me->mat = MEM_dupallocN(mb->mat);
1977                                         for (a = 0; a < newob->totcol; a++) id_us_plus((ID *)me->mat[a]);
1978                                 }
1979
1980                                 convert_ensure_curve_cache(&eval_ctx, bmain, scene, baseob);
1981                                 BKE_mesh_from_metaball(&baseob->curve_cache->disp, newob->data);
1982
1983                                 if (obact->type == OB_MBALL) {
1984                                         basact = basen;
1985                                 }
1986
1987                                 mballConverted = 1;
1988                         }
1989                 }
1990                 else {
1991                         continue;
1992                 }
1993
1994                 /* tag obdata if it was been changed */
1995
1996                 /* If the original object is active then make this object active */
1997                 if (basen) {
1998                         if (ob == obact) {
1999                                 /* store new active base to update BASACT */
2000                                 basact = basen;
2001                         }
2002
2003                         basen = NULL;
2004                 }
2005
2006                 if (!keep_original && (ob->flag & OB_DONE)) {
2007                         DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
2008                         ((ID *)ob->data)->tag &= ~LIB_TAG_DOIT; /* flag not to convert this datablock again */
2009                 }
2010         }
2011         BLI_freelistN(&selected_editable_bases);
2012
2013         if (!keep_original) {
2014                 if (mballConverted) {
2015                         FOREACH_SCENE_OBJECT(scene, ob_mball)
2016                         {
2017                                 if (ob->type == OB_MBALL) {
2018                                         if (ob->flag & OB_DONE) {
2019                                                 Object *ob_basis = NULL;
2020                                                 if (BKE_mball_is_basis(ob_mball) ||
2021                                                     ((ob_basis = BKE_mball_basis_find(scene, ob_mball)) && (ob_basis->flag & OB_DONE)))
2022                                                 {
2023                                                         ED_base_object_free_and_unlink(bmain, scene, ob_mball);
2024                                                 }
2025                                         }
2026                                 }
2027                         }
2028                         FOREACH_SCENE_OBJECT_END
2029                 }
2030
2031                 /* delete object should renew depsgraph */
2032                 DEG_relations_tag_update(bmain);
2033         }
2034
2035 // XXX  ED_object_editmode_enter(C, 0);
2036 // XXX  exit_editmode(C, EM_FREEDATA|EM_WAITCURSOR); /* freedata, but no undo */
2037
2038         if (basact) {
2039                 /* active base was changed */
2040                 ED_object_base_activate(C, basact);
2041                 BASACT_NEW = basact;
2042         }
2043         else if (BASACT_NEW->object->flag & OB_DONE) {
2044                 WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, BASACT_NEW->object);
2045                 WM_event_add_notifier(C, NC_OBJECT | ND_DATA, BASACT_NEW->object);
2046         }
2047
2048         DEG_relations_tag_update(bmain);
2049         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, scene);
2050         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
2051
2052         return OPERATOR_FINISHED;
2053 }
2054
2055
2056 void OBJECT_OT_convert(wmOperatorType *ot)
2057 {
2058         /* identifiers */
2059         ot->name = "Convert to";
2060         ot->description = "Convert selected objects to another type";
2061         ot->idname = "OBJECT_OT_convert";
2062
2063         /* api callbacks */
2064         ot->invoke = WM_menu_invoke;
2065         ot->exec = convert_exec;
2066         ot->poll = convert_poll;
2067
2068         /* flags */
2069         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2070
2071         /* properties */
2072         ot->prop = RNA_def_enum(ot->srna, "target", convert_target_items, OB_MESH, "Target", "Type of object to convert to");
2073         RNA_def_boolean(ot->srna, "keep_original", 0, "Keep Original", "Keep original objects instead of replacing them");
2074 }
2075
2076 /**************************** Duplicate ************************/
2077
2078 /* 
2079  * dupflag: a flag made from constants declared in DNA_userdef_types.h
2080  * The flag tells adduplicate() whether to copy data linked to the object, or to reference the existing data.
2081  * U.dupflag for default operations or you can construct a flag as python does
2082  * if the dupflag is 0 then no data will be copied (linked duplicate) */
2083
2084 /* used below, assumes id.new is correct */
2085 /* leaves selection of base/object unaltered */
2086 /* Does set ID->newid pointers. */
2087 static Base *object_add_duplicate_internal(Main *bmain, Scene *scene, SceneLayer *sl, Object *ob, int dupflag)
2088 {
2089 #define ID_NEW_REMAP_US(a)      if (      (a)->id.newid) { (a) = (void *)(a)->id.newid;       (a)->id.us++; }
2090 #define ID_NEW_REMAP_US2(a)     if (((ID *)a)->newid)    { (a) = ((ID  *)a)->newid;     ((ID *)a)->us++;    }
2091
2092         Base *basen = NULL;
2093         Material ***matarar;
2094         Object *obn;
2095         ID *id;
2096         int a, didit;
2097
2098         if (ob->mode & OB_MODE_POSE) {
2099                 ; /* nothing? */
2100         }
2101         else {
2102                 obn = ID_NEW_SET(ob, BKE_object_copy(bmain, ob));
2103                 DEG_id_tag_update(&obn->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
2104
2105                 BKE_collection_object_add_from(scene, ob, obn);
2106                 basen = BKE_scene_layer_base_find(sl, obn);
2107
2108                 /* 1) duplis should end up in same group as the original
2109                  * 2) Rigid Body sim participants MUST always be part of a group...
2110                  */
2111                 // XXX: is 2) really a good measure here?
2112                 if ((ob->flag & OB_FROMGROUP) != 0 || ob->rigidbody_object || ob->rigidbody_constraint) {
2113                         Group *group;
2114                         for (group = bmain->group.first; group; group = group->id.next) {
2115                                 if (BKE_group_object_exists(group, ob))
2116                                         BKE_group_object_add(group, obn);
2117                         }
2118                 }
2119
2120                 /* duplicates using userflags */
2121                 if (dupflag & USER_DUP_ACT) {
2122                         BKE_animdata_copy_id_action(&obn->id, true);
2123                 }
2124
2125                 if (dupflag & USER_DUP_MAT) {
2126                         for (a = 0; a < obn->totcol; a++) {
2127                                 id = (ID *)obn->mat[a];
2128                                 if (id) {
2129                                         ID_NEW_REMAP_US(obn->mat[a])
2130                                         else {
2131                                                 obn->mat[a] = ID_NEW_SET(obn->mat[a], BKE_material_copy(bmain, obn->mat[a]));
2132                                         }
2133                                         id_us_min(id);
2134
2135                                         if (dupflag & USER_DUP_ACT) {
2136                                                 BKE_animdata_copy_id_action(&obn->mat[a]->id, true);
2137                                         }
2138                                 }
2139                         }
2140                 }
2141                 if (dupflag & USER_DUP_PSYS) {
2142                         ParticleSystem *psys;
2143                         for (psys = obn->particlesystem.first; psys; psys = psys->next) {
2144                                 id = (ID *) psys->part;
2145                                 if (id) {
2146                                         ID_NEW_REMAP_US(psys->part)
2147                                         else {
2148                                                 psys->part = ID_NEW_SET(psys->part, BKE_particlesettings_copy(bmain, psys->part));
2149                                         }
2150
2151                                         if (dupflag & USER_DUP_ACT) {
2152                                                 BKE_animdata_copy_id_action(&psys->part->id, true);
2153                                         }
2154
2155                                         id_us_min(id);
2156                                 }
2157                         }
2158                 }
2159
2160                 id = obn->data;
2161                 didit = 0;
2162
2163                 switch (obn->type) {
2164                         case OB_MESH:
2165                                 if (dupflag & USER_DUP_MESH) {
2166                                         ID_NEW_REMAP_US2(obn->data)
2167                                         else {
2168                                                 obn->data = ID_NEW_SET(obn->data, BKE_mesh_copy(bmain, obn->data));
2169                                                 didit = 1;
2170                                         }
2171                                         id_us_min(id);
2172                                 }
2173                                 break;
2174                         case OB_CURVE:
2175                                 if (dupflag & USER_DUP_CURVE) {
2176                                         ID_NEW_REMAP_US2(obn->data)
2177                                         else {
2178                                                 obn->data = ID_NEW_SET(obn->data, BKE_curve_copy(bmain, obn->data));
2179                                                 didit = 1;
2180                                         }
2181                                         id_us_min(id);
2182                                 }
2183                                 break;
2184                         case OB_SURF:
2185                                 if (dupflag & USER_DUP_SURF) {
2186                                         ID_NEW_REMAP_US2(obn->data)
2187                                         else {
2188                                                 obn->data = ID_NEW_SET(obn->data, BKE_curve_copy(bmain, obn->data));
2189                                                 didit = 1;
2190                                         }
2191                                         id_us_min(id);
2192                                 }
2193                                 break;
2194                         case OB_FONT:
2195                                 if (dupflag & USER_DUP_FONT) {
2196                                         ID_NEW_REMAP_US2(obn->data)
2197                                         else {
2198                                                 obn->data = ID_NEW_SET(obn->data, BKE_curve_copy(bmain, obn->data));
2199                                                 didit = 1;
2200                                         }
2201                                         id_us_min(id);
2202                                 }
2203                                 break;
2204                         case OB_MBALL:
2205                                 if (dupflag & USER_DUP_MBALL) {
2206                                         ID_NEW_REMAP_US2(obn->data)
2207                                         else {
2208                                                 obn->data = ID_NEW_SET(obn->data, BKE_mball_copy(bmain, obn->data));
2209                                                 didit = 1;
2210                                         }
2211                                         id_us_min(id);
2212                                 }
2213                                 break;
2214                         case OB_LAMP:
2215                                 if (dupflag & USER_DUP_LAMP) {
2216                                         ID_NEW_REMAP_US2(obn->data)
2217                                         else {
2218                                                 obn->data = ID_NEW_SET(obn->data, BKE_lamp_copy(bmain, obn->data));
2219                                                 didit = 1;
2220                                         }
2221                                         id_us_min(id);
2222                                 }
2223                                 break;
2224                         case OB_ARMATURE:
2225                                 DEG_id_tag_update(&obn->id, OB_RECALC_DATA);
2226                                 if (obn->pose)
2227                                         BKE_pose_tag_recalc(bmain, obn->pose);
2228                                 if (dupflag & USER_DUP_ARM) {
2229                                         ID_NEW_REMAP_US2(obn->data)
2230                                         else {
2231                                                 obn->data = ID_NEW_SET(obn->data, BKE_armature_copy(bmain, obn->data));
2232                                                 BKE_pose_rebuild(obn, obn->data);
2233                                                 didit = 1;
2234                                         }
2235                                         id_us_min(id);
2236                                 }
2237                                 break;
2238                         case OB_LATTICE:
2239                                 if (dupflag != 0) {
2240                                         ID_NEW_REMAP_US2(obn->data)
2241                                         else {
2242                                                 obn->data = ID_NEW_SET(obn->data, BKE_lattice_copy(bmain, obn->data));
2243                                                 didit = 1;
2244                                         }
2245                                         id_us_min(id);
2246                                 }
2247                                 break;
2248                         case OB_CAMERA:
2249                                 if (dupflag != 0) {
2250                                         ID_NEW_REMAP_US2(obn->data)
2251                                         else {
2252                                                 obn->data = ID_NEW_SET(obn->data, BKE_camera_copy(bmain, obn->data));
2253                                                 didit = 1;
2254                                         }
2255                                         id_us_min(id);
2256                                 }
2257                                 break;
2258                         case OB_SPEAKER:
2259                                 if (dupflag != 0) {
2260                                         ID_NEW_REMAP_US2(obn->data)
2261                                         else {
2262                                                 obn->data = ID_NEW_SET(obn->data, BKE_speaker_copy(bmain, obn->data));
2263                                                 didit = 1;
2264                                         }
2265                                         id_us_min(id);
2266                                 }
2267                                 break;
2268                 }
2269
2270                 /* check if obdata is copied */
2271                 if (didit) {
2272                         Key *key = BKE_key_from_object(obn);
2273
2274                         if (dupflag & USER_DUP_ACT) {
2275                                 bActuator *act;
2276
2277                                 BKE_animdata_copy_id_action((ID *)obn->data, true);
2278                                 if (key) {
2279                                         BKE_animdata_copy_id_action((ID *)key, true);
2280                                 }
2281
2282                                 /* Update the duplicated action in the action actuators */
2283                                 /* XXX TODO this code is all wrong! actact->act is user-refcounted (see readfile.c),
2284                                  * and what about other ID pointers of other BGE logic bricks,
2285                                  * and since this is object-level, why is it only ran if obdata was duplicated??? -mont29 */
2286                                 for (act = obn->actuators.first; act; act = act->next) {
2287                                         if (act->type == ACT_ACTION) {
2288                                                 bActionActuator *actact = (bActionActuator *) act->data;
2289                                                 if (ob->adt && actact->act == ob->adt->action) {
2290                                                         actact->act = obn->adt->action;
2291                                                 }
2292                                         }
2293                                 }
2294                         }
2295
2296                         if (dupflag & USER_DUP_MAT) {
2297                                 matarar = give_matarar(obn);
2298                                 if (matarar) {
2299                                         for (a = 0; a < obn->totcol; a++) {
2300                                                 id = (ID *)(*matarar)[a];
2301                                                 if (id) {
2302                                                         ID_NEW_REMAP_US((*matarar)[a])
2303                                                         else {
2304                                                                 (*matarar)[a] = ID_NEW_SET((*matarar)[a], BKE_material_copy(bmain, (*matarar)[a]));
2305                                                         }
2306                                                         id_us_min(id);
2307                                                 }
2308                                         }
2309                                 }
2310                         }
2311                 }
2312         }
2313         return basen;
2314
2315 #undef ID_NEW_REMAP_US
2316 #undef ID_NEW_REMAP_US2
2317 }
2318
2319 /* single object duplicate, if dupflag==0, fully linked, else it uses the flags given */
2320 /* leaves selection of base/object unaltered.
2321  * note: don't call this within a loop since clear_* funcs loop over the entire database.
2322  * note: caller must do DAG_relations_tag_update(bmain);
2323  *       this is not done automatic since we may duplicate many objects in a batch */
2324 Base *ED_object_add_duplicate(Main *bmain, Scene *scene, SceneLayer *sl, Base *base, int dupflag)
2325 {
2326         Base *basen;
2327         Object *ob;
2328
2329         clear_sca_new_poins();  /* BGE logic */
2330
2331         basen = object_add_duplicate_internal(bmain, scene, sl, base->object, dupflag);
2332         if (basen == NULL) {
2333                 return NULL;
2334         }
2335
2336         ob = basen->object;
2337
2338         /* link own references to the newly duplicated data [#26816] */
2339         BKE_libblock_relink_to_newid(&ob->id);
2340         set_sca_new_poins_ob(ob);
2341
2342         /* DAG_relations_tag_update(bmain); */ /* caller must do */
2343
2344         if (ob->data) {
2345                 ED_render_id_flush_update(bmain, ob->data);
2346         }
2347
2348         BKE_main_id_clear_newpoins(bmain);
2349
2350         return basen;
2351 }
2352
2353 /* contextual operator dupli */
2354 static int duplicate_exec(bContext *C, wmOperator *op)
2355 {
2356         Main *bmain = CTX_data_main(C);
2357         Scene *scene = CTX_data_scene(C);
2358         SceneLayer *sl = CTX_data_scene_layer(C);
2359         const bool linked = RNA_boolean_get(op->ptr, "linked");
2360         int dupflag = (linked) ? 0 : U.dupflag;
2361
2362         clear_sca_new_poins();  /* BGE logic */
2363
2364         CTX_DATA_BEGIN (C, Base *, base, selected_bases)
2365         {
2366                 Base *basen = object_add_duplicate_internal(bmain, scene, sl, base->object, dupflag);
2367
2368                 /* note that this is safe to do with this context iterator,
2369                  * the list is made in advance */
2370                 ED_object_base_select(base, BA_DESELECT);
2371                 ED_object_base_select(basen, BA_SELECT);
2372
2373                 if (basen == NULL) {
2374                         continue;
2375                 }
2376
2377                 /* new object becomes active */
2378                 if (BASACT_NEW == base)
2379                         ED_object_base_activate(C, basen);
2380
2381                 if (basen->object->data) {
2382                         DEG_id_tag_update(basen->object->data, 0);
2383                 }
2384         }
2385         CTX_DATA_END;
2386
2387         copy_object_set_idnew(C);
2388
2389         BKE_main_id_clear_newpoins(bmain);
2390
2391         DEG_relations_tag_update(bmain);
2392         /* TODO(sergey): Use proper flag for tagging here. */
2393         DEG_id_tag_update(&CTX_data_scene(C)->id, 0);
2394
2395         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
2396
2397         return OPERATOR_FINISHED;
2398 }
2399
2400 void OBJECT_OT_duplicate(wmOperatorType *ot)
2401 {
2402         PropertyRNA *prop;
2403
2404         /* identifiers */
2405         ot->name = "Duplicate Objects";
2406         ot->description = "Duplicate selected objects";
2407         ot->idname = "OBJECT_OT_duplicate";
2408
2409         /* api callbacks */
2410         ot->exec = duplicate_exec;
2411         ot->poll = ED_operator_objectmode;
2412
2413         /* flags */
2414         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2415
2416         /* to give to transform */
2417         RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data");
2418         prop = RNA_def_enum(ot->srna, "mode", rna_enum_transform_mode_types, TFM_TRANSLATION, "Mode", "");
2419         RNA_def_property_flag(prop, PROP_HIDDEN);
2420 }
2421
2422 /* **************** add named object, for dragdrop ************* */
2423
2424 static int add_named_exec(bContext *C, wmOperator *op)
2425 {
2426         wmWindow *win = CTX_wm_window(C);
2427         const wmEvent *event = win ? win->eventstate : NULL;
2428         Main *bmain = CTX_data_main(C);
2429         Scene *scene = CTX_data_scene(C);
2430         SceneLayer *sl = CTX_data_scene_layer(C);
2431         Base *basen;
2432         Object *ob;
2433         const bool linked = RNA_boolean_get(op->ptr, "linked");
2434         int dupflag = (linked) ? 0 : U.dupflag;
2435         char name[MAX_ID_NAME - 2];
2436
2437         /* find object, create fake base */
2438         RNA_string_get(op->ptr, "name", name);
2439         ob = (Object *)BKE_libblock_find_name(ID_OB, name);
2440
2441         if (ob == NULL) {
2442                 BKE_report(op->reports, RPT_ERROR, "Object not found");
2443                 return OPERATOR_CANCELLED;
2444         }
2445
2446         /* prepare dupli */
2447         clear_sca_new_poins();  /* BGE logic */
2448
2449         basen = object_add_duplicate_internal(bmain, scene, sl, ob, dupflag);
2450         BKE_scene_object_base_flag_sync_from_object(basen);
2451
2452         if (basen == NULL) {
2453                 BKE_report(op->reports, RPT_ERROR, "Object could not be duplicated");
2454                 return OPERATOR_CANCELLED;
2455         }
2456
2457         basen->object->restrictflag &= ~OB_RESTRICT_VIEW;
2458
2459         if (event) {
2460                 ARegion *ar = CTX_wm_region(C);
2461                 const int mval[2] = {event->x - ar->winrct.xmin,
2462                                      event->y - ar->winrct.ymin};
2463                 ED_object_location_from_view(C, basen->object->loc);
2464                 ED_view3d_cursor3d_position(C, basen->object->loc, mval);
2465         }
2466         
2467         ED_object_base_select(basen, BA_SELECT);
2468         ED_object_base_activate(C, basen);
2469
2470         copy_object_set_idnew(C);
2471
2472         BKE_main_id_clear_newpoins(bmain);
2473
2474         DEG_relations_tag_update(bmain);
2475
2476         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT | ND_OB_ACTIVE, scene);
2477
2478         return OPERATOR_FINISHED;
2479 }
2480
2481 void OBJECT_OT_add_named(wmOperatorType *ot)
2482 {
2483         /* identifiers */
2484         ot->name = "Add Named Object";
2485         ot->description = "Add named object";
2486         ot->idname = "OBJECT_OT_add_named";
2487
2488         /* api callbacks */
2489         ot->exec = add_named_exec;
2490         ot->poll = ED_operator_objectmode;
2491
2492         /* flags */
2493         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2494
2495         RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data");
2496         RNA_def_string(ot->srna, "name", NULL, MAX_ID_NAME - 2, "Name", "Object name to add");
2497 }
2498
2499 /**************************** Join *************************/
2500
2501 static int join_poll(bContext *C)
2502 {
2503         Object *ob = CTX_data_active_object(C);
2504
2505         if (!ob || ID_IS_LINKED_DATABLOCK(ob)) return 0;
2506
2507         if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_ARMATURE))
2508                 return ED_operator_screenactive(C);
2509         else
2510                 return 0;
2511 }
2512
2513 static int join_exec(bContext *C, wmOperator *op)
2514 {
2515         Scene *scene = CTX_data_scene(C);
2516         Object *ob = CTX_data_active_object(C);
2517
2518         if (scene->obedit) {
2519                 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in edit mode");
2520                 return OPERATOR_CANCELLED;
2521         }
2522         else if (BKE_object_obdata_is_libdata(ob)) {
2523                 BKE_report(op->reports, RPT_ERROR, "Cannot edit external libdata");
2524                 return OPERATOR_CANCELLED;
2525         }
2526
2527         if (ob->type == OB_MESH)
2528                 return join_mesh_exec(C, op);
2529         else if (ELEM(ob->type, OB_CURVE, OB_SURF))
2530                 return join_curve_exec(C, op);
2531         else if (ob->type == OB_ARMATURE)
2532                 return join_armature_exec(C, op);
2533
2534         return OPERATOR_CANCELLED;
2535 }
2536
2537 void OBJECT_OT_join(wmOperatorType *ot)
2538 {
2539         /* identifiers */
2540         ot->name = "Join";
2541         ot->description = "Join selected objects into active object";
2542         ot->idname = "OBJECT_OT_join";
2543
2544         /* api callbacks */
2545         ot->exec = join_exec;
2546         ot->poll = join_poll;
2547
2548         /* flags */
2549         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2550 }
2551
2552 /**************************** Join as Shape Key*************************/
2553
2554 static int join_shapes_poll(bContext *C)
2555 {
2556         Object *ob = CTX_data_active_object(C);
2557
2558         if (!ob || ID_IS_LINKED_DATABLOCK(ob)) return 0;
2559
2560         /* only meshes supported at the moment */
2561         if (ob->type == OB_MESH)
2562                 return ED_operator_screenactive(C);
2563         else
2564                 return 0;
2565 }
2566
2567 static int join_shapes_exec(bContext *C, wmOperator *op)
2568 {
2569         Scene *scene = CTX_data_scene(C);
2570         Object *ob = CTX_data_active_object(C);
2571
2572         if (scene->obedit) {
2573                 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in edit mode");
2574                 return OPERATOR_CANCELLED;
2575         }
2576         else if (BKE_object_obdata_is_libdata(ob)) {
2577                 BKE_report(op->reports, RPT_ERROR, "Cannot edit external libdata");
2578                 return OPERATOR_CANCELLED;
2579         }
2580
2581         if (ob->type == OB_MESH)
2582                 return join_mesh_shapes_exec(C, op);
2583
2584         return OPERATOR_CANCELLED;
2585 }
2586
2587 void OBJECT_OT_join_shapes(wmOperatorType *ot)
2588 {
2589         /* identifiers */
2590         ot->name = "Join as Shapes";
2591         ot->description = "Merge selected objects to shapes of active object";
2592         ot->idname = "OBJECT_OT_join_shapes";
2593
2594         /* api callbacks */
2595         ot->exec = join_shapes_exec;
2596         ot->poll = join_shapes_poll;
2597
2598         /* flags */
2599         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2600 }