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