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