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