fix [#29183] Make duplicates real: parent informations of the group is not copied
[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_curve_types.h"
38 #include "DNA_group_types.h"
39 #include "DNA_lamp_types.h"
40 #include "DNA_key_types.h"
41 #include "DNA_material_types.h"
42 #include "DNA_mesh_types.h"
43 #include "DNA_meta_types.h"
44 #include "DNA_object_fluidsim.h"
45 #include "DNA_object_force.h"
46 #include "DNA_scene_types.h"
47 #include "DNA_speaker_types.h"
48 #include "DNA_vfont_types.h"
49
50 #include "BLI_math.h"
51 #include "BLI_string.h"
52 #include "BLI_listbase.h"
53 #include "BLI_utildefines.h"
54
55 #include "BKE_anim.h"
56 #include "BKE_animsys.h"
57 #include "BKE_armature.h"
58 #include "BKE_camera.h"
59 #include "BKE_constraint.h"
60 #include "BKE_context.h"
61 #include "BKE_curve.h"
62 #include "BKE_depsgraph.h"
63 #include "BKE_DerivedMesh.h"
64 #include "BKE_displist.h"
65 #include "BKE_effect.h"
66 #include "BKE_group.h"
67 #include "BKE_lamp.h"
68 #include "BKE_lattice.h"
69 #include "BKE_library.h"
70 #include "BKE_key.h"
71 #include "BKE_main.h"
72 #include "BKE_material.h"
73 #include "BKE_mball.h"
74 #include "BKE_mesh.h"
75 #include "BKE_modifier.h"
76 #include "BKE_nla.h"
77 #include "BKE_object.h"
78 #include "BKE_particle.h"
79 #include "BKE_report.h"
80 #include "BKE_sca.h"
81 #include "BKE_speaker.h"
82 #include "BKE_texture.h"
83
84 #include "RNA_access.h"
85 #include "RNA_define.h"
86 #include "RNA_enum_types.h"
87
88 #include "WM_api.h"
89 #include "WM_types.h"
90
91 #include "ED_armature.h"
92 #include "ED_curve.h"
93 #include "ED_mball.h"
94 #include "ED_mesh.h"
95 #include "ED_object.h"
96 #include "ED_render.h"
97 #include "ED_screen.h"
98 #include "ED_transform.h"
99 #include "ED_view3d.h"
100
101 #include "UI_interface.h"
102 #include "UI_resources.h"
103
104 #include "object_intern.h"
105
106 /************************** Exported *****************************/
107
108 void ED_object_location_from_view(bContext *C, float *loc)
109 {
110         View3D *v3d= CTX_wm_view3d(C);
111         Scene *scene= CTX_data_scene(C);
112         float *cursor;
113         
114         cursor = give_cursor(scene, v3d);
115
116         copy_v3_v3(loc, cursor);
117 }
118
119 void ED_object_rotation_from_view(bContext *C, float *rot)
120 {
121         RegionView3D *rv3d= CTX_wm_region_view3d(C);
122         if(rv3d) {
123                 float quat[4];
124                 copy_qt_qt(quat, rv3d->viewquat);
125                 quat[0]= -quat[0];
126                 quat_to_eul(rot, quat);
127         }
128         else {
129                 zero_v3(rot);
130         }
131 }
132
133 void ED_object_base_init_transform(bContext *C, Base *base, float *loc, float *rot)
134 {
135         Object *ob= base->object;
136         Scene *scene= CTX_data_scene(C);
137         
138         if (!scene) return;
139         
140         if (loc)
141                 copy_v3_v3(ob->loc, loc);
142         
143         if (rot)
144                 copy_v3_v3(ob->rot, rot);
145         
146         where_is_object(scene, ob);
147 }
148
149 /* uses context to figure out transform for primitive */
150 /* returns standard diameter */
151 float ED_object_new_primitive_matrix(bContext *C, Object *obedit, float *loc, float *rot, float primmat[][4])
152 {
153         View3D *v3d =CTX_wm_view3d(C);
154         float mat[3][3], rmat[3][3], cmat[3][3], imat[3][3];
155         
156         unit_m4(primmat);
157         
158         eul_to_mat3(rmat, rot);
159         invert_m3(rmat);
160         
161         /* inverse transform for initial rotation and object */
162         copy_m3_m4(mat, obedit->obmat);
163         mul_m3_m3m3(cmat, rmat, mat);
164         invert_m3_m3(imat, cmat);
165         copy_m4_m3(primmat, imat);
166         
167         /* center */
168         copy_v3_v3(primmat[3], loc);
169         sub_v3_v3v3(primmat[3], primmat[3], obedit->obmat[3]);
170         invert_m3_m3(imat, mat);
171         mul_m3_v3(imat, primmat[3]);
172         
173         if(v3d) return v3d->grid;
174         return 1.0f;
175 }
176
177 /********************* Add Object Operator ********************/
178
179 void view_align_update(struct Main *UNUSED(main), struct Scene *UNUSED(scene), struct PointerRNA *ptr)
180 {
181         RNA_struct_idprops_unset(ptr, "rotation");
182 }
183
184 void ED_object_add_generic_props(wmOperatorType *ot, int do_editmode)
185 {
186         PropertyRNA *prop;
187         
188         /* note: this property gets hidden for add-camera operator */
189         prop= RNA_def_boolean(ot->srna, "view_align", 0, "Align to View", "Align the new object to the view");
190         RNA_def_property_update_runtime(prop, view_align_update);
191
192         if(do_editmode) {
193                 prop= RNA_def_boolean(ot->srna, "enter_editmode", 0, "Enter Editmode", "Enter editmode when adding this object");
194                 RNA_def_property_flag(prop, PROP_HIDDEN);
195         }
196         
197         RNA_def_float_vector_xyz(ot->srna, "location", 3, NULL, -FLT_MAX, FLT_MAX, "Location", "Location for the newly added object", -FLT_MAX, FLT_MAX);
198         RNA_def_float_rotation(ot->srna, "rotation", 3, NULL, -FLT_MAX, FLT_MAX, "Rotation", "Rotation for the newly added object", (float)-M_PI * 2.0f, (float)M_PI * 2.0f);
199         
200         prop = RNA_def_boolean_layer_member(ot->srna, "layers", 20, NULL, "Layer", "");
201         RNA_def_property_flag(prop, PROP_HIDDEN);
202 }
203
204 static void object_add_generic_invoke_options(bContext *C, wmOperator *op)
205 {
206         if(RNA_struct_find_property(op->ptr, "enter_editmode")) /* optional */
207                 if (!RNA_property_is_set(op->ptr, "enter_editmode"))
208                         RNA_boolean_set(op->ptr, "enter_editmode", U.flag & USER_ADD_EDITMODE);
209         
210         if(!RNA_property_is_set(op->ptr, "location")) {
211                 float loc[3];
212                 
213                 ED_object_location_from_view(C, loc);
214                 RNA_float_set_array(op->ptr, "location", loc);
215         }
216          
217         if(!RNA_property_is_set(op->ptr, "layers")) {
218                 View3D *v3d = CTX_wm_view3d(C);
219                 Scene *scene = CTX_data_scene(C);
220                 int a, values[20], layer;
221                 
222                 if(v3d) {
223                         layer = (v3d->scenelock && !v3d->localvd)? scene->layact: v3d->layact;
224
225                         for(a=0; a<20; a++)
226                                 values[a]= (layer & (1<<a));
227                 }
228                 else {
229                         layer = scene->layact;
230
231                         for(a=0; a<20; a++)
232                                 values[a]= (layer & (1<<a));
233                 }
234                 
235                 RNA_boolean_set_array(op->ptr, "layers", values);
236         }
237 }
238
239 int ED_object_add_generic_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
240 {
241         object_add_generic_invoke_options(C, op);
242         return op->type->exec(C, op);
243 }
244
245 int ED_object_add_generic_get_opts(bContext *C, wmOperator *op, float *loc, float *rot, int *enter_editmode, unsigned int *layer)
246 {
247         View3D *v3d = CTX_wm_view3d(C);
248         int a, layer_values[20];
249         int view_align;
250         
251         *enter_editmode = FALSE;
252         if(RNA_struct_find_property(op->ptr, "enter_editmode") && RNA_boolean_get(op->ptr, "enter_editmode")) {
253                 *enter_editmode = TRUE;
254         }
255
256         if(RNA_property_is_set(op->ptr, "layers")) {
257                 RNA_boolean_get_array(op->ptr, "layers", layer_values);
258                 *layer= 0;
259                 for(a=0; a<20; a++) {
260                         if(layer_values[a])
261                                 *layer |= (1 << a);
262                         else
263                                 *layer &= ~(1 << a);
264                 }
265         }
266         else {
267                 /* not set, use the scenes layers */
268                 Scene *scene = CTX_data_scene(C);
269                 *layer = scene->layact;
270         }
271
272         /* in local view we additionally add local view layers,
273            not part of operator properties */
274         if(v3d && v3d->localvd)
275                 *layer |= v3d->lay;
276
277         if(RNA_property_is_set(op->ptr, "rotation"))
278                 view_align = FALSE;
279         else if (RNA_property_is_set(op->ptr, "view_align"))
280                 view_align = RNA_boolean_get(op->ptr, "view_align");
281         else {
282                 view_align = U.flag & USER_ADD_VIEWALIGNED;
283                 RNA_boolean_set(op->ptr, "view_align", view_align);
284         }
285         
286         if (view_align) {
287                 ED_object_rotation_from_view(C, rot);
288                 RNA_float_set_array(op->ptr, "rotation", rot);
289         }
290         else
291                 RNA_float_get_array(op->ptr, "rotation", rot);
292         
293
294         RNA_float_get_array(op->ptr, "location", loc);
295
296         if(*layer == 0) {
297                 BKE_report(op->reports, RPT_ERROR, "Property 'layer' has no values set");
298                 return 0;
299         }
300
301         return 1;
302 }
303
304 /* for object add primitive operators */
305 /* do not call undo push in this function (users of this function have to) */
306 Object *ED_object_add_type(bContext *C, int type, float *loc, float *rot, int enter_editmode, unsigned int layer)
307 {
308         Main *bmain= CTX_data_main(C);
309         Scene *scene= CTX_data_scene(C);
310         Object *ob;
311         
312         /* for as long scene has editmode... */
313         if (CTX_data_edit_object(C)) 
314                 ED_object_exit_editmode(C, EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR|EM_DO_UNDO); /* freedata, and undo */
315         
316         /* deselects all, sets scene->basact */
317         ob= add_object(scene, type);
318         BASACT->lay = ob->lay = layer;
319         /* editor level activate, notifiers */
320         ED_base_object_activate(C, BASACT);
321
322         /* more editor stuff */
323         ED_object_base_init_transform(C, BASACT, loc, rot);
324
325         DAG_scene_sort(bmain, scene);
326         ED_render_id_flush_update(bmain, ob->data);
327
328         if(enter_editmode)
329                 ED_object_enter_editmode(C, EM_IGNORE_LAYER);
330
331         WM_event_add_notifier(C, NC_SCENE|ND_LAYER_CONTENT, scene);
332
333         return ob;
334 }
335
336 /* for object add operator */
337 static int object_add_exec(bContext *C, wmOperator *op)
338 {
339         int enter_editmode;
340         unsigned int layer;
341         float loc[3], rot[3];
342         
343         if(!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer))
344                 return OPERATOR_CANCELLED;
345
346         ED_object_add_type(C, RNA_enum_get(op->ptr, "type"), loc, rot, enter_editmode, layer);
347         
348         return OPERATOR_FINISHED;
349 }
350
351 void OBJECT_OT_add(wmOperatorType *ot)
352 {
353         /* identifiers */
354         ot->name= "Add Object";
355         ot->description = "Add an object to the scene";
356         ot->idname= "OBJECT_OT_add";
357         
358         /* api callbacks */
359         ot->invoke= ED_object_add_generic_invoke;
360         ot->exec= object_add_exec;
361         
362         ot->poll= ED_operator_objectmode;
363         
364         /* flags */
365         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
366         
367         RNA_def_enum(ot->srna, "type", object_type_items, 0, "Type", "");
368
369         ED_object_add_generic_props(ot, TRUE);
370 }
371
372 /********************* Add Effector Operator ********************/
373 /* copy from rna_object_force.c*/
374 static EnumPropertyItem field_type_items[] = {
375         {PFIELD_FORCE, "FORCE", ICON_FORCE_FORCE, "Force", ""},
376         {PFIELD_WIND, "WIND", ICON_FORCE_WIND, "Wind", ""},
377         {PFIELD_VORTEX, "VORTEX", ICON_FORCE_VORTEX, "Vortex", ""},
378         {PFIELD_MAGNET, "MAGNET", ICON_FORCE_MAGNETIC, "Magnetic", ""},
379         {PFIELD_HARMONIC, "HARMONIC", ICON_FORCE_HARMONIC, "Harmonic", ""},
380         {PFIELD_CHARGE, "CHARGE", ICON_FORCE_CHARGE, "Charge", ""},
381         {PFIELD_LENNARDJ, "LENNARDJ", ICON_FORCE_LENNARDJONES, "Lennard-Jones", ""},
382         {PFIELD_TEXTURE, "TEXTURE", ICON_FORCE_TEXTURE, "Texture", ""},
383         {PFIELD_GUIDE, "GUIDE", ICON_FORCE_CURVE, "Curve Guide", ""},
384         {PFIELD_BOID, "BOID", ICON_FORCE_BOID, "Boid", ""},
385         {PFIELD_TURBULENCE, "TURBULENCE", ICON_FORCE_TURBULENCE, "Turbulence", ""},
386         {PFIELD_DRAG, "DRAG", ICON_FORCE_DRAG, "Drag", ""},
387         {0, NULL, 0, NULL, NULL}};
388
389 /* for effector add primitive operators */
390 static Object *effector_add_type(bContext *C, wmOperator *op, int type)
391 {
392         Object *ob;
393         int enter_editmode;
394         unsigned int layer;
395         float loc[3], rot[3];
396         float mat[4][4];
397         
398         object_add_generic_invoke_options(C, op);
399
400         if(!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer))
401                 return NULL;
402
403         if(type==PFIELD_GUIDE) {
404                 ob= ED_object_add_type(C, OB_CURVE, loc, rot, FALSE, layer);
405                 rename_id(&ob->id, "CurveGuide");
406
407                 ((Curve*)ob->data)->flag |= CU_PATH|CU_3D;
408                 ED_object_enter_editmode(C, 0);
409                 ED_object_new_primitive_matrix(C, ob, loc, rot, mat);
410                 BLI_addtail(object_editcurve_get(ob), add_nurbs_primitive(C, mat, CU_NURBS|CU_PRIM_PATH, 1));
411
412                 if(!enter_editmode)
413                         ED_object_exit_editmode(C, EM_FREEDATA);
414         }
415         else {
416                 ob= ED_object_add_type(C, OB_EMPTY, loc, rot, FALSE, layer);
417                 rename_id(&ob->id, "Field");
418
419                 switch(type) {
420                         case PFIELD_WIND:
421                         case PFIELD_VORTEX:
422                                 ob->empty_drawtype = OB_SINGLE_ARROW;
423                                 break;
424                 }
425         }
426
427         ob->pd= object_add_collision_fields(type);
428
429         DAG_scene_sort(CTX_data_main(C), CTX_data_scene(C));
430
431         return ob;
432 }
433
434 /* for object add operator */
435 static int effector_add_exec(bContext *C, wmOperator *op)
436 {
437         if(effector_add_type(C, op, RNA_enum_get(op->ptr, "type")) == NULL)
438                 return OPERATOR_CANCELLED;
439
440         return OPERATOR_FINISHED;
441 }
442
443 void OBJECT_OT_effector_add(wmOperatorType *ot)
444 {
445         /* identifiers */
446         ot->name= "Add Effector";
447         ot->description = "Add an empty object with a physics effector to the scene";
448         ot->idname= "OBJECT_OT_effector_add";
449         
450         /* api callbacks */
451         ot->invoke= WM_menu_invoke;
452         ot->exec= effector_add_exec;
453         
454         ot->poll= ED_operator_objectmode;
455         
456         /* flags */
457         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
458         
459         ot->prop= RNA_def_enum(ot->srna, "type", field_type_items, 0, "Type", "");
460
461         ED_object_add_generic_props(ot, TRUE);
462 }
463
464 /* ***************** Add Camera *************** */
465
466 static int object_camera_add_exec(bContext *C, wmOperator *op)
467 {
468         View3D *v3d = CTX_wm_view3d(C);
469         Scene *scene= CTX_data_scene(C);
470         Object *ob;
471         int enter_editmode;
472         unsigned int layer;
473         float loc[3], rot[3];
474         
475         /* force view align for cameras */
476         RNA_boolean_set(op->ptr, "view_align", 1);
477         
478         object_add_generic_invoke_options(C, op);
479
480         if(!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer))
481                 return OPERATOR_CANCELLED;
482
483         ob= ED_object_add_type(C, OB_CAMERA, loc, rot, FALSE, layer);
484         
485         if (v3d) {
486                 if (v3d->camera == NULL)
487                         v3d->camera = ob;
488                 if (v3d->scenelock && scene->camera==NULL) {
489                         scene->camera = ob;
490                 }
491         }
492
493         return OPERATOR_FINISHED;
494 }
495
496 void OBJECT_OT_camera_add(wmOperatorType *ot)
497 {
498         PropertyRNA *prop;
499         
500         /* identifiers */
501         ot->name= "Add Camera";
502         ot->description = "Add a camera object to the scene";
503         ot->idname= "OBJECT_OT_camera_add";
504         
505         /* api callbacks */
506         ot->exec= object_camera_add_exec;
507         ot->poll= ED_operator_objectmode;
508         
509         /* flags */
510         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
511                 
512         ED_object_add_generic_props(ot, TRUE);
513         
514         /* hide this for cameras, default */
515         prop= RNA_struct_type_find_property(ot->srna, "view_align");
516         RNA_def_property_flag(prop, PROP_HIDDEN);
517
518 }
519
520
521 /* ***************** add primitives *************** */
522 static int object_metaball_add_exec(bContext *C, wmOperator *op)
523 {
524         Object *obedit= CTX_data_edit_object(C);
525         /*MetaElem *elem;*/ /*UNUSED*/
526         int newob= 0;
527         int enter_editmode;
528         unsigned int layer;
529         float loc[3], rot[3];
530         float mat[4][4];
531         
532         object_add_generic_invoke_options(C, op); // XXX these props don't get set right when only exec() is called
533
534         if(!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer))
535                 return OPERATOR_CANCELLED;
536         
537         if(obedit==NULL || obedit->type!=OB_MBALL) {
538                 obedit= ED_object_add_type(C, OB_MBALL, loc, rot, TRUE, layer);
539                 newob = 1;
540         }
541         else DAG_id_tag_update(&obedit->id, OB_RECALC_DATA);
542         
543         ED_object_new_primitive_matrix(C, obedit, loc, rot, mat);
544         
545         /* elem= (MetaElem *) */ add_metaball_primitive(C, mat, RNA_enum_get(op->ptr, "type"), newob);
546
547         /* userdef */
548         if (newob && !enter_editmode) {
549                 ED_object_exit_editmode(C, EM_FREEDATA);
550         }
551         
552         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit);
553         
554         return OPERATOR_FINISHED;
555 }
556
557 static int object_metaball_add_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
558 {
559         Object *obedit= CTX_data_edit_object(C);
560         uiPopupMenu *pup;
561         uiLayout *layout;
562
563         object_add_generic_invoke_options(C, op);
564
565         pup= uiPupMenuBegin(C, op->type->name, ICON_NONE);
566         layout= uiPupMenuLayout(pup);
567         if(!obedit || obedit->type == OB_MBALL)
568                 uiItemsEnumO(layout, op->type->idname, "type");
569         else
570                 uiItemsEnumO(layout, "OBJECT_OT_metaball_add", "type");
571         uiPupMenuEnd(C, pup);
572
573         return OPERATOR_CANCELLED;
574 }
575
576 void OBJECT_OT_metaball_add(wmOperatorType *ot)
577 {
578         /* identifiers */
579         ot->name= "Add Metaball";
580         ot->description= "Add an metaball object to the scene";
581         ot->idname= "OBJECT_OT_metaball_add";
582
583         /* api callbacks */
584         ot->invoke= object_metaball_add_invoke;
585         ot->exec= object_metaball_add_exec;
586         ot->poll= ED_operator_scene_editable;
587
588         /* flags */
589         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
590         
591         RNA_def_enum(ot->srna, "type", metaelem_type_items, 0, "Primitive", "");
592         ED_object_add_generic_props(ot, TRUE);
593 }
594
595 static int object_add_text_exec(bContext *C, wmOperator *op)
596 {
597         Object *obedit= CTX_data_edit_object(C);
598         int enter_editmode;
599         unsigned int layer;
600         float loc[3], rot[3];
601         
602         object_add_generic_invoke_options(C, op); // XXX these props don't get set right when only exec() is called
603         if(!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer))
604                 return OPERATOR_CANCELLED;
605         
606         if(obedit && obedit->type==OB_FONT)
607                 return OPERATOR_CANCELLED;
608
609         obedit= ED_object_add_type(C, OB_FONT, loc, rot, enter_editmode, layer);
610         
611         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit);
612         
613         return OPERATOR_FINISHED;
614 }
615
616 void OBJECT_OT_text_add(wmOperatorType *ot)
617 {
618         /* identifiers */
619         ot->name= "Add Text";
620         ot->description = "Add a text object to the scene";
621         ot->idname= "OBJECT_OT_text_add";
622         
623         /* api callbacks */
624         ot->invoke= ED_object_add_generic_invoke;
625         ot->exec= object_add_text_exec;
626         ot->poll= ED_operator_objectmode;
627         
628         /* flags */
629         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
630         ED_object_add_generic_props(ot, TRUE);
631 }
632
633 static int object_armature_add_exec(bContext *C, wmOperator *op)
634 {
635         Object *obedit= CTX_data_edit_object(C);
636         View3D *v3d= CTX_wm_view3d(C);
637         RegionView3D *rv3d= CTX_wm_region_view3d(C);
638         int newob= 0;
639         int enter_editmode;
640         unsigned int layer;
641         float loc[3], rot[3];
642         
643         object_add_generic_invoke_options(C, op); // XXX these props don't get set right when only exec() is called
644         if(!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer))
645                 return OPERATOR_CANCELLED;
646         
647         if ((obedit==NULL) || (obedit->type != OB_ARMATURE)) {
648                 obedit= ED_object_add_type(C, OB_ARMATURE, loc, rot, TRUE, layer);
649                 ED_object_enter_editmode(C, 0);
650                 newob = 1;
651         }
652         else DAG_id_tag_update(&obedit->id, OB_RECALC_DATA);
653         
654         if(obedit==NULL) {
655                 BKE_report(op->reports, RPT_ERROR, "Cannot create editmode armature");
656                 return OPERATOR_CANCELLED;
657         }
658         
659         /* v3d and rv3d are allowed to be NULL */
660         add_primitive_bone(CTX_data_scene(C), v3d, rv3d);
661
662         /* userdef */
663         if (newob && !enter_editmode)
664                 ED_object_exit_editmode(C, EM_FREEDATA);
665         
666         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit);
667         
668         return OPERATOR_FINISHED;
669 }
670
671 void OBJECT_OT_armature_add(wmOperatorType *ot)
672 {       
673         /* identifiers */
674         ot->name= "Add Armature";
675         ot->description = "Add an armature object to the scene";
676         ot->idname= "OBJECT_OT_armature_add";
677         
678         /* api callbacks */
679         ot->invoke= ED_object_add_generic_invoke;
680         ot->exec= object_armature_add_exec;
681         ot->poll= ED_operator_objectmode;
682         
683         /* flags */
684         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
685         ED_object_add_generic_props(ot, TRUE);
686 }
687
688 static const char *get_lamp_defname(int type)
689 {
690         switch (type) {
691                 case LA_LOCAL: return "Point";
692                 case LA_SUN: return "Sun";
693                 case LA_SPOT: return "Spot";
694                 case LA_HEMI: return "Hemi";
695                 case LA_AREA: return "Area";
696                 default:
697                         return "Lamp";
698         }
699 }
700
701 static int object_lamp_add_exec(bContext *C, wmOperator *op)
702 {
703         Object *ob;
704         int type= RNA_enum_get(op->ptr, "type");
705         int enter_editmode;
706         unsigned int layer;
707         float loc[3], rot[3];
708         
709         object_add_generic_invoke_options(C, op);
710         if(!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer))
711                 return OPERATOR_CANCELLED;
712
713         ob= ED_object_add_type(C, OB_LAMP, loc, rot, FALSE, layer);
714         ((Lamp*)ob->data)->type= type;
715         rename_id((ID *)ob, get_lamp_defname(type));
716         rename_id((ID *)ob->data, get_lamp_defname(type));
717         
718         return OPERATOR_FINISHED;
719 }
720
721 void OBJECT_OT_lamp_add(wmOperatorType *ot)
722 {       
723         static EnumPropertyItem lamp_type_items[] = {
724                 {LA_LOCAL, "POINT", ICON_LAMP_POINT, "Point", "Omnidirectional point light source"},
725                 {LA_SUN, "SUN", ICON_LAMP_SUN, "Sun", "Constant direction parallel ray light source"},
726                 {LA_SPOT, "SPOT", ICON_LAMP_SPOT, "Spot", "Directional cone light source"},
727                 {LA_HEMI, "HEMI", ICON_LAMP_HEMI, "Hemi", "180 degree constant light source"},
728                 {LA_AREA, "AREA", ICON_LAMP_AREA, "Area", "Directional area light source"},
729                 {0, NULL, 0, NULL, NULL}};
730
731         /* identifiers */
732         ot->name= "Add Lamp";
733         ot->description = "Add a lamp object to the scene";
734         ot->idname= "OBJECT_OT_lamp_add";
735         
736         /* api callbacks */
737         ot->invoke= WM_menu_invoke;
738         ot->exec= object_lamp_add_exec;
739         ot->poll= ED_operator_objectmode;
740         
741         /* flags */
742         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
743
744         /* properties */
745         ot->prop= RNA_def_enum(ot->srna, "type", lamp_type_items, 0, "Type", "");
746
747         ED_object_add_generic_props(ot, FALSE);
748 }
749
750 static int group_instance_add_exec(bContext *C, wmOperator *op)
751 {
752         Group *group= BLI_findlink(&CTX_data_main(C)->group, RNA_enum_get(op->ptr, "group"));
753
754         int enter_editmode;
755         unsigned int layer;
756         float loc[3], rot[3];
757         
758         object_add_generic_invoke_options(C, op);
759         if(!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer))
760                 return OPERATOR_CANCELLED;
761
762         if(group) {
763                 Main *bmain= CTX_data_main(C);
764                 Scene *scene= CTX_data_scene(C);
765                 Object *ob= ED_object_add_type(C, OB_EMPTY, loc, rot, FALSE, layer);
766                 rename_id(&ob->id, group->id.name+2);
767                 ob->dup_group= group;
768                 ob->transflag |= OB_DUPLIGROUP;
769                 id_lib_extern(&group->id);
770
771                 /* works without this except if you try render right after, see: 22027 */
772                 DAG_scene_sort(bmain, scene);
773
774                 WM_event_add_notifier(C, NC_SCENE|ND_OB_ACTIVE, CTX_data_scene(C));
775
776                 return OPERATOR_FINISHED;
777         }
778
779         return OPERATOR_CANCELLED;
780 }
781
782 static int object_speaker_add_exec(bContext *C, wmOperator *op)
783 {
784         Object *ob;
785         int enter_editmode;
786         unsigned int layer;
787         float loc[3], rot[3];
788         Scene *scene = CTX_data_scene(C);
789
790         object_add_generic_invoke_options(C, op);
791         if(!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer))
792                 return OPERATOR_CANCELLED;
793
794         ob= ED_object_add_type(C, OB_SPEAKER, loc, rot, FALSE, layer);
795         
796         /* to make it easier to start using this immediately in NLA, a default sound clip is created
797          * ready to be moved around to retime the sound and/or make new sound clips
798          */
799         {
800                 /* create new data for NLA hierarchy */
801                 AnimData *adt = BKE_id_add_animdata(&ob->id);
802                 NlaTrack *nlt = add_nlatrack(adt, NULL);
803                 NlaStrip *strip = add_nla_soundstrip(CTX_data_scene(C), ob->data);
804                 strip->start = CFRA;
805                 strip->end += strip->start;
806                 
807                 /* hook them up */
808                 BKE_nlatrack_add_strip(nlt, strip);
809                 
810                 /* auto-name the strip, and give the track an interesting name  */
811                 strcpy(nlt->name, "SoundTrack");
812                 BKE_nlastrip_validate_name(adt, strip);
813                 
814                 WM_event_add_notifier(C, NC_ANIMATION|ND_NLA|NA_EDITED, NULL);
815         }
816
817         return OPERATOR_FINISHED;
818 }
819
820 void OBJECT_OT_speaker_add(wmOperatorType *ot)
821 {
822         /* identifiers */
823         ot->name= "Add Speaker";
824         ot->description = "Add a speaker object to the scene";
825         ot->idname= "OBJECT_OT_speaker_add";
826
827         /* api callbacks */
828         ot->exec= object_speaker_add_exec;
829         ot->poll= ED_operator_objectmode;
830
831         /* flags */
832         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
833
834         ED_object_add_generic_props(ot, TRUE);
835 }
836
837 /* only used as menu */
838 void OBJECT_OT_group_instance_add(wmOperatorType *ot)
839 {
840         PropertyRNA *prop;
841
842         /* identifiers */
843         ot->name= "Add Group Instance";
844         ot->description = "Add a dupligroup instance";
845         ot->idname= "OBJECT_OT_group_instance_add";
846
847         /* api callbacks */
848         ot->invoke= WM_enum_search_invoke;
849         ot->exec= group_instance_add_exec;
850
851         ot->poll= ED_operator_objectmode;
852
853         /* flags */
854         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
855
856         /* properties */
857         prop= RNA_def_enum(ot->srna, "group", DummyRNA_NULL_items, 0, "Group", "");
858         RNA_def_enum_funcs(prop, RNA_group_itemf);
859         ot->prop= prop;
860         ED_object_add_generic_props(ot, FALSE);
861 }
862
863 /**************************** Delete Object *************************/
864
865 /* remove base from a specific scene */
866 /* note: now unlinks constraints as well */
867 void ED_base_object_free_and_unlink(Main *bmain, Scene *scene, Base *base)
868 {
869         DAG_id_type_tag(bmain, ID_OB);
870         BLI_remlink(&scene->base, base);
871         free_libblock_us(&bmain->object, base->object);
872         if(scene->basact==base) scene->basact= NULL;
873         MEM_freeN(base);
874 }
875
876 static int object_delete_exec(bContext *C, wmOperator *UNUSED(op))
877 {
878         Main *bmain= CTX_data_main(C);
879         Scene *scene= CTX_data_scene(C);
880         /* int islamp= 0; */ /* UNUSED */
881         
882         if(CTX_data_edit_object(C)) 
883                 return OPERATOR_CANCELLED;
884         
885         CTX_DATA_BEGIN(C, Base*, base, selected_bases) {
886
887                 /* if(base->object->type==OB_LAMP) islamp= 1; */
888
889                 /* deselect object -- it could be used in other scenes */
890                 base->object->flag &= ~SELECT;
891
892                 /* remove from current scene only */
893                 ED_base_object_free_and_unlink(bmain, scene, base);
894         }
895         CTX_DATA_END;
896
897         DAG_scene_sort(bmain, scene);
898         DAG_ids_flush_update(bmain, 0);
899         
900         WM_event_add_notifier(C, NC_SCENE|ND_OB_ACTIVE, scene);
901         WM_event_add_notifier(C, NC_SCENE|ND_LAYER_CONTENT, scene);
902         
903         return OPERATOR_FINISHED;
904 }
905
906 void OBJECT_OT_delete(wmOperatorType *ot)
907 {
908         /* identifiers */
909         ot->name= "Delete";
910         ot->description = "Delete selected objects";
911         ot->idname= "OBJECT_OT_delete";
912         
913         /* api callbacks */
914         ot->invoke= WM_operator_confirm;
915         ot->exec= object_delete_exec;
916         ot->poll= ED_operator_objectmode;
917         
918         /* flags */
919         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
920 }
921
922 /**************************** Copy Utilities ******************************/
923
924 /* after copying objects, copied data should get new pointers */
925 static void copy_object_set_idnew(bContext *C, int dupflag)
926 {
927         Main *bmain= CTX_data_main(C);
928         Material *ma, *mao;
929         ID *id;
930         int a;
931         
932         /* XXX check object pointers */
933         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
934                 object_relink(ob);
935         }
936         CTX_DATA_END;
937         
938         /* materials */
939         if( dupflag & USER_DUP_MAT) {
940                 mao= bmain->mat.first;
941                 while(mao) {
942                         if(mao->id.newid) {
943                                 
944                                 ma= (Material *)mao->id.newid;
945                                 
946                                 if(dupflag & USER_DUP_TEX) {
947                                         for(a=0; a<MAX_MTEX; a++) {
948                                                 if(ma->mtex[a]) {
949                                                         id= (ID *)ma->mtex[a]->tex;
950                                                         if(id) {
951                                                                 ID_NEW_US(ma->mtex[a]->tex)
952                                                                 else ma->mtex[a]->tex= copy_texture(ma->mtex[a]->tex);
953                                                                 id->us--;
954                                                         }
955                                                 }
956                                         }
957                                 }
958 #if 0 // XXX old animation system
959                                 id= (ID *)ma->ipo;
960                                 if(id) {
961                                         ID_NEW_US(ma->ipo)
962                                         else ma->ipo= copy_ipo(ma->ipo);
963                                         id->us--;
964                                 }
965 #endif // XXX old animation system
966                         }
967                         mao= mao->id.next;
968                 }
969         }
970         
971 #if 0 // XXX old animation system
972         /* lamps */
973         if( dupflag & USER_DUP_IPO) {
974                 Lamp *la= bmain->lamp.first;
975                 while(la) {
976                         if(la->id.newid) {
977                                 Lamp *lan= (Lamp *)la->id.newid;
978                                 id= (ID *)lan->ipo;
979                                 if(id) {
980                                         ID_NEW_US(lan->ipo)
981                                         else lan->ipo= copy_ipo(lan->ipo);
982                                         id->us--;
983                                 }
984                         }
985                         la= la->id.next;
986                 }
987         }
988         
989         /* ipos */
990         ipo= bmain->ipo.first;
991         while(ipo) {
992                 if(ipo->id.lib==NULL && ipo->id.newid) {
993                         Ipo *ipon= (Ipo *)ipo->id.newid;
994                         IpoCurve *icu;
995                         for(icu= ipon->curve.first; icu; icu= icu->next) {
996                                 if(icu->driver) {
997                                         ID_NEW(icu->driver->ob);
998                                 }
999                         }
1000                 }
1001                 ipo= ipo->id.next;
1002         }
1003 #endif // XXX old animation system
1004         
1005         set_sca_new_poins();
1006         
1007         clear_id_newpoins();
1008 }
1009
1010 /********************* Make Duplicates Real ************************/
1011
1012 static void make_object_duplilist_real(bContext *C, Scene *scene, Base *base,
1013                                        const short use_base_parent,
1014                                        const short use_hierarchy)
1015 {
1016         ListBase *lb;
1017         DupliObject *dob;
1018
1019         if(!(base->object->transflag & OB_DUPLI))
1020                 return;
1021         
1022         lb= object_duplilist(scene, base->object);
1023         
1024         for(dob= lb->first; dob; dob= dob->next) {
1025                 Base *basen;
1026                 Object *ob= copy_object(dob->ob);
1027                 /* font duplis can have a totcol without material, we get them from parent
1028                 * should be implemented better...
1029                 */
1030                 if(ob->mat==NULL) ob->totcol= 0;
1031                 
1032                 basen= MEM_dupallocN(base);
1033                 basen->flag &= ~(OB_FROMDUPLI|OB_FROMGROUP);
1034                 ob->flag= basen->flag;
1035                 basen->lay= base->lay;
1036                 BLI_addhead(&scene->base, basen);       /* addhead: othwise eternal loop */
1037                 basen->object= ob;
1038                 
1039                 /* make sure apply works */
1040                 BKE_free_animdata(&ob->id);     
1041                 ob->adt = NULL;
1042                 
1043                 ob->parent= NULL;
1044                 ob->constraints.first= ob->constraints.last= NULL;
1045                 ob->disp.first= ob->disp.last= NULL;
1046                 ob->transflag &= ~OB_DUPLI;     
1047                 ob->lay= base->lay;
1048                 
1049                 copy_m4_m4(ob->obmat, dob->mat);
1050                 object_apply_mat4(ob, ob->obmat, FALSE, FALSE);
1051         }
1052         
1053         if (use_hierarchy) {
1054                 for(dob= lb->first; dob; dob= dob->next) {
1055                         /* original parents */
1056                         Object *ob_src=     dob->ob;
1057                         Object *ob_src_par= ob_src->parent;
1058
1059                         Object *ob_dst=     (Object *)ob_src->id.newid;
1060
1061                         if (ob_src_par && ob_src_par->id.newid) {
1062                                 /* the parent was also made real, parent newly real duplis */
1063                                 Object *ob_dst_par= (Object *)ob_src_par->id.newid;
1064
1065                                 /* allow for all possible parent types */
1066                                 ob_dst->partype= ob_src->partype;
1067                                 BLI_strncpy(ob_dst->parsubstr, ob_src->parsubstr, sizeof(ob_dst->parsubstr));
1068                                 ob_dst->par1= ob_src->par1;
1069                                 ob_dst->par2= ob_src->par2;
1070                                 ob_dst->par3= ob_src->par3;
1071
1072                                 copy_m4_m4(ob_dst->parentinv, ob_src->parentinv);
1073
1074                                 ob_dst->parent= ob_dst_par;
1075                         }
1076                         else if (use_base_parent) {
1077                                 ob_dst->parent= base->object;
1078                                 ob_dst->partype= PAROBJECT;
1079                         }
1080
1081                         if (ob_dst->parent) {
1082                                 invert_m4_m4(ob_dst->parentinv, dob->mat);
1083
1084                                 /* note, this may be the parent of other objects, but it should
1085                                  * still work out ok */
1086                                 object_apply_mat4(ob_dst, dob->mat, FALSE, TRUE);
1087
1088                                 /* to set ob_dst->orig and incase theres any other discrepicies */
1089                                 DAG_id_tag_update(&ob_dst->id, OB_RECALC_OB);
1090                         }
1091                 }
1092         }
1093         else if (use_base_parent) {
1094                 /* since we are ignoring the internal hierarchy - parent all to the
1095                  * base object */
1096                 for(dob= lb->first; dob; dob= dob->next) {
1097                         /* original parents */
1098                         Object *ob_src=     dob->ob;
1099                         Object *ob_dst=     (Object *)ob_src->id.newid;
1100
1101                         ob_dst->parent= base->object;
1102                         ob_dst->partype= PAROBJECT;
1103
1104                         /* similer to the code above, see comments */
1105                         invert_m4_m4(ob_dst->parentinv, dob->mat);
1106                         object_apply_mat4(ob_dst, dob->mat, FALSE, TRUE);
1107                         DAG_id_tag_update(&ob_dst->id, OB_RECALC_OB);
1108
1109
1110                 }
1111         }
1112
1113         copy_object_set_idnew(C, 0);
1114         
1115         free_object_duplilist(lb);
1116         
1117         base->object->transflag &= ~OB_DUPLI;
1118 }
1119
1120 static int object_duplicates_make_real_exec(bContext *C, wmOperator *op)
1121 {
1122         Main *bmain= CTX_data_main(C);
1123         Scene *scene= CTX_data_scene(C);
1124
1125         const short use_base_parent= RNA_boolean_get(op->ptr, "use_base_parent");
1126         const short use_hierarchy= RNA_boolean_get(op->ptr, "use_hierarchy");
1127         
1128         clear_id_newpoins();
1129                 
1130         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
1131                 make_object_duplilist_real(C, scene, base, use_base_parent, use_hierarchy);
1132
1133                 /* dependencies were changed */
1134                 WM_event_add_notifier(C, NC_OBJECT|ND_PARENT, base->object);
1135         }
1136         CTX_DATA_END;
1137
1138         DAG_scene_sort(bmain, scene);
1139         DAG_ids_flush_update(bmain, 0);
1140         WM_event_add_notifier(C, NC_SCENE, scene);
1141         WM_main_add_notifier(NC_OBJECT|ND_DRAW, NULL);
1142         
1143         return OPERATOR_FINISHED;
1144 }
1145
1146 void OBJECT_OT_duplicates_make_real(wmOperatorType *ot)
1147 {
1148         
1149         /* identifiers */
1150         ot->name= "Make Duplicates Real";
1151         ot->description = "Make dupli objects attached to this object real";
1152         ot->idname= "OBJECT_OT_duplicates_make_real";
1153         
1154         /* api callbacks */
1155         ot->exec= object_duplicates_make_real_exec;
1156         
1157         ot->poll= ED_operator_objectmode;
1158         
1159         /* flags */
1160         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1161
1162         RNA_def_boolean(ot->srna, "use_base_parent", 0, "Parent", "Parent newly created objects to the original duplicator");
1163         RNA_def_boolean(ot->srna, "use_hierarchy", 0, "Keep Hierarchy", "Maintain parent child relationships");
1164 }
1165
1166 /**************************** Convert **************************/
1167
1168 static EnumPropertyItem convert_target_items[]= {
1169         {OB_CURVE, "CURVE", ICON_OUTLINER_OB_CURVE, "Curve from Mesh/Text", ""},
1170         {OB_MESH, "MESH", ICON_OUTLINER_OB_MESH, "Mesh from Curve/Meta/Surf/Text", ""},
1171         {0, NULL, 0, NULL, NULL}};
1172
1173 static void curvetomesh(Scene *scene, Object *ob) 
1174 {
1175         if(ob->disp.first == NULL)
1176                 makeDispListCurveTypes(scene, ob, 0); /* force creation */
1177
1178         nurbs_to_mesh(ob); /* also does users */
1179
1180         if(ob->type == OB_MESH)
1181                 object_free_modifiers(ob);
1182 }
1183
1184 static int convert_poll(bContext *C)
1185 {
1186         Object *obact= CTX_data_active_object(C);
1187         Scene *scene= CTX_data_scene(C);
1188
1189         return (!scene->id.lib && obact && scene->obedit != obact && (obact->flag & SELECT) && !(obact->id.lib));
1190 }
1191
1192 /* Helper for convert_exec */
1193 static Base *duplibase_for_convert(Scene *scene, Base *base, Object *ob)
1194 {
1195         Object *obn;
1196         Base *basen;
1197
1198         if (ob == NULL) {
1199                 ob= base->object;
1200         }
1201
1202         obn= copy_object(ob);
1203         obn->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
1204
1205         basen= MEM_mallocN(sizeof(Base), "duplibase");
1206         *basen= *base;
1207         BLI_addhead(&scene->base, basen);       /* addhead: otherwise eternal loop */
1208         basen->object= obn;
1209         basen->flag |= SELECT;
1210         obn->flag |= SELECT;
1211         base->flag &= ~SELECT;
1212         ob->flag &= ~SELECT;
1213
1214         return basen;
1215 }
1216
1217 static int convert_exec(bContext *C, wmOperator *op)
1218 {
1219         Main *bmain= CTX_data_main(C);
1220         Scene *scene= CTX_data_scene(C);
1221         Base *basen=NULL, *basact=NULL, *basedel=NULL;
1222         Object *ob, *ob1, *newob, *obact= CTX_data_active_object(C);
1223         DerivedMesh *dm;
1224         Curve *cu;
1225         Nurb *nu;
1226         MetaBall *mb;
1227         Mesh *me;
1228         const short target= RNA_enum_get(op->ptr, "target");
1229         const short keep_original= RNA_boolean_get(op->ptr, "keep_original");
1230         int a, mballConverted= 0;
1231
1232         /* don't forget multiple users! */
1233
1234         /* reset flags */
1235         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
1236                 ob= base->object;
1237                 ob->flag &= ~OB_DONE;
1238
1239                 /* flag data thats not been edited (only needed for !keep_original) */
1240                 if(ob->data) {
1241                         ((ID *)ob->data)->flag |= LIB_DOIT;
1242                 }
1243         }
1244         CTX_DATA_END;
1245
1246         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
1247                 ob= base->object;
1248
1249                 if(ob->flag & OB_DONE || !IS_TAGGED(ob->data)) {
1250                         if (ob->type != target) {
1251                                 base->flag &= ~SELECT;
1252                                 ob->flag &= ~SELECT;
1253                         }
1254
1255                         /* obdata already modified */
1256                         if(!IS_TAGGED(ob->data)) {
1257                                 /* When 2 objects with linked data are selected, converting both
1258                                  * would keep modifiers on all but the converted object [#26003] */
1259                                 if(ob->type == OB_MESH) {
1260                                         object_free_modifiers(ob);      /* after derivedmesh calls! */
1261                                 }
1262                         }
1263                 }
1264                 else if (ob->type==OB_MESH && target == OB_CURVE) {
1265                         ob->flag |= OB_DONE;
1266
1267                         if (keep_original) {
1268                                 basen= duplibase_for_convert(scene, base, NULL);
1269                                 newob= basen->object;
1270
1271                                 /* decrement original mesh's usage count  */
1272                                 me= newob->data;
1273                                 me->id.us--;
1274
1275                                 /* make a new copy of the mesh */
1276                                 newob->data= copy_mesh(me);
1277                         } else {
1278                                 newob = ob;
1279                         }
1280
1281                         mesh_to_curve(scene, newob);
1282
1283                         if(newob->type==OB_CURVE)
1284                                 object_free_modifiers(newob);   /* after derivedmesh calls! */
1285                 }
1286                 else if(ob->type==OB_MESH && ob->modifiers.first) { /* converting a mesh with no modifiers causes a segfault */
1287                         ob->flag |= OB_DONE;
1288
1289                         if (keep_original) {
1290                                 basen= duplibase_for_convert(scene, base, NULL);
1291                                 newob= basen->object;
1292
1293                                 /* decrement original mesh's usage count  */
1294                                 me= newob->data;
1295                                 me->id.us--;
1296
1297                                 /* make a new copy of the mesh */
1298                                 newob->data= copy_mesh(me);
1299                         } else {
1300                                 newob = ob;
1301                                 ob->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
1302                         }
1303
1304                         /* make new mesh data from the original copy */
1305                         /* note: get the mesh from the original, not from the copy in some
1306                          * cases this doesnt give correct results (when MDEF is used for eg)
1307                          */
1308                         dm= mesh_get_derived_final(scene, newob, CD_MASK_MESH);
1309                         /* dm= mesh_create_derived_no_deform(ob1, NULL);        this was called original (instead of get_derived). man o man why! (ton) */
1310
1311                         DM_to_mesh(dm, newob->data);
1312
1313                         dm->release(dm);
1314                         object_free_modifiers(newob);   /* after derivedmesh calls! */
1315                 }
1316                 else if(ob->type==OB_FONT) {
1317                         ob->flag |= OB_DONE;
1318
1319                         if (keep_original) {
1320                                 basen= duplibase_for_convert(scene, base, NULL);
1321                                 newob= basen->object;
1322
1323                                 /* decrement original curve's usage count  */
1324                                 ((Curve *)newob->data)->id.us--;
1325
1326                                 /* make a new copy of the curve */
1327                                 newob->data= copy_curve(ob->data);
1328                         } else {
1329                                 newob= ob;
1330                         }
1331
1332                         cu= newob->data;
1333
1334                         if (!newob->disp.first)
1335                                 makeDispListCurveTypes(scene, newob, 0);
1336
1337                         newob->type= OB_CURVE;
1338
1339                         if(cu->vfont) {
1340                                 cu->vfont->id.us--;
1341                                 cu->vfont= NULL;
1342                         }
1343                         if(cu->vfontb) {
1344                                 cu->vfontb->id.us--;
1345                                 cu->vfontb= NULL;
1346                         }
1347                         if(cu->vfonti) {
1348                                 cu->vfonti->id.us--;
1349                                 cu->vfonti= NULL;
1350                         }
1351                         if(cu->vfontbi) {
1352                                 cu->vfontbi->id.us--;
1353                                 cu->vfontbi= NULL;
1354                         }
1355
1356                         if (!keep_original) {
1357                                 /* other users */
1358                                 if(cu->id.us>1) {
1359                                         for(ob1= bmain->object.first; ob1; ob1=ob1->id.next) {
1360                                                 if(ob1->data==ob->data) {
1361                                                         ob1->type= OB_CURVE;
1362                                                         ob1->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
1363                                                 }
1364                                         }
1365                                 }
1366                         }
1367
1368                         for(nu=cu->nurb.first; nu; nu=nu->next)
1369                                 nu->charidx= 0;
1370
1371                         if(target == OB_MESH) {
1372                                 curvetomesh(scene, newob);
1373
1374                                 /* meshes doesn't use displist */
1375                                 freedisplist(&newob->disp);
1376                         }
1377                 }
1378                 else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
1379                         ob->flag |= OB_DONE;
1380
1381                         if(target == OB_MESH) {
1382                                 if (keep_original) {
1383                                         basen= duplibase_for_convert(scene, base, NULL);
1384                                         newob= basen->object;
1385
1386                                         /* decrement original curve's usage count  */
1387                                         ((Curve *)newob->data)->id.us--;
1388
1389                                         /* make a new copy of the curve */
1390                                         newob->data= copy_curve(ob->data);
1391                                 } else {
1392                                         newob= ob;
1393
1394                                         /* meshes doesn't use displist */
1395                                         freedisplist(&newob->disp);
1396                                 }
1397
1398                                 curvetomesh(scene, newob);
1399                         }
1400                 }
1401                 else if(ob->type==OB_MBALL && target == OB_MESH) {
1402                         Object *baseob;
1403
1404                         base->flag &= ~SELECT;
1405                         ob->flag &= ~SELECT;
1406
1407                         baseob= find_basis_mball(scene, ob);
1408
1409                         if (ob != baseob) {
1410                                 /* if motherball is converting it would be marked as done later */
1411                                 ob->flag |= OB_DONE;
1412                         }
1413
1414                         if (!baseob->disp.first) {
1415                                 makeDispListMBall(scene, baseob);
1416                         }
1417
1418                         if(!(baseob->flag & OB_DONE)) {
1419                                 baseob->flag |= OB_DONE;
1420
1421                                 basen= duplibase_for_convert(scene, base, baseob);
1422                                 newob= basen->object;
1423
1424                                 mb= newob->data;
1425                                 mb->id.us--;
1426
1427                                 newob->data= add_mesh("Mesh");
1428                                 newob->type= OB_MESH;
1429
1430                                 me= newob->data;
1431                                 me->totcol= mb->totcol;
1432                                 if(newob->totcol) {
1433                                         me->mat= MEM_dupallocN(mb->mat);
1434                                         for(a=0; a<newob->totcol; a++) id_us_plus((ID *)me->mat[a]);
1435                                 }
1436
1437                                 mball_to_mesh(&baseob->disp, newob->data);
1438
1439                                 if (obact->type == OB_MBALL) {
1440                                         basact= basen;
1441                                 }
1442
1443                                 mballConverted= 1;
1444                         }
1445                 }
1446                 else {
1447                         continue;
1448                 }
1449
1450                 /* tag obdata if it was been changed */
1451
1452                 /* If the original object is active then make this object active */
1453                 if(basen) {
1454                         if(ob == obact) {
1455                                 /* store new active base to update BASACT */
1456                                 basact= basen;
1457                         }
1458
1459                         basen= NULL;
1460                 }
1461
1462                 if (!keep_original && (ob->flag & OB_DONE)) {
1463                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1464                         ((ID *)ob->data)->flag &= ~LIB_DOIT; /* flag not to convert this datablock again */
1465                 }
1466
1467                 /* delete original if needed */
1468                 if(basedel) {
1469                         if(!keep_original)
1470                                 ED_base_object_free_and_unlink(bmain, scene, basedel);  
1471
1472                         basedel = NULL;
1473                 }
1474         }
1475         CTX_DATA_END;
1476
1477         if(!keep_original) {
1478                 if (mballConverted) {
1479                         Base *base= scene->base.first, *tmpbase;
1480                         while (base) {
1481                                 ob= base->object;
1482                                 tmpbase= base;
1483                                 base= base->next;
1484
1485                                 if (ob->type == OB_MBALL) {
1486                                         ED_base_object_free_and_unlink(bmain, scene, tmpbase);
1487                                 }
1488                         }
1489                 }
1490
1491                 /* delete object should renew depsgraph */
1492                 DAG_scene_sort(bmain, scene);
1493         }
1494
1495 // XXX  ED_object_enter_editmode(C, 0);
1496 // XXX  exit_editmode(C, EM_FREEDATA|EM_WAITCURSOR); /* freedata, but no undo */
1497
1498         if (basact) {
1499                 /* active base was changed */
1500                 ED_base_object_activate(C, basact);
1501                 BASACT= basact;
1502         } else if (BASACT->object->flag & OB_DONE) {
1503                 WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, BASACT->object);
1504                 WM_event_add_notifier(C, NC_OBJECT|ND_DATA, BASACT->object);
1505         }
1506
1507         DAG_scene_sort(bmain, scene);
1508         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, scene);
1509         WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene);
1510
1511         return OPERATOR_FINISHED;
1512 }
1513
1514
1515 void OBJECT_OT_convert(wmOperatorType *ot)
1516 {
1517         /* identifiers */
1518         ot->name= "Convert to";
1519         ot->description = "Convert selected objects to another type";
1520         ot->idname= "OBJECT_OT_convert";
1521         
1522         /* api callbacks */
1523         ot->invoke= WM_menu_invoke;
1524         ot->exec= convert_exec;
1525         ot->poll= convert_poll;
1526         
1527         /* flags */
1528         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1529
1530         /* properties */
1531         ot->prop= RNA_def_enum(ot->srna, "target", convert_target_items, OB_MESH, "Target", "Type of object to convert to");
1532         RNA_def_boolean(ot->srna, "keep_original", 0, "Keep Original", "Keep original objects instead of replacing them");
1533 }
1534
1535 /**************************** Duplicate ************************/
1536
1537 /* 
1538         dupflag: a flag made from constants declared in DNA_userdef_types.h
1539         The flag tells adduplicate() weather to copy data linked to the object, or to reference the existing data.
1540         U.dupflag for default operations or you can construct a flag as python does
1541         if the dupflag is 0 then no data will be copied (linked duplicate) */
1542
1543 /* used below, assumes id.new is correct */
1544 /* leaves selection of base/object unaltered */
1545 static Base *object_add_duplicate_internal(Main *bmain, Scene *scene, Base *base, int dupflag)
1546 {
1547         Base *basen= NULL;
1548         Material ***matarar;
1549         Object *ob, *obn;
1550         ID *id;
1551         int a, didit;
1552
1553         ob= base->object;
1554         if(ob->mode & OB_MODE_POSE) {
1555                 ; /* nothing? */
1556         }
1557         else {
1558                 obn= copy_object(ob);
1559                 obn->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
1560                 
1561                 basen= MEM_mallocN(sizeof(Base), "duplibase");
1562                 *basen= *base;
1563                 BLI_addhead(&scene->base, basen);       /* addhead: prevent eternal loop */
1564                 basen->object= obn;
1565                 
1566                 if(basen->flag & OB_FROMGROUP) {
1567                         Group *group;
1568                         for(group= bmain->group.first; group; group= group->id.next) {
1569                                 if(object_in_group(ob, group))
1570                                         add_to_group(group, obn, scene, basen);
1571                         }
1572                 }
1573                 
1574                 /* duplicates using userflags */
1575                 if(dupflag & USER_DUP_ACT) {
1576                         BKE_copy_animdata_id_action(&obn->id);
1577                 }
1578                 
1579                 if(dupflag & USER_DUP_MAT) {
1580                         for(a=0; a<obn->totcol; a++) {
1581                                 id= (ID *)obn->mat[a];
1582                                 if(id) {
1583                                         ID_NEW_US(obn->mat[a])
1584                                         else obn->mat[a]= copy_material(obn->mat[a]);
1585                                         id->us--;
1586                                         
1587                                         if(dupflag & USER_DUP_ACT) {
1588                                                 BKE_copy_animdata_id_action(&obn->mat[a]->id);
1589                                         }
1590                                 }
1591                         }
1592                 }
1593                 if(dupflag & USER_DUP_PSYS) {
1594                         ParticleSystem *psys;
1595                         for(psys=obn->particlesystem.first; psys; psys=psys->next) {
1596                                 id= (ID*) psys->part;
1597                                 if(id) {
1598                                         ID_NEW_US(psys->part)
1599                                         else psys->part= psys_copy_settings(psys->part);
1600                                         
1601                                         if(dupflag & USER_DUP_ACT) {
1602                                                 BKE_copy_animdata_id_action(&psys->part->id);
1603                                         }
1604
1605                                         id->us--;
1606                                 }
1607                         }
1608                 }
1609                 
1610                 id= obn->data;
1611                 didit= 0;
1612                 
1613                 switch(obn->type) {
1614                         case OB_MESH:
1615                                 if(dupflag & USER_DUP_MESH) {
1616                                         ID_NEW_US2( obn->data )
1617                                         else {
1618                                                 obn->data= copy_mesh(obn->data);
1619                                                 
1620                                                 if(obn->fluidsimSettings) {
1621                                                         obn->fluidsimSettings->orgMesh = (Mesh *)obn->data;
1622                                                 }
1623                                                 
1624                                                 didit= 1;
1625                                         }
1626                                         id->us--;
1627                                 }
1628                                 break;
1629                         case OB_CURVE:
1630                                 if(dupflag & USER_DUP_CURVE) {
1631                                         ID_NEW_US2(obn->data )
1632                                         else {
1633                                                 obn->data= copy_curve(obn->data);
1634                                                 didit= 1;
1635                                         }
1636                                         id->us--;
1637                                 }
1638                                 break;
1639                         case OB_SURF:
1640                                 if(dupflag & USER_DUP_SURF) {
1641                                         ID_NEW_US2( obn->data )
1642                                         else {
1643                                                 obn->data= copy_curve(obn->data);
1644                                                 didit= 1;
1645                                         }
1646                                         id->us--;
1647                                 }
1648                                 break;
1649                         case OB_FONT:
1650                                 if(dupflag & USER_DUP_FONT) {
1651                                         ID_NEW_US2( obn->data )
1652                                         else {
1653                                                 obn->data= copy_curve(obn->data);
1654                                                 didit= 1;
1655                                         }
1656                                         id->us--;
1657                                 }
1658                                 break;
1659                         case OB_MBALL:
1660                                 if(dupflag & USER_DUP_MBALL) {
1661                                         ID_NEW_US2(obn->data )
1662                                         else {
1663                                                 obn->data= copy_mball(obn->data);
1664                                                 didit= 1;
1665                                         }
1666                                         id->us--;
1667                                 }
1668                                 break;
1669                         case OB_LAMP:
1670                                 if(dupflag & USER_DUP_LAMP) {
1671                                         ID_NEW_US2(obn->data )
1672                                         else {
1673                                                 obn->data= copy_lamp(obn->data);
1674                                                 didit= 1;
1675                                         }
1676                                         id->us--;
1677                                 }
1678                                 break;
1679                                 
1680                         case OB_ARMATURE:
1681                                 obn->recalc |= OB_RECALC_DATA;
1682                                 if(obn->pose) obn->pose->flag |= POSE_RECALC;
1683                                         
1684                                         if(dupflag & USER_DUP_ARM) {
1685                                                 ID_NEW_US2(obn->data )
1686                                                 else {
1687                                                         obn->data= copy_armature(obn->data);
1688                                                         armature_rebuild_pose(obn, obn->data);
1689                                                         didit= 1;
1690                                                 }
1691                                                 id->us--;
1692                                         }
1693                                                 
1694                                                 break;
1695                                 
1696                         case OB_LATTICE:
1697                                 if(dupflag!=0) {
1698                                         ID_NEW_US2(obn->data )
1699                                         else {
1700                                                 obn->data= copy_lattice(obn->data);
1701                                                 didit= 1;
1702                                         }
1703                                         id->us--;
1704                                 }
1705                                 break;
1706                         case OB_CAMERA:
1707                                 if(dupflag!=0) {
1708                                         ID_NEW_US2(obn->data )
1709                                         else {
1710                                                 obn->data= copy_camera(obn->data);
1711                                                 didit= 1;
1712                                         }
1713                                         id->us--;
1714                                 }
1715                                 break;
1716                         case OB_SPEAKER:
1717                                 if(dupflag!=0) {
1718                                         ID_NEW_US2(obn->data )
1719                                         else {
1720                                                 obn->data= copy_speaker(obn->data);
1721                                                 didit= 1;
1722                                         }
1723                                         id->us--;
1724                                 }
1725                                 break;
1726
1727                 }
1728
1729                 /* check if obdata is copied */
1730                 if(didit) {
1731                         Key *key = ob_get_key(obn);
1732                         
1733                         if(dupflag & USER_DUP_ACT) {
1734                                 BKE_copy_animdata_id_action((ID *)obn->data);
1735                                 if(key) BKE_copy_animdata_id_action((ID*)key);
1736                         }
1737                         
1738                         if(dupflag & USER_DUP_MAT) {
1739                                 matarar= give_matarar(obn);
1740                                 if(matarar) {
1741                                         for(a=0; a<obn->totcol; a++) {
1742                                                 id= (ID *)(*matarar)[a];
1743                                                 if(id) {
1744                                                         ID_NEW_US( (*matarar)[a] )
1745                                                         else (*matarar)[a]= copy_material((*matarar)[a]);
1746                                                         
1747                                                         id->us--;
1748                                                 }
1749                                         }
1750                                 }
1751                         }
1752                 }
1753         }
1754         return basen;
1755 }
1756
1757 /* single object duplicate, if dupflag==0, fully linked, else it uses the flags given */
1758 /* leaves selection of base/object unaltered.
1759  * note: don't call this within a loop since clear_* funcs loop over the entire database. */
1760 Base *ED_object_add_duplicate(Main *bmain, Scene *scene, Base *base, int dupflag)
1761 {
1762         Base *basen;
1763         Object *ob;
1764
1765         clear_id_newpoins();
1766         clear_sca_new_poins();  /* sensor/contr/act */
1767
1768         basen= object_add_duplicate_internal(bmain, scene, base, dupflag);
1769         if (basen == NULL) {
1770                 return NULL;
1771         }
1772
1773         ob= basen->object;
1774
1775         /* link own references to the newly duplicated data [#26816] */
1776         object_relink(ob);
1777         set_sca_new_poins_ob(ob);
1778
1779         DAG_scene_sort(bmain, scene);
1780         ED_render_id_flush_update(bmain, ob->data);
1781
1782         return basen;
1783 }
1784
1785 /* contextual operator dupli */
1786 static int duplicate_exec(bContext *C, wmOperator *op)
1787 {
1788         Main *bmain= CTX_data_main(C);
1789         Scene *scene= CTX_data_scene(C);
1790         int linked= RNA_boolean_get(op->ptr, "linked");
1791         int dupflag= (linked)? 0: U.dupflag;
1792         
1793         clear_id_newpoins();
1794         clear_sca_new_poins();  /* sensor/contr/act */
1795         
1796         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
1797                 Base *basen= object_add_duplicate_internal(bmain, scene, base, dupflag);
1798                 
1799                 /* note that this is safe to do with this context iterator,
1800                    the list is made in advance */
1801                 ED_base_object_select(base, BA_DESELECT);
1802
1803                 if (basen == NULL) {
1804                         continue;
1805                 }
1806
1807                 /* new object becomes active */
1808                 if(BASACT==base)
1809                         ED_base_object_activate(C, basen);
1810
1811                 if(basen->object->data) {
1812                         DAG_id_tag_update(basen->object->data, 0);
1813                 }
1814         }
1815         CTX_DATA_END;
1816
1817         copy_object_set_idnew(C, dupflag);
1818
1819         DAG_scene_sort(bmain, scene);
1820         DAG_ids_flush_update(bmain, 0);
1821
1822         WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene);
1823
1824         return OPERATOR_FINISHED;
1825 }
1826
1827 void OBJECT_OT_duplicate(wmOperatorType *ot)
1828 {
1829         PropertyRNA *prop;
1830         
1831         /* identifiers */
1832         ot->name= "Duplicate Objects";
1833         ot->description = "Duplicate selected objects";
1834         ot->idname= "OBJECT_OT_duplicate";
1835         
1836         /* api callbacks */
1837         ot->exec= duplicate_exec;
1838         ot->poll= ED_operator_objectmode;
1839         
1840         /* flags */
1841         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1842         
1843         /* to give to transform */
1844         RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data");
1845         prop= RNA_def_enum(ot->srna, "mode", transform_mode_types, TFM_TRANSLATION, "Mode", "");
1846         RNA_def_property_flag(prop, PROP_HIDDEN);
1847 }
1848
1849 /* **************** add named object, for dragdrop ************* */
1850
1851
1852 static int add_named_exec(bContext *C, wmOperator *op)
1853 {
1854         Main *bmain= CTX_data_main(C);
1855         Scene *scene= CTX_data_scene(C);
1856         Base *basen, *base;
1857         Object *ob;
1858         int linked= RNA_boolean_get(op->ptr, "linked");
1859         int dupflag= (linked)? 0: U.dupflag;
1860         char name[32];
1861
1862         /* find object, create fake base */
1863         RNA_string_get(op->ptr, "name", name);
1864         ob= (Object *)find_id("OB", name);
1865         if(ob==NULL) 
1866                 return OPERATOR_CANCELLED;
1867
1868         base= MEM_callocN(sizeof(Base), "duplibase");
1869         base->object= ob;
1870         base->flag= ob->flag;
1871
1872         /* prepare dupli */
1873         clear_id_newpoins();
1874         clear_sca_new_poins();  /* sensor/contr/act */
1875
1876         basen= object_add_duplicate_internal(bmain, scene, base, dupflag);
1877
1878         if (basen == NULL) {
1879                 MEM_freeN(base);
1880                 return OPERATOR_CANCELLED;
1881         }
1882
1883         basen->lay= basen->object->lay= scene->lay;
1884
1885         ED_object_location_from_view(C, basen->object->loc);
1886         ED_base_object_activate(C, basen);
1887
1888         copy_object_set_idnew(C, dupflag);
1889
1890         DAG_scene_sort(bmain, scene);
1891         DAG_ids_flush_update(bmain, 0);
1892
1893         MEM_freeN(base);
1894
1895         WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene);
1896
1897         return OPERATOR_FINISHED;
1898 }
1899
1900 void OBJECT_OT_add_named(wmOperatorType *ot)
1901 {
1902         /* identifiers */
1903         ot->name= "Add Named Object";
1904         ot->description = "Add named object";
1905         ot->idname= "OBJECT_OT_add_named";
1906         
1907         /* api callbacks */
1908         ot->exec= add_named_exec;
1909         ot->poll= ED_operator_objectmode;
1910         
1911         /* flags */
1912         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1913         
1914         RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data");
1915         RNA_def_string(ot->srna, "name", "Cube", 24, "Name", "Object name to add");
1916 }
1917
1918
1919
1920 /**************************** Join *************************/
1921 static int join_poll(bContext *C)
1922 {
1923         Object *ob= CTX_data_active_object(C);
1924         
1925         if (!ob || ob->id.lib) return 0;
1926         
1927         if (ELEM4(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_ARMATURE))
1928                 return ED_operator_screenactive(C);
1929         else
1930                 return 0;
1931 }
1932
1933
1934 static int join_exec(bContext *C, wmOperator *op)
1935 {
1936         Scene *scene= CTX_data_scene(C);
1937         Object *ob= CTX_data_active_object(C);
1938
1939         if(scene->obedit) {
1940                 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in editmode");
1941                 return OPERATOR_CANCELLED;
1942         }
1943         else if(object_data_is_libdata(ob)) {
1944                 BKE_report(op->reports, RPT_ERROR, "Can't edit external libdata");
1945                 return OPERATOR_CANCELLED;
1946         }
1947
1948         if(ob->type == OB_MESH)
1949                 return join_mesh_exec(C, op);
1950         else if(ELEM(ob->type, OB_CURVE, OB_SURF))
1951                 return join_curve_exec(C, op);
1952         else if(ob->type == OB_ARMATURE)
1953                 return join_armature_exec(C, op);
1954         
1955         return OPERATOR_CANCELLED;
1956 }
1957
1958 void OBJECT_OT_join(wmOperatorType *ot)
1959 {
1960         /* identifiers */
1961         ot->name= "Join";
1962         ot->description = "Join selected objects into active object";
1963         ot->idname= "OBJECT_OT_join";
1964         
1965         /* api callbacks */
1966         ot->exec= join_exec;
1967         ot->poll= join_poll;
1968         
1969         /* flags */
1970         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1971 }
1972
1973 /**************************** Join as Shape Key*************************/
1974 static int join_shapes_poll(bContext *C)
1975 {
1976         Object *ob= CTX_data_active_object(C);
1977         
1978         if (!ob || ob->id.lib) return 0;
1979         
1980         /* only meshes supported at the moment */
1981         if (ob->type == OB_MESH)
1982                 return ED_operator_screenactive(C);
1983         else
1984                 return 0;
1985 }
1986
1987 static int join_shapes_exec(bContext *C, wmOperator *op)
1988 {
1989         Scene *scene= CTX_data_scene(C);
1990         Object *ob= CTX_data_active_object(C);
1991         
1992         if(scene->obedit) {
1993                 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in editmode");
1994                 return OPERATOR_CANCELLED;
1995         }
1996         else if(object_data_is_libdata(ob)) {
1997                 BKE_report(op->reports, RPT_ERROR, "Can't edit external libdata");
1998                 return OPERATOR_CANCELLED;
1999         }
2000         
2001         if(ob->type == OB_MESH)
2002                 return join_mesh_shapes_exec(C, op);
2003         
2004         return OPERATOR_CANCELLED;
2005 }
2006
2007 void OBJECT_OT_join_shapes(wmOperatorType *ot)
2008 {
2009         /* identifiers */
2010         ot->name= "Join as Shapes";
2011         ot->description = "Merge selected objects to shapes of active object";
2012         ot->idname= "OBJECT_OT_join_shapes";
2013         
2014         /* api callbacks */
2015         ot->exec= join_shapes_exec;
2016         ot->poll= join_shapes_poll;
2017         
2018         /* flags */
2019         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2020 }