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