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