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