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