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