svn merge -r39286:39385 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender.git] / source / blender / editors / object / object_add.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation, 2002-2008 full recode
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/object/object_add.c
29  *  \ingroup edobj
30  */
31
32
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "MEM_guardedalloc.h"
37
38 #include "DNA_curve_types.h"
39 #include "DNA_group_types.h"
40 #include "DNA_lamp_types.h"
41 #include "DNA_material_types.h"
42 #include "DNA_mesh_types.h"
43 #include "DNA_meta_types.h"
44 #include "DNA_object_fluidsim.h"
45 #include "DNA_object_force.h"
46 #include "DNA_scene_types.h"
47 #include "DNA_vfont_types.h"
48
49 #include "BLI_math.h"
50 #include "BLI_listbase.h"
51 #include "BLI_utildefines.h"
52
53 #include "BKE_anim.h"
54 #include "BKE_animsys.h"
55 #include "BKE_armature.h"
56 #include "BKE_constraint.h"
57 #include "BKE_context.h"
58 #include "BKE_curve.h"
59 #include "BKE_depsgraph.h"
60 #include "BKE_DerivedMesh.h"
61 #include "BKE_displist.h"
62 #include "BKE_effect.h"
63 #include "BKE_group.h"
64 #include "BKE_lattice.h"
65 #include "BKE_library.h"
66 #include "BKE_main.h"
67 #include "BKE_material.h"
68 #include "BKE_mball.h"
69 #include "BKE_mesh.h"
70 #include "BKE_modifier.h"
71 #include "BKE_object.h"
72 #include "BKE_particle.h"
73 #include "BKE_report.h"
74 #include "BKE_sca.h"
75 #include "BKE_texture.h"
76
77 #include "RNA_access.h"
78 #include "RNA_define.h"
79 #include "RNA_enum_types.h"
80
81 #include "WM_api.h"
82 #include "WM_types.h"
83
84 #include "ED_armature.h"
85 #include "ED_curve.h"
86 #include "ED_mball.h"
87 #include "ED_mesh.h"
88 #include "ED_object.h"
89 #include "ED_render.h"
90 #include "ED_screen.h"
91 #include "ED_transform.h"
92 #include "ED_view3d.h"
93
94 #include "UI_interface.h"
95 #include "UI_resources.h"
96
97 #include "object_intern.h"
98
99 /************************** Exported *****************************/
100
101 void ED_object_location_from_view(bContext *C, float *loc)
102 {
103         View3D *v3d= CTX_wm_view3d(C);
104         Scene *scene= CTX_data_scene(C);
105         float *cursor;
106         
107         cursor = give_cursor(scene, v3d);
108
109         copy_v3_v3(loc, cursor);
110 }
111
112 void ED_object_rotation_from_view(bContext *C, float *rot)
113 {
114         RegionView3D *rv3d= CTX_wm_region_view3d(C);
115         if(rv3d) {
116                 float quat[4];
117                 copy_qt_qt(quat, rv3d->viewquat);
118                 quat[0]= -quat[0];
119                 quat_to_eul(rot, quat);
120         }
121         else {
122                 zero_v3(rot);
123         }
124 }
125
126 void ED_object_base_init_transform(bContext *C, Base *base, float *loc, float *rot)
127 {
128         Object *ob= base->object;
129         Scene *scene= CTX_data_scene(C);
130         
131         if (!scene) return;
132         
133         if (loc)
134                 copy_v3_v3(ob->loc, loc);
135         
136         if (rot)
137                 copy_v3_v3(ob->rot, rot);
138         
139         where_is_object(scene, ob);
140 }
141
142 /* uses context to figure out transform for primitive */
143 /* returns standard diameter */
144 float ED_object_new_primitive_matrix(bContext *C, Object *obedit, float *loc, float *rot, float primmat[][4])
145 {
146         View3D *v3d =CTX_wm_view3d(C);
147         float mat[3][3], rmat[3][3], cmat[3][3], imat[3][3];
148         
149         unit_m4(primmat);
150         
151         eul_to_mat3(rmat, rot);
152         invert_m3(rmat);
153         
154         /* inverse transform for initial rotation and object */
155         copy_m3_m4(mat, obedit->obmat);
156         mul_m3_m3m3(cmat, rmat, mat);
157         invert_m3_m3(imat, cmat);
158         copy_m4_m3(primmat, imat);
159         
160         /* center */
161         copy_v3_v3(primmat[3], loc);
162         sub_v3_v3v3(primmat[3], primmat[3], obedit->obmat[3]);
163         invert_m3_m3(imat, mat);
164         mul_m3_v3(imat, primmat[3]);
165         
166         if(v3d) return v3d->grid;
167         return 1.0f;
168 }
169
170 /********************* Add Object Operator ********************/
171
172 void ED_object_add_generic_props(wmOperatorType *ot, int do_editmode)
173 {
174         PropertyRNA *prop;
175         
176         /* note: this property gets hidden for add-camera operator */
177         RNA_def_boolean(ot->srna, "view_align", 0, "Align to View", "Align the new object to the view");
178
179         if(do_editmode) {
180                 prop= RNA_def_boolean(ot->srna, "enter_editmode", 0, "Enter Editmode", "Enter editmode when adding this object");
181                 RNA_def_property_flag(prop, PROP_HIDDEN);
182         }
183         
184         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);
185         RNA_def_float_rotation(ot->srna, "rotation", 3, NULL, -FLT_MAX, FLT_MAX, "Rotation", "Rotation for the newly added object", -M_PI * 2.0f, M_PI * 2.0f);
186         
187         prop = RNA_def_boolean_layer_member(ot->srna, "layers", 20, NULL, "Layer", "");
188         RNA_def_property_flag(prop, PROP_HIDDEN);
189 }
190
191 static void object_add_generic_invoke_options(bContext *C, wmOperator *op)
192 {
193         if(RNA_struct_find_property(op->ptr, "enter_editmode")) /* optional */
194                 if (!RNA_property_is_set(op->ptr, "enter_editmode"))
195                         RNA_boolean_set(op->ptr, "enter_editmode", U.flag & USER_ADD_EDITMODE);
196         
197         if(!RNA_property_is_set(op->ptr, "location")) {
198                 float loc[3];
199                 
200                 ED_object_location_from_view(C, loc);
201                 RNA_float_set_array(op->ptr, "location", loc);
202         }
203          
204         if(!RNA_property_is_set(op->ptr, "layers")) {
205                 View3D *v3d = CTX_wm_view3d(C);
206                 Scene *scene = CTX_data_scene(C);
207                 int a, values[20], layer;
208                 
209                 if(v3d) {
210                         layer = (v3d->scenelock && !v3d->localvd)? scene->layact: v3d->layact;
211
212                         for(a=0; a<20; a++)
213                                 values[a]= (layer & (1<<a));
214                 }
215                 else {
216                         layer = scene->layact;
217
218                         for(a=0; a<20; a++)
219                                 values[a]= (layer & (1<<a));
220                 }
221                 
222                 RNA_boolean_set_array(op->ptr, "layers", values);
223         }
224 }
225
226 int ED_object_add_generic_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
227 {
228         object_add_generic_invoke_options(C, op);
229         return op->type->exec(C, op);
230 }
231
232 int ED_object_add_generic_get_opts(bContext *C, wmOperator *op, float *loc, 
233         float *rot, int *enter_editmode, unsigned int *layer, int *is_view_aligned)
234 {
235         View3D *v3d = CTX_wm_view3d(C);
236         int a, layer_values[20];
237         int view_align;
238         
239         *enter_editmode = FALSE;
240         if(RNA_struct_find_property(op->ptr, "enter_editmode") && RNA_boolean_get(op->ptr, "enter_editmode")) {
241                 *enter_editmode = TRUE;
242         }
243
244         if(RNA_property_is_set(op->ptr, "layers")) {
245                 RNA_boolean_get_array(op->ptr, "layers", layer_values);
246                 *layer= 0;
247                 for(a=0; a<20; a++) {
248                         if(layer_values[a])
249                                 *layer |= (1 << a);
250                         else
251                                 *layer &= ~(1 << a);
252                 }
253         }
254         else {
255                 /* not set, use the scenes layers */
256                 Scene *scene = CTX_data_scene(C);
257                 *layer = scene->layact;
258         }
259
260         /* in local view we additionally add local view layers,
261            not part of operator properties */
262         if(v3d && v3d->localvd)
263                 *layer |= v3d->lay;
264
265         if(RNA_property_is_set(op->ptr, "rotation"))
266                 view_align = FALSE;
267         else if (RNA_property_is_set(op->ptr, "view_align"))
268                 view_align = RNA_boolean_get(op->ptr, "view_align");
269         else {
270                 view_align = U.flag & USER_ADD_VIEWALIGNED;
271                 RNA_boolean_set(op->ptr, "view_align", view_align);
272         }
273         
274         if (view_align)
275                 ED_object_rotation_from_view(C, rot);
276         else
277                 RNA_float_get_array(op->ptr, "rotation", rot);
278         
279         if (is_view_aligned)
280                 *is_view_aligned = view_align;
281         
282         RNA_float_get_array(op->ptr, "location", loc);
283
284         if(*layer == 0) {
285                 BKE_report(op->reports, RPT_ERROR, "Property 'layer' has no values set");
286                 return 0;
287         }
288
289         return 1;
290 }
291
292 /* for object add primitive operators */
293 /* do not call undo push in this function (users of this function have to) */
294 Object *ED_object_add_type(bContext *C, int type, float *loc, float *rot, 
295         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, NULL))
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, NULL))
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, NULL))
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, NULL))
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, NULL))
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, NULL))
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, NULL))
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, NULL))
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 /* only used as menu */
772 void OBJECT_OT_group_instance_add(wmOperatorType *ot)
773 {
774         PropertyRNA *prop;
775
776         /* identifiers */
777         ot->name= "Add Group Instance";
778         ot->description = "Add a dupligroup instance";
779         ot->idname= "OBJECT_OT_group_instance_add";
780
781         /* api callbacks */
782         ot->invoke= WM_enum_search_invoke;
783         ot->exec= group_instance_add_exec;
784
785         ot->poll= ED_operator_objectmode;
786
787         /* flags */
788         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
789
790         /* properties */
791         prop= RNA_def_enum(ot->srna, "group", DummyRNA_NULL_items, 0, "Group", "");
792         RNA_def_enum_funcs(prop, RNA_group_itemf);
793         ot->prop= prop;
794         ED_object_add_generic_props(ot, FALSE);
795 }
796
797 /**************************** Delete Object *************************/
798
799 /* remove base from a specific scene */
800 /* note: now unlinks constraints as well */
801 void ED_base_object_free_and_unlink(Main *bmain, Scene *scene, Base *base)
802 {
803         BLI_remlink(&scene->base, base);
804         free_libblock_us(&bmain->object, base->object);
805         if(scene->basact==base) scene->basact= NULL;
806         MEM_freeN(base);
807 }
808
809 static int object_delete_exec(bContext *C, wmOperator *UNUSED(op))
810 {
811         Main *bmain= CTX_data_main(C);
812         Scene *scene= CTX_data_scene(C);
813         /* int islamp= 0; */ /* UNUSED */
814         
815         if(CTX_data_edit_object(C)) 
816                 return OPERATOR_CANCELLED;
817         
818         CTX_DATA_BEGIN(C, Base*, base, selected_bases) {
819
820                 /* if(base->object->type==OB_LAMP) islamp= 1; */
821
822                 /* deselect object -- it could be used in other scenes */
823                 base->object->flag &= ~SELECT;
824
825                 /* remove from current scene only */
826                 ED_base_object_free_and_unlink(bmain, scene, base);
827         }
828         CTX_DATA_END;
829
830         DAG_scene_sort(bmain, scene);
831         DAG_ids_flush_update(bmain, 0);
832         
833         WM_event_add_notifier(C, NC_SCENE|ND_OB_ACTIVE, scene);
834         WM_event_add_notifier(C, NC_SCENE|ND_LAYER_CONTENT, scene);
835         
836         return OPERATOR_FINISHED;
837 }
838
839 void OBJECT_OT_delete(wmOperatorType *ot)
840 {
841         /* identifiers */
842         ot->name= "Delete";
843         ot->description = "Delete selected objects";
844         ot->idname= "OBJECT_OT_delete";
845         
846         /* api callbacks */
847         ot->invoke= WM_operator_confirm;
848         ot->exec= object_delete_exec;
849         ot->poll= ED_operator_objectmode;
850         
851         /* flags */
852         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
853 }
854
855 /**************************** Copy Utilities ******************************/
856
857 /* after copying objects, copied data should get new pointers */
858 static void copy_object_set_idnew(bContext *C, int dupflag)
859 {
860         Main *bmain= CTX_data_main(C);
861         Material *ma, *mao;
862         ID *id;
863         int a;
864         
865         /* XXX check object pointers */
866         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
867                 object_relink(ob);
868         }
869         CTX_DATA_END;
870         
871         /* materials */
872         if( dupflag & USER_DUP_MAT) {
873                 mao= bmain->mat.first;
874                 while(mao) {
875                         if(mao->id.newid) {
876                                 
877                                 ma= (Material *)mao->id.newid;
878                                 
879                                 if(dupflag & USER_DUP_TEX) {
880                                         for(a=0; a<MAX_MTEX; a++) {
881                                                 if(ma->mtex[a]) {
882                                                         id= (ID *)ma->mtex[a]->tex;
883                                                         if(id) {
884                                                                 ID_NEW_US(ma->mtex[a]->tex)
885                                                                 else ma->mtex[a]->tex= copy_texture(ma->mtex[a]->tex);
886                                                                 id->us--;
887                                                         }
888                                                 }
889                                         }
890                                 }
891 #if 0 // XXX old animation system
892                                 id= (ID *)ma->ipo;
893                                 if(id) {
894                                         ID_NEW_US(ma->ipo)
895                                         else ma->ipo= copy_ipo(ma->ipo);
896                                         id->us--;
897                                 }
898 #endif // XXX old animation system
899                         }
900                         mao= mao->id.next;
901                 }
902         }
903         
904 #if 0 // XXX old animation system
905         /* lamps */
906         if( dupflag & USER_DUP_IPO) {
907                 Lamp *la= bmain->lamp.first;
908                 while(la) {
909                         if(la->id.newid) {
910                                 Lamp *lan= (Lamp *)la->id.newid;
911                                 id= (ID *)lan->ipo;
912                                 if(id) {
913                                         ID_NEW_US(lan->ipo)
914                                         else lan->ipo= copy_ipo(lan->ipo);
915                                         id->us--;
916                                 }
917                         }
918                         la= la->id.next;
919                 }
920         }
921         
922         /* ipos */
923         ipo= bmain->ipo.first;
924         while(ipo) {
925                 if(ipo->id.lib==NULL && ipo->id.newid) {
926                         Ipo *ipon= (Ipo *)ipo->id.newid;
927                         IpoCurve *icu;
928                         for(icu= ipon->curve.first; icu; icu= icu->next) {
929                                 if(icu->driver) {
930                                         ID_NEW(icu->driver->ob);
931                                 }
932                         }
933                 }
934                 ipo= ipo->id.next;
935         }
936 #endif // XXX old animation system
937         
938         set_sca_new_poins();
939         
940         clear_id_newpoins();
941 }
942
943 /********************* Make Duplicates Real ************************/
944
945 static void make_object_duplilist_real(bContext *C, Scene *scene, Base *base)
946 {
947         Base *basen;
948         Object *ob;
949         ListBase *lb;
950         DupliObject *dob;
951         
952         if(!base && !(base = BASACT))
953                 return;
954         
955         if(!(base->object->transflag & OB_DUPLI))
956                 return;
957         
958         lb= object_duplilist(scene, base->object);
959         
960         for(dob= lb->first; dob; dob= dob->next) {
961                 ob= copy_object(dob->ob);
962                 /* font duplis can have a totcol without material, we get them from parent
963                 * should be implemented better...
964                 */
965                 if(ob->mat==NULL) ob->totcol= 0;
966                 
967                 basen= MEM_dupallocN(base);
968                 basen->flag &= ~(OB_FROMDUPLI|OB_FROMGROUP);
969                 ob->flag= basen->flag;
970                 basen->lay= base->lay;
971                 BLI_addhead(&scene->base, basen);       /* addhead: othwise eternal loop */
972                 basen->object= ob;
973                 
974                 /* make sure apply works */
975                 BKE_free_animdata(&ob->id);     
976                 ob->adt = NULL;
977                 
978                 ob->parent= NULL;
979                 ob->constraints.first= ob->constraints.last= NULL;
980                 ob->disp.first= ob->disp.last= NULL;
981                 ob->transflag &= ~OB_DUPLI;     
982                 ob->lay= base->lay;
983                 
984                 copy_m4_m4(ob->obmat, dob->mat);
985                 object_apply_mat4(ob, ob->obmat, FALSE, FALSE);
986         }
987         
988         copy_object_set_idnew(C, 0);
989         
990         free_object_duplilist(lb);
991         
992         base->object->transflag &= ~OB_DUPLI;   
993 }
994
995 static int object_duplicates_make_real_exec(bContext *C, wmOperator *UNUSED(op))
996 {
997         Main *bmain= CTX_data_main(C);
998         Scene *scene= CTX_data_scene(C);
999         
1000         clear_id_newpoins();
1001                 
1002         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
1003                 make_object_duplilist_real(C, scene, base);
1004
1005                 /* dependencies were changed */
1006                 WM_event_add_notifier(C, NC_OBJECT|ND_PARENT, base->object);
1007         }
1008         CTX_DATA_END;
1009
1010         DAG_scene_sort(bmain, scene);
1011         DAG_ids_flush_update(bmain, 0);
1012         WM_event_add_notifier(C, NC_SCENE, scene);
1013         WM_main_add_notifier(NC_OBJECT|ND_DRAW, NULL);
1014         
1015         return OPERATOR_FINISHED;
1016 }
1017
1018 void OBJECT_OT_duplicates_make_real(wmOperatorType *ot)
1019 {
1020         
1021         /* identifiers */
1022         ot->name= "Make Duplicates Real";
1023         ot->description = "Make dupli objects attached to this object real";
1024         ot->idname= "OBJECT_OT_duplicates_make_real";
1025         
1026         /* api callbacks */
1027         ot->exec= object_duplicates_make_real_exec;
1028         
1029         ot->poll= ED_operator_objectmode;
1030         
1031         /* flags */
1032         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1033 }
1034
1035 /**************************** Convert **************************/
1036
1037 static EnumPropertyItem convert_target_items[]= {
1038         {OB_CURVE, "CURVE", ICON_OUTLINER_OB_CURVE, "Curve from Mesh/Text", ""},
1039         {OB_MESH, "MESH", ICON_OUTLINER_OB_MESH, "Mesh from Curve/Meta/Surf/Text", ""},
1040         {0, NULL, 0, NULL, NULL}};
1041
1042 static void curvetomesh(Scene *scene, Object *ob) 
1043 {
1044         if(ob->disp.first == NULL)
1045                 makeDispListCurveTypes(scene, ob, 0); /* force creation */
1046
1047         nurbs_to_mesh(ob); /* also does users */
1048
1049         if(ob->type == OB_MESH)
1050                 object_free_modifiers(ob);
1051 }
1052
1053 static int convert_poll(bContext *C)
1054 {
1055         Object *obact= CTX_data_active_object(C);
1056         Scene *scene= CTX_data_scene(C);
1057
1058         return (!scene->id.lib && obact && scene->obedit != obact && (obact->flag & SELECT) && !(obact->id.lib));
1059 }
1060
1061 /* Helper for convert_exec */
1062 static Base *duplibase_for_convert(Scene *scene, Base *base, Object *ob)
1063 {
1064         Object *obn;
1065         Base *basen;
1066
1067         if (ob == NULL) {
1068                 ob= base->object;
1069         }
1070
1071         obn= copy_object(ob);
1072         obn->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
1073
1074         basen= MEM_mallocN(sizeof(Base), "duplibase");
1075         *basen= *base;
1076         BLI_addhead(&scene->base, basen);       /* addhead: otherwise eternal loop */
1077         basen->object= obn;
1078         basen->flag |= SELECT;
1079         obn->flag |= SELECT;
1080         base->flag &= ~SELECT;
1081         ob->flag &= ~SELECT;
1082
1083         return basen;
1084 }
1085
1086 static int convert_exec(bContext *C, wmOperator *op)
1087 {
1088         Main *bmain= CTX_data_main(C);
1089         Scene *scene= CTX_data_scene(C);
1090         Base *basen=NULL, *basact=NULL, *basedel=NULL;
1091         Object *ob, *ob1, *newob, *obact= CTX_data_active_object(C);
1092         DerivedMesh *dm;
1093         Curve *cu;
1094         Nurb *nu;
1095         MetaBall *mb;
1096         Mesh *me;
1097         const short target= RNA_enum_get(op->ptr, "target");
1098         const short keep_original= RNA_boolean_get(op->ptr, "keep_original");
1099         int a, mballConverted= 0;
1100
1101         /* don't forget multiple users! */
1102
1103         /* reset flags */
1104         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
1105                 ob= base->object;
1106                 ob->flag &= ~OB_DONE;
1107
1108                 /* flag data thats not been edited (only needed for !keep_original) */
1109                 if(ob->data) {
1110                         ((ID *)ob->data)->flag |= LIB_DOIT;
1111                 }
1112         }
1113         CTX_DATA_END;
1114
1115         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
1116                 ob= base->object;
1117
1118                 if(ob->flag & OB_DONE || !IS_TAGGED(ob->data)) {
1119                         if (ob->type != target) {
1120                                 base->flag &= ~SELECT;
1121                                 ob->flag &= ~SELECT;
1122                         }
1123
1124                         /* obdata already modified */
1125                         if(!IS_TAGGED(ob->data)) {
1126                                 /* When 2 objects with linked data are selected, converting both
1127                                  * would keep modifiers on all but the converted object [#26003] */
1128                                 if(ob->type == OB_MESH) {
1129                                         object_free_modifiers(ob);      /* after derivedmesh calls! */
1130                                 }
1131                         }
1132                 }
1133                 else if (ob->type==OB_MESH && target == OB_CURVE) {
1134                         ob->flag |= OB_DONE;
1135
1136                         if (keep_original) {
1137                                 basen= duplibase_for_convert(scene, base, NULL);
1138                                 newob= basen->object;
1139
1140                                 /* decrement original mesh's usage count  */
1141                                 me= newob->data;
1142                                 me->id.us--;
1143
1144                                 /* make a new copy of the mesh */
1145                                 newob->data= copy_mesh(me);
1146                         } else {
1147                                 newob = ob;
1148                         }
1149
1150                         mesh_to_curve(scene, newob);
1151
1152                         if(newob->type==OB_CURVE)
1153                                 object_free_modifiers(newob);   /* after derivedmesh calls! */
1154                 }
1155                 else if(ob->type==OB_MESH && ob->modifiers.first) { /* converting a mesh with no modifiers causes a segfault */
1156                         ob->flag |= OB_DONE;
1157
1158                         if (keep_original) {
1159                                 basen= duplibase_for_convert(scene, base, NULL);
1160                                 newob= basen->object;
1161
1162                                 /* decrement original mesh's usage count  */
1163                                 me= newob->data;
1164                                 me->id.us--;
1165
1166                                 /* make a new copy of the mesh */
1167                                 newob->data= copy_mesh(me);
1168                         } else {
1169                                 newob = ob;
1170                                 ob->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
1171                         }
1172
1173                         /* make new mesh data from the original copy */
1174                         /* note: get the mesh from the original, not from the copy in some
1175                          * cases this doesnt give correct results (when MDEF is used for eg)
1176                          */
1177                         dm= mesh_get_derived_final(scene, newob, CD_MASK_MESH);
1178                         /* dm= mesh_create_derived_no_deform(ob1, NULL);        this was called original (instead of get_derived). man o man why! (ton) */
1179
1180                         DM_to_mesh(dm, newob->data, newob);
1181
1182                         dm->release(dm);
1183                         object_free_modifiers(newob);   /* after derivedmesh calls! */
1184                 }
1185                 else if(ob->type==OB_FONT) {
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 curve's usage count  */
1193                                 ((Curve *)newob->data)->id.us--;
1194
1195                                 /* make a new copy of the curve */
1196                                 newob->data= copy_curve(ob->data);
1197                         } else {
1198                                 newob= ob;
1199                         }
1200
1201                         cu= newob->data;
1202
1203                         if (!newob->disp.first)
1204                                 makeDispListCurveTypes(scene, newob, 0);
1205
1206                         newob->type= OB_CURVE;
1207
1208                         if(cu->vfont) {
1209                                 cu->vfont->id.us--;
1210                                 cu->vfont= NULL;
1211                         }
1212                         if(cu->vfontb) {
1213                                 cu->vfontb->id.us--;
1214                                 cu->vfontb= NULL;
1215                         }
1216                         if(cu->vfonti) {
1217                                 cu->vfonti->id.us--;
1218                                 cu->vfonti= NULL;
1219                         }
1220                         if(cu->vfontbi) {
1221                                 cu->vfontbi->id.us--;
1222                                 cu->vfontbi= NULL;
1223                         }
1224
1225                         if (!keep_original) {
1226                                 /* other users */
1227                                 if(cu->id.us>1) {
1228                                         for(ob1= bmain->object.first; ob1; ob1=ob1->id.next) {
1229                                                 if(ob1->data==ob->data) {
1230                                                         ob1->type= OB_CURVE;
1231                                                         ob1->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
1232                                                 }
1233                                         }
1234                                 }
1235                         }
1236
1237                         for(nu=cu->nurb.first; nu; nu=nu->next)
1238                                 nu->charidx= 0;
1239
1240                         if(target == OB_MESH) {
1241                                 curvetomesh(scene, newob);
1242
1243                                 /* meshes doesn't use displist */
1244                                 freedisplist(&newob->disp);
1245                         }
1246                 }
1247                 else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
1248                         ob->flag |= OB_DONE;
1249
1250                         if(target == OB_MESH) {
1251                                 if (keep_original) {
1252                                         basen= duplibase_for_convert(scene, base, NULL);
1253                                         newob= basen->object;
1254
1255                                         /* decrement original curve's usage count  */
1256                                         ((Curve *)newob->data)->id.us--;
1257
1258                                         /* make a new copy of the curve */
1259                                         newob->data= copy_curve(ob->data);
1260                                 } else {
1261                                         newob= ob;
1262
1263                                         /* meshes doesn't use displist */
1264                                         freedisplist(&newob->disp);
1265                                 }
1266
1267                                 curvetomesh(scene, newob);
1268                         }
1269                 }
1270                 else if(ob->type==OB_MBALL && target == OB_MESH) {
1271                         Object *baseob;
1272
1273                         base->flag &= ~SELECT;
1274                         ob->flag &= ~SELECT;
1275
1276                         baseob= find_basis_mball(scene, ob);
1277
1278                         if (ob != baseob) {
1279                                 /* if motherball is converting it would be marked as done later */
1280                                 ob->flag |= OB_DONE;
1281                         }
1282
1283                         if (!baseob->disp.first) {
1284                                 makeDispListMBall(scene, baseob);
1285                         }
1286
1287                         if(!(baseob->flag & OB_DONE)) {
1288                                 baseob->flag |= OB_DONE;
1289
1290                                 basen= duplibase_for_convert(scene, base, baseob);
1291                                 newob= basen->object;
1292
1293                                 mb= newob->data;
1294                                 mb->id.us--;
1295
1296                                 newob->data= add_mesh("Mesh");
1297                                 newob->type= OB_MESH;
1298
1299                                 me= newob->data;
1300                                 me->totcol= mb->totcol;
1301                                 if(newob->totcol) {
1302                                         me->mat= MEM_dupallocN(mb->mat);
1303                                         for(a=0; a<newob->totcol; a++) id_us_plus((ID *)me->mat[a]);
1304                                 }
1305
1306                                 mball_to_mesh(&baseob->disp, newob->data);
1307
1308                                 if (obact->type == OB_MBALL) {
1309                                         basact= basen;
1310                                 }
1311
1312                                 mballConverted= 1;
1313                         }
1314                 }
1315                 else {
1316                         continue;
1317                 }
1318
1319                 /* tag obdata if it was been changed */
1320
1321                 /* If the original object is active then make this object active */
1322                 if(basen) {
1323                         if(ob == obact) {
1324                                 /* store new active base to update BASACT */
1325                                 basact= basen;
1326                         }
1327
1328                         basen= NULL;
1329                 }
1330
1331                 if (!keep_original && (ob->flag & OB_DONE)) {
1332                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1333                         ((ID *)ob->data)->flag &= ~LIB_DOIT; /* flag not to convert this datablock again */
1334                 }
1335
1336                 /* delete original if needed */
1337                 if(basedel) {
1338                         if(!keep_original)
1339                                 ED_base_object_free_and_unlink(bmain, scene, basedel);  
1340
1341                         basedel = NULL;
1342                 }
1343         }
1344         CTX_DATA_END;
1345
1346         if(!keep_original) {
1347                 if (mballConverted) {
1348                         Base *base= scene->base.first, *tmpbase;
1349                         while (base) {
1350                                 ob= base->object;
1351                                 tmpbase= base;
1352                                 base= base->next;
1353
1354                                 if (ob->type == OB_MBALL) {
1355                                         ED_base_object_free_and_unlink(bmain, scene, tmpbase);
1356                                 }
1357                         }
1358                 }
1359
1360                 /* delete object should renew depsgraph */
1361                 DAG_scene_sort(bmain, scene);
1362         }
1363
1364 // XXX  ED_object_enter_editmode(C, 0);
1365 // XXX  exit_editmode(C, EM_FREEDATA|EM_WAITCURSOR); /* freedata, but no undo */
1366
1367         if (basact) {
1368                 /* active base was changed */
1369                 ED_base_object_activate(C, basact);
1370                 BASACT= basact;
1371         } else if (BASACT->object->flag & OB_DONE) {
1372                 WM_event_add_notifier(C, NC_OBJECT|ND_MODIFIER, BASACT->object);
1373                 WM_event_add_notifier(C, NC_OBJECT|ND_DATA, BASACT->object);
1374         }
1375
1376         DAG_scene_sort(bmain, scene);
1377         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, scene);
1378         WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene);
1379
1380         return OPERATOR_FINISHED;
1381 }
1382
1383
1384 void OBJECT_OT_convert(wmOperatorType *ot)
1385 {
1386         /* identifiers */
1387         ot->name= "Convert to";
1388         ot->description = "Convert selected objects to another type";
1389         ot->idname= "OBJECT_OT_convert";
1390         
1391         /* api callbacks */
1392         ot->invoke= WM_menu_invoke;
1393         ot->exec= convert_exec;
1394         ot->poll= convert_poll;
1395         
1396         /* flags */
1397         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1398
1399         /* properties */
1400         ot->prop= RNA_def_enum(ot->srna, "target", convert_target_items, OB_MESH, "Target", "Type of object to convert to");
1401         RNA_def_boolean(ot->srna, "keep_original", 0, "Keep Original", "Keep original objects instead of replacing them");
1402 }
1403
1404 /**************************** Duplicate ************************/
1405
1406 /* 
1407         dupflag: a flag made from constants declared in DNA_userdef_types.h
1408         The flag tells adduplicate() weather to copy data linked to the object, or to reference the existing data.
1409         U.dupflag for default operations or you can construct a flag as python does
1410         if the dupflag is 0 then no data will be copied (linked duplicate) */
1411
1412 /* used below, assumes id.new is correct */
1413 /* leaves selection of base/object unaltered */
1414 static Base *object_add_duplicate_internal(Main *bmain, Scene *scene, Base *base, int dupflag)
1415 {
1416         Base *basen= NULL;
1417         Material ***matarar;
1418         Object *ob, *obn;
1419         ID *id;
1420         int a, didit;
1421
1422         ob= base->object;
1423         if(ob->mode & OB_MODE_POSE) {
1424                 ; /* nothing? */
1425         }
1426         else {
1427                 obn= copy_object(ob);
1428                 obn->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
1429                 
1430                 basen= MEM_mallocN(sizeof(Base), "duplibase");
1431                 *basen= *base;
1432                 BLI_addhead(&scene->base, basen);       /* addhead: prevent eternal loop */
1433                 basen->object= obn;
1434                 
1435                 if(basen->flag & OB_FROMGROUP) {
1436                         Group *group;
1437                         for(group= bmain->group.first; group; group= group->id.next) {
1438                                 if(object_in_group(ob, group))
1439                                         add_to_group(group, obn, scene, basen);
1440                         }
1441                 }
1442                 
1443                 /* duplicates using userflags */
1444 #if 0 // XXX old animation system                               
1445                 if(dupflag & USER_DUP_IPO) {
1446                         bConstraintChannel *chan;
1447                         id= (ID *)obn->ipo;
1448                         
1449                         if(id) {
1450                                 ID_NEW_US( obn->ipo)
1451                                 else obn->ipo= copy_ipo(obn->ipo);
1452                                 id->us--;
1453                         }
1454                         /* Handle constraint ipos */
1455                         for (chan=obn->constraintChannels.first; chan; chan=chan->next){
1456                                 id= (ID *)chan->ipo;
1457                                 if(id) {
1458                                         ID_NEW_US( chan->ipo)
1459                                         else chan->ipo= copy_ipo(chan->ipo);
1460                                         id->us--;
1461                                 }
1462                         }
1463                 }
1464 #endif // XXX old animation system
1465                 
1466                 if(dupflag & USER_DUP_ACT) {
1467                         BKE_copy_animdata_id_action(&obn->id);
1468                 }
1469                 
1470                 if(dupflag & USER_DUP_MAT) {
1471                         for(a=0; a<obn->totcol; a++) {
1472                                 id= (ID *)obn->mat[a];
1473                                 if(id) {
1474                                         ID_NEW_US(obn->mat[a])
1475                                         else obn->mat[a]= copy_material(obn->mat[a]);
1476                                         id->us--;
1477                                         
1478                                         if(dupflag & USER_DUP_ACT) {
1479                                                 BKE_copy_animdata_id_action(&obn->mat[a]->id);
1480                                         }
1481                                 }
1482                         }
1483                 }
1484                 if(dupflag & USER_DUP_PSYS) {
1485                         ParticleSystem *psys;
1486                         for(psys=obn->particlesystem.first; psys; psys=psys->next) {
1487                                 id= (ID*) psys->part;
1488                                 if(id) {
1489                                         ID_NEW_US(psys->part)
1490                                         else psys->part= psys_copy_settings(psys->part);
1491                                         
1492                                         if(dupflag & USER_DUP_ACT) {
1493                                                 BKE_copy_animdata_id_action(&psys->part->id);
1494                                         }
1495
1496                                         id->us--;
1497                                 }
1498                         }
1499                 }
1500                 
1501                 id= obn->data;
1502                 didit= 0;
1503                 
1504                 switch(obn->type) {
1505                         case OB_MESH:
1506                                 if(dupflag & USER_DUP_MESH) {
1507                                         ID_NEW_US2( obn->data )
1508                                         else {
1509                                                 obn->data= copy_mesh(obn->data);
1510                                                 
1511                                                 if(obn->fluidsimSettings) {
1512                                                         obn->fluidsimSettings->orgMesh = (Mesh *)obn->data;
1513                                                 }
1514                                                 
1515                                                 didit= 1;
1516                                         }
1517                                         id->us--;
1518                                 }
1519                                 break;
1520                         case OB_CURVE:
1521                                 if(dupflag & USER_DUP_CURVE) {
1522                                         ID_NEW_US2(obn->data )
1523                                         else {
1524                                                 obn->data= copy_curve(obn->data);
1525                                                 didit= 1;
1526                                         }
1527                                         id->us--;
1528                                 }
1529                                 break;
1530                         case OB_SURF:
1531                                 if(dupflag & USER_DUP_SURF) {
1532                                         ID_NEW_US2( obn->data )
1533                                         else {
1534                                                 obn->data= copy_curve(obn->data);
1535                                                 didit= 1;
1536                                         }
1537                                         id->us--;
1538                                 }
1539                                 break;
1540                         case OB_FONT:
1541                                 if(dupflag & USER_DUP_FONT) {
1542                                         ID_NEW_US2( obn->data )
1543                                         else {
1544                                                 obn->data= copy_curve(obn->data);
1545                                                 didit= 1;
1546                                         }
1547                                         id->us--;
1548                                 }
1549                                 break;
1550                         case OB_MBALL:
1551                                 if(dupflag & USER_DUP_MBALL) {
1552                                         ID_NEW_US2(obn->data )
1553                                         else {
1554                                                 obn->data= copy_mball(obn->data);
1555                                                 didit= 1;
1556                                         }
1557                                         id->us--;
1558                                 }
1559                                 break;
1560                         case OB_LAMP:
1561                                 if(dupflag & USER_DUP_LAMP) {
1562                                         ID_NEW_US2(obn->data )
1563                                         else {
1564                                                 obn->data= copy_lamp(obn->data);
1565                                                 didit= 1;
1566                                         }
1567                                         id->us--;
1568                                 }
1569                                 break;
1570                                 
1571                         case OB_ARMATURE:
1572                                 obn->recalc |= OB_RECALC_DATA;
1573                                 if(obn->pose) obn->pose->flag |= POSE_RECALC;
1574                                         
1575                                         if(dupflag & USER_DUP_ARM) {
1576                                                 ID_NEW_US2(obn->data )
1577                                                 else {
1578                                                         obn->data= copy_armature(obn->data);
1579                                                         armature_rebuild_pose(obn, obn->data);
1580                                                         didit= 1;
1581                                                 }
1582                                                 id->us--;
1583                                         }
1584                                                 
1585                                                 break;
1586                                 
1587                         case OB_LATTICE:
1588                                 if(dupflag!=0) {
1589                                         ID_NEW_US2(obn->data )
1590                                         else {
1591                                                 obn->data= copy_lattice(obn->data);
1592                                                 didit= 1;
1593                                         }
1594                                         id->us--;
1595                                 }
1596                                 break;
1597                         case OB_CAMERA:
1598                                 if(dupflag!=0) {
1599                                         ID_NEW_US2(obn->data )
1600                                         else {
1601                                                 obn->data= copy_camera(obn->data);
1602                                                 didit= 1;
1603                                         }
1604                                         id->us--;
1605                                 }
1606                                 break;
1607                 }
1608
1609                 /* check if obdata is copied */
1610                 if(didit) {
1611                         if(dupflag & USER_DUP_ACT) {
1612                                 BKE_copy_animdata_id_action((ID *)obn->data);
1613                         }
1614                         
1615                         if(dupflag & USER_DUP_MAT) {
1616                                 matarar= give_matarar(obn);
1617                                 if(matarar) {
1618                                         for(a=0; a<obn->totcol; a++) {
1619                                                 id= (ID *)(*matarar)[a];
1620                                                 if(id) {
1621                                                         ID_NEW_US( (*matarar)[a] )
1622                                                         else (*matarar)[a]= copy_material((*matarar)[a]);
1623                                                         
1624                                                         id->us--;
1625                                                 }
1626                                         }
1627                                 }
1628                         }
1629                 }
1630         }
1631         return basen;
1632 }
1633
1634 /* single object duplicate, if dupflag==0, fully linked, else it uses the flags given */
1635 /* leaves selection of base/object unaltered.
1636  * note: don't call this within a loop since clear_* funcs loop over the entire database. */
1637 Base *ED_object_add_duplicate(Main *bmain, Scene *scene, Base *base, int dupflag)
1638 {
1639         Base *basen;
1640         Object *ob;
1641
1642         clear_id_newpoins();
1643         clear_sca_new_poins();  /* sensor/contr/act */
1644
1645         basen= object_add_duplicate_internal(bmain, scene, base, dupflag);
1646         if (basen == NULL) {
1647                 return NULL;
1648         }
1649
1650         ob= basen->object;
1651
1652         /* link own references to the newly duplicated data [#26816] */
1653         object_relink(ob);
1654         set_sca_new_poins_ob(ob);
1655
1656         DAG_scene_sort(bmain, scene);
1657         ED_render_id_flush_update(bmain, ob->data);
1658
1659         return basen;
1660 }
1661
1662 /* contextual operator dupli */
1663 static int duplicate_exec(bContext *C, wmOperator *op)
1664 {
1665         Main *bmain= CTX_data_main(C);
1666         Scene *scene= CTX_data_scene(C);
1667         int linked= RNA_boolean_get(op->ptr, "linked");
1668         int dupflag= (linked)? 0: U.dupflag;
1669         
1670         clear_id_newpoins();
1671         clear_sca_new_poins();  /* sensor/contr/act */
1672         
1673         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
1674                 Base *basen= object_add_duplicate_internal(bmain, scene, base, dupflag);
1675                 
1676                 /* note that this is safe to do with this context iterator,
1677                    the list is made in advance */
1678                 ED_base_object_select(base, BA_DESELECT);
1679
1680                 if (basen == NULL) {
1681                         continue;
1682                 }
1683
1684                 /* new object becomes active */
1685                 if(BASACT==base)
1686                         ED_base_object_activate(C, basen);
1687
1688                 if(basen->object->data) {
1689                         DAG_id_tag_update(basen->object->data, 0);
1690                 }
1691         }
1692         CTX_DATA_END;
1693
1694         copy_object_set_idnew(C, dupflag);
1695
1696         DAG_scene_sort(bmain, scene);
1697         DAG_ids_flush_update(bmain, 0);
1698
1699         WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene);
1700
1701         return OPERATOR_FINISHED;
1702 }
1703
1704 void OBJECT_OT_duplicate(wmOperatorType *ot)
1705 {
1706         PropertyRNA *prop;
1707         
1708         /* identifiers */
1709         ot->name= "Duplicate Objects";
1710         ot->description = "Duplicate selected objects";
1711         ot->idname= "OBJECT_OT_duplicate";
1712         
1713         /* api callbacks */
1714         ot->exec= duplicate_exec;
1715         ot->poll= ED_operator_objectmode;
1716         
1717         /* flags */
1718         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1719         
1720         /* to give to transform */
1721         RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data");
1722         prop= RNA_def_enum(ot->srna, "mode", transform_mode_types, TFM_TRANSLATION, "Mode", "");
1723         RNA_def_property_flag(prop, PROP_HIDDEN);
1724 }
1725
1726 /* **************** add named object, for dragdrop ************* */
1727
1728
1729 static int add_named_exec(bContext *C, wmOperator *op)
1730 {
1731         Main *bmain= CTX_data_main(C);
1732         Scene *scene= CTX_data_scene(C);
1733         Base *basen, *base;
1734         Object *ob;
1735         int linked= RNA_boolean_get(op->ptr, "linked");
1736         int dupflag= (linked)? 0: U.dupflag;
1737         char name[32];
1738
1739         /* find object, create fake base */
1740         RNA_string_get(op->ptr, "name", name);
1741         ob= (Object *)find_id("OB", name);
1742         if(ob==NULL) 
1743                 return OPERATOR_CANCELLED;
1744
1745         base= MEM_callocN(sizeof(Base), "duplibase");
1746         base->object= ob;
1747         base->flag= ob->flag;
1748
1749         /* prepare dupli */
1750         clear_id_newpoins();
1751         clear_sca_new_poins();  /* sensor/contr/act */
1752
1753         basen= object_add_duplicate_internal(bmain, scene, base, dupflag);
1754
1755         if (basen == NULL) {
1756                 MEM_freeN(base);
1757                 return OPERATOR_CANCELLED;
1758         }
1759
1760         basen->lay= basen->object->lay= scene->lay;
1761
1762         ED_object_location_from_view(C, basen->object->loc);
1763         ED_base_object_activate(C, basen);
1764
1765         copy_object_set_idnew(C, dupflag);
1766
1767         DAG_scene_sort(bmain, scene);
1768         DAG_ids_flush_update(bmain, 0);
1769
1770         MEM_freeN(base);
1771
1772         WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene);
1773
1774         return OPERATOR_FINISHED;
1775 }
1776
1777 void OBJECT_OT_add_named(wmOperatorType *ot)
1778 {
1779         /* identifiers */
1780         ot->name= "Add Named Object";
1781         ot->description = "Add named object";
1782         ot->idname= "OBJECT_OT_add_named";
1783         
1784         /* api callbacks */
1785         ot->exec= add_named_exec;
1786         ot->poll= ED_operator_objectmode;
1787         
1788         /* flags */
1789         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1790         
1791         RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data");
1792         RNA_def_string(ot->srna, "name", "Cube", 24, "Name", "Object name to add");
1793 }
1794
1795
1796
1797 /**************************** Join *************************/
1798 static int join_poll(bContext *C)
1799 {
1800         Object *ob= CTX_data_active_object(C);
1801         
1802         if (!ob || ob->id.lib) return 0;
1803         
1804         if (ELEM4(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_ARMATURE))
1805                 return ED_operator_screenactive(C);
1806         else
1807                 return 0;
1808 }
1809
1810
1811 static int join_exec(bContext *C, wmOperator *op)
1812 {
1813         Scene *scene= CTX_data_scene(C);
1814         Object *ob= CTX_data_active_object(C);
1815
1816         if(scene->obedit) {
1817                 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in editmode");
1818                 return OPERATOR_CANCELLED;
1819         }
1820         else if(object_data_is_libdata(ob)) {
1821                 BKE_report(op->reports, RPT_ERROR, "Can't edit external libdata");
1822                 return OPERATOR_CANCELLED;
1823         }
1824
1825         if(ob->type == OB_MESH)
1826                 return join_mesh_exec(C, op);
1827         else if(ELEM(ob->type, OB_CURVE, OB_SURF))
1828                 return join_curve_exec(C, op);
1829         else if(ob->type == OB_ARMATURE)
1830                 return join_armature_exec(C, op);
1831         
1832         return OPERATOR_CANCELLED;
1833 }
1834
1835 void OBJECT_OT_join(wmOperatorType *ot)
1836 {
1837         /* identifiers */
1838         ot->name= "Join";
1839         ot->description = "Join selected objects into active object";
1840         ot->idname= "OBJECT_OT_join";
1841         
1842         /* api callbacks */
1843         ot->exec= join_exec;
1844         ot->poll= join_poll;
1845         
1846         /* flags */
1847         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1848 }
1849
1850 /**************************** Join as Shape Key*************************/
1851 static int join_shapes_poll(bContext *C)
1852 {
1853         Object *ob= CTX_data_active_object(C);
1854         
1855         if (!ob || ob->id.lib) return 0;
1856         
1857         /* only meshes supported at the moment */
1858         if (ob->type == OB_MESH)
1859                 return ED_operator_screenactive(C);
1860         else
1861                 return 0;
1862 }
1863
1864 static int join_shapes_exec(bContext *C, wmOperator *op)
1865 {
1866         Scene *scene= CTX_data_scene(C);
1867         Object *ob= CTX_data_active_object(C);
1868         
1869         if(scene->obedit) {
1870                 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in editmode");
1871                 return OPERATOR_CANCELLED;
1872         }
1873         else if(object_data_is_libdata(ob)) {
1874                 BKE_report(op->reports, RPT_ERROR, "Can't edit external libdata");
1875                 return OPERATOR_CANCELLED;
1876         }
1877         
1878         if(ob->type == OB_MESH)
1879                 return join_mesh_shapes_exec(C, op);
1880         
1881         return OPERATOR_CANCELLED;
1882 }
1883
1884 void OBJECT_OT_join_shapes(wmOperatorType *ot)
1885 {
1886         /* identifiers */
1887         ot->name= "Join as Shapes";
1888         ot->description = "Merge selected objects to shapes of active object";
1889         ot->idname= "OBJECT_OT_join_shapes";
1890         
1891         /* api callbacks */
1892         ot->exec= join_shapes_exec;
1893         ot->poll= join_shapes_poll;
1894         
1895         /* flags */
1896         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1897 }