Fix use of uninitialized variable in some cases, in recent bugfix.
[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, *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                 Object *newob = NULL;
1693                 Base *base = link->ptr.data;
1694                 ob = base->object;
1695
1696                 if (ob->flag & OB_DONE || !IS_TAGGED(ob->data)) {
1697                         if (ob->type != target) {
1698                                 base->flag &= ~SELECT;
1699                                 ob->flag &= ~SELECT;
1700                         }
1701
1702                         /* obdata already modified */
1703                         if (!IS_TAGGED(ob->data)) {
1704                                 /* When 2 objects with linked data are selected, converting both
1705                                  * would keep modifiers on all but the converted object [#26003] */
1706                                 if (ob->type == OB_MESH) {
1707                                         BKE_object_free_modifiers(ob, 0);  /* after derivedmesh calls! */
1708                                 }
1709                         }
1710                 }
1711                 else if (ob->type == OB_MESH && target == OB_CURVE) {
1712                         ob->flag |= OB_DONE;
1713
1714                         if (keep_original) {
1715                                 basen = duplibase_for_convert(bmain, scene, base, NULL);
1716                                 newob = basen->object;
1717
1718                                 /* decrement original mesh's usage count  */
1719                                 me = newob->data;
1720                                 id_us_min(&me->id);
1721
1722                                 /* make a new copy of the mesh */
1723                                 newob->data = BKE_mesh_copy(bmain, me);
1724                         }
1725                         else {
1726                                 newob = ob;
1727                         }
1728
1729                         BKE_mesh_to_curve(bmain, scene, newob);
1730
1731                         if (newob->type == OB_CURVE) {
1732                                 BKE_object_free_modifiers(newob, 0);   /* after derivedmesh calls! */
1733                                 ED_rigidbody_object_remove(bmain, scene, newob);
1734                         }
1735                 }
1736                 else if (ob->type == OB_MESH) {
1737                         ob->flag |= OB_DONE;
1738
1739                         if (keep_original) {
1740                                 basen = duplibase_for_convert(bmain, scene, base, NULL);
1741                                 newob = basen->object;
1742
1743                                 /* decrement original mesh's usage count  */
1744                                 me = newob->data;
1745                                 id_us_min(&me->id);
1746
1747                                 /* make a new copy of the mesh */
1748                                 newob->data = BKE_mesh_copy(bmain, me);
1749                         }
1750                         else {
1751                                 newob = ob;
1752                                 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1753                         }
1754
1755                         /* make new mesh data from the original copy */
1756                         /* note: get the mesh from the original, not from the copy in some
1757                          * cases this doesnt give correct results (when MDEF is used for eg)
1758                          */
1759                         dm = mesh_get_derived_final(scene, newob, CD_MASK_MESH);
1760
1761                         DM_to_mesh(dm, newob->data, newob, CD_MASK_MESH, true);
1762
1763                         /* re-tessellation is called by DM_to_mesh */
1764
1765                         BKE_object_free_modifiers(newob, 0);   /* after derivedmesh calls! */
1766                 }
1767                 else if (ob->type == OB_FONT) {
1768                         ob->flag |= OB_DONE;
1769
1770                         if (keep_original) {
1771                                 basen = duplibase_for_convert(bmain, scene, base, NULL);
1772                                 newob = basen->object;
1773
1774                                 /* decrement original curve's usage count  */
1775                                 id_us_min(&((Curve *)newob->data)->id);
1776
1777                                 /* make a new copy of the curve */
1778                                 newob->data = BKE_curve_copy(bmain, ob->data);
1779                         }
1780                         else {
1781                                 newob = ob;
1782                         }
1783
1784                         cu = newob->data;
1785
1786                         /* TODO(sergey): Ideally DAG will create nurbs list for a curve data
1787                          *               datablock, but for until we've got granular update
1788                          *               lets take care by selves.
1789                          */
1790                         BKE_vfont_to_curve(newob, FO_EDIT);
1791
1792                         newob->type = OB_CURVE;
1793                         cu->type = OB_CURVE;
1794
1795                         if (cu->vfont) {
1796                                 id_us_min(&cu->vfont->id);
1797                                 cu->vfont = NULL;
1798                         }
1799                         if (cu->vfontb) {
1800                                 id_us_min(&cu->vfontb->id);
1801                                 cu->vfontb = NULL;
1802                         }
1803                         if (cu->vfonti) {
1804                                 id_us_min(&cu->vfonti->id);
1805                                 cu->vfonti = NULL;
1806                         }
1807                         if (cu->vfontbi) {
1808                                 id_us_min(&cu->vfontbi->id);
1809                                 cu->vfontbi = NULL;
1810                         }
1811
1812                         if (!keep_original) {
1813                                 /* other users */
1814                                 if (cu->id.us > 1) {
1815                                         for (ob1 = bmain->object.first; ob1; ob1 = ob1->id.next) {
1816                                                 if (ob1->data == ob->data) {
1817                                                         ob1->type = OB_CURVE;
1818                                                         DAG_id_tag_update(&ob1->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1819                                                 }
1820                                         }
1821                                 }
1822                         }
1823
1824                         for (nu = cu->nurb.first; nu; nu = nu->next)
1825                                 nu->charidx = 0;
1826
1827                         cu->flag &= ~CU_3D;
1828                         BKE_curve_curve_dimension_update(cu);
1829
1830                         if (target == OB_MESH) {
1831                                 curvetomesh(bmain, scene, newob);
1832
1833                                 /* meshes doesn't use displist */
1834                                 BKE_object_free_curve_cache(newob);
1835                         }
1836                 }
1837                 else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
1838                         ob->flag |= OB_DONE;
1839
1840                         if (target == OB_MESH) {
1841                                 if (keep_original) {
1842                                         basen = duplibase_for_convert(bmain, scene, base, NULL);
1843                                         newob = basen->object;
1844
1845                                         /* decrement original curve's usage count  */
1846                                         id_us_min(&((Curve *)newob->data)->id);
1847
1848                                         /* make a new copy of the curve */
1849                                         newob->data = BKE_curve_copy(bmain, ob->data);
1850                                 }
1851                                 else {
1852                                         newob = ob;
1853                                 }
1854
1855                                 curvetomesh(bmain, scene, newob);
1856
1857                                 /* meshes doesn't use displist */
1858                                 BKE_object_free_curve_cache(newob);
1859                         }
1860                 }
1861                 else if (ob->type == OB_MBALL && target == OB_MESH) {
1862                         Object *baseob;
1863
1864                         base->flag &= ~SELECT;
1865                         ob->flag &= ~SELECT;
1866
1867                         baseob = BKE_mball_basis_find(bmain, bmain->eval_ctx, scene, ob);
1868
1869                         if (ob != baseob) {
1870                                 /* if motherball is converting it would be marked as done later */
1871                                 ob->flag |= OB_DONE;
1872                         }
1873
1874                         if (!(baseob->flag & OB_DONE)) {
1875                                 baseob->flag |= OB_DONE;
1876
1877                                 basen = duplibase_for_convert(bmain, scene, base, baseob);
1878                                 newob = basen->object;
1879
1880                                 mb = newob->data;
1881                                 id_us_min(&mb->id);
1882
1883                                 newob->data = BKE_mesh_add(bmain, "Mesh");
1884                                 newob->type = OB_MESH;
1885
1886                                 me = newob->data;
1887                                 me->totcol = mb->totcol;
1888                                 if (newob->totcol) {
1889                                         me->mat = MEM_dupallocN(mb->mat);
1890                                         for (a = 0; a < newob->totcol; a++) id_us_plus((ID *)me->mat[a]);
1891                                 }
1892
1893                                 convert_ensure_curve_cache(bmain, scene, baseob);
1894                                 BKE_mesh_from_metaball(&baseob->curve_cache->disp, newob->data);
1895
1896                                 if (obact->type == OB_MBALL) {
1897                                         basact = basen;
1898                                 }
1899
1900                                 mballConverted = 1;
1901                         }
1902                 }
1903                 else {
1904                         continue;
1905                 }
1906
1907                 /* Ensure new object has consistent material data with its new obdata. */
1908                 if (newob) {
1909                         test_object_materials(bmain, newob, newob->data);
1910                 }
1911
1912                 /* tag obdata if it was been changed */
1913
1914                 /* If the original object is active then make this object active */
1915                 if (basen) {
1916                         if (ob == obact) {
1917                                 /* store new active base to update BASACT */
1918                                 basact = basen;
1919                         }
1920
1921                         basen = NULL;
1922                 }
1923
1924                 if (!keep_original && (ob->flag & OB_DONE)) {
1925                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1926                         ((ID *)ob->data)->tag &= ~LIB_TAG_DOIT; /* flag not to convert this datablock again */
1927                 }
1928         }
1929         BLI_freelistN(&selected_editable_bases);
1930
1931         if (!keep_original) {
1932                 if (mballConverted) {
1933                         Base *base, *base_next;
1934
1935                         for (base = scene->base.first; base; base = base_next) {
1936                                 base_next = base->next;
1937
1938                                 ob = base->object;
1939                                 if (ob->type == OB_MBALL) {
1940                                         if (ob->flag & OB_DONE) {
1941                                                 Object *ob_basis = NULL;
1942                                                 if (BKE_mball_is_basis(ob) ||
1943                                                     ((ob_basis = BKE_mball_basis_find(bmain, bmain->eval_ctx, scene, ob)) && (ob_basis->flag & OB_DONE)))
1944                                                 {
1945                                                         ED_base_object_free_and_unlink(bmain, scene, base);
1946                                                 }
1947                                         }
1948                                 }
1949                         }
1950                 }
1951
1952                 /* delete object should renew depsgraph */
1953                 DAG_relations_tag_update(bmain);
1954         }
1955
1956 // XXX  ED_object_editmode_enter(C, 0);
1957 // XXX  exit_editmode(C, EM_FREEDATA|EM_WAITCURSOR); /* freedata, but no undo */
1958
1959         if (basact) {
1960                 /* active base was changed */
1961                 ED_base_object_activate(C, basact);
1962                 BASACT = basact;
1963         }
1964         else if (BASACT->object->flag & OB_DONE) {
1965                 WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, BASACT->object);
1966                 WM_event_add_notifier(C, NC_OBJECT | ND_DATA, BASACT->object);
1967         }
1968
1969         DAG_relations_tag_update(bmain);
1970         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, scene);
1971         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
1972
1973         return OPERATOR_FINISHED;
1974 }
1975
1976
1977 void OBJECT_OT_convert(wmOperatorType *ot)
1978 {
1979         /* identifiers */
1980         ot->name = "Convert to";
1981         ot->description = "Convert selected objects to another type";
1982         ot->idname = "OBJECT_OT_convert";
1983
1984         /* api callbacks */
1985         ot->invoke = WM_menu_invoke;
1986         ot->exec = convert_exec;
1987         ot->poll = convert_poll;
1988
1989         /* flags */
1990         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1991
1992         /* properties */
1993         ot->prop = RNA_def_enum(ot->srna, "target", convert_target_items, OB_MESH, "Target", "Type of object to convert to");
1994         RNA_def_boolean(ot->srna, "keep_original", 0, "Keep Original", "Keep original objects instead of replacing them");
1995 }
1996
1997 /**************************** Duplicate ************************/
1998
1999 /*
2000  * dupflag: a flag made from constants declared in DNA_userdef_types.h
2001  * The flag tells adduplicate() whether to copy data linked to the object, or to reference the existing data.
2002  * U.dupflag for default operations or you can construct a flag as python does
2003  * if the dupflag is 0 then no data will be copied (linked duplicate) */
2004
2005 /* used below, assumes id.new is correct */
2006 /* leaves selection of base/object unaltered */
2007 /* Does set ID->newid pointers. */
2008 static Base *object_add_duplicate_internal(Main *bmain, Scene *scene, Base *base, int dupflag)
2009 {
2010 #define ID_NEW_REMAP_US(a)      if (      (a)->id.newid) { (a) = (void *)(a)->id.newid;       (a)->id.us++; }
2011 #define ID_NEW_REMAP_US2(a)     if (((ID *)a)->newid)    { (a) = ((ID  *)a)->newid;     ((ID *)a)->us++;    }
2012
2013         Base *basen = NULL;
2014         Material ***matarar;
2015         Object *ob, *obn;
2016         ID *id;
2017         int a, didit;
2018
2019         ob = base->object;
2020         if (ob->mode & OB_MODE_POSE) {
2021                 ; /* nothing? */
2022         }
2023         else {
2024                 obn = ID_NEW_SET(ob, BKE_object_copy(bmain, ob));
2025                 DAG_id_tag_update(&obn->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
2026
2027                 basen = MEM_mallocN(sizeof(Base), "duplibase");
2028                 *basen = *base;
2029                 BLI_addhead(&scene->base, basen);   /* addhead: prevent eternal loop */
2030                 basen->object = obn;
2031
2032                 /* 1) duplis should end up in same group as the original
2033                  * 2) Rigid Body sim participants MUST always be part of a group...
2034                  */
2035                 // XXX: is 2) really a good measure here?
2036                 if ((basen->flag & OB_FROMGROUP) || ob->rigidbody_object || ob->rigidbody_constraint) {
2037                         Group *group;
2038                         for (group = bmain->group.first; group; group = group->id.next) {
2039                                 if (BKE_group_object_exists(group, ob))
2040                                         BKE_group_object_add(group, obn, scene, basen);
2041                         }
2042                 }
2043
2044                 /* duplicates using userflags */
2045                 if (dupflag & USER_DUP_ACT) {
2046                         BKE_animdata_copy_id_action(bmain, &obn->id, true);
2047                 }
2048
2049                 if (dupflag & USER_DUP_MAT) {
2050                         for (a = 0; a < obn->totcol; a++) {
2051                                 id = (ID *)obn->mat[a];
2052                                 if (id) {
2053                                         ID_NEW_REMAP_US(obn->mat[a])
2054                                         else {
2055                                                 obn->mat[a] = ID_NEW_SET(obn->mat[a], BKE_material_copy(bmain, obn->mat[a]));
2056                                         }
2057                                         id_us_min(id);
2058
2059                                         if (dupflag & USER_DUP_ACT) {
2060                                                 BKE_animdata_copy_id_action(bmain, &obn->mat[a]->id, true);
2061                                         }
2062                                 }
2063                         }
2064                 }
2065                 if (dupflag & USER_DUP_PSYS) {
2066                         ParticleSystem *psys;
2067                         for (psys = obn->particlesystem.first; psys; psys = psys->next) {
2068                                 id = (ID *) psys->part;
2069                                 if (id) {
2070                                         ID_NEW_REMAP_US(psys->part)
2071                                         else {
2072                                                 psys->part = ID_NEW_SET(psys->part, BKE_particlesettings_copy(bmain, psys->part));
2073                                         }
2074
2075                                         if (dupflag & USER_DUP_ACT) {
2076                                                 BKE_animdata_copy_id_action(bmain, &psys->part->id, true);
2077                                         }
2078
2079                                         id_us_min(id);
2080                                 }
2081                         }
2082                 }
2083
2084                 id = obn->data;
2085                 didit = 0;
2086
2087                 switch (obn->type) {
2088                         case OB_MESH:
2089                                 if (dupflag & USER_DUP_MESH) {
2090                                         ID_NEW_REMAP_US2(obn->data)
2091                                         else {
2092                                                 obn->data = ID_NEW_SET(obn->data, BKE_mesh_copy(bmain, obn->data));
2093                                                 didit = 1;
2094                                         }
2095                                         id_us_min(id);
2096                                 }
2097                                 break;
2098                         case OB_CURVE:
2099                                 if (dupflag & USER_DUP_CURVE) {
2100                                         ID_NEW_REMAP_US2(obn->data)
2101                                         else {
2102                                                 obn->data = ID_NEW_SET(obn->data, BKE_curve_copy(bmain, obn->data));
2103                                                 didit = 1;
2104                                         }
2105                                         id_us_min(id);
2106                                 }
2107                                 break;
2108                         case OB_SURF:
2109                                 if (dupflag & USER_DUP_SURF) {
2110                                         ID_NEW_REMAP_US2(obn->data)
2111                                         else {
2112                                                 obn->data = ID_NEW_SET(obn->data, BKE_curve_copy(bmain, obn->data));
2113                                                 didit = 1;
2114                                         }
2115                                         id_us_min(id);
2116                                 }
2117                                 break;
2118                         case OB_FONT:
2119                                 if (dupflag & USER_DUP_FONT) {
2120                                         ID_NEW_REMAP_US2(obn->data)
2121                                         else {
2122                                                 obn->data = ID_NEW_SET(obn->data, BKE_curve_copy(bmain, obn->data));
2123                                                 didit = 1;
2124                                         }
2125                                         id_us_min(id);
2126                                 }
2127                                 break;
2128                         case OB_MBALL:
2129                                 if (dupflag & USER_DUP_MBALL) {
2130                                         ID_NEW_REMAP_US2(obn->data)
2131                                         else {
2132                                                 obn->data = ID_NEW_SET(obn->data, BKE_mball_copy(bmain, obn->data));
2133                                                 didit = 1;
2134                                         }
2135                                         id_us_min(id);
2136                                 }
2137                                 break;
2138                         case OB_LAMP:
2139                                 if (dupflag & USER_DUP_LAMP) {
2140                                         ID_NEW_REMAP_US2(obn->data)
2141                                         else {
2142                                                 obn->data = ID_NEW_SET(obn->data, BKE_lamp_copy(bmain, obn->data));
2143                                                 didit = 1;
2144                                         }
2145                                         id_us_min(id);
2146                                 }
2147                                 break;
2148                         case OB_ARMATURE:
2149                                 DAG_id_tag_update(&obn->id, OB_RECALC_DATA);
2150                                 if (obn->pose)
2151                                         BKE_pose_tag_recalc(bmain, obn->pose);
2152                                 if (dupflag & USER_DUP_ARM) {
2153                                         ID_NEW_REMAP_US2(obn->data)
2154                                         else {
2155                                                 obn->data = ID_NEW_SET(obn->data, BKE_armature_copy(bmain, obn->data));
2156                                                 BKE_pose_rebuild(obn, obn->data);
2157                                                 didit = 1;
2158                                         }
2159                                         id_us_min(id);
2160                                 }
2161                                 break;
2162                         case OB_LATTICE:
2163                                 if (dupflag != 0) {
2164                                         ID_NEW_REMAP_US2(obn->data)
2165                                         else {
2166                                                 obn->data = ID_NEW_SET(obn->data, BKE_lattice_copy(bmain, obn->data));
2167                                                 didit = 1;
2168                                         }
2169                                         id_us_min(id);
2170                                 }
2171                                 break;
2172                         case OB_CAMERA:
2173                                 if (dupflag != 0) {
2174                                         ID_NEW_REMAP_US2(obn->data)
2175                                         else {
2176                                                 obn->data = ID_NEW_SET(obn->data, BKE_camera_copy(bmain, obn->data));
2177                                                 didit = 1;
2178                                         }
2179                                         id_us_min(id);
2180                                 }
2181                                 break;
2182                         case OB_SPEAKER:
2183                                 if (dupflag != 0) {
2184                                         ID_NEW_REMAP_US2(obn->data)
2185                                         else {
2186                                                 obn->data = ID_NEW_SET(obn->data, BKE_speaker_copy(bmain, obn->data));
2187                                                 didit = 1;
2188                                         }
2189                                         id_us_min(id);
2190                                 }
2191                                 break;
2192                 }
2193
2194                 /* check if obdata is copied */
2195                 if (didit) {
2196                         Key *key = BKE_key_from_object(obn);
2197
2198                         Key *oldkey = BKE_key_from_object(ob);
2199                         if (oldkey != NULL) {
2200                                 ID_NEW_SET(oldkey, key);
2201                         }
2202
2203                         if (dupflag & USER_DUP_ACT) {
2204                                 bActuator *act;
2205
2206                                 BKE_animdata_copy_id_action(bmain, (ID *)obn->data, true);
2207                                 if (key) {
2208                                         BKE_animdata_copy_id_action(bmain, (ID *)key, true);
2209                                 }
2210
2211                                 /* Update the duplicated action in the action actuators */
2212                                 /* XXX TODO this code is all wrong! actact->act is user-refcounted (see readfile.c),
2213                                  * and what about other ID pointers of other BGE logic bricks,
2214                                  * and since this is object-level, why is it only ran if obdata was duplicated??? -mont29 */
2215                                 for (act = obn->actuators.first; act; act = act->next) {
2216                                         if (act->type == ACT_ACTION) {
2217                                                 bActionActuator *actact = (bActionActuator *) act->data;
2218                                                 if (ob->adt && actact->act == ob->adt->action) {
2219                                                         actact->act = obn->adt->action;
2220                                                 }
2221                                         }
2222                                 }
2223                         }
2224
2225                         if (dupflag & USER_DUP_MAT) {
2226                                 matarar = give_matarar(obn);
2227                                 if (matarar) {
2228                                         for (a = 0; a < obn->totcol; a++) {
2229                                                 id = (ID *)(*matarar)[a];
2230                                                 if (id) {
2231                                                         ID_NEW_REMAP_US((*matarar)[a])
2232                                                         else {
2233                                                                 (*matarar)[a] = ID_NEW_SET((*matarar)[a], BKE_material_copy(bmain, (*matarar)[a]));
2234                                                         }
2235                                                         id_us_min(id);
2236                                                 }
2237                                         }
2238                                 }
2239                         }
2240                 }
2241         }
2242         return basen;
2243
2244 #undef ID_NEW_REMAP_US
2245 #undef ID_NEW_REMAP_US2
2246 }
2247
2248 /* single object duplicate, if dupflag==0, fully linked, else it uses the flags given */
2249 /* leaves selection of base/object unaltered.
2250  * note: don't call this within a loop since clear_* funcs loop over the entire database.
2251  * note: caller must do DAG_relations_tag_update(bmain);
2252  *       this is not done automatic since we may duplicate many objects in a batch */
2253 Base *ED_object_add_duplicate(Main *bmain, Scene *scene, Base *base, int dupflag)
2254 {
2255         Base *basen;
2256         Object *ob;
2257
2258         clear_sca_new_poins();  /* BGE logic */
2259
2260         basen = object_add_duplicate_internal(bmain, scene, base, dupflag);
2261         if (basen == NULL) {
2262                 return NULL;
2263         }
2264
2265         ob = basen->object;
2266
2267         /* link own references to the newly duplicated data [#26816] */
2268         BKE_libblock_relink_to_newid(&ob->id);
2269         set_sca_new_poins_ob(ob);
2270
2271         /* DAG_relations_tag_update(bmain); */ /* caller must do */
2272
2273         if (ob->data) {
2274                 ED_render_id_flush_update(bmain, ob->data);
2275         }
2276
2277         BKE_main_id_clear_newpoins(bmain);
2278
2279         return basen;
2280 }
2281
2282 /* contextual operator dupli */
2283 static int duplicate_exec(bContext *C, wmOperator *op)
2284 {
2285         Main *bmain = CTX_data_main(C);
2286         Scene *scene = CTX_data_scene(C);
2287         const bool linked = RNA_boolean_get(op->ptr, "linked");
2288         int dupflag = (linked) ? 0 : U.dupflag;
2289
2290         clear_sca_new_poins();  /* BGE logic */
2291
2292         CTX_DATA_BEGIN (C, Base *, base, selected_bases)
2293         {
2294                 Base *basen = object_add_duplicate_internal(bmain, scene, base, dupflag);
2295
2296                 /* note that this is safe to do with this context iterator,
2297                  * the list is made in advance */
2298                 ED_base_object_select(base, BA_DESELECT);
2299
2300                 if (basen == NULL) {
2301                         continue;
2302                 }
2303
2304                 /* new object becomes active */
2305                 if (BASACT == base)
2306                         ED_base_object_activate(C, basen);
2307
2308                 if (basen->object->data) {
2309                         DAG_id_tag_update(basen->object->data, 0);
2310                 }
2311         }
2312         CTX_DATA_END;
2313
2314         copy_object_set_idnew(C);
2315
2316         BKE_main_id_clear_newpoins(bmain);
2317
2318         DAG_relations_tag_update(bmain);
2319
2320         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
2321
2322         return OPERATOR_FINISHED;
2323 }
2324
2325 void OBJECT_OT_duplicate(wmOperatorType *ot)
2326 {
2327         PropertyRNA *prop;
2328
2329         /* identifiers */
2330         ot->name = "Duplicate Objects";
2331         ot->description = "Duplicate selected objects";
2332         ot->idname = "OBJECT_OT_duplicate";
2333
2334         /* api callbacks */
2335         ot->exec = duplicate_exec;
2336         ot->poll = ED_operator_objectmode;
2337
2338         /* flags */
2339         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2340
2341         /* to give to transform */
2342         RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data");
2343         prop = RNA_def_enum(ot->srna, "mode", rna_enum_transform_mode_types, TFM_TRANSLATION, "Mode", "");
2344         RNA_def_property_flag(prop, PROP_HIDDEN);
2345 }
2346
2347 /* **************** add named object, for dragdrop ************* */
2348
2349 static int add_named_exec(bContext *C, wmOperator *op)
2350 {
2351         wmWindow *win = CTX_wm_window(C);
2352         const wmEvent *event = win ? win->eventstate : NULL;
2353         Main *bmain = CTX_data_main(C);
2354         View3D *v3d = CTX_wm_view3d(C);  /* may be NULL */
2355         Scene *scene = CTX_data_scene(C);
2356         Base *basen, *base;
2357         Object *ob;
2358         const bool linked = RNA_boolean_get(op->ptr, "linked");
2359         int dupflag = (linked) ? 0 : U.dupflag;
2360         char name[MAX_ID_NAME - 2];
2361
2362         /* find object, create fake base */
2363         RNA_string_get(op->ptr, "name", name);
2364         ob = (Object *)BKE_libblock_find_name(bmain, ID_OB, name);
2365
2366         if (ob == NULL) {
2367                 BKE_report(op->reports, RPT_ERROR, "Object not found");
2368                 return OPERATOR_CANCELLED;
2369         }
2370
2371         base = MEM_callocN(sizeof(Base), "duplibase");
2372         base->object = ob;
2373         base->flag = ob->flag;
2374
2375         /* prepare dupli */
2376         clear_sca_new_poins();  /* BGE logic */
2377
2378         basen = object_add_duplicate_internal(bmain, scene, base, dupflag);
2379
2380         if (basen == NULL) {
2381                 MEM_freeN(base);
2382                 BKE_report(op->reports, RPT_ERROR, "Object could not be duplicated");
2383                 return OPERATOR_CANCELLED;
2384         }
2385
2386         basen->lay = basen->object->lay = BKE_screen_view3d_layer_active(v3d, scene);
2387         basen->object->restrictflag &= ~OB_RESTRICT_VIEW;
2388
2389         if (event) {
2390                 ARegion *ar = CTX_wm_region(C);
2391                 const int mval[2] = {event->x - ar->winrct.xmin,
2392                                      event->y - ar->winrct.ymin};
2393                 ED_object_location_from_view(C, basen->object->loc);
2394                 ED_view3d_cursor3d_position(C, mval, basen->object->loc);
2395         }
2396
2397         ED_base_object_select(basen, BA_SELECT);
2398         ED_base_object_activate(C, basen);
2399
2400         copy_object_set_idnew(C);
2401
2402         BKE_main_id_clear_newpoins(bmain);
2403
2404         DAG_relations_tag_update(bmain);
2405
2406         MEM_freeN(base);
2407
2408         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
2409         WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
2410
2411         return OPERATOR_FINISHED;
2412 }
2413
2414 void OBJECT_OT_add_named(wmOperatorType *ot)
2415 {
2416         /* identifiers */
2417         ot->name = "Add Named Object";
2418         ot->description = "Add named object";
2419         ot->idname = "OBJECT_OT_add_named";
2420
2421         /* api callbacks */
2422         ot->exec = add_named_exec;
2423         ot->poll = ED_operator_objectmode;
2424
2425         /* flags */
2426         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2427
2428         RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data");
2429         RNA_def_string(ot->srna, "name", NULL, MAX_ID_NAME - 2, "Name", "Object name to add");
2430 }
2431
2432 /**************************** Join *************************/
2433
2434 static bool join_poll(bContext *C)
2435 {
2436         Object *ob = CTX_data_active_object(C);
2437
2438         if (!ob || ID_IS_LINKED(ob)) return 0;
2439
2440         if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_ARMATURE))
2441                 return ED_operator_screenactive(C);
2442         else
2443                 return 0;
2444 }
2445
2446 static int join_exec(bContext *C, wmOperator *op)
2447 {
2448         Scene *scene = CTX_data_scene(C);
2449         Object *ob = CTX_data_active_object(C);
2450
2451         if (scene->obedit) {
2452                 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in edit mode");
2453                 return OPERATOR_CANCELLED;
2454         }
2455         else if (BKE_object_obdata_is_libdata(ob)) {
2456                 BKE_report(op->reports, RPT_ERROR, "Cannot edit external libdata");
2457                 return OPERATOR_CANCELLED;
2458         }
2459
2460         if (ob->type == OB_MESH)
2461                 return join_mesh_exec(C, op);
2462         else if (ELEM(ob->type, OB_CURVE, OB_SURF))
2463                 return join_curve_exec(C, op);
2464         else if (ob->type == OB_ARMATURE)
2465                 return join_armature_exec(C, op);
2466
2467         return OPERATOR_CANCELLED;
2468 }
2469
2470 void OBJECT_OT_join(wmOperatorType *ot)
2471 {
2472         /* identifiers */
2473         ot->name = "Join";
2474         ot->description = "Join selected objects into active object";
2475         ot->idname = "OBJECT_OT_join";
2476
2477         /* api callbacks */
2478         ot->exec = join_exec;
2479         ot->poll = join_poll;
2480
2481         /* flags */
2482         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2483 }
2484
2485 /**************************** Join as Shape Key*************************/
2486
2487 static bool join_shapes_poll(bContext *C)
2488 {
2489         Object *ob = CTX_data_active_object(C);
2490
2491         if (!ob || ID_IS_LINKED(ob)) return 0;
2492
2493         /* only meshes supported at the moment */
2494         if (ob->type == OB_MESH)
2495                 return ED_operator_screenactive(C);
2496         else
2497                 return 0;
2498 }
2499
2500 static int join_shapes_exec(bContext *C, wmOperator *op)
2501 {
2502         Scene *scene = CTX_data_scene(C);
2503         Object *ob = CTX_data_active_object(C);
2504
2505         if (scene->obedit) {
2506                 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in edit mode");
2507                 return OPERATOR_CANCELLED;
2508         }
2509         else if (BKE_object_obdata_is_libdata(ob)) {
2510                 BKE_report(op->reports, RPT_ERROR, "Cannot edit external libdata");
2511                 return OPERATOR_CANCELLED;
2512         }
2513
2514         if (ob->type == OB_MESH)
2515                 return join_mesh_shapes_exec(C, op);
2516
2517         return OPERATOR_CANCELLED;
2518 }
2519
2520 void OBJECT_OT_join_shapes(wmOperatorType *ot)
2521 {
2522         /* identifiers */
2523         ot->name = "Join as Shapes";
2524         ot->description = "Merge selected objects to shapes of active object";
2525         ot->idname = "OBJECT_OT_join_shapes";
2526
2527         /* api callbacks */
2528         ot->exec = join_shapes_exec;
2529         ot->poll = join_shapes_poll;
2530
2531         /* flags */
2532         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2533 }