Made minor revisions (no functional changes).
[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 *op)
889 {
890         Main *bmain= CTX_data_main(C);
891         Scene *scene= CTX_data_scene(C);
892         const short use_global= RNA_boolean_get(op->ptr, "global");
893         /* int islamp= 0; */ /* UNUSED */
894         
895         if(CTX_data_edit_object(C)) 
896                 return OPERATOR_CANCELLED;
897         
898         CTX_DATA_BEGIN(C, Base*, base, selected_bases) {
899
900                 /* if(base->object->type==OB_LAMP) islamp= 1; */
901
902                 /* deselect object -- it could be used in other scenes */
903                 base->object->flag &= ~SELECT;
904
905                 /* remove from current scene only */
906                 ED_base_object_free_and_unlink(bmain, scene, base);
907
908                 if (use_global) {
909                         Scene *scene_iter;
910                         Base *base_other;
911
912                         for (scene_iter= bmain->scene.first; scene_iter; scene_iter= scene_iter->id.next) {
913                                 if (scene_iter != scene && !(scene_iter->id.lib)) {
914                                         base_other= object_in_scene(base->object, scene_iter);
915                                         if (base_other) {
916                                                 ED_base_object_free_and_unlink(bmain, scene_iter, base_other);
917                                         }
918                                 }
919                         }
920                 }
921                 /* end global */
922
923         }
924         CTX_DATA_END;
925
926         DAG_scene_sort(bmain, scene);
927         DAG_ids_flush_update(bmain, 0);
928         
929         WM_event_add_notifier(C, NC_SCENE|ND_OB_ACTIVE, scene);
930         WM_event_add_notifier(C, NC_SCENE|ND_LAYER_CONTENT, scene);
931         
932         return OPERATOR_FINISHED;
933 }
934
935 void OBJECT_OT_delete(wmOperatorType *ot)
936 {
937         /* identifiers */
938         ot->name= "Delete";
939         ot->description = "Delete selected objects";
940         ot->idname= "OBJECT_OT_delete";
941         
942         /* api callbacks */
943         ot->invoke= WM_operator_confirm;
944         ot->exec= object_delete_exec;
945         ot->poll= ED_operator_objectmode;
946         
947         /* flags */
948         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
949
950         RNA_def_boolean(ot->srna, "global", 0, "Delete Globally", "Remove object from all scenes");
951 }
952
953 /**************************** Copy Utilities ******************************/
954
955 /* after copying objects, copied data should get new pointers */
956 static void copy_object_set_idnew(bContext *C, int dupflag)
957 {
958         Main *bmain= CTX_data_main(C);
959         Material *ma, *mao;
960         ID *id;
961         int a;
962         
963         /* XXX check object pointers */
964         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
965                 object_relink(ob);
966         }
967         CTX_DATA_END;
968         
969         /* materials */
970         if( dupflag & USER_DUP_MAT) {
971                 mao= bmain->mat.first;
972                 while(mao) {
973                         if(mao->id.newid) {
974                                 
975                                 ma= (Material *)mao->id.newid;
976                                 
977                                 if(dupflag & USER_DUP_TEX) {
978                                         for(a=0; a<MAX_MTEX; a++) {
979                                                 if(ma->mtex[a]) {
980                                                         id= (ID *)ma->mtex[a]->tex;
981                                                         if(id) {
982                                                                 ID_NEW_US(ma->mtex[a]->tex)
983                                                                 else ma->mtex[a]->tex= copy_texture(ma->mtex[a]->tex);
984                                                                 id->us--;
985                                                         }
986                                                 }
987                                         }
988                                 }
989 #if 0 // XXX old animation system
990                                 id= (ID *)ma->ipo;
991                                 if(id) {
992                                         ID_NEW_US(ma->ipo)
993                                         else ma->ipo= copy_ipo(ma->ipo);
994                                         id->us--;
995                                 }
996 #endif // XXX old animation system
997                         }
998                         mao= mao->id.next;
999                 }
1000         }
1001         
1002 #if 0 // XXX old animation system
1003         /* lamps */
1004         if( dupflag & USER_DUP_IPO) {
1005                 Lamp *la= bmain->lamp.first;
1006                 while(la) {
1007                         if(la->id.newid) {
1008                                 Lamp *lan= (Lamp *)la->id.newid;
1009                                 id= (ID *)lan->ipo;
1010                                 if(id) {
1011                                         ID_NEW_US(lan->ipo)
1012                                         else lan->ipo= copy_ipo(lan->ipo);
1013                                         id->us--;
1014                                 }
1015                         }
1016                         la= la->id.next;
1017                 }
1018         }
1019         
1020         /* ipos */
1021         ipo= bmain->ipo.first;
1022         while(ipo) {
1023                 if(ipo->id.lib==NULL && ipo->id.newid) {
1024                         Ipo *ipon= (Ipo *)ipo->id.newid;
1025                         IpoCurve *icu;
1026                         for(icu= ipon->curve.first; icu; icu= icu->next) {
1027                                 if(icu->driver) {
1028                                         ID_NEW(icu->driver->ob);
1029                                 }
1030                         }
1031                 }
1032                 ipo= ipo->id.next;
1033         }
1034 #endif // XXX old animation system
1035         
1036         set_sca_new_poins();
1037         
1038         clear_id_newpoins();
1039 }
1040
1041 /********************* Make Duplicates Real ************************/
1042
1043 static void make_object_duplilist_real(bContext *C, Scene *scene, Base *base,
1044                                        const short use_base_parent,
1045                                        const short use_hierarchy)
1046 {
1047         ListBase *lb;
1048         DupliObject *dob;
1049
1050         if(!(base->object->transflag & OB_DUPLI))
1051                 return;
1052         
1053         lb= object_duplilist(scene, base->object);
1054         
1055         for(dob= lb->first; dob; dob= dob->next) {
1056                 Base *basen;
1057                 Object *ob= copy_object(dob->ob);
1058                 /* font duplis can have a totcol without material, we get them from parent
1059                 * should be implemented better...
1060                 */
1061                 if(ob->mat==NULL) ob->totcol= 0;
1062                 
1063                 basen= MEM_dupallocN(base);
1064                 basen->flag &= ~(OB_FROMDUPLI|OB_FROMGROUP);
1065                 ob->flag= basen->flag;
1066                 basen->lay= base->lay;
1067                 BLI_addhead(&scene->base, basen);       /* addhead: othwise eternal loop */
1068                 basen->object= ob;
1069                 
1070                 /* make sure apply works */
1071                 BKE_free_animdata(&ob->id);     
1072                 ob->adt = NULL;
1073                 
1074                 ob->parent= NULL;
1075                 ob->constraints.first= ob->constraints.last= NULL;
1076                 ob->disp.first= ob->disp.last= NULL;
1077                 ob->transflag &= ~OB_DUPLI;     
1078                 ob->lay= base->lay;
1079                 
1080                 copy_m4_m4(ob->obmat, dob->mat);
1081                 object_apply_mat4(ob, ob->obmat, FALSE, FALSE);
1082         }
1083         
1084         if (use_hierarchy) {
1085                 for(dob= lb->first; dob; dob= dob->next) {
1086                         /* original parents */
1087                         Object *ob_src=     dob->ob;
1088                         Object *ob_src_par= ob_src->parent;
1089
1090                         Object *ob_dst=     (Object *)ob_src->id.newid;
1091
1092                         if (ob_src_par && ob_src_par->id.newid) {
1093                                 /* the parent was also made real, parent newly real duplis */
1094                                 Object *ob_dst_par= (Object *)ob_src_par->id.newid;
1095
1096                                 /* allow for all possible parent types */
1097                                 ob_dst->partype= ob_src->partype;
1098                                 BLI_strncpy(ob_dst->parsubstr, ob_src->parsubstr, sizeof(ob_dst->parsubstr));
1099                                 ob_dst->par1= ob_src->par1;
1100                                 ob_dst->par2= ob_src->par2;
1101                                 ob_dst->par3= ob_src->par3;
1102
1103                                 copy_m4_m4(ob_dst->parentinv, ob_src->parentinv);
1104
1105                                 ob_dst->parent= ob_dst_par;
1106                         }
1107                         else if (use_base_parent) {
1108                                 ob_dst->parent= base->object;
1109                                 ob_dst->partype= PAROBJECT;
1110                         }
1111
1112                         if (ob_dst->parent) {
1113                                 invert_m4_m4(ob_dst->parentinv, dob->mat);
1114
1115                                 /* note, this may be the parent of other objects, but it should
1116                                  * still work out ok */
1117                                 object_apply_mat4(ob_dst, dob->mat, FALSE, TRUE);
1118
1119                                 /* to set ob_dst->orig and incase theres any other discrepicies */
1120                                 DAG_id_tag_update(&ob_dst->id, OB_RECALC_OB);
1121                         }
1122                 }
1123         }
1124         else if (use_base_parent) {
1125                 /* since we are ignoring the internal hierarchy - parent all to the
1126                  * base object */
1127                 for(dob= lb->first; dob; dob= dob->next) {
1128                         /* original parents */
1129                         Object *ob_src=     dob->ob;
1130                         Object *ob_dst=     (Object *)ob_src->id.newid;
1131
1132                         ob_dst->parent= base->object;
1133                         ob_dst->partype= PAROBJECT;
1134
1135                         /* similer to the code above, see comments */
1136                         invert_m4_m4(ob_dst->parentinv, dob->mat);
1137                         object_apply_mat4(ob_dst, dob->mat, FALSE, TRUE);
1138                         DAG_id_tag_update(&ob_dst->id, OB_RECALC_OB);
1139
1140
1141                 }
1142         }
1143
1144         copy_object_set_idnew(C, 0);
1145         
1146         free_object_duplilist(lb);
1147         
1148         base->object->transflag &= ~OB_DUPLI;
1149 }
1150
1151 static int object_duplicates_make_real_exec(bContext *C, wmOperator *op)
1152 {
1153         Main *bmain= CTX_data_main(C);
1154         Scene *scene= CTX_data_scene(C);
1155
1156         const short use_base_parent= RNA_boolean_get(op->ptr, "use_base_parent");
1157         const short use_hierarchy= RNA_boolean_get(op->ptr, "use_hierarchy");
1158         
1159         clear_id_newpoins();
1160                 
1161         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
1162                 make_object_duplilist_real(C, scene, base, use_base_parent, use_hierarchy);
1163
1164                 /* dependencies were changed */
1165                 WM_event_add_notifier(C, NC_OBJECT|ND_PARENT, base->object);
1166         }
1167         CTX_DATA_END;
1168
1169         DAG_scene_sort(bmain, scene);
1170         DAG_ids_flush_update(bmain, 0);
1171         WM_event_add_notifier(C, NC_SCENE, scene);
1172         WM_main_add_notifier(NC_OBJECT|ND_DRAW, NULL);
1173         
1174         return OPERATOR_FINISHED;
1175 }
1176
1177 void OBJECT_OT_duplicates_make_real(wmOperatorType *ot)
1178 {
1179         
1180         /* identifiers */
1181         ot->name= "Make Duplicates Real";
1182         ot->description = "Make dupli objects attached to this object real";
1183         ot->idname= "OBJECT_OT_duplicates_make_real";
1184         
1185         /* api callbacks */
1186         ot->exec= object_duplicates_make_real_exec;
1187         
1188         ot->poll= ED_operator_objectmode;
1189         
1190         /* flags */
1191         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1192
1193         RNA_def_boolean(ot->srna, "use_base_parent", 0, "Parent", "Parent newly created objects to the original duplicator");
1194         RNA_def_boolean(ot->srna, "use_hierarchy", 0, "Keep Hierarchy", "Maintain parent child relationships");
1195 }
1196
1197 /**************************** Convert **************************/
1198
1199 static EnumPropertyItem convert_target_items[]= {
1200         {OB_CURVE, "CURVE", ICON_OUTLINER_OB_CURVE, "Curve from Mesh/Text", ""},
1201         {OB_MESH, "MESH", ICON_OUTLINER_OB_MESH, "Mesh from Curve/Meta/Surf/Text", ""},
1202         {0, NULL, 0, NULL, NULL}};
1203
1204 static void curvetomesh(Scene *scene, Object *ob) 
1205 {
1206         if(ob->disp.first == NULL)
1207                 makeDispListCurveTypes(scene, ob, 0); /* force creation */
1208
1209         nurbs_to_mesh(ob); /* also does users */
1210
1211         if(ob->type == OB_MESH)
1212                 object_free_modifiers(ob);
1213 }
1214
1215 static int convert_poll(bContext *C)
1216 {
1217         Object *obact= CTX_data_active_object(C);
1218         Scene *scene= CTX_data_scene(C);
1219
1220         return (!scene->id.lib && obact && scene->obedit != obact && (obact->flag & SELECT) && !(obact->id.lib));
1221 }
1222
1223 /* Helper for convert_exec */
1224 static Base *duplibase_for_convert(Scene *scene, Base *base, Object *ob)
1225 {
1226         Object *obn;
1227         Base *basen;
1228
1229         if (ob == NULL) {
1230                 ob= base->object;
1231         }
1232
1233         obn= copy_object(ob);
1234         obn->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
1235
1236         basen= MEM_mallocN(sizeof(Base), "duplibase");
1237         *basen= *base;
1238         BLI_addhead(&scene->base, basen);       /* addhead: otherwise eternal loop */
1239         basen->object= obn;
1240         basen->flag |= SELECT;
1241         obn->flag |= SELECT;
1242         base->flag &= ~SELECT;
1243         ob->flag &= ~SELECT;
1244
1245         return basen;
1246 }
1247
1248 static int convert_exec(bContext *C, wmOperator *op)
1249 {
1250         Main *bmain= CTX_data_main(C);
1251         Scene *scene= CTX_data_scene(C);
1252         Base *basen=NULL, *basact=NULL, *basedel=NULL;
1253         Object *ob, *ob1, *newob, *obact= CTX_data_active_object(C);
1254         DerivedMesh *dm;
1255         Curve *cu;
1256         Nurb *nu;
1257         MetaBall *mb;
1258         Mesh *me;
1259         const short target= RNA_enum_get(op->ptr, "target");
1260         const short keep_original= RNA_boolean_get(op->ptr, "keep_original");
1261         int a, mballConverted= 0;
1262
1263         /* don't forget multiple users! */
1264
1265         /* reset flags */
1266         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
1267                 ob= base->object;
1268                 ob->flag &= ~OB_DONE;
1269
1270                 /* flag data thats not been edited (only needed for !keep_original) */
1271                 if(ob->data) {
1272                         ((ID *)ob->data)->flag |= LIB_DOIT;
1273                 }
1274         }
1275         CTX_DATA_END;
1276
1277         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
1278                 ob= base->object;
1279
1280                 if(ob->flag & OB_DONE || !IS_TAGGED(ob->data)) {
1281                         if (ob->type != target) {
1282                                 base->flag &= ~SELECT;
1283                                 ob->flag &= ~SELECT;
1284                         }
1285
1286                         /* obdata already modified */
1287                         if(!IS_TAGGED(ob->data)) {
1288                                 /* When 2 objects with linked data are selected, converting both
1289                                  * would keep modifiers on all but the converted object [#26003] */
1290                                 if(ob->type == OB_MESH) {
1291                                         object_free_modifiers(ob);      /* after derivedmesh calls! */
1292                                 }
1293                         }
1294                 }
1295                 else if (ob->type==OB_MESH && target == OB_CURVE) {
1296                         ob->flag |= OB_DONE;
1297
1298                         if (keep_original) {
1299                                 basen= duplibase_for_convert(scene, base, NULL);
1300                                 newob= basen->object;
1301
1302                                 /* decrement original mesh's usage count  */
1303                                 me= newob->data;
1304                                 me->id.us--;
1305
1306                                 /* make a new copy of the mesh */
1307                                 newob->data= copy_mesh(me);
1308                         } else {
1309                                 newob = ob;
1310                         }
1311
1312                         mesh_to_curve(scene, newob);
1313
1314                         if(newob->type==OB_CURVE)
1315                                 object_free_modifiers(newob);   /* after derivedmesh calls! */
1316                 }
1317                 else if(ob->type==OB_MESH && ob->modifiers.first) { /* converting a mesh with no modifiers causes a segfault */
1318                         ob->flag |= OB_DONE;
1319
1320                         if (keep_original) {
1321                                 basen= duplibase_for_convert(scene, base, NULL);
1322                                 newob= basen->object;
1323
1324                                 /* decrement original mesh's usage count  */
1325                                 me= newob->data;
1326                                 me->id.us--;
1327
1328                                 /* make a new copy of the mesh */
1329                                 newob->data= copy_mesh(me);
1330                         } else {
1331                                 newob = ob;
1332                                 ob->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
1333                         }
1334
1335                         /* make new mesh data from the original copy */
1336                         /* note: get the mesh from the original, not from the copy in some
1337                          * cases this doesnt give correct results (when MDEF is used for eg)
1338                          */
1339                         dm= mesh_get_derived_final(scene, newob, CD_MASK_MESH);
1340                         /* dm= mesh_create_derived_no_deform(ob1, NULL);        this was called original (instead of get_derived). man o man why! (ton) */
1341
1342                         DM_to_mesh(dm, newob->data);
1343
1344                         dm->release(dm);
1345                         object_free_modifiers(newob);   /* after derivedmesh calls! */
1346                 }
1347                 else if(ob->type==OB_FONT) {
1348                         ob->flag |= OB_DONE;
1349
1350                         if (keep_original) {
1351                                 basen= duplibase_for_convert(scene, base, NULL);
1352                                 newob= basen->object;
1353
1354                                 /* decrement original curve's usage count  */
1355                                 ((Curve *)newob->data)->id.us--;
1356
1357                                 /* make a new copy of the curve */
1358                                 newob->data= copy_curve(ob->data);
1359                         } else {
1360                                 newob= ob;
1361                         }
1362
1363                         cu= newob->data;
1364
1365                         if (!newob->disp.first)
1366                                 makeDispListCurveTypes(scene, newob, 0);
1367
1368                         newob->type= OB_CURVE;
1369
1370                         if(cu->vfont) {
1371                                 cu->vfont->id.us--;
1372                                 cu->vfont= NULL;
1373                         }
1374                         if(cu->vfontb) {
1375                                 cu->vfontb->id.us--;
1376                                 cu->vfontb= NULL;
1377                         }
1378                         if(cu->vfonti) {
1379                                 cu->vfonti->id.us--;
1380                                 cu->vfonti= NULL;
1381                         }
1382                         if(cu->vfontbi) {
1383                                 cu->vfontbi->id.us--;
1384                                 cu->vfontbi= NULL;
1385                         }
1386
1387                         if (!keep_original) {
1388                                 /* other users */
1389                                 if(cu->id.us>1) {
1390                                         for(ob1= bmain->object.first; ob1; ob1=ob1->id.next) {
1391                                                 if(ob1->data==ob->data) {
1392                                                         ob1->type= OB_CURVE;
1393                                                         ob1->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
1394                                                 }
1395                                         }
1396                                 }
1397                         }
1398
1399                         for(nu=cu->nurb.first; nu; nu=nu->next)
1400                                 nu->charidx= 0;
1401
1402                         if(target == OB_MESH) {
1403                                 curvetomesh(scene, newob);
1404
1405                                 /* meshes doesn't use displist */
1406                                 freedisplist(&newob->disp);
1407                         }
1408                 }
1409                 else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
1410                         ob->flag |= OB_DONE;
1411
1412                         if(target == OB_MESH) {
1413                                 if (keep_original) {
1414                                         basen= duplibase_for_convert(scene, base, NULL);
1415                                         newob= basen->object;
1416
1417                                         /* decrement original curve's usage count  */
1418                                         ((Curve *)newob->data)->id.us--;
1419
1420                                         /* make a new copy of the curve */
1421                                         newob->data= copy_curve(ob->data);
1422                                 } else {
1423                                         newob= ob;
1424
1425                                         /* meshes doesn't use displist */
1426                                         freedisplist(&newob->disp);
1427                                 }
1428
1429                                 curvetomesh(scene, newob);
1430                         }
1431                 }
1432                 else if(ob->type==OB_MBALL && target == OB_MESH) {
1433                         Object *baseob;
1434
1435                         base->flag &= ~SELECT;
1436                         ob->flag &= ~SELECT;
1437
1438                         baseob= find_basis_mball(scene, ob);
1439
1440                         if (ob != baseob) {
1441                                 /* if motherball is converting it would be marked as done later */
1442                                 ob->flag |= OB_DONE;
1443                         }
1444
1445                         if (!baseob->disp.first) {
1446                                 makeDispListMBall(scene, baseob);
1447                         }
1448
1449                         if(!(baseob->flag & OB_DONE)) {
1450                                 baseob->flag |= OB_DONE;
1451
1452                                 basen= duplibase_for_convert(scene, base, baseob);
1453                                 newob= basen->object;
1454
1455                                 mb= newob->data;
1456                                 mb->id.us--;
1457
1458                                 newob->data= add_mesh("Mesh");
1459                                 newob->type= OB_MESH;
1460
1461                                 me= newob->data;
1462                                 me->totcol= mb->totcol;
1463                                 if(newob->totcol) {
1464                                         me->mat= MEM_dupallocN(mb->mat);
1465                                         for(a=0; a<newob->totcol; a++) id_us_plus((ID *)me->mat[a]);
1466                                 }
1467
1468                                 mball_to_mesh(&baseob->disp, newob->data);
1469
1470                                 if (obact->type == OB_MBALL) {
1471                                         basact= basen;
1472                                 }
1473
1474                                 mballConverted= 1;
1475                         }
1476                 }
1477                 else {
1478                         continue;
1479                 }
1480
1481                 /* tag obdata if it was been changed */
1482
1483                 /* If the original object is active then make this object active */
1484                 if(basen) {
1485                         if(ob == obact) {
1486                                 /* store new active base to update BASACT */
1487                                 basact= basen;
1488                         }
1489
1490                         basen= NULL;
1491                 }
1492
1493                 if (!keep_original && (ob->flag & OB_DONE)) {
1494                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1495                         ((ID *)ob->data)->flag &= ~LIB_DOIT; /* flag not to convert this datablock again */
1496                 }
1497
1498                 /* delete original if needed */
1499                 if(basedel) {
1500                         if(!keep_original)
1501                                 ED_base_object_free_and_unlink(bmain, scene, basedel);  
1502
1503                         basedel = NULL;
1504                 }
1505         }
1506         CTX_DATA_END;
1507
1508         if(!keep_original) {
1509                 if (mballConverted) {
1510                         Base *base= scene->base.first, *tmpbase;
1511                         while (base) {
1512                                 ob= base->object;
1513                                 tmpbase= base;
1514                                 base= base->next;
1515
1516                                 if (ob->type == OB_MBALL) {
1517                                         ED_base_object_free_and_unlink(bmain, scene, tmpbase);
1518                                 }
1519                         }
1520                 }
1521
1522                 /* delete object should renew depsgraph */
1523                 DAG_scene_sort(bmain, scene);
1524         }
1525
1526 // XXX  ED_object_enter_editmode(C, 0);
1527 // XXX  exit_editmode(C, EM_FREEDATA|EM_WAITCURSOR); /* freedata, but no undo */
1528
1529         if (basact) {
1530                 /* active base was changed */
1531                 ED_base_object_activate(C, basact);
1532                 BASACT= basact;
1533         } else if (BASACT->object->flag & OB_DONE) {
1534                 WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, BASACT->object);
1535                 WM_event_add_notifier(C, NC_OBJECT|ND_DATA, BASACT->object);
1536         }
1537
1538         DAG_scene_sort(bmain, scene);
1539         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, scene);
1540         WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene);
1541
1542         return OPERATOR_FINISHED;
1543 }
1544
1545
1546 void OBJECT_OT_convert(wmOperatorType *ot)
1547 {
1548         /* identifiers */
1549         ot->name= "Convert to";
1550         ot->description = "Convert selected objects to another type";
1551         ot->idname= "OBJECT_OT_convert";
1552         
1553         /* api callbacks */
1554         ot->invoke= WM_menu_invoke;
1555         ot->exec= convert_exec;
1556         ot->poll= convert_poll;
1557         
1558         /* flags */
1559         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1560
1561         /* properties */
1562         ot->prop= RNA_def_enum(ot->srna, "target", convert_target_items, OB_MESH, "Target", "Type of object to convert to");
1563         RNA_def_boolean(ot->srna, "keep_original", 0, "Keep Original", "Keep original objects instead of replacing them");
1564 }
1565
1566 /**************************** Duplicate ************************/
1567
1568 /* 
1569         dupflag: a flag made from constants declared in DNA_userdef_types.h
1570         The flag tells adduplicate() weather to copy data linked to the object, or to reference the existing data.
1571         U.dupflag for default operations or you can construct a flag as python does
1572         if the dupflag is 0 then no data will be copied (linked duplicate) */
1573
1574 /* used below, assumes id.new is correct */
1575 /* leaves selection of base/object unaltered */
1576 static Base *object_add_duplicate_internal(Main *bmain, Scene *scene, Base *base, int dupflag)
1577 {
1578         Base *basen= NULL;
1579         Material ***matarar;
1580         Object *ob, *obn;
1581         ID *id;
1582         int a, didit;
1583
1584         ob= base->object;
1585         if(ob->mode & OB_MODE_POSE) {
1586                 ; /* nothing? */
1587         }
1588         else {
1589                 obn= copy_object(ob);
1590                 obn->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
1591                 
1592                 basen= MEM_mallocN(sizeof(Base), "duplibase");
1593                 *basen= *base;
1594                 BLI_addhead(&scene->base, basen);       /* addhead: prevent eternal loop */
1595                 basen->object= obn;
1596                 
1597                 if(basen->flag & OB_FROMGROUP) {
1598                         Group *group;
1599                         for(group= bmain->group.first; group; group= group->id.next) {
1600                                 if(object_in_group(ob, group))
1601                                         add_to_group(group, obn, scene, basen);
1602                         }
1603                 }
1604                 
1605                 /* duplicates using userflags */
1606                 if(dupflag & USER_DUP_ACT) {
1607                         BKE_copy_animdata_id_action(&obn->id);
1608                 }
1609                 
1610                 if(dupflag & USER_DUP_MAT) {
1611                         for(a=0; a<obn->totcol; a++) {
1612                                 id= (ID *)obn->mat[a];
1613                                 if(id) {
1614                                         ID_NEW_US(obn->mat[a])
1615                                         else obn->mat[a]= copy_material(obn->mat[a]);
1616                                         id->us--;
1617                                         
1618                                         if(dupflag & USER_DUP_ACT) {
1619                                                 BKE_copy_animdata_id_action(&obn->mat[a]->id);
1620                                         }
1621                                 }
1622                         }
1623                 }
1624                 if(dupflag & USER_DUP_PSYS) {
1625                         ParticleSystem *psys;
1626                         for(psys=obn->particlesystem.first; psys; psys=psys->next) {
1627                                 id= (ID*) psys->part;
1628                                 if(id) {
1629                                         ID_NEW_US(psys->part)
1630                                         else psys->part= psys_copy_settings(psys->part);
1631                                         
1632                                         if(dupflag & USER_DUP_ACT) {
1633                                                 BKE_copy_animdata_id_action(&psys->part->id);
1634                                         }
1635
1636                                         id->us--;
1637                                 }
1638                         }
1639                 }
1640                 
1641                 id= obn->data;
1642                 didit= 0;
1643                 
1644                 switch(obn->type) {
1645                         case OB_MESH:
1646                                 if(dupflag & USER_DUP_MESH) {
1647                                         ID_NEW_US2( obn->data )
1648                                         else {
1649                                                 obn->data= copy_mesh(obn->data);
1650                                                 
1651                                                 if(obn->fluidsimSettings) {
1652                                                         obn->fluidsimSettings->orgMesh = (Mesh *)obn->data;
1653                                                 }
1654                                                 
1655                                                 didit= 1;
1656                                         }
1657                                         id->us--;
1658                                 }
1659                                 break;
1660                         case OB_CURVE:
1661                                 if(dupflag & USER_DUP_CURVE) {
1662                                         ID_NEW_US2(obn->data )
1663                                         else {
1664                                                 obn->data= copy_curve(obn->data);
1665                                                 didit= 1;
1666                                         }
1667                                         id->us--;
1668                                 }
1669                                 break;
1670                         case OB_SURF:
1671                                 if(dupflag & USER_DUP_SURF) {
1672                                         ID_NEW_US2( obn->data )
1673                                         else {
1674                                                 obn->data= copy_curve(obn->data);
1675                                                 didit= 1;
1676                                         }
1677                                         id->us--;
1678                                 }
1679                                 break;
1680                         case OB_FONT:
1681                                 if(dupflag & USER_DUP_FONT) {
1682                                         ID_NEW_US2( obn->data )
1683                                         else {
1684                                                 obn->data= copy_curve(obn->data);
1685                                                 didit= 1;
1686                                         }
1687                                         id->us--;
1688                                 }
1689                                 break;
1690                         case OB_MBALL:
1691                                 if(dupflag & USER_DUP_MBALL) {
1692                                         ID_NEW_US2(obn->data )
1693                                         else {
1694                                                 obn->data= copy_mball(obn->data);
1695                                                 didit= 1;
1696                                         }
1697                                         id->us--;
1698                                 }
1699                                 break;
1700                         case OB_LAMP:
1701                                 if(dupflag & USER_DUP_LAMP) {
1702                                         ID_NEW_US2(obn->data )
1703                                         else {
1704                                                 obn->data= copy_lamp(obn->data);
1705                                                 didit= 1;
1706                                         }
1707                                         id->us--;
1708                                 }
1709                                 break;
1710                                 
1711                         case OB_ARMATURE:
1712                                 obn->recalc |= OB_RECALC_DATA;
1713                                 if(obn->pose) obn->pose->flag |= POSE_RECALC;
1714                                         
1715                                         if(dupflag & USER_DUP_ARM) {
1716                                                 ID_NEW_US2(obn->data )
1717                                                 else {
1718                                                         obn->data= copy_armature(obn->data);
1719                                                         armature_rebuild_pose(obn, obn->data);
1720                                                         didit= 1;
1721                                                 }
1722                                                 id->us--;
1723                                         }
1724                                                 
1725                                                 break;
1726                                 
1727                         case OB_LATTICE:
1728                                 if(dupflag!=0) {
1729                                         ID_NEW_US2(obn->data )
1730                                         else {
1731                                                 obn->data= copy_lattice(obn->data);
1732                                                 didit= 1;
1733                                         }
1734                                         id->us--;
1735                                 }
1736                                 break;
1737                         case OB_CAMERA:
1738                                 if(dupflag!=0) {
1739                                         ID_NEW_US2(obn->data )
1740                                         else {
1741                                                 obn->data= copy_camera(obn->data);
1742                                                 didit= 1;
1743                                         }
1744                                         id->us--;
1745                                 }
1746                                 break;
1747                         case OB_SPEAKER:
1748                                 if(dupflag!=0) {
1749                                         ID_NEW_US2(obn->data )
1750                                         else {
1751                                                 obn->data= copy_speaker(obn->data);
1752                                                 didit= 1;
1753                                         }
1754                                         id->us--;
1755                                 }
1756                                 break;
1757
1758                 }
1759
1760                 /* check if obdata is copied */
1761                 if(didit) {
1762                         Key *key = ob_get_key(obn);
1763                         
1764                         if(dupflag & USER_DUP_ACT) {
1765                                 BKE_copy_animdata_id_action((ID *)obn->data);
1766                                 if(key) BKE_copy_animdata_id_action((ID*)key);
1767                         }
1768                         
1769                         if(dupflag & USER_DUP_MAT) {
1770                                 matarar= give_matarar(obn);
1771                                 if(matarar) {
1772                                         for(a=0; a<obn->totcol; a++) {
1773                                                 id= (ID *)(*matarar)[a];
1774                                                 if(id) {
1775                                                         ID_NEW_US( (*matarar)[a] )
1776                                                         else (*matarar)[a]= copy_material((*matarar)[a]);
1777                                                         
1778                                                         id->us--;
1779                                                 }
1780                                         }
1781                                 }
1782                         }
1783                 }
1784         }
1785         return basen;
1786 }
1787
1788 /* single object duplicate, if dupflag==0, fully linked, else it uses the flags given */
1789 /* leaves selection of base/object unaltered.
1790  * note: don't call this within a loop since clear_* funcs loop over the entire database. */
1791 Base *ED_object_add_duplicate(Main *bmain, Scene *scene, Base *base, int dupflag)
1792 {
1793         Base *basen;
1794         Object *ob;
1795
1796         clear_id_newpoins();
1797         clear_sca_new_poins();  /* sensor/contr/act */
1798
1799         basen= object_add_duplicate_internal(bmain, scene, base, dupflag);
1800         if (basen == NULL) {
1801                 return NULL;
1802         }
1803
1804         ob= basen->object;
1805
1806         /* link own references to the newly duplicated data [#26816] */
1807         object_relink(ob);
1808         set_sca_new_poins_ob(ob);
1809
1810         DAG_scene_sort(bmain, scene);
1811         ED_render_id_flush_update(bmain, ob->data);
1812
1813         return basen;
1814 }
1815
1816 /* contextual operator dupli */
1817 static int duplicate_exec(bContext *C, wmOperator *op)
1818 {
1819         Main *bmain= CTX_data_main(C);
1820         Scene *scene= CTX_data_scene(C);
1821         int linked= RNA_boolean_get(op->ptr, "linked");
1822         int dupflag= (linked)? 0: U.dupflag;
1823         
1824         clear_id_newpoins();
1825         clear_sca_new_poins();  /* sensor/contr/act */
1826         
1827         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
1828                 Base *basen= object_add_duplicate_internal(bmain, scene, base, dupflag);
1829                 
1830                 /* note that this is safe to do with this context iterator,
1831                    the list is made in advance */
1832                 ED_base_object_select(base, BA_DESELECT);
1833
1834                 if (basen == NULL) {
1835                         continue;
1836                 }
1837
1838                 /* new object becomes active */
1839                 if(BASACT==base)
1840                         ED_base_object_activate(C, basen);
1841
1842                 if(basen->object->data) {
1843                         DAG_id_tag_update(basen->object->data, 0);
1844                 }
1845         }
1846         CTX_DATA_END;
1847
1848         copy_object_set_idnew(C, dupflag);
1849
1850         DAG_scene_sort(bmain, scene);
1851         DAG_ids_flush_update(bmain, 0);
1852
1853         WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene);
1854
1855         return OPERATOR_FINISHED;
1856 }
1857
1858 void OBJECT_OT_duplicate(wmOperatorType *ot)
1859 {
1860         PropertyRNA *prop;
1861         
1862         /* identifiers */
1863         ot->name= "Duplicate Objects";
1864         ot->description = "Duplicate selected objects";
1865         ot->idname= "OBJECT_OT_duplicate";
1866         
1867         /* api callbacks */
1868         ot->exec= duplicate_exec;
1869         ot->poll= ED_operator_objectmode;
1870         
1871         /* flags */
1872         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1873         
1874         /* to give to transform */
1875         RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data");
1876         prop= RNA_def_enum(ot->srna, "mode", transform_mode_types, TFM_TRANSLATION, "Mode", "");
1877         RNA_def_property_flag(prop, PROP_HIDDEN);
1878 }
1879
1880 /* **************** add named object, for dragdrop ************* */
1881
1882
1883 static int add_named_exec(bContext *C, wmOperator *op)
1884 {
1885         Main *bmain= CTX_data_main(C);
1886         Scene *scene= CTX_data_scene(C);
1887         Base *basen, *base;
1888         Object *ob;
1889         int linked= RNA_boolean_get(op->ptr, "linked");
1890         int dupflag= (linked)? 0: U.dupflag;
1891         char name[32];
1892
1893         /* find object, create fake base */
1894         RNA_string_get(op->ptr, "name", name);
1895         ob= (Object *)find_id("OB", name);
1896         if(ob==NULL) 
1897                 return OPERATOR_CANCELLED;
1898
1899         base= MEM_callocN(sizeof(Base), "duplibase");
1900         base->object= ob;
1901         base->flag= ob->flag;
1902
1903         /* prepare dupli */
1904         clear_id_newpoins();
1905         clear_sca_new_poins();  /* sensor/contr/act */
1906
1907         basen= object_add_duplicate_internal(bmain, scene, base, dupflag);
1908
1909         if (basen == NULL) {
1910                 MEM_freeN(base);
1911                 return OPERATOR_CANCELLED;
1912         }
1913
1914         basen->lay= basen->object->lay= scene->lay;
1915
1916         ED_object_location_from_view(C, basen->object->loc);
1917         ED_base_object_activate(C, basen);
1918
1919         copy_object_set_idnew(C, dupflag);
1920
1921         DAG_scene_sort(bmain, scene);
1922         DAG_ids_flush_update(bmain, 0);
1923
1924         MEM_freeN(base);
1925
1926         WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene);
1927
1928         return OPERATOR_FINISHED;
1929 }
1930
1931 void OBJECT_OT_add_named(wmOperatorType *ot)
1932 {
1933         /* identifiers */
1934         ot->name= "Add Named Object";
1935         ot->description = "Add named object";
1936         ot->idname= "OBJECT_OT_add_named";
1937         
1938         /* api callbacks */
1939         ot->exec= add_named_exec;
1940         ot->poll= ED_operator_objectmode;
1941         
1942         /* flags */
1943         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1944         
1945         RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data");
1946         RNA_def_string(ot->srna, "name", "Cube", 24, "Name", "Object name to add");
1947 }
1948
1949
1950
1951 /**************************** Join *************************/
1952 static int join_poll(bContext *C)
1953 {
1954         Object *ob= CTX_data_active_object(C);
1955         
1956         if (!ob || ob->id.lib) return 0;
1957         
1958         if (ELEM4(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_ARMATURE))
1959                 return ED_operator_screenactive(C);
1960         else
1961                 return 0;
1962 }
1963
1964
1965 static int join_exec(bContext *C, wmOperator *op)
1966 {
1967         Scene *scene= CTX_data_scene(C);
1968         Object *ob= CTX_data_active_object(C);
1969
1970         if(scene->obedit) {
1971                 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in editmode");
1972                 return OPERATOR_CANCELLED;
1973         }
1974         else if(object_data_is_libdata(ob)) {
1975                 BKE_report(op->reports, RPT_ERROR, "Can't edit external libdata");
1976                 return OPERATOR_CANCELLED;
1977         }
1978
1979         if(ob->type == OB_MESH)
1980                 return join_mesh_exec(C, op);
1981         else if(ELEM(ob->type, OB_CURVE, OB_SURF))
1982                 return join_curve_exec(C, op);
1983         else if(ob->type == OB_ARMATURE)
1984                 return join_armature_exec(C, op);
1985         
1986         return OPERATOR_CANCELLED;
1987 }
1988
1989 void OBJECT_OT_join(wmOperatorType *ot)
1990 {
1991         /* identifiers */
1992         ot->name= "Join";
1993         ot->description = "Join selected objects into active object";
1994         ot->idname= "OBJECT_OT_join";
1995         
1996         /* api callbacks */
1997         ot->exec= join_exec;
1998         ot->poll= join_poll;
1999         
2000         /* flags */
2001         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2002 }
2003
2004 /**************************** Join as Shape Key*************************/
2005 static int join_shapes_poll(bContext *C)
2006 {
2007         Object *ob= CTX_data_active_object(C);
2008         
2009         if (!ob || ob->id.lib) return 0;
2010         
2011         /* only meshes supported at the moment */
2012         if (ob->type == OB_MESH)
2013                 return ED_operator_screenactive(C);
2014         else
2015                 return 0;
2016 }
2017
2018 static int join_shapes_exec(bContext *C, wmOperator *op)
2019 {
2020         Scene *scene= CTX_data_scene(C);
2021         Object *ob= CTX_data_active_object(C);
2022         
2023         if(scene->obedit) {
2024                 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in editmode");
2025                 return OPERATOR_CANCELLED;
2026         }
2027         else if(object_data_is_libdata(ob)) {
2028                 BKE_report(op->reports, RPT_ERROR, "Can't edit external libdata");
2029                 return OPERATOR_CANCELLED;
2030         }
2031         
2032         if(ob->type == OB_MESH)
2033                 return join_mesh_shapes_exec(C, op);
2034         
2035         return OPERATOR_CANCELLED;
2036 }
2037
2038 void OBJECT_OT_join_shapes(wmOperatorType *ot)
2039 {
2040         /* identifiers */
2041         ot->name= "Join as Shapes";
2042         ot->description = "Merge selected objects to shapes of active object";
2043         ot->idname= "OBJECT_OT_join_shapes";
2044         
2045         /* api callbacks */
2046         ot->exec= join_shapes_exec;
2047         ot->poll= join_shapes_poll;
2048         
2049         /* flags */
2050         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2051 }