3D Audio GSoC:
[blender-staging.git] / source / blender / editors / object / object_add.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation, 2002-2008 full recode
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/object/object_add.c
29  *  \ingroup edobj
30  */
31
32
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "MEM_guardedalloc.h"
37
38 #include "DNA_anim_types.h"
39 #include "DNA_curve_types.h"
40 #include "DNA_group_types.h"
41 #include "DNA_lamp_types.h"
42 #include "DNA_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         Scene *scene = CTX_data_scene(C);
778
779         object_add_generic_invoke_options(C, op);
780         if(!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer))
781                 return OPERATOR_CANCELLED;
782
783         ob= ED_object_add_type(C, OB_SPEAKER, loc, rot, FALSE, layer);
784         
785         /* to make it easier to start using this immediately in NLA, a default sound clip is created
786          * ready to be moved around to retime the sound and/or make new sound clips
787          */
788         {
789                 /* create new data for NLA hierarchy */
790                 AnimData *adt = BKE_id_add_animdata(&ob->id);
791                 NlaTrack *nlt = add_nlatrack(adt, NULL);
792                 NlaStrip *strip = add_nla_soundstrip(CTX_data_scene(C), ob->data);
793                 strip->start = CFRA;
794                 strip->end += strip->start;
795                 
796                 /* hook them up */
797                 BKE_nlatrack_add_strip(nlt, strip);
798                 
799                 /* auto-name the strip, and give the track an interesting name  */
800                 strcpy(nlt->name, "SoundTrack");
801                 BKE_nlastrip_validate_name(adt, strip);
802                 
803                 WM_event_add_notifier(C, NC_ANIMATION|ND_NLA|NA_EDITED, NULL);
804         }
805
806         return OPERATOR_FINISHED;
807 }
808
809 void OBJECT_OT_speaker_add(wmOperatorType *ot)
810 {
811         /* identifiers */
812         ot->name= "Add Speaker";
813         ot->description = "Add a speaker object to the scene";
814         ot->idname= "OBJECT_OT_speaker_add";
815
816         /* api callbacks */
817         ot->exec= object_speaker_add_exec;
818         ot->poll= ED_operator_objectmode;
819
820         /* flags */
821         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
822
823         ED_object_add_generic_props(ot, TRUE);
824 }
825
826 /* only used as menu */
827 void OBJECT_OT_group_instance_add(wmOperatorType *ot)
828 {
829         PropertyRNA *prop;
830
831         /* identifiers */
832         ot->name= "Add Group Instance";
833         ot->description = "Add a dupligroup instance";
834         ot->idname= "OBJECT_OT_group_instance_add";
835
836         /* api callbacks */
837         ot->invoke= WM_enum_search_invoke;
838         ot->exec= group_instance_add_exec;
839
840         ot->poll= ED_operator_objectmode;
841
842         /* flags */
843         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
844
845         /* properties */
846         prop= RNA_def_enum(ot->srna, "group", DummyRNA_NULL_items, 0, "Group", "");
847         RNA_def_enum_funcs(prop, RNA_group_itemf);
848         ot->prop= prop;
849         ED_object_add_generic_props(ot, FALSE);
850 }
851
852 /**************************** Delete Object *************************/
853
854 /* remove base from a specific scene */
855 /* note: now unlinks constraints as well */
856 void ED_base_object_free_and_unlink(Main *bmain, Scene *scene, Base *base)
857 {
858         BLI_remlink(&scene->base, base);
859         free_libblock_us(&bmain->object, base->object);
860         if(scene->basact==base) scene->basact= NULL;
861         MEM_freeN(base);
862 }
863
864 static int object_delete_exec(bContext *C, wmOperator *UNUSED(op))
865 {
866         Main *bmain= CTX_data_main(C);
867         Scene *scene= CTX_data_scene(C);
868         /* int islamp= 0; */ /* UNUSED */
869         
870         if(CTX_data_edit_object(C)) 
871                 return OPERATOR_CANCELLED;
872         
873         CTX_DATA_BEGIN(C, Base*, base, selected_bases) {
874
875                 /* if(base->object->type==OB_LAMP) islamp= 1; */
876
877                 /* deselect object -- it could be used in other scenes */
878                 base->object->flag &= ~SELECT;
879
880                 /* remove from current scene only */
881                 ED_base_object_free_and_unlink(bmain, scene, base);
882         }
883         CTX_DATA_END;
884
885         DAG_scene_sort(bmain, scene);
886         DAG_ids_flush_update(bmain, 0);
887         
888         WM_event_add_notifier(C, NC_SCENE|ND_OB_ACTIVE, scene);
889         WM_event_add_notifier(C, NC_SCENE|ND_LAYER_CONTENT, scene);
890         
891         return OPERATOR_FINISHED;
892 }
893
894 void OBJECT_OT_delete(wmOperatorType *ot)
895 {
896         /* identifiers */
897         ot->name= "Delete";
898         ot->description = "Delete selected objects";
899         ot->idname= "OBJECT_OT_delete";
900         
901         /* api callbacks */
902         ot->invoke= WM_operator_confirm;
903         ot->exec= object_delete_exec;
904         ot->poll= ED_operator_objectmode;
905         
906         /* flags */
907         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
908 }
909
910 /**************************** Copy Utilities ******************************/
911
912 /* after copying objects, copied data should get new pointers */
913 static void copy_object_set_idnew(bContext *C, int dupflag)
914 {
915         Main *bmain= CTX_data_main(C);
916         Material *ma, *mao;
917         ID *id;
918         int a;
919         
920         /* XXX check object pointers */
921         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
922                 object_relink(ob);
923         }
924         CTX_DATA_END;
925         
926         /* materials */
927         if( dupflag & USER_DUP_MAT) {
928                 mao= bmain->mat.first;
929                 while(mao) {
930                         if(mao->id.newid) {
931                                 
932                                 ma= (Material *)mao->id.newid;
933                                 
934                                 if(dupflag & USER_DUP_TEX) {
935                                         for(a=0; a<MAX_MTEX; a++) {
936                                                 if(ma->mtex[a]) {
937                                                         id= (ID *)ma->mtex[a]->tex;
938                                                         if(id) {
939                                                                 ID_NEW_US(ma->mtex[a]->tex)
940                                                                 else ma->mtex[a]->tex= copy_texture(ma->mtex[a]->tex);
941                                                                 id->us--;
942                                                         }
943                                                 }
944                                         }
945                                 }
946 #if 0 // XXX old animation system
947                                 id= (ID *)ma->ipo;
948                                 if(id) {
949                                         ID_NEW_US(ma->ipo)
950                                         else ma->ipo= copy_ipo(ma->ipo);
951                                         id->us--;
952                                 }
953 #endif // XXX old animation system
954                         }
955                         mao= mao->id.next;
956                 }
957         }
958         
959 #if 0 // XXX old animation system
960         /* lamps */
961         if( dupflag & USER_DUP_IPO) {
962                 Lamp *la= bmain->lamp.first;
963                 while(la) {
964                         if(la->id.newid) {
965                                 Lamp *lan= (Lamp *)la->id.newid;
966                                 id= (ID *)lan->ipo;
967                                 if(id) {
968                                         ID_NEW_US(lan->ipo)
969                                         else lan->ipo= copy_ipo(lan->ipo);
970                                         id->us--;
971                                 }
972                         }
973                         la= la->id.next;
974                 }
975         }
976         
977         /* ipos */
978         ipo= bmain->ipo.first;
979         while(ipo) {
980                 if(ipo->id.lib==NULL && ipo->id.newid) {
981                         Ipo *ipon= (Ipo *)ipo->id.newid;
982                         IpoCurve *icu;
983                         for(icu= ipon->curve.first; icu; icu= icu->next) {
984                                 if(icu->driver) {
985                                         ID_NEW(icu->driver->ob);
986                                 }
987                         }
988                 }
989                 ipo= ipo->id.next;
990         }
991 #endif // XXX old animation system
992         
993         set_sca_new_poins();
994         
995         clear_id_newpoins();
996 }
997
998 /********************* Make Duplicates Real ************************/
999
1000 static void make_object_duplilist_real(bContext *C, Scene *scene, Base *base)
1001 {
1002         Base *basen;
1003         Object *ob;
1004         ListBase *lb;
1005         DupliObject *dob;
1006         
1007         if(!base && !(base = BASACT))
1008                 return;
1009         
1010         if(!(base->object->transflag & OB_DUPLI))
1011                 return;
1012         
1013         lb= object_duplilist(scene, base->object);
1014         
1015         for(dob= lb->first; dob; dob= dob->next) {
1016                 ob= copy_object(dob->ob);
1017                 /* font duplis can have a totcol without material, we get them from parent
1018                 * should be implemented better...
1019                 */
1020                 if(ob->mat==NULL) ob->totcol= 0;
1021                 
1022                 basen= MEM_dupallocN(base);
1023                 basen->flag &= ~(OB_FROMDUPLI|OB_FROMGROUP);
1024                 ob->flag= basen->flag;
1025                 basen->lay= base->lay;
1026                 BLI_addhead(&scene->base, basen);       /* addhead: othwise eternal loop */
1027                 basen->object= ob;
1028                 
1029                 /* make sure apply works */
1030                 BKE_free_animdata(&ob->id);     
1031                 ob->adt = NULL;
1032                 
1033                 ob->parent= NULL;
1034                 ob->constraints.first= ob->constraints.last= NULL;
1035                 ob->disp.first= ob->disp.last= NULL;
1036                 ob->transflag &= ~OB_DUPLI;     
1037                 ob->lay= base->lay;
1038                 
1039                 copy_m4_m4(ob->obmat, dob->mat);
1040                 object_apply_mat4(ob, ob->obmat, FALSE, FALSE);
1041         }
1042         
1043         copy_object_set_idnew(C, 0);
1044         
1045         free_object_duplilist(lb);
1046         
1047         base->object->transflag &= ~OB_DUPLI;   
1048 }
1049
1050 static int object_duplicates_make_real_exec(bContext *C, wmOperator *UNUSED(op))
1051 {
1052         Main *bmain= CTX_data_main(C);
1053         Scene *scene= CTX_data_scene(C);
1054         
1055         clear_id_newpoins();
1056                 
1057         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
1058                 make_object_duplilist_real(C, scene, base);
1059
1060                 /* dependencies were changed */
1061                 WM_event_add_notifier(C, NC_OBJECT|ND_PARENT, base->object);
1062         }
1063         CTX_DATA_END;
1064
1065         DAG_scene_sort(bmain, scene);
1066         DAG_ids_flush_update(bmain, 0);
1067         WM_event_add_notifier(C, NC_SCENE, scene);
1068         WM_main_add_notifier(NC_OBJECT|ND_DRAW, NULL);
1069         
1070         return OPERATOR_FINISHED;
1071 }
1072
1073 void OBJECT_OT_duplicates_make_real(wmOperatorType *ot)
1074 {
1075         
1076         /* identifiers */
1077         ot->name= "Make Duplicates Real";
1078         ot->description = "Make dupli objects attached to this object real";
1079         ot->idname= "OBJECT_OT_duplicates_make_real";
1080         
1081         /* api callbacks */
1082         ot->exec= object_duplicates_make_real_exec;
1083         
1084         ot->poll= ED_operator_objectmode;
1085         
1086         /* flags */
1087         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1088 }
1089
1090 /**************************** Convert **************************/
1091
1092 static EnumPropertyItem convert_target_items[]= {
1093         {OB_CURVE, "CURVE", ICON_OUTLINER_OB_CURVE, "Curve from Mesh/Text", ""},
1094         {OB_MESH, "MESH", ICON_OUTLINER_OB_MESH, "Mesh from Curve/Meta/Surf/Text", ""},
1095         {0, NULL, 0, NULL, NULL}};
1096
1097 static void curvetomesh(Scene *scene, Object *ob) 
1098 {
1099         if(ob->disp.first == NULL)
1100                 makeDispListCurveTypes(scene, ob, 0); /* force creation */
1101
1102         nurbs_to_mesh(ob); /* also does users */
1103
1104         if(ob->type == OB_MESH)
1105                 object_free_modifiers(ob);
1106 }
1107
1108 static int convert_poll(bContext *C)
1109 {
1110         Object *obact= CTX_data_active_object(C);
1111         Scene *scene= CTX_data_scene(C);
1112
1113         return (!scene->id.lib && obact && scene->obedit != obact && (obact->flag & SELECT) && !(obact->id.lib));
1114 }
1115
1116 /* Helper for convert_exec */
1117 static Base *duplibase_for_convert(Scene *scene, Base *base, Object *ob)
1118 {
1119         Object *obn;
1120         Base *basen;
1121
1122         if (ob == NULL) {
1123                 ob= base->object;
1124         }
1125
1126         obn= copy_object(ob);
1127         obn->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
1128
1129         basen= MEM_mallocN(sizeof(Base), "duplibase");
1130         *basen= *base;
1131         BLI_addhead(&scene->base, basen);       /* addhead: otherwise eternal loop */
1132         basen->object= obn;
1133         basen->flag |= SELECT;
1134         obn->flag |= SELECT;
1135         base->flag &= ~SELECT;
1136         ob->flag &= ~SELECT;
1137
1138         return basen;
1139 }
1140
1141 static int convert_exec(bContext *C, wmOperator *op)
1142 {
1143         Main *bmain= CTX_data_main(C);
1144         Scene *scene= CTX_data_scene(C);
1145         Base *basen=NULL, *basact=NULL, *basedel=NULL;
1146         Object *ob, *ob1, *newob, *obact= CTX_data_active_object(C);
1147         DerivedMesh *dm;
1148         Curve *cu;
1149         Nurb *nu;
1150         MetaBall *mb;
1151         Mesh *me;
1152         const short target= RNA_enum_get(op->ptr, "target");
1153         const short keep_original= RNA_boolean_get(op->ptr, "keep_original");
1154         int a, mballConverted= 0;
1155
1156         /* don't forget multiple users! */
1157
1158         /* reset flags */
1159         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
1160                 ob= base->object;
1161                 ob->flag &= ~OB_DONE;
1162
1163                 /* flag data thats not been edited (only needed for !keep_original) */
1164                 if(ob->data) {
1165                         ((ID *)ob->data)->flag |= LIB_DOIT;
1166                 }
1167         }
1168         CTX_DATA_END;
1169
1170         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
1171                 ob= base->object;
1172
1173                 if(ob->flag & OB_DONE || !IS_TAGGED(ob->data)) {
1174                         if (ob->type != target) {
1175                                 base->flag &= ~SELECT;
1176                                 ob->flag &= ~SELECT;
1177                         }
1178
1179                         /* obdata already modified */
1180                         if(!IS_TAGGED(ob->data)) {
1181                                 /* When 2 objects with linked data are selected, converting both
1182                                  * would keep modifiers on all but the converted object [#26003] */
1183                                 if(ob->type == OB_MESH) {
1184                                         object_free_modifiers(ob);      /* after derivedmesh calls! */
1185                                 }
1186                         }
1187                 }
1188                 else if (ob->type==OB_MESH && target == OB_CURVE) {
1189                         ob->flag |= OB_DONE;
1190
1191                         if (keep_original) {
1192                                 basen= duplibase_for_convert(scene, base, NULL);
1193                                 newob= basen->object;
1194
1195                                 /* decrement original mesh's usage count  */
1196                                 me= newob->data;
1197                                 me->id.us--;
1198
1199                                 /* make a new copy of the mesh */
1200                                 newob->data= copy_mesh(me);
1201                         } else {
1202                                 newob = ob;
1203                         }
1204
1205                         mesh_to_curve(scene, newob);
1206
1207                         if(newob->type==OB_CURVE)
1208                                 object_free_modifiers(newob);   /* after derivedmesh calls! */
1209                 }
1210                 else if(ob->type==OB_MESH && ob->modifiers.first) { /* converting a mesh with no modifiers causes a segfault */
1211                         ob->flag |= OB_DONE;
1212
1213                         if (keep_original) {
1214                                 basen= duplibase_for_convert(scene, base, NULL);
1215                                 newob= basen->object;
1216
1217                                 /* decrement original mesh's usage count  */
1218                                 me= newob->data;
1219                                 me->id.us--;
1220
1221                                 /* make a new copy of the mesh */
1222                                 newob->data= copy_mesh(me);
1223                         } else {
1224                                 newob = ob;
1225                                 ob->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
1226                         }
1227
1228                         /* make new mesh data from the original copy */
1229                         /* note: get the mesh from the original, not from the copy in some
1230                          * cases this doesnt give correct results (when MDEF is used for eg)
1231                          */
1232                         dm= mesh_get_derived_final(scene, newob, CD_MASK_MESH);
1233                         /* dm= mesh_create_derived_no_deform(ob1, NULL);        this was called original (instead of get_derived). man o man why! (ton) */
1234
1235                         DM_to_mesh(dm, newob->data);
1236
1237                         dm->release(dm);
1238                         object_free_modifiers(newob);   /* after derivedmesh calls! */
1239                 }
1240                 else if(ob->type==OB_FONT) {
1241                         ob->flag |= OB_DONE;
1242
1243                         if (keep_original) {
1244                                 basen= duplibase_for_convert(scene, base, NULL);
1245                                 newob= basen->object;
1246
1247                                 /* decrement original curve's usage count  */
1248                                 ((Curve *)newob->data)->id.us--;
1249
1250                                 /* make a new copy of the curve */
1251                                 newob->data= copy_curve(ob->data);
1252                         } else {
1253                                 newob= ob;
1254                         }
1255
1256                         cu= newob->data;
1257
1258                         if (!newob->disp.first)
1259                                 makeDispListCurveTypes(scene, newob, 0);
1260
1261                         newob->type= OB_CURVE;
1262
1263                         if(cu->vfont) {
1264                                 cu->vfont->id.us--;
1265                                 cu->vfont= NULL;
1266                         }
1267                         if(cu->vfontb) {
1268                                 cu->vfontb->id.us--;
1269                                 cu->vfontb= NULL;
1270                         }
1271                         if(cu->vfonti) {
1272                                 cu->vfonti->id.us--;
1273                                 cu->vfonti= NULL;
1274                         }
1275                         if(cu->vfontbi) {
1276                                 cu->vfontbi->id.us--;
1277                                 cu->vfontbi= NULL;
1278                         }
1279
1280                         if (!keep_original) {
1281                                 /* other users */
1282                                 if(cu->id.us>1) {
1283                                         for(ob1= bmain->object.first; ob1; ob1=ob1->id.next) {
1284                                                 if(ob1->data==ob->data) {
1285                                                         ob1->type= OB_CURVE;
1286                                                         ob1->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
1287                                                 }
1288                                         }
1289                                 }
1290                         }
1291
1292                         for(nu=cu->nurb.first; nu; nu=nu->next)
1293                                 nu->charidx= 0;
1294
1295                         if(target == OB_MESH) {
1296                                 curvetomesh(scene, newob);
1297
1298                                 /* meshes doesn't use displist */
1299                                 freedisplist(&newob->disp);
1300                         }
1301                 }
1302                 else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
1303                         ob->flag |= OB_DONE;
1304
1305                         if(target == OB_MESH) {
1306                                 if (keep_original) {
1307                                         basen= duplibase_for_convert(scene, base, NULL);
1308                                         newob= basen->object;
1309
1310                                         /* decrement original curve's usage count  */
1311                                         ((Curve *)newob->data)->id.us--;
1312
1313                                         /* make a new copy of the curve */
1314                                         newob->data= copy_curve(ob->data);
1315                                 } else {
1316                                         newob= ob;
1317
1318                                         /* meshes doesn't use displist */
1319                                         freedisplist(&newob->disp);
1320                                 }
1321
1322                                 curvetomesh(scene, newob);
1323                         }
1324                 }
1325                 else if(ob->type==OB_MBALL && target == OB_MESH) {
1326                         Object *baseob;
1327
1328                         base->flag &= ~SELECT;
1329                         ob->flag &= ~SELECT;
1330
1331                         baseob= find_basis_mball(scene, ob);
1332
1333                         if (ob != baseob) {
1334                                 /* if motherball is converting it would be marked as done later */
1335                                 ob->flag |= OB_DONE;
1336                         }
1337
1338                         if (!baseob->disp.first) {
1339                                 makeDispListMBall(scene, baseob);
1340                         }
1341
1342                         if(!(baseob->flag & OB_DONE)) {
1343                                 baseob->flag |= OB_DONE;
1344
1345                                 basen= duplibase_for_convert(scene, base, baseob);
1346                                 newob= basen->object;
1347
1348                                 mb= newob->data;
1349                                 mb->id.us--;
1350
1351                                 newob->data= add_mesh("Mesh");
1352                                 newob->type= OB_MESH;
1353
1354                                 me= newob->data;
1355                                 me->totcol= mb->totcol;
1356                                 if(newob->totcol) {
1357                                         me->mat= MEM_dupallocN(mb->mat);
1358                                         for(a=0; a<newob->totcol; a++) id_us_plus((ID *)me->mat[a]);
1359                                 }
1360
1361                                 mball_to_mesh(&baseob->disp, newob->data);
1362
1363                                 if (obact->type == OB_MBALL) {
1364                                         basact= basen;
1365                                 }
1366
1367                                 mballConverted= 1;
1368                         }
1369                 }
1370                 else {
1371                         continue;
1372                 }
1373
1374                 /* tag obdata if it was been changed */
1375
1376                 /* If the original object is active then make this object active */
1377                 if(basen) {
1378                         if(ob == obact) {
1379                                 /* store new active base to update BASACT */
1380                                 basact= basen;
1381                         }
1382
1383                         basen= NULL;
1384                 }
1385
1386                 if (!keep_original && (ob->flag & OB_DONE)) {
1387                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1388                         ((ID *)ob->data)->flag &= ~LIB_DOIT; /* flag not to convert this datablock again */
1389                 }
1390
1391                 /* delete original if needed */
1392                 if(basedel) {
1393                         if(!keep_original)
1394                                 ED_base_object_free_and_unlink(bmain, scene, basedel);  
1395
1396                         basedel = NULL;
1397                 }
1398         }
1399         CTX_DATA_END;
1400
1401         if(!keep_original) {
1402                 if (mballConverted) {
1403                         Base *base= scene->base.first, *tmpbase;
1404                         while (base) {
1405                                 ob= base->object;
1406                                 tmpbase= base;
1407                                 base= base->next;
1408
1409                                 if (ob->type == OB_MBALL) {
1410                                         ED_base_object_free_and_unlink(bmain, scene, tmpbase);
1411                                 }
1412                         }
1413                 }
1414
1415                 /* delete object should renew depsgraph */
1416                 DAG_scene_sort(bmain, scene);
1417         }
1418
1419 // XXX  ED_object_enter_editmode(C, 0);
1420 // XXX  exit_editmode(C, EM_FREEDATA|EM_WAITCURSOR); /* freedata, but no undo */
1421
1422         if (basact) {
1423                 /* active base was changed */
1424                 ED_base_object_activate(C, basact);
1425                 BASACT= basact;
1426         } else if (BASACT->object->flag & OB_DONE) {
1427                 WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, BASACT->object);
1428                 WM_event_add_notifier(C, NC_OBJECT|ND_DATA, BASACT->object);
1429         }
1430
1431         DAG_scene_sort(bmain, scene);
1432         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, scene);
1433         WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene);
1434
1435         return OPERATOR_FINISHED;
1436 }
1437
1438
1439 void OBJECT_OT_convert(wmOperatorType *ot)
1440 {
1441         /* identifiers */
1442         ot->name= "Convert to";
1443         ot->description = "Convert selected objects to another type";
1444         ot->idname= "OBJECT_OT_convert";
1445         
1446         /* api callbacks */
1447         ot->invoke= WM_menu_invoke;
1448         ot->exec= convert_exec;
1449         ot->poll= convert_poll;
1450         
1451         /* flags */
1452         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1453
1454         /* properties */
1455         ot->prop= RNA_def_enum(ot->srna, "target", convert_target_items, OB_MESH, "Target", "Type of object to convert to");
1456         RNA_def_boolean(ot->srna, "keep_original", 0, "Keep Original", "Keep original objects instead of replacing them");
1457 }
1458
1459 /**************************** Duplicate ************************/
1460
1461 /* 
1462         dupflag: a flag made from constants declared in DNA_userdef_types.h
1463         The flag tells adduplicate() weather to copy data linked to the object, or to reference the existing data.
1464         U.dupflag for default operations or you can construct a flag as python does
1465         if the dupflag is 0 then no data will be copied (linked duplicate) */
1466
1467 /* used below, assumes id.new is correct */
1468 /* leaves selection of base/object unaltered */
1469 static Base *object_add_duplicate_internal(Main *bmain, Scene *scene, Base *base, int dupflag)
1470 {
1471         Base *basen= NULL;
1472         Material ***matarar;
1473         Object *ob, *obn;
1474         ID *id;
1475         int a, didit;
1476
1477         ob= base->object;
1478         if(ob->mode & OB_MODE_POSE) {
1479                 ; /* nothing? */
1480         }
1481         else {
1482                 obn= copy_object(ob);
1483                 obn->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
1484                 
1485                 basen= MEM_mallocN(sizeof(Base), "duplibase");
1486                 *basen= *base;
1487                 BLI_addhead(&scene->base, basen);       /* addhead: prevent eternal loop */
1488                 basen->object= obn;
1489                 
1490                 if(basen->flag & OB_FROMGROUP) {
1491                         Group *group;
1492                         for(group= bmain->group.first; group; group= group->id.next) {
1493                                 if(object_in_group(ob, group))
1494                                         add_to_group(group, obn, scene, basen);
1495                         }
1496                 }
1497                 
1498                 /* duplicates using userflags */
1499 #if 0 // XXX old animation system                               
1500                 if(dupflag & USER_DUP_IPO) {
1501                         bConstraintChannel *chan;
1502                         id= (ID *)obn->ipo;
1503                         
1504                         if(id) {
1505                                 ID_NEW_US( obn->ipo)
1506                                 else obn->ipo= copy_ipo(obn->ipo);
1507                                 id->us--;
1508                         }
1509                         /* Handle constraint ipos */
1510                         for (chan=obn->constraintChannels.first; chan; chan=chan->next){
1511                                 id= (ID *)chan->ipo;
1512                                 if(id) {
1513                                         ID_NEW_US( chan->ipo)
1514                                         else chan->ipo= copy_ipo(chan->ipo);
1515                                         id->us--;
1516                                 }
1517                         }
1518                 }
1519 #endif // XXX old animation system
1520                 
1521                 if(dupflag & USER_DUP_ACT) {
1522                         BKE_copy_animdata_id_action(&obn->id);
1523                 }
1524                 
1525                 if(dupflag & USER_DUP_MAT) {
1526                         for(a=0; a<obn->totcol; a++) {
1527                                 id= (ID *)obn->mat[a];
1528                                 if(id) {
1529                                         ID_NEW_US(obn->mat[a])
1530                                         else obn->mat[a]= copy_material(obn->mat[a]);
1531                                         id->us--;
1532                                         
1533                                         if(dupflag & USER_DUP_ACT) {
1534                                                 BKE_copy_animdata_id_action(&obn->mat[a]->id);
1535                                         }
1536                                 }
1537                         }
1538                 }
1539                 if(dupflag & USER_DUP_PSYS) {
1540                         ParticleSystem *psys;
1541                         for(psys=obn->particlesystem.first; psys; psys=psys->next) {
1542                                 id= (ID*) psys->part;
1543                                 if(id) {
1544                                         ID_NEW_US(psys->part)
1545                                         else psys->part= psys_copy_settings(psys->part);
1546                                         
1547                                         if(dupflag & USER_DUP_ACT) {
1548                                                 BKE_copy_animdata_id_action(&psys->part->id);
1549                                         }
1550
1551                                         id->us--;
1552                                 }
1553                         }
1554                 }
1555                 
1556                 id= obn->data;
1557                 didit= 0;
1558                 
1559                 switch(obn->type) {
1560                         case OB_MESH:
1561                                 if(dupflag & USER_DUP_MESH) {
1562                                         ID_NEW_US2( obn->data )
1563                                         else {
1564                                                 obn->data= copy_mesh(obn->data);
1565                                                 
1566                                                 if(obn->fluidsimSettings) {
1567                                                         obn->fluidsimSettings->orgMesh = (Mesh *)obn->data;
1568                                                 }
1569                                                 
1570                                                 didit= 1;
1571                                         }
1572                                         id->us--;
1573                                 }
1574                                 break;
1575                         case OB_CURVE:
1576                                 if(dupflag & USER_DUP_CURVE) {
1577                                         ID_NEW_US2(obn->data )
1578                                         else {
1579                                                 obn->data= copy_curve(obn->data);
1580                                                 didit= 1;
1581                                         }
1582                                         id->us--;
1583                                 }
1584                                 break;
1585                         case OB_SURF:
1586                                 if(dupflag & USER_DUP_SURF) {
1587                                         ID_NEW_US2( obn->data )
1588                                         else {
1589                                                 obn->data= copy_curve(obn->data);
1590                                                 didit= 1;
1591                                         }
1592                                         id->us--;
1593                                 }
1594                                 break;
1595                         case OB_FONT:
1596                                 if(dupflag & USER_DUP_FONT) {
1597                                         ID_NEW_US2( obn->data )
1598                                         else {
1599                                                 obn->data= copy_curve(obn->data);
1600                                                 didit= 1;
1601                                         }
1602                                         id->us--;
1603                                 }
1604                                 break;
1605                         case OB_MBALL:
1606                                 if(dupflag & USER_DUP_MBALL) {
1607                                         ID_NEW_US2(obn->data )
1608                                         else {
1609                                                 obn->data= copy_mball(obn->data);
1610                                                 didit= 1;
1611                                         }
1612                                         id->us--;
1613                                 }
1614                                 break;
1615                         case OB_LAMP:
1616                                 if(dupflag & USER_DUP_LAMP) {
1617                                         ID_NEW_US2(obn->data )
1618                                         else {
1619                                                 obn->data= copy_lamp(obn->data);
1620                                                 didit= 1;
1621                                         }
1622                                         id->us--;
1623                                 }
1624                                 break;
1625                                 
1626                         case OB_ARMATURE:
1627                                 obn->recalc |= OB_RECALC_DATA;
1628                                 if(obn->pose) obn->pose->flag |= POSE_RECALC;
1629                                         
1630                                         if(dupflag & USER_DUP_ARM) {
1631                                                 ID_NEW_US2(obn->data )
1632                                                 else {
1633                                                         obn->data= copy_armature(obn->data);
1634                                                         armature_rebuild_pose(obn, obn->data);
1635                                                         didit= 1;
1636                                                 }
1637                                                 id->us--;
1638                                         }
1639                                                 
1640                                                 break;
1641                                 
1642                         case OB_LATTICE:
1643                                 if(dupflag!=0) {
1644                                         ID_NEW_US2(obn->data )
1645                                         else {
1646                                                 obn->data= copy_lattice(obn->data);
1647                                                 didit= 1;
1648                                         }
1649                                         id->us--;
1650                                 }
1651                                 break;
1652                         case OB_CAMERA:
1653                                 if(dupflag!=0) {
1654                                         ID_NEW_US2(obn->data )
1655                                         else {
1656                                                 obn->data= copy_camera(obn->data);
1657                                                 didit= 1;
1658                                         }
1659                                         id->us--;
1660                                 }
1661                                 break;
1662                         case OB_SPEAKER:
1663                                 if(dupflag!=0) {
1664                                         ID_NEW_US2(obn->data )
1665                                         else {
1666                                                 obn->data= copy_speaker(obn->data);
1667                                                 didit= 1;
1668                                         }
1669                                         id->us--;
1670                                 }
1671                                 break;
1672
1673                 }
1674
1675                 /* check if obdata is copied */
1676                 if(didit) {
1677                         if(dupflag & USER_DUP_ACT) {
1678                                 BKE_copy_animdata_id_action((ID *)obn->data);
1679                         }
1680                         
1681                         if(dupflag & USER_DUP_MAT) {
1682                                 matarar= give_matarar(obn);
1683                                 if(matarar) {
1684                                         for(a=0; a<obn->totcol; a++) {
1685                                                 id= (ID *)(*matarar)[a];
1686                                                 if(id) {
1687                                                         ID_NEW_US( (*matarar)[a] )
1688                                                         else (*matarar)[a]= copy_material((*matarar)[a]);
1689                                                         
1690                                                         id->us--;
1691                                                 }
1692                                         }
1693                                 }
1694                         }
1695                 }
1696         }
1697         return basen;
1698 }
1699
1700 /* single object duplicate, if dupflag==0, fully linked, else it uses the flags given */
1701 /* leaves selection of base/object unaltered.
1702  * note: don't call this within a loop since clear_* funcs loop over the entire database. */
1703 Base *ED_object_add_duplicate(Main *bmain, Scene *scene, Base *base, int dupflag)
1704 {
1705         Base *basen;
1706         Object *ob;
1707
1708         clear_id_newpoins();
1709         clear_sca_new_poins();  /* sensor/contr/act */
1710
1711         basen= object_add_duplicate_internal(bmain, scene, base, dupflag);
1712         if (basen == NULL) {
1713                 return NULL;
1714         }
1715
1716         ob= basen->object;
1717
1718         /* link own references to the newly duplicated data [#26816] */
1719         object_relink(ob);
1720         set_sca_new_poins_ob(ob);
1721
1722         DAG_scene_sort(bmain, scene);
1723         ED_render_id_flush_update(bmain, ob->data);
1724
1725         return basen;
1726 }
1727
1728 /* contextual operator dupli */
1729 static int duplicate_exec(bContext *C, wmOperator *op)
1730 {
1731         Main *bmain= CTX_data_main(C);
1732         Scene *scene= CTX_data_scene(C);
1733         int linked= RNA_boolean_get(op->ptr, "linked");
1734         int dupflag= (linked)? 0: U.dupflag;
1735         
1736         clear_id_newpoins();
1737         clear_sca_new_poins();  /* sensor/contr/act */
1738         
1739         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
1740                 Base *basen= object_add_duplicate_internal(bmain, scene, base, dupflag);
1741                 
1742                 /* note that this is safe to do with this context iterator,
1743                    the list is made in advance */
1744                 ED_base_object_select(base, BA_DESELECT);
1745
1746                 if (basen == NULL) {
1747                         continue;
1748                 }
1749
1750                 /* new object becomes active */
1751                 if(BASACT==base)
1752                         ED_base_object_activate(C, basen);
1753
1754                 if(basen->object->data) {
1755                         DAG_id_tag_update(basen->object->data, 0);
1756                 }
1757         }
1758         CTX_DATA_END;
1759
1760         copy_object_set_idnew(C, dupflag);
1761
1762         DAG_scene_sort(bmain, scene);
1763         DAG_ids_flush_update(bmain, 0);
1764
1765         WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene);
1766
1767         return OPERATOR_FINISHED;
1768 }
1769
1770 void OBJECT_OT_duplicate(wmOperatorType *ot)
1771 {
1772         PropertyRNA *prop;
1773         
1774         /* identifiers */
1775         ot->name= "Duplicate Objects";
1776         ot->description = "Duplicate selected objects";
1777         ot->idname= "OBJECT_OT_duplicate";
1778         
1779         /* api callbacks */
1780         ot->exec= duplicate_exec;
1781         ot->poll= ED_operator_objectmode;
1782         
1783         /* flags */
1784         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1785         
1786         /* to give to transform */
1787         RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data");
1788         prop= RNA_def_enum(ot->srna, "mode", transform_mode_types, TFM_TRANSLATION, "Mode", "");
1789         RNA_def_property_flag(prop, PROP_HIDDEN);
1790 }
1791
1792 /* **************** add named object, for dragdrop ************* */
1793
1794
1795 static int add_named_exec(bContext *C, wmOperator *op)
1796 {
1797         Main *bmain= CTX_data_main(C);
1798         Scene *scene= CTX_data_scene(C);
1799         Base *basen, *base;
1800         Object *ob;
1801         int linked= RNA_boolean_get(op->ptr, "linked");
1802         int dupflag= (linked)? 0: U.dupflag;
1803         char name[32];
1804
1805         /* find object, create fake base */
1806         RNA_string_get(op->ptr, "name", name);
1807         ob= (Object *)find_id("OB", name);
1808         if(ob==NULL) 
1809                 return OPERATOR_CANCELLED;
1810
1811         base= MEM_callocN(sizeof(Base), "duplibase");
1812         base->object= ob;
1813         base->flag= ob->flag;
1814
1815         /* prepare dupli */
1816         clear_id_newpoins();
1817         clear_sca_new_poins();  /* sensor/contr/act */
1818
1819         basen= object_add_duplicate_internal(bmain, scene, base, dupflag);
1820
1821         if (basen == NULL) {
1822                 MEM_freeN(base);
1823                 return OPERATOR_CANCELLED;
1824         }
1825
1826         basen->lay= basen->object->lay= scene->lay;
1827
1828         ED_object_location_from_view(C, basen->object->loc);
1829         ED_base_object_activate(C, basen);
1830
1831         copy_object_set_idnew(C, dupflag);
1832
1833         DAG_scene_sort(bmain, scene);
1834         DAG_ids_flush_update(bmain, 0);
1835
1836         MEM_freeN(base);
1837
1838         WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene);
1839
1840         return OPERATOR_FINISHED;
1841 }
1842
1843 void OBJECT_OT_add_named(wmOperatorType *ot)
1844 {
1845         /* identifiers */
1846         ot->name= "Add Named Object";
1847         ot->description = "Add named object";
1848         ot->idname= "OBJECT_OT_add_named";
1849         
1850         /* api callbacks */
1851         ot->exec= add_named_exec;
1852         ot->poll= ED_operator_objectmode;
1853         
1854         /* flags */
1855         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1856         
1857         RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data");
1858         RNA_def_string(ot->srna, "name", "Cube", 24, "Name", "Object name to add");
1859 }
1860
1861
1862
1863 /**************************** Join *************************/
1864 static int join_poll(bContext *C)
1865 {
1866         Object *ob= CTX_data_active_object(C);
1867         
1868         if (!ob || ob->id.lib) return 0;
1869         
1870         if (ELEM4(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_ARMATURE))
1871                 return ED_operator_screenactive(C);
1872         else
1873                 return 0;
1874 }
1875
1876
1877 static int join_exec(bContext *C, wmOperator *op)
1878 {
1879         Scene *scene= CTX_data_scene(C);
1880         Object *ob= CTX_data_active_object(C);
1881
1882         if(scene->obedit) {
1883                 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in editmode");
1884                 return OPERATOR_CANCELLED;
1885         }
1886         else if(object_data_is_libdata(ob)) {
1887                 BKE_report(op->reports, RPT_ERROR, "Can't edit external libdata");
1888                 return OPERATOR_CANCELLED;
1889         }
1890
1891         if(ob->type == OB_MESH)
1892                 return join_mesh_exec(C, op);
1893         else if(ELEM(ob->type, OB_CURVE, OB_SURF))
1894                 return join_curve_exec(C, op);
1895         else if(ob->type == OB_ARMATURE)
1896                 return join_armature_exec(C, op);
1897         
1898         return OPERATOR_CANCELLED;
1899 }
1900
1901 void OBJECT_OT_join(wmOperatorType *ot)
1902 {
1903         /* identifiers */
1904         ot->name= "Join";
1905         ot->description = "Join selected objects into active object";
1906         ot->idname= "OBJECT_OT_join";
1907         
1908         /* api callbacks */
1909         ot->exec= join_exec;
1910         ot->poll= join_poll;
1911         
1912         /* flags */
1913         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1914 }
1915
1916 /**************************** Join as Shape Key*************************/
1917 static int join_shapes_poll(bContext *C)
1918 {
1919         Object *ob= CTX_data_active_object(C);
1920         
1921         if (!ob || ob->id.lib) return 0;
1922         
1923         /* only meshes supported at the moment */
1924         if (ob->type == OB_MESH)
1925                 return ED_operator_screenactive(C);
1926         else
1927                 return 0;
1928 }
1929
1930 static int join_shapes_exec(bContext *C, wmOperator *op)
1931 {
1932         Scene *scene= CTX_data_scene(C);
1933         Object *ob= CTX_data_active_object(C);
1934         
1935         if(scene->obedit) {
1936                 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in editmode");
1937                 return OPERATOR_CANCELLED;
1938         }
1939         else if(object_data_is_libdata(ob)) {
1940                 BKE_report(op->reports, RPT_ERROR, "Can't edit external libdata");
1941                 return OPERATOR_CANCELLED;
1942         }
1943         
1944         if(ob->type == OB_MESH)
1945                 return join_mesh_shapes_exec(C, op);
1946         
1947         return OPERATOR_CANCELLED;
1948 }
1949
1950 void OBJECT_OT_join_shapes(wmOperatorType *ot)
1951 {
1952         /* identifiers */
1953         ot->name= "Join as Shapes";
1954         ot->description = "Merge selected objects to shapes of active object";
1955         ot->idname= "OBJECT_OT_join_shapes";
1956         
1957         /* api callbacks */
1958         ot->exec= join_shapes_exec;
1959         ot->poll= join_shapes_poll;
1960         
1961         /* flags */
1962         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1963 }