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