Cleanup: mostly namings in `make_object_duplilist_real` code.
[blender.git] / source / blender / editors / object / object_add.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation, 2002-2008 full recode
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/object/object_add.c
27  *  \ingroup edobj
28  */
29
30
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "MEM_guardedalloc.h"
35
36 #include "DNA_anim_types.h"
37 #include "DNA_camera_types.h"
38 #include "DNA_curve_types.h"
39 #include "DNA_group_types.h"
40 #include "DNA_lamp_types.h"
41 #include "DNA_key_types.h"
42 #include "DNA_material_types.h"
43 #include "DNA_mesh_types.h"
44 #include "DNA_meta_types.h"
45 #include "DNA_object_fluidsim.h"
46 #include "DNA_object_force.h"
47 #include "DNA_object_types.h"
48 #include "DNA_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 #include "BKE_texture.h"
94
95 #include "RNA_access.h"
96 #include "RNA_define.h"
97 #include "RNA_enum_types.h"
98
99 #include "WM_api.h"
100 #include "WM_types.h"
101
102 #include "ED_armature.h"
103 #include "ED_curve.h"
104 #include "ED_mball.h"
105 #include "ED_mesh.h"
106 #include "ED_node.h"
107 #include "ED_object.h"
108 #include "ED_physics.h"
109 #include "ED_render.h"
110 #include "ED_screen.h"
111 #include "ED_transform.h"
112 #include "ED_view3d.h"
113
114 #include "UI_resources.h"
115
116 #include "GPU_material.h"
117
118 #include "object_intern.h"
119
120 /* this is an exact copy of the define in rna_lamp.c
121  * kept here because of linking order.
122  * Icons are only defined here */
123 EnumPropertyItem rna_enum_lamp_type_items[] = {
124         {LA_LOCAL, "POINT", ICON_LAMP_POINT, "Point", "Omnidirectional point light source"},
125         {LA_SUN, "SUN", ICON_LAMP_SUN, "Sun", "Constant direction parallel ray light source"},
126         {LA_SPOT, "SPOT", ICON_LAMP_SPOT, "Spot", "Directional cone light source"},
127         {LA_HEMI, "HEMI", ICON_LAMP_HEMI, "Hemi", "180 degree constant light source"},
128         {LA_AREA, "AREA", ICON_LAMP_AREA, "Area", "Directional area light source"},
129         {0, NULL, 0, NULL, NULL}
130 };
131
132 /* copy from rna_object_force.c */
133 static EnumPropertyItem field_type_items[] = {
134         {PFIELD_FORCE, "FORCE", ICON_FORCE_FORCE, "Force", ""},
135         {PFIELD_WIND, "WIND", ICON_FORCE_WIND, "Wind", ""},
136         {PFIELD_VORTEX, "VORTEX", ICON_FORCE_VORTEX, "Vortex", ""},
137         {PFIELD_MAGNET, "MAGNET", ICON_FORCE_MAGNETIC, "Magnetic", ""},
138         {PFIELD_HARMONIC, "HARMONIC", ICON_FORCE_HARMONIC, "Harmonic", ""},
139         {PFIELD_CHARGE, "CHARGE", ICON_FORCE_CHARGE, "Charge", ""},
140         {PFIELD_LENNARDJ, "LENNARDJ", ICON_FORCE_LENNARDJONES, "Lennard-Jones", ""},
141         {PFIELD_TEXTURE, "TEXTURE", ICON_FORCE_TEXTURE, "Texture", ""},
142         {PFIELD_GUIDE, "GUIDE", ICON_FORCE_CURVE, "Curve Guide", ""},
143         {PFIELD_BOID, "BOID", ICON_FORCE_BOID, "Boid", ""},
144         {PFIELD_TURBULENCE, "TURBULENCE", ICON_FORCE_TURBULENCE, "Turbulence", ""},
145         {PFIELD_DRAG, "DRAG", ICON_FORCE_DRAG, "Drag", ""},
146         {PFIELD_SMOKEFLOW, "SMOKE", ICON_FORCE_SMOKEFLOW, "Smoke Flow", ""},
147         {0, NULL, 0, NULL, NULL}
148 };
149
150 /************************** Exported *****************************/
151
152 void ED_object_location_from_view(bContext *C, float loc[3])
153 {
154         View3D *v3d = CTX_wm_view3d(C);
155         Scene *scene = CTX_data_scene(C);
156         const float *cursor;
157
158         cursor = ED_view3d_cursor3d_get(scene, v3d);
159
160         copy_v3_v3(loc, cursor);
161 }
162
163 void ED_object_rotation_from_view(bContext *C, float rot[3], const char align_axis)
164 {
165         RegionView3D *rv3d = CTX_wm_region_view3d(C);
166
167         BLI_assert(align_axis >= 'X' && align_axis <= 'Z');
168
169         if (rv3d) {
170                 float quat[4];
171
172                 switch (align_axis) {
173                         case 'X':
174                         {
175                                 float quat_y[4];
176                                 axis_angle_to_quat(quat_y, rv3d->viewinv[1], -M_PI_2);
177                                 mul_qt_qtqt(quat, rv3d->viewquat, quat_y);
178                                 quat[0] = -quat[0];
179
180                                 quat_to_eul(rot, quat);
181                                 break;
182                         }
183                         case 'Y':
184                         {
185                                 copy_qt_qt(quat, rv3d->viewquat);
186                                 quat[0] = -quat[0];
187
188                                 quat_to_eul(rot, quat);
189                                 rot[0] -= (float)M_PI_2;
190                                 break;
191                         }
192                         case 'Z':
193                         {
194                                 copy_qt_qt(quat, rv3d->viewquat);
195                                 quat[0] = -quat[0];
196
197                                 quat_to_eul(rot, quat);
198                                 break;
199                         }
200                 }
201
202         }
203         else {
204                 zero_v3(rot);
205         }
206 }
207
208 void ED_object_base_init_transform(bContext *C, Base *base, const float loc[3], const float rot[3])
209 {
210         Object *ob = base->object;
211         Scene *scene = CTX_data_scene(C);
212
213         if (!scene) return;
214
215         if (loc)
216                 copy_v3_v3(ob->loc, loc);
217
218         if (rot)
219                 copy_v3_v3(ob->rot, rot);
220
221         BKE_object_where_is_calc(scene, ob);
222 }
223
224 /* Uses context to figure out transform for primitive.
225  * Returns standard diameter. */
226 float ED_object_new_primitive_matrix(
227         bContext *C, Object *obedit,
228         const float loc[3], const float rot[3], float primmat[4][4])
229 {
230         Scene *scene = CTX_data_scene(C);
231         View3D *v3d = CTX_wm_view3d(C);
232         float mat[3][3], rmat[3][3], cmat[3][3], imat[3][3];
233
234         unit_m4(primmat);
235
236         eul_to_mat3(rmat, rot);
237         invert_m3(rmat);
238
239         /* inverse transform for initial rotation and object */
240         copy_m3_m4(mat, obedit->obmat);
241         mul_m3_m3m3(cmat, rmat, mat);
242         invert_m3_m3(imat, cmat);
243         copy_m4_m3(primmat, imat);
244
245         /* center */
246         copy_v3_v3(primmat[3], loc);
247         sub_v3_v3v3(primmat[3], primmat[3], obedit->obmat[3]);
248         invert_m3_m3(imat, mat);
249         mul_m3_v3(imat, primmat[3]);
250
251         {
252                 const float dia = v3d ? ED_view3d_grid_scale(scene, v3d, NULL) : ED_scene_grid_scale(scene, NULL);
253                 return dia;
254         }
255
256         // return 1.0f;
257 }
258
259 /********************* Add Object Operator ********************/
260
261 static void view_align_update(struct Main *UNUSED(main), struct Scene *UNUSED(scene), struct PointerRNA *ptr)
262 {
263         RNA_struct_idprops_unset(ptr, "rotation");
264 }
265
266 void ED_object_add_unit_props(wmOperatorType *ot)
267 {
268         RNA_def_float_distance(ot->srna, "radius", 1.0f, 0.0, OBJECT_ADD_SIZE_MAXF, "Radius", "", 0.001, 100.00);
269 }
270
271 void ED_object_add_generic_props(wmOperatorType *ot, bool do_editmode)
272 {
273         PropertyRNA *prop;
274
275         /* note: this property gets hidden for add-camera operator */
276         prop = RNA_def_boolean(ot->srna, "view_align", 0, "Align to View", "Align the new object to the view");
277         RNA_def_property_update_runtime(prop, view_align_update);
278
279         if (do_editmode) {
280                 prop = RNA_def_boolean(ot->srna, "enter_editmode", 0, "Enter Editmode",
281                                        "Enter editmode when adding this object");
282                 RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
283         }
284
285         prop = RNA_def_float_vector_xyz(ot->srna, "location", 3, NULL, -OBJECT_ADD_SIZE_MAXF, OBJECT_ADD_SIZE_MAXF,
286                                         "Location", "Location for the newly added object", -1000.0f, 1000.0f);
287         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
288         prop = RNA_def_float_rotation(ot->srna, "rotation", 3, NULL, -OBJECT_ADD_SIZE_MAXF, OBJECT_ADD_SIZE_MAXF,
289                                       "Rotation", "Rotation for the newly added object",
290                                       DEG2RADF(-360.0f), DEG2RADF(360.0f));
291         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
292
293         prop = RNA_def_boolean_layer_member(ot->srna, "layers", 20, NULL, "Layer", "");
294         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
295 }
296
297 void ED_object_add_mesh_props(wmOperatorType *ot)
298 {
299         RNA_def_boolean(ot->srna, "calc_uvs", false, "Generate UVs", "Generate a default UV map");
300 }
301
302 bool ED_object_add_generic_get_opts(bContext *C, wmOperator *op, const char view_align_axis,
303                                     float loc[3], float rot[3],
304                                     bool *enter_editmode, unsigned int *layer, bool *is_view_aligned)
305 {
306         View3D *v3d = CTX_wm_view3d(C);
307         unsigned int _layer;
308         PropertyRNA *prop;
309
310         /* Switch to Edit mode? optional prop */
311         if ((prop = RNA_struct_find_property(op->ptr, "enter_editmode"))) {
312                 bool _enter_editmode;
313                 if (!enter_editmode)
314                         enter_editmode = &_enter_editmode;
315
316                 if (RNA_property_is_set(op->ptr, prop) && enter_editmode)
317                         *enter_editmode = RNA_property_boolean_get(op->ptr, prop);
318                 else {
319                         *enter_editmode = (U.flag & USER_ADD_EDITMODE) != 0;
320                         RNA_property_boolean_set(op->ptr, prop, *enter_editmode);
321                 }
322         }
323
324         /* Get layers! */
325         {
326                 int a, layer_values[20];
327                 if (!layer)
328                         layer = &_layer;
329
330                 prop = RNA_struct_find_property(op->ptr, "layers");
331                 if (RNA_property_is_set(op->ptr, prop)) {
332                         RNA_property_boolean_get_array(op->ptr, prop, layer_values);
333                         *layer = 0;
334                         for (a = 0; a < 20; a++) {
335                                 if (layer_values[a])
336                                         *layer |= (1 << a);
337                         }
338                 }
339                 else {
340                         Scene *scene = CTX_data_scene(C);
341                         *layer = BKE_screen_view3d_layer_active_ex(v3d, scene, false);
342                         for (a = 0; a < 20; a++) {
343                                 layer_values[a] = (*layer & (1 << a)) != 0;
344                         }
345                         RNA_property_boolean_set_array(op->ptr, prop, layer_values);
346                 }
347
348                 /* in local view we additionally add local view layers,
349                  * not part of operator properties */
350                 if (v3d && v3d->localvd)
351                         *layer |= v3d->lay;
352         }
353
354         /* Location! */
355         {
356                 float _loc[3];
357                 if (!loc)
358                         loc = _loc;
359
360                 if (RNA_struct_property_is_set(op->ptr, "location")) {
361                         RNA_float_get_array(op->ptr, "location", loc);
362                 }
363                 else {
364                         ED_object_location_from_view(C, loc);
365                         RNA_float_set_array(op->ptr, "location", loc);
366                 }
367         }
368
369         /* Rotation! */
370         {
371                 bool _is_view_aligned;
372                 float _rot[3];
373                 if (!is_view_aligned)
374                         is_view_aligned = &_is_view_aligned;
375                 if (!rot)
376                         rot = _rot;
377
378                 if (RNA_struct_property_is_set(op->ptr, "rotation"))
379                         *is_view_aligned = false;
380                 else if (RNA_struct_property_is_set(op->ptr, "view_align"))
381                         *is_view_aligned = RNA_boolean_get(op->ptr, "view_align");
382                 else {
383                         *is_view_aligned = (U.flag & USER_ADD_VIEWALIGNED) != 0;
384                         RNA_boolean_set(op->ptr, "view_align", *is_view_aligned);
385                 }
386
387                 if (*is_view_aligned) {
388                         ED_object_rotation_from_view(C, rot, view_align_axis);
389                         RNA_float_set_array(op->ptr, "rotation", rot);
390                 }
391                 else
392                         RNA_float_get_array(op->ptr, "rotation", rot);
393         }
394
395         if (layer && *layer == 0) {
396                 BKE_report(op->reports, RPT_ERROR, "Property 'layer' has no values set");
397                 return false;
398         }
399
400         return true;
401 }
402
403 /* For object add primitive operators.
404  * Do not call undo push in this function (users of this function have to). */
405 Object *ED_object_add_type(
406         bContext *C,
407         int type, const char *name,
408         const float loc[3], const float rot[3],
409         bool enter_editmode, unsigned int layer)
410 {
411         Main *bmain = CTX_data_main(C);
412         Scene *scene = CTX_data_scene(C);
413         Object *ob;
414
415         /* for as long scene has editmode... */
416         if (CTX_data_edit_object(C)) 
417                 ED_object_editmode_exit(C, EM_FREEDATA | EM_FREEUNDO | EM_WAITCURSOR | EM_DO_UNDO);  /* freedata, and undo */
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_edit_bone_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, ob->loc, event->mval);
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         Group *group;
972         unsigned int layer;
973         float loc[3], rot[3];
974         
975         if (RNA_struct_property_is_set(op->ptr, "name")) {
976                 char name[MAX_ID_NAME - 2];
977                 
978                 RNA_string_get(op->ptr, "name", name);
979                 group = (Group *)BKE_libblock_find_name(ID_GR, name);
980                 
981                 if (0 == RNA_struct_property_is_set(op->ptr, "location")) {
982                         wmEvent *event = CTX_wm_window(C)->eventstate;
983                         ARegion *ar = CTX_wm_region(C);
984                         const int mval[2] = {event->x - ar->winrct.xmin,
985                                              event->y - ar->winrct.ymin};
986                         ED_object_location_from_view(C, loc);
987                         ED_view3d_cursor3d_position(C, loc, mval);
988                         RNA_float_set_array(op->ptr, "location", loc);
989                 }
990         }
991         else
992                 group = BLI_findlink(&CTX_data_main(C)->group, RNA_enum_get(op->ptr, "group"));
993
994         if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, NULL, &layer, NULL))
995                 return OPERATOR_CANCELLED;
996
997         if (group) {
998                 Main *bmain = CTX_data_main(C);
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 = add_nlatrack(adt, NULL);
1064                 NlaStrip *strip = add_nla_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_DATABLOCK(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->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 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->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(ob); /* also does users */
1575
1576         if (ob->type == OB_MESH) {
1577                 BKE_object_free_modifiers(ob);
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 int 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_DATABLOCK(scene) && obact && scene->obedit != obact &&
1591                 (obact->flag & SELECT) && !ID_IS_LINKED_DATABLOCK(obact));
1592 }
1593
1594 /* Helper for convert_exec */
1595 static Base *duplibase_for_convert(Main *bmain, Scene *scene, Base *base, Object *ob)
1596 {
1597         Object *obn;
1598         Base *basen;
1599
1600         if (ob == NULL) {
1601                 ob = base->object;
1602         }
1603
1604         obn = BKE_object_copy(bmain, ob);
1605         DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1606
1607         basen = MEM_mallocN(sizeof(Base), "duplibase");
1608         *basen = *base;
1609         BLI_addhead(&scene->base, basen);   /* addhead: otherwise eternal loop */
1610         basen->object = obn;
1611         basen->flag |= SELECT;
1612         obn->flag |= SELECT;
1613         base->flag &= ~SELECT;
1614         ob->flag &= ~SELECT;
1615
1616         return basen;
1617 }
1618
1619 static int convert_exec(bContext *C, wmOperator *op)
1620 {
1621         Main *bmain = CTX_data_main(C);
1622         Scene *scene = CTX_data_scene(C);
1623         Base *basen = NULL, *basact = NULL;
1624         Object *ob, *ob1, *newob, *obact = CTX_data_active_object(C);
1625         DerivedMesh *dm;
1626         Curve *cu;
1627         Nurb *nu;
1628         MetaBall *mb;
1629         Mesh *me;
1630         const short target = RNA_enum_get(op->ptr, "target");
1631         bool keep_original = RNA_boolean_get(op->ptr, "keep_original");
1632         int a, mballConverted = 0;
1633
1634         /* don't forget multiple users! */
1635
1636         {
1637                 Base *base;
1638
1639                 for (base = scene->base.first; base; base = base->next) {
1640                         ob = base->object;
1641                         ob->flag &= ~OB_DONE;
1642
1643                         /* flag data thats not been edited (only needed for !keep_original) */
1644                         if (ob->data) {
1645                                 ((ID *)ob->data)->tag |= LIB_TAG_DOIT;
1646                         }
1647
1648                         /* possible metaball basis is not in this scene */
1649                         if (ob->type == OB_MBALL && target == OB_MESH) {
1650                                 if (BKE_mball_is_basis(ob) == false) {
1651                                         Object *ob_basis;
1652                                         ob_basis = BKE_mball_basis_find(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_DATABLOCK(ob) || (ob->data && ID_IS_LINKED_DATABLOCK(ob->data))) && !keep_original) {
1677                                 keep_original = true;
1678                                 BKE_reportf(op->reports, RPT_INFO,
1679                                             "Converting some linked object/object data, enforcing 'Keep Original' option to True");
1680                         }
1681
1682                         DAG_id_tag_update(&base->object->id, OB_RECALC_DATA);
1683                 }
1684
1685                 uint64_t customdata_mask_prev = scene->customdata_mask;
1686                 scene->customdata_mask |= CD_MASK_MESH;
1687                 BKE_scene_update_tagged(bmain->eval_ctx, bmain, scene);
1688                 scene->customdata_mask = customdata_mask_prev;
1689         }
1690
1691         for (CollectionPointerLink *link = selected_editable_bases.first; link; link = link->next) {
1692                 Base *base = link->ptr.data;
1693                 ob = base->object;
1694
1695                 if (ob->flag & OB_DONE || !IS_TAGGED(ob->data)) {
1696                         if (ob->type != target) {
1697                                 base->flag &= ~SELECT;
1698                                 ob->flag &= ~SELECT;
1699                         }
1700
1701                         /* obdata already modified */
1702                         if (!IS_TAGGED(ob->data)) {
1703                                 /* When 2 objects with linked data are selected, converting both
1704                                  * would keep modifiers on all but the converted object [#26003] */
1705                                 if (ob->type == OB_MESH) {
1706                                         BKE_object_free_modifiers(ob);  /* after derivedmesh calls! */
1707                                 }
1708                         }
1709                 }
1710                 else if (ob->type == OB_MESH && target == OB_CURVE) {
1711                         ob->flag |= OB_DONE;
1712
1713                         if (keep_original) {
1714                                 basen = duplibase_for_convert(bmain, scene, base, NULL);
1715                                 newob = basen->object;
1716
1717                                 /* decrement original mesh's usage count  */
1718                                 me = newob->data;
1719                                 id_us_min(&me->id);
1720
1721                                 /* make a new copy of the mesh */
1722                                 newob->data = BKE_mesh_copy(bmain, me);
1723                         }
1724                         else {
1725                                 newob = ob;
1726                         }
1727
1728                         BKE_mesh_to_curve(scene, newob);
1729
1730                         if (newob->type == OB_CURVE) {
1731                                 BKE_object_free_modifiers(newob);   /* after derivedmesh calls! */
1732                                 ED_rigidbody_object_remove(bmain, scene, newob);
1733                         }
1734                 }
1735                 else if (ob->type == OB_MESH) {
1736                         ob->flag |= OB_DONE;
1737
1738                         if (keep_original) {
1739                                 basen = duplibase_for_convert(bmain, scene, base, NULL);
1740                                 newob = basen->object;
1741
1742                                 /* decrement original mesh's usage count  */
1743                                 me = newob->data;
1744                                 id_us_min(&me->id);
1745
1746                                 /* make a new copy of the mesh */
1747                                 newob->data = BKE_mesh_copy(bmain, me);
1748                         }
1749                         else {
1750                                 newob = ob;
1751                                 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1752                         }
1753
1754                         /* make new mesh data from the original copy */
1755                         /* note: get the mesh from the original, not from the copy in some
1756                          * cases this doesnt give correct results (when MDEF is used for eg)
1757                          */
1758                         dm = mesh_get_derived_final(scene, newob, CD_MASK_MESH);
1759
1760                         DM_to_mesh(dm, newob->data, newob, CD_MASK_MESH, true);
1761
1762                         /* re-tessellation is called by DM_to_mesh */
1763
1764                         BKE_object_free_modifiers(newob);   /* after derivedmesh calls! */
1765                 }
1766                 else if (ob->type == OB_FONT) {
1767                         ob->flag |= OB_DONE;
1768
1769                         if (keep_original) {
1770                                 basen = duplibase_for_convert(bmain, scene, base, NULL);
1771                                 newob = basen->object;
1772
1773                                 /* decrement original curve's usage count  */
1774                                 id_us_min(&((Curve *)newob->data)->id);
1775
1776                                 /* make a new copy of the curve */
1777                                 newob->data = BKE_curve_copy(bmain, ob->data);
1778                         }
1779                         else {
1780                                 newob = ob;
1781                         }
1782
1783                         cu = newob->data;
1784
1785                         /* TODO(sergey): Ideally DAG will create nurbs list for a curve data
1786                          *               datablock, but for until we've got granular update
1787                          *               lets take care by selves.
1788                          */
1789                         BKE_vfont_to_curve(bmain, newob, FO_EDIT);
1790
1791                         newob->type = OB_CURVE;
1792                         cu->type = OB_CURVE;
1793
1794                         if (cu->vfont) {
1795                                 id_us_min(&cu->vfont->id);
1796                                 cu->vfont = NULL;
1797                         }
1798                         if (cu->vfontb) {
1799                                 id_us_min(&cu->vfontb->id);
1800                                 cu->vfontb = NULL;
1801                         }
1802                         if (cu->vfonti) {
1803                                 id_us_min(&cu->vfonti->id);
1804                                 cu->vfonti = NULL;
1805                         }
1806                         if (cu->vfontbi) {
1807                                 id_us_min(&cu->vfontbi->id);
1808                                 cu->vfontbi = NULL;
1809                         }
1810
1811                         if (!keep_original) {
1812                                 /* other users */
1813                                 if (cu->id.us > 1) {
1814                                         for (ob1 = bmain->object.first; ob1; ob1 = ob1->id.next) {
1815                                                 if (ob1->data == ob->data) {
1816                                                         ob1->type = OB_CURVE;
1817                                                         DAG_id_tag_update(&ob1->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1818                                                 }
1819                                         }
1820                                 }
1821                         }
1822
1823                         for (nu = cu->nurb.first; nu; nu = nu->next)
1824                                 nu->charidx = 0;
1825
1826                         cu->flag &= ~CU_3D;
1827                         BKE_curve_curve_dimension_update(cu);
1828
1829                         if (target == OB_MESH) {
1830                                 curvetomesh(bmain, scene, newob);
1831
1832                                 /* meshes doesn't use displist */
1833                                 BKE_object_free_curve_cache(newob);
1834                         }
1835                 }
1836                 else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
1837                         ob->flag |= OB_DONE;
1838
1839                         if (target == OB_MESH) {
1840                                 if (keep_original) {
1841                                         basen = duplibase_for_convert(bmain, scene, base, NULL);
1842                                         newob = basen->object;
1843
1844                                         /* decrement original curve's usage count  */
1845                                         id_us_min(&((Curve *)newob->data)->id);
1846
1847                                         /* make a new copy of the curve */
1848                                         newob->data = BKE_curve_copy(bmain, ob->data);
1849                                 }
1850                                 else {
1851                                         newob = ob;
1852                                 }
1853
1854                                 curvetomesh(bmain, scene, newob);
1855
1856                                 /* meshes doesn't use displist */
1857                                 BKE_object_free_curve_cache(newob);
1858                         }
1859                 }
1860                 else if (ob->type == OB_MBALL && target == OB_MESH) {
1861                         Object *baseob;
1862
1863                         base->flag &= ~SELECT;
1864                         ob->flag &= ~SELECT;
1865
1866                         baseob = BKE_mball_basis_find(scene, ob);
1867
1868                         if (ob != baseob) {
1869                                 /* if motherball is converting it would be marked as done later */
1870                                 ob->flag |= OB_DONE;
1871                         }
1872
1873                         if (!(baseob->flag & OB_DONE)) {
1874                                 baseob->flag |= OB_DONE;
1875
1876                                 basen = duplibase_for_convert(bmain, scene, base, baseob);
1877                                 newob = basen->object;
1878
1879                                 mb = newob->data;
1880                                 id_us_min(&mb->id);
1881
1882                                 newob->data = BKE_mesh_add(bmain, "Mesh");
1883                                 newob->type = OB_MESH;
1884
1885                                 me = newob->data;
1886                                 me->totcol = mb->totcol;
1887                                 if (newob->totcol) {
1888                                         me->mat = MEM_dupallocN(mb->mat);
1889                                         for (a = 0; a < newob->totcol; a++) id_us_plus((ID *)me->mat[a]);
1890                                 }
1891
1892                                 convert_ensure_curve_cache(bmain, scene, baseob);
1893                                 BKE_mesh_from_metaball(&baseob->curve_cache->disp, newob->data);
1894
1895                                 if (obact->type == OB_MBALL) {
1896                                         basact = basen;
1897                                 }
1898
1899                                 mballConverted = 1;
1900                         }
1901                 }
1902                 else {
1903                         continue;
1904                 }
1905
1906                 /* tag obdata if it was been changed */
1907
1908                 /* If the original object is active then make this object active */
1909                 if (basen) {
1910                         if (ob == obact) {
1911                                 /* store new active base to update BASACT */
1912                                 basact = basen;
1913                         }
1914
1915                         basen = NULL;
1916                 }
1917
1918                 if (!keep_original && (ob->flag & OB_DONE)) {
1919                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1920                         ((ID *)ob->data)->tag &= ~LIB_TAG_DOIT; /* flag not to convert this datablock again */
1921                 }
1922         }
1923         BLI_freelistN(&selected_editable_bases);
1924
1925         if (!keep_original) {
1926                 if (mballConverted) {
1927                         Base *base, *base_next;
1928
1929                         for (base = scene->base.first; base; base = base_next) {
1930                                 base_next = base->next;
1931
1932                                 ob = base->object;
1933                                 if (ob->type == OB_MBALL) {
1934                                         if (ob->flag & OB_DONE) {
1935                                                 Object *ob_basis = NULL;
1936                                                 if (BKE_mball_is_basis(ob) ||
1937                                                     ((ob_basis = BKE_mball_basis_find(scene, ob)) && (ob_basis->flag & OB_DONE)))
1938                                                 {
1939                                                         ED_base_object_free_and_unlink(bmain, scene, base);
1940                                                 }
1941                                         }
1942                                 }
1943                         }
1944                 }
1945
1946                 /* delete object should renew depsgraph */
1947                 DAG_relations_tag_update(bmain);
1948         }
1949
1950 // XXX  ED_object_editmode_enter(C, 0);
1951 // XXX  exit_editmode(C, EM_FREEDATA|EM_WAITCURSOR); /* freedata, but no undo */
1952
1953         if (basact) {
1954                 /* active base was changed */
1955                 ED_base_object_activate(C, basact);
1956                 BASACT = basact;
1957         }
1958         else if (BASACT->object->flag & OB_DONE) {
1959                 WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, BASACT->object);
1960                 WM_event_add_notifier(C, NC_OBJECT | ND_DATA, BASACT->object);
1961         }
1962
1963         DAG_relations_tag_update(bmain);
1964         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, scene);
1965         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
1966
1967         return OPERATOR_FINISHED;
1968 }
1969
1970
1971 void OBJECT_OT_convert(wmOperatorType *ot)
1972 {
1973         /* identifiers */
1974         ot->name = "Convert to";
1975         ot->description = "Convert selected objects to another type";
1976         ot->idname = "OBJECT_OT_convert";
1977
1978         /* api callbacks */
1979         ot->invoke = WM_menu_invoke;
1980         ot->exec = convert_exec;
1981         ot->poll = convert_poll;
1982
1983         /* flags */
1984         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1985
1986         /* properties */
1987         ot->prop = RNA_def_enum(ot->srna, "target", convert_target_items, OB_MESH, "Target", "Type of object to convert to");
1988         RNA_def_boolean(ot->srna, "keep_original", 0, "Keep Original", "Keep original objects instead of replacing them");
1989 }
1990
1991 /**************************** Duplicate ************************/
1992
1993 /* 
1994  * dupflag: a flag made from constants declared in DNA_userdef_types.h
1995  * The flag tells adduplicate() whether to copy data linked to the object, or to reference the existing data.
1996  * U.dupflag for default operations or you can construct a flag as python does
1997  * if the dupflag is 0 then no data will be copied (linked duplicate) */
1998
1999 /* used below, assumes id.new is correct */
2000 /* leaves selection of base/object unaltered */
2001 /* Does set ID->newid pointers. */
2002 static Base *object_add_duplicate_internal(Main *bmain, Scene *scene, Base *base, int dupflag)
2003 {
2004 #define ID_NEW_REMAP_US(a)      if (      (a)->id.newid) { (a) = (void *)(a)->id.newid;       (a)->id.us++; }
2005 #define ID_NEW_REMAP_US2(a)     if (((ID *)a)->newid)    { (a) = ((ID  *)a)->newid;     ((ID *)a)->us++;    }
2006
2007         Base *basen = NULL;
2008         Material ***matarar;
2009         Object *ob, *obn;
2010         ID *id;
2011         int a, didit;
2012
2013         ob = base->object;
2014         if (ob->mode & OB_MODE_POSE) {
2015                 ; /* nothing? */
2016         }
2017         else {
2018                 obn = ID_NEW_SET(ob, BKE_object_copy(bmain, ob));
2019                 DAG_id_tag_update(&obn->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
2020
2021                 basen = MEM_mallocN(sizeof(Base), "duplibase");
2022                 *basen = *base;
2023                 BLI_addhead(&scene->base, basen);   /* addhead: prevent eternal loop */
2024                 basen->object = obn;
2025
2026                 /* 1) duplis should end up in same group as the original
2027                  * 2) Rigid Body sim participants MUST always be part of a group...
2028                  */
2029                 // XXX: is 2) really a good measure here?
2030                 if ((basen->flag & OB_FROMGROUP) || ob->rigidbody_object || ob->rigidbody_constraint) {
2031                         Group *group;
2032                         for (group = bmain->group.first; group; group = group->id.next) {
2033                                 if (BKE_group_object_exists(group, ob))
2034                                         BKE_group_object_add(group, obn, scene, basen);
2035                         }
2036                 }
2037
2038                 /* duplicates using userflags */
2039                 if (dupflag & USER_DUP_ACT) {
2040                         BKE_animdata_copy_id_action(&obn->id, true);
2041                 }
2042
2043                 if (dupflag & USER_DUP_MAT) {
2044                         for (a = 0; a < obn->totcol; a++) {
2045                                 id = (ID *)obn->mat[a];
2046                                 if (id) {
2047                                         ID_NEW_REMAP_US(obn->mat[a])
2048                                         else {
2049                                                 obn->mat[a] = ID_NEW_SET(obn->mat[a], BKE_material_copy(bmain, obn->mat[a]));
2050                                         }
2051                                         id_us_min(id);
2052
2053                                         if (dupflag & USER_DUP_ACT) {
2054                                                 BKE_animdata_copy_id_action(&obn->mat[a]->id, true);
2055                                         }
2056                                 }
2057                         }
2058                 }
2059                 if (dupflag & USER_DUP_PSYS) {
2060                         ParticleSystem *psys;
2061                         for (psys = obn->particlesystem.first; psys; psys = psys->next) {
2062                                 id = (ID *) psys->part;
2063                                 if (id) {
2064                                         ID_NEW_REMAP_US(psys->part)
2065                                         else {
2066                                                 psys->part = ID_NEW_SET(psys->part, BKE_particlesettings_copy(bmain, psys->part));
2067                                         }
2068
2069                                         if (dupflag & USER_DUP_ACT) {
2070                                                 BKE_animdata_copy_id_action(&psys->part->id, true);
2071                                         }
2072
2073                                         id_us_min(id);
2074                                 }
2075                         }
2076                 }
2077
2078                 id = obn->data;
2079                 didit = 0;
2080
2081                 switch (obn->type) {
2082                         case OB_MESH:
2083                                 if (dupflag & USER_DUP_MESH) {
2084                                         ID_NEW_REMAP_US2(obn->data)
2085                                         else {
2086                                                 obn->data = ID_NEW_SET(obn->data, BKE_mesh_copy(bmain, obn->data));
2087                                                 didit = 1;
2088                                         }
2089                                         id_us_min(id);
2090                                 }
2091                                 break;
2092                         case OB_CURVE:
2093                                 if (dupflag & USER_DUP_CURVE) {
2094                                         ID_NEW_REMAP_US2(obn->data)
2095                                         else {
2096                                                 obn->data = ID_NEW_SET(obn->data, BKE_curve_copy(bmain, obn->data));
2097                                                 didit = 1;
2098                                         }
2099                                         id_us_min(id);
2100                                 }
2101                                 break;
2102                         case OB_SURF:
2103                                 if (dupflag & USER_DUP_SURF) {
2104                                         ID_NEW_REMAP_US2(obn->data)
2105                                         else {
2106                                                 obn->data = ID_NEW_SET(obn->data, BKE_curve_copy(bmain, obn->data));
2107                                                 didit = 1;
2108                                         }
2109                                         id_us_min(id);
2110                                 }
2111                                 break;
2112                         case OB_FONT:
2113                                 if (dupflag & USER_DUP_FONT) {
2114                                         ID_NEW_REMAP_US2(obn->data)
2115                                         else {
2116                                                 obn->data = ID_NEW_SET(obn->data, BKE_curve_copy(bmain, obn->data));
2117                                                 didit = 1;
2118                                         }
2119                                         id_us_min(id);
2120                                 }
2121                                 break;
2122                         case OB_MBALL:
2123                                 if (dupflag & USER_DUP_MBALL) {
2124                                         ID_NEW_REMAP_US2(obn->data)
2125                                         else {
2126                                                 obn->data = ID_NEW_SET(obn->data, BKE_mball_copy(bmain, obn->data));
2127                                                 didit = 1;
2128                                         }
2129                                         id_us_min(id);
2130                                 }
2131                                 break;
2132                         case OB_LAMP:
2133                                 if (dupflag & USER_DUP_LAMP) {
2134                                         ID_NEW_REMAP_US2(obn->data)
2135                                         else {
2136                                                 obn->data = ID_NEW_SET(obn->data, BKE_lamp_copy(bmain, obn->data));
2137                                                 didit = 1;
2138                                         }
2139                                         id_us_min(id);
2140                                 }
2141                                 break;
2142                         case OB_ARMATURE:
2143                                 DAG_id_tag_update(&obn->id, OB_RECALC_DATA);
2144                                 if (obn->pose)
2145                                         BKE_pose_tag_recalc(bmain, obn->pose);
2146                                 if (dupflag & USER_DUP_ARM) {
2147                                         ID_NEW_REMAP_US2(obn->data)
2148                                         else {
2149                                                 obn->data = ID_NEW_SET(obn->data, BKE_armature_copy(bmain, obn->data));
2150                                                 BKE_pose_rebuild(obn, obn->data);
2151                                                 didit = 1;
2152                                         }
2153                                         id_us_min(id);
2154                                 }
2155                                 break;
2156                         case OB_LATTICE:
2157                                 if (dupflag != 0) {
2158                                         ID_NEW_REMAP_US2(obn->data)
2159                                         else {
2160                                                 obn->data = ID_NEW_SET(obn->data, BKE_lattice_copy(bmain, obn->data));
2161                                                 didit = 1;
2162                                         }
2163                                         id_us_min(id);
2164                                 }
2165                                 break;
2166                         case OB_CAMERA:
2167                                 if (dupflag != 0) {
2168                                         ID_NEW_REMAP_US2(obn->data)
2169                                         else {
2170                                                 obn->data = ID_NEW_SET(obn->data, BKE_camera_copy(bmain, obn->data));
2171                                                 didit = 1;
2172                                         }
2173                                         id_us_min(id);
2174                                 }
2175                                 break;
2176                         case OB_SPEAKER:
2177                                 if (dupflag != 0) {
2178                                         ID_NEW_REMAP_US2(obn->data)
2179                                         else {
2180                                                 obn->data = ID_NEW_SET(obn->data, BKE_speaker_copy(bmain, obn->data));
2181                                                 didit = 1;
2182                                         }
2183                                         id_us_min(id);
2184                                 }
2185                                 break;
2186                 }
2187
2188                 /* check if obdata is copied */
2189                 if (didit) {
2190                         Key *key = BKE_key_from_object(obn);
2191
2192                         if (dupflag & USER_DUP_ACT) {
2193                                 bActuator *act;
2194
2195                                 BKE_animdata_copy_id_action((ID *)obn->data, true);
2196                                 if (key) {
2197                                         BKE_animdata_copy_id_action((ID *)key, true);
2198                                 }
2199
2200                                 /* Update the duplicated action in the action actuators */
2201                                 /* XXX TODO this code is all wrong! actact->act is user-refcounted (see readfile.c),
2202                                  * and what about other ID pointers of other BGE logic bricks,
2203                                  * and since this is object-level, why is it only ran if obdata was duplicated??? -mont29 */
2204                                 for (act = obn->actuators.first; act; act = act->next) {
2205                                         if (act->type == ACT_ACTION) {
2206                                                 bActionActuator *actact = (bActionActuator *) act->data;
2207                                                 if (ob->adt && actact->act == ob->adt->action) {
2208                                                         actact->act = obn->adt->action;
2209                                                 }
2210                                         }
2211                                 }
2212                         }
2213
2214                         if (dupflag & USER_DUP_MAT) {
2215                                 matarar = give_matarar(obn);
2216                                 if (matarar) {
2217                                         for (a = 0; a < obn->totcol; a++) {
2218                                                 id = (ID *)(*matarar)[a];
2219                                                 if (id) {
2220                                                         ID_NEW_REMAP_US((*matarar)[a])
2221                                                         else {
2222                                                                 (*matarar)[a] = ID_NEW_SET((*matarar)[a], BKE_material_copy(bmain, (*matarar)[a]));
2223                                                         }
2224                                                         id_us_min(id);
2225                                                 }
2226                                         }
2227                                 }
2228                         }
2229                 }
2230         }
2231         return basen;
2232
2233 #undef ID_NEW_REMAP_US
2234 #undef ID_NEW_REMAP_US2
2235 }
2236
2237 /* single object duplicate, if dupflag==0, fully linked, else it uses the flags given */
2238 /* leaves selection of base/object unaltered.
2239  * note: don't call this within a loop since clear_* funcs loop over the entire database.
2240  * note: caller must do DAG_relations_tag_update(bmain);
2241  *       this is not done automatic since we may duplicate many objects in a batch */
2242 Base *ED_object_add_duplicate(Main *bmain, Scene *scene, Base *base, int dupflag)
2243 {
2244         Base *basen;
2245         Object *ob;
2246
2247         clear_sca_new_poins();  /* BGE logic */
2248
2249         basen = object_add_duplicate_internal(bmain, scene, base, dupflag);
2250         if (basen == NULL) {
2251                 return NULL;
2252         }
2253
2254         ob = basen->object;
2255
2256         /* link own references to the newly duplicated data [#26816] */
2257         BKE_libblock_relink_to_newid(&ob->id);
2258         set_sca_new_poins_ob(ob);
2259
2260         /* DAG_relations_tag_update(bmain); */ /* caller must do */
2261
2262         if (ob->data) {
2263                 ED_render_id_flush_update(bmain, ob->data);
2264         }
2265
2266         BKE_main_id_clear_newpoins(bmain);
2267
2268         return basen;
2269 }
2270
2271 /* contextual operator dupli */
2272 static int duplicate_exec(bContext *C, wmOperator *op)
2273 {
2274         Main *bmain = CTX_data_main(C);
2275         Scene *scene = CTX_data_scene(C);
2276         const bool linked = RNA_boolean_get(op->ptr, "linked");
2277         int dupflag = (linked) ? 0 : U.dupflag;
2278
2279         clear_sca_new_poins();  /* BGE logic */
2280
2281         CTX_DATA_BEGIN (C, Base *, base, selected_bases)
2282         {
2283                 Base *basen = object_add_duplicate_internal(bmain, scene, base, dupflag);
2284
2285                 /* note that this is safe to do with this context iterator,
2286                  * the list is made in advance */
2287                 ED_base_object_select(base, BA_DESELECT);
2288
2289                 if (basen == NULL) {
2290                         continue;
2291                 }
2292
2293                 /* new object becomes active */
2294                 if (BASACT == base)
2295                         ED_base_object_activate(C, basen);
2296
2297                 if (basen->object->data) {
2298                         DAG_id_tag_update(basen->object->data, 0);
2299                 }
2300         }
2301         CTX_DATA_END;
2302
2303         copy_object_set_idnew(C);
2304
2305         BKE_main_id_clear_newpoins(bmain);
2306
2307         DAG_relations_tag_update(bmain);
2308
2309         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
2310
2311         return OPERATOR_FINISHED;
2312 }
2313
2314 void OBJECT_OT_duplicate(wmOperatorType *ot)
2315 {
2316         PropertyRNA *prop;
2317
2318         /* identifiers */
2319         ot->name = "Duplicate Objects";
2320         ot->description = "Duplicate selected objects";
2321         ot->idname = "OBJECT_OT_duplicate";
2322
2323         /* api callbacks */
2324         ot->exec = duplicate_exec;
2325         ot->poll = ED_operator_objectmode;
2326
2327         /* flags */
2328         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2329
2330         /* to give to transform */
2331         RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data");
2332         prop = RNA_def_enum(ot->srna, "mode", rna_enum_transform_mode_types, TFM_TRANSLATION, "Mode", "");
2333         RNA_def_property_flag(prop, PROP_HIDDEN);
2334 }
2335
2336 /* **************** add named object, for dragdrop ************* */
2337
2338 static int add_named_exec(bContext *C, wmOperator *op)
2339 {
2340         wmWindow *win = CTX_wm_window(C);
2341         const wmEvent *event = win ? win->eventstate : NULL;
2342         Main *bmain = CTX_data_main(C);
2343         View3D *v3d = CTX_wm_view3d(C);  /* may be NULL */
2344         Scene *scene = CTX_data_scene(C);
2345         Base *basen, *base;
2346         Object *ob;
2347         const bool linked = RNA_boolean_get(op->ptr, "linked");
2348         int dupflag = (linked) ? 0 : U.dupflag;
2349         char name[MAX_ID_NAME - 2];
2350
2351         /* find object, create fake base */
2352         RNA_string_get(op->ptr, "name", name);
2353         ob = (Object *)BKE_libblock_find_name(ID_OB, name);
2354
2355         if (ob == NULL) {
2356                 BKE_report(op->reports, RPT_ERROR, "Object not found");
2357                 return OPERATOR_CANCELLED;
2358         }
2359
2360         base = MEM_callocN(sizeof(Base), "duplibase");
2361         base->object = ob;
2362         base->flag = ob->flag;
2363
2364         /* prepare dupli */
2365         clear_sca_new_poins();  /* BGE logic */
2366
2367         basen = object_add_duplicate_internal(bmain, scene, base, dupflag);
2368
2369         if (basen == NULL) {
2370                 MEM_freeN(base);
2371                 BKE_report(op->reports, RPT_ERROR, "Object could not be duplicated");
2372                 return OPERATOR_CANCELLED;
2373         }
2374
2375         basen->lay = basen->object->lay = BKE_screen_view3d_layer_active(v3d, scene);
2376         basen->object->restrictflag &= ~OB_RESTRICT_VIEW;
2377
2378         if (event) {
2379                 ARegion *ar = CTX_wm_region(C);
2380                 const int mval[2] = {event->x - ar->winrct.xmin,
2381                                      event->y - ar->winrct.ymin};
2382                 ED_object_location_from_view(C, basen->object->loc);
2383                 ED_view3d_cursor3d_position(C, basen->object->loc, mval);
2384         }
2385         
2386         ED_base_object_select(basen, BA_SELECT);
2387         ED_base_object_activate(C, basen);
2388
2389         copy_object_set_idnew(C);
2390
2391         BKE_main_id_clear_newpoins(bmain);
2392
2393         DAG_relations_tag_update(bmain);
2394
2395         MEM_freeN(base);
2396
2397         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT | ND_OB_ACTIVE, scene);
2398
2399         return OPERATOR_FINISHED;
2400 }
2401
2402 void OBJECT_OT_add_named(wmOperatorType *ot)
2403 {
2404         /* identifiers */
2405         ot->name = "Add Named Object";
2406         ot->description = "Add named object";
2407         ot->idname = "OBJECT_OT_add_named";
2408
2409         /* api callbacks */
2410         ot->exec = add_named_exec;
2411         ot->poll = ED_operator_objectmode;
2412
2413         /* flags */
2414         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2415
2416         RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data");
2417         RNA_def_string(ot->srna, "name", NULL, MAX_ID_NAME - 2, "Name", "Object name to add");
2418 }
2419
2420 /**************************** Join *************************/
2421
2422 static int join_poll(bContext *C)
2423 {
2424         Object *ob = CTX_data_active_object(C);
2425
2426         if (!ob || ID_IS_LINKED_DATABLOCK(ob)) return 0;
2427
2428         if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_ARMATURE))
2429                 return ED_operator_screenactive(C);
2430         else
2431                 return 0;
2432 }
2433
2434 static int join_exec(bContext *C, wmOperator *op)
2435 {
2436         Scene *scene = CTX_data_scene(C);
2437         Object *ob = CTX_data_active_object(C);
2438
2439         if (scene->obedit) {
2440                 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in edit mode");
2441                 return OPERATOR_CANCELLED;
2442         }
2443         else if (BKE_object_obdata_is_libdata(ob)) {
2444                 BKE_report(op->reports, RPT_ERROR, "Cannot edit external libdata");
2445                 return OPERATOR_CANCELLED;
2446         }
2447
2448         if (ob->type == OB_MESH)
2449                 return join_mesh_exec(C, op);
2450         else if (ELEM(ob->type, OB_CURVE, OB_SURF))
2451                 return join_curve_exec(C, op);
2452         else if (ob->type == OB_ARMATURE)
2453                 return join_armature_exec(C, op);
2454
2455         return OPERATOR_CANCELLED;
2456 }
2457
2458 void OBJECT_OT_join(wmOperatorType *ot)
2459 {
2460         /* identifiers */
2461         ot->name = "Join";
2462         ot->description = "Join selected objects into active object";
2463         ot->idname = "OBJECT_OT_join";
2464
2465         /* api callbacks */
2466         ot->exec = join_exec;
2467         ot->poll = join_poll;
2468
2469         /* flags */
2470         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2471 }
2472
2473 /**************************** Join as Shape Key*************************/
2474
2475 static int join_shapes_poll(bContext *C)
2476 {
2477         Object *ob = CTX_data_active_object(C);
2478
2479         if (!ob || ID_IS_LINKED_DATABLOCK(ob)) return 0;
2480
2481         /* only meshes supported at the moment */
2482         if (ob->type == OB_MESH)
2483                 return ED_operator_screenactive(C);
2484         else
2485                 return 0;
2486 }
2487
2488 static int join_shapes_exec(bContext *C, wmOperator *op)
2489 {
2490         Scene *scene = CTX_data_scene(C);
2491         Object *ob = CTX_data_active_object(C);
2492
2493         if (scene->obedit) {
2494                 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in edit mode");
2495                 return OPERATOR_CANCELLED;
2496         }
2497         else if (BKE_object_obdata_is_libdata(ob)) {
2498                 BKE_report(op->reports, RPT_ERROR, "Cannot edit external libdata");
2499                 return OPERATOR_CANCELLED;
2500         }
2501
2502         if (ob->type == OB_MESH)
2503                 return join_mesh_shapes_exec(C, op);
2504
2505         return OPERATOR_CANCELLED;
2506 }
2507
2508 void OBJECT_OT_join_shapes(wmOperatorType *ot)
2509 {
2510         /* identifiers */
2511         ot->name = "Join as Shapes";
2512         ot->description = "Merge selected objects to shapes of active object";
2513         ot->idname = "OBJECT_OT_join_shapes";
2514
2515         /* api callbacks */
2516         ot->exec = join_shapes_exec;
2517         ot->poll = join_shapes_poll;
2518
2519         /* flags */
2520         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2521 }