'Add Group' back in the add object menu
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 #include <stdlib.h>
29 #include <string.h>
30
31 #include "MEM_guardedalloc.h"
32
33 #include "DNA_action_types.h"
34 #include "DNA_curve_types.h"
35 #include "DNA_group_types.h"
36 #include "DNA_lamp_types.h"
37 #include "DNA_material_types.h"
38 #include "DNA_mesh_types.h"
39 #include "DNA_meta_types.h"
40 #include "DNA_object_fluidsim.h"
41 #include "DNA_object_types.h"
42 #include "DNA_object_force.h"
43 #include "DNA_scene_types.h"
44 #include "DNA_screen_types.h"
45 #include "DNA_userdef_types.h"
46 #include "DNA_view3d_types.h"
47 #include "DNA_vfont_types.h"
48
49 #include "BLI_arithb.h"
50 #include "BLI_listbase.h"
51
52 #include "BKE_anim.h"
53 #include "BKE_armature.h"
54 #include "BKE_constraint.h"
55 #include "BKE_context.h"
56 #include "BKE_curve.h"
57 #include "BKE_customdata.h"
58 #include "BKE_depsgraph.h"
59 #include "BKE_DerivedMesh.h"
60 #include "BKE_displist.h"
61 #include "BKE_effect.h"
62 #include "BKE_global.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_scene.h"
76 #include "BKE_texture.h"
77 #include "BKE_utildefines.h"
78
79 #include "RNA_access.h"
80 #include "RNA_define.h"
81 #include "RNA_enum_types.h"
82
83 #include "WM_api.h"
84 #include "WM_types.h"
85
86 #include "ED_anim_api.h"
87 #include "ED_armature.h"
88 #include "ED_curve.h"
89 #include "ED_mball.h"
90 #include "ED_mesh.h"
91 #include "ED_object.h"
92 #include "ED_screen.h"
93 #include "ED_transform.h"
94
95 #include "UI_interface.h"
96 #include "UI_resources.h"
97
98 #include "object_intern.h"
99
100 /************************** Exported *****************************/
101
102 void ED_object_base_init_from_view(bContext *C, Base *base)
103 {
104         View3D *v3d= CTX_wm_view3d(C);
105         Scene *scene= CTX_data_scene(C);
106         Object *ob= base->object;
107         
108         if (scene==NULL)
109                 return;
110         
111         if (v3d==NULL) {
112                 base->lay = scene->lay;
113                 VECCOPY(ob->loc, scene->cursor);
114         } 
115         else {
116                 if (v3d->localvd) {
117                         base->lay= ob->lay= v3d->layact | v3d->lay;
118                         VECCOPY(ob->loc, v3d->cursor);
119                 } 
120                 else {
121                         base->lay= ob->lay= v3d->layact;
122                         VECCOPY(ob->loc, scene->cursor);
123                 }
124                 
125                 if (U.flag & USER_ADD_VIEWALIGNED) {
126                         ARegion *ar= CTX_wm_region(C);
127                         if(ar) {
128                                 RegionView3D *rv3d= ar->regiondata;
129                                 
130                                 rv3d->viewquat[0]= -rv3d->viewquat[0];
131                                 QuatToEul(rv3d->viewquat, ob->rot);
132                                 rv3d->viewquat[0]= -rv3d->viewquat[0];
133                         }
134                 }
135         }
136         where_is_object(scene, ob);
137 }
138
139 /********************* Add Object Operator ********************/
140
141 void add_object_draw(Scene *scene, View3D *v3d, int type)       /* for toolbox or menus, only non-editmode stuff */
142 {
143         /* keep here to get things compile, remove later */
144 }
145
146 /* for object add primitive operators */
147 static Object *object_add_type(bContext *C, int type)
148 {
149         Scene *scene= CTX_data_scene(C);
150         Object *ob;
151         
152         /* for as long scene has editmode... */
153         if (CTX_data_edit_object(C)) 
154                 ED_object_exit_editmode(C, EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR); /* freedata, and undo */
155         
156         /* deselects all, sets scene->basact */
157         ob= add_object(scene, type);
158         /* editor level activate, notifiers */
159         ED_base_object_activate(C, BASACT);
160
161         /* more editor stuff */
162         ED_object_base_init_from_view(C, BASACT);
163
164         DAG_scene_sort(scene);
165
166         return ob;
167 }
168
169 /* for object add operator */
170 static int object_add_exec(bContext *C, wmOperator *op)
171 {
172         object_add_type(C, RNA_enum_get(op->ptr, "type"));
173         
174         return OPERATOR_FINISHED;
175 }
176
177 void OBJECT_OT_add(wmOperatorType *ot)
178 {
179         /* identifiers */
180         ot->name= "Add Object";
181         ot->description = "Add an object to the scene.";
182         ot->idname= "OBJECT_OT_add";
183         
184         /* api callbacks */
185         ot->invoke= WM_menu_invoke;
186         ot->exec= object_add_exec;
187         
188         ot->poll= ED_operator_scene_editable;
189         
190         /* flags */
191         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
192         
193         RNA_def_enum(ot->srna, "type", object_type_items, 0, "Type", "");
194 }
195
196 /********************* Add Effector Operator ********************/
197 /* copy from rna_object_force.c*/
198 static EnumPropertyItem field_type_items[] = {
199         {0, "NONE", 0, "None", ""},
200         {PFIELD_FORCE, "FORCE", 0, "Force", ""},
201         {PFIELD_WIND, "WIND", 0, "Wind", ""},
202         {PFIELD_VORTEX, "VORTEX", 0, "Vortex", ""},
203         {PFIELD_MAGNET, "MAGNET", 0, "Magnetic", ""},
204         {PFIELD_HARMONIC, "HARMONIC", 0, "Harmonic", ""},
205         {PFIELD_CHARGE, "CHARGE", 0, "Charge", ""},
206         {PFIELD_LENNARDJ, "LENNARDJ", 0, "Lennard-Jones", ""},
207         {PFIELD_TEXTURE, "TEXTURE", 0, "Texture", ""},
208         {PFIELD_GUIDE, "GUIDE", 0, "Curve Guide", ""},
209         {PFIELD_BOID, "BOID", 0, "Boid", ""},
210         {PFIELD_TURBULENCE, "TURBULENCE", 0, "Turbulence", ""},
211         {PFIELD_DRAG, "DRAG", 0, "Drag", ""},
212         {0, NULL, 0, NULL, NULL}};
213
214 void add_effector_draw(Scene *scene, View3D *v3d, int type)     /* for toolbox or menus, only non-editmode stuff */
215 {
216         /* keep here to get things compile, remove later */
217 }
218
219 /* for effector add primitive operators */
220 static Object *effector_add_type(bContext *C, int type)
221 {
222         Scene *scene= CTX_data_scene(C);
223         Object *ob;
224         
225         /* for as long scene has editmode... */
226         if (CTX_data_edit_object(C)) 
227                 ED_object_exit_editmode(C, EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR); /* freedata, and undo */
228         
229         /* deselects all, sets scene->basact */
230         if(type==PFIELD_GUIDE) {
231                 ob = add_object(scene, OB_CURVE);
232                 ((Curve*)ob->data)->flag |= CU_PATH|CU_3D;
233                 ED_object_enter_editmode(C, 0);
234                 BLI_addtail(curve_get_editcurve(ob), add_nurbs_primitive(C, CU_NURBS|CU_PRIM_PATH, 1));
235                 ED_object_exit_editmode(C, EM_FREEDATA);
236         }
237         else
238                 ob=     add_object(scene, OB_EMPTY);
239
240         ob->pd= object_add_collision_fields(type);
241
242         /* editor level activate, notifiers */
243         ED_base_object_activate(C, BASACT);
244
245         /* more editor stuff */
246         ED_object_base_init_from_view(C, BASACT);
247
248         DAG_scene_sort(scene);
249
250         return ob;
251 }
252
253 /* for object add operator */
254 static int effector_add_exec(bContext *C, wmOperator *op)
255 {
256         effector_add_type(C, RNA_int_get(op->ptr, "type"));
257         
258         return OPERATOR_FINISHED;
259 }
260
261 void OBJECT_OT_effector_add(wmOperatorType *ot)
262 {
263         /* identifiers */
264         ot->name= "Add Effector";
265         ot->description = "Add an empty object with a physics effector to the scene.";
266         ot->idname= "OBJECT_OT_effector_add";
267         
268         /* api callbacks */
269         ot->invoke= WM_menu_invoke;
270         ot->exec= effector_add_exec;
271         
272         ot->poll= ED_operator_scene_editable;
273         
274         /* flags */
275         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
276         
277         RNA_def_enum(ot->srna, "type", field_type_items, 0, "Type", "");
278 }
279
280 /* ***************** add primitives *************** */
281 /* ******  work both in and outside editmode ****** */
282
283 static EnumPropertyItem prop_mesh_types[] = {
284         {0, "PLANE", ICON_MESH_PLANE, "Plane", ""},
285         {1, "CUBE", ICON_MESH_CUBE, "Cube", ""},
286         {2, "CIRCLE", ICON_MESH_CIRCLE, "Circle", ""},
287         {3, "UVSPHERE", ICON_MESH_UVSPHERE, "UVsphere", ""},
288         {4, "ICOSPHERE", ICON_MESH_ICOSPHERE, "Icosphere", ""},
289         {5, "CYLINDER", ICON_MESH_TUBE, "Cylinder", ""},
290         {6, "CONE", ICON_MESH_CONE, "Cone", ""},
291         {0, "", 0, NULL, NULL},
292         {7, "GRID", ICON_MESH_GRID, "Grid", ""},
293         {8, "MONKEY", ICON_MESH_MONKEY, "Monkey", ""},
294         {0, NULL, 0, NULL, NULL}
295 };
296
297 static int object_add_mesh_exec(bContext *C, wmOperator *op)
298 {
299         Object *obedit= CTX_data_edit_object(C);
300         int newob= 0;
301         
302         if(obedit==NULL || obedit->type!=OB_MESH) {
303                 object_add_type(C, OB_MESH);
304                 ED_object_enter_editmode(C, EM_DO_UNDO);
305                 newob = 1;
306         }
307         else DAG_id_flush_update(&obedit->id, OB_RECALC_DATA);
308
309         switch(RNA_enum_get(op->ptr, "type")) {
310                 case 0:
311                         WM_operator_name_call(C, "MESH_OT_primitive_plane_add", WM_OP_INVOKE_REGION_WIN, NULL);
312                         break;
313                 case 1:
314                         WM_operator_name_call(C, "MESH_OT_primitive_cube_add", WM_OP_INVOKE_REGION_WIN, NULL);
315                         break;
316                 case 2:
317                         WM_operator_name_call(C, "MESH_OT_primitive_circle_add", WM_OP_INVOKE_REGION_WIN, NULL);
318                         break;
319                 case 3:
320                         WM_operator_name_call(C, "MESH_OT_primitive_uv_sphere_add", WM_OP_INVOKE_REGION_WIN, NULL);
321                         break;
322                 case 4:
323                         WM_operator_name_call(C, "MESH_OT_primitive_ico_sphere_add", WM_OP_INVOKE_REGION_WIN, NULL);
324                         break;
325                 case 5:
326                         WM_operator_name_call(C, "MESH_OT_primitive_cylinder_add", WM_OP_INVOKE_REGION_WIN, NULL);
327                         break;
328                 case 6:
329                         WM_operator_name_call(C, "MESH_OT_primitive_cone_add", WM_OP_INVOKE_REGION_WIN, NULL);
330                         break;
331                 case 7:
332                         WM_operator_name_call(C, "MESH_OT_primitive_grid_add", WM_OP_INVOKE_REGION_WIN, NULL);
333                         break;
334                 case 8:
335                         WM_operator_name_call(C, "MESH_OT_primitive_monkey_add", WM_OP_INVOKE_REGION_WIN, NULL);
336                         break;
337         }
338         /* userdef */
339         if (newob && (U.flag & USER_ADD_EDITMODE)==0) {
340                 ED_object_exit_editmode(C, EM_FREEDATA);
341         }
342         
343         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit);
344         
345         return OPERATOR_FINISHED;
346 }
347
348
349 void OBJECT_OT_mesh_add(wmOperatorType *ot)
350 {
351         /* identifiers */
352         ot->name= "Add Mesh";
353         ot->description = "Add a mesh object to the scene.";
354         ot->idname= "OBJECT_OT_mesh_add";
355         
356         /* api callbacks */
357         ot->invoke= WM_menu_invoke;
358         ot->exec= object_add_mesh_exec;
359         
360         ot->poll= ED_operator_scene_editable;
361         
362         /* flags: no register or undo, this operator calls operators */
363         ot->flag= 0; //OPTYPE_REGISTER|OPTYPE_UNDO;
364         
365         RNA_def_enum(ot->srna, "type", prop_mesh_types, 0, "Primitive", "");
366 }
367
368 static EnumPropertyItem prop_curve_types[] = {
369         {CU_BEZIER|CU_PRIM_CURVE, "BEZIER_CURVE", ICON_CURVE_BEZCURVE, "Bezier Curve", ""},
370         {CU_BEZIER|CU_PRIM_CIRCLE, "BEZIER_CIRCLE", ICON_CURVE_BEZCIRCLE, "Bezier Circle", ""},
371         {CU_NURBS|CU_PRIM_CURVE, "NURBS_CURVE", ICON_CURVE_NCURVE, "NURBS Curve", ""},
372         {CU_NURBS|CU_PRIM_CIRCLE, "NURBS_CIRCLE", ICON_CURVE_NCIRCLE, "NURBS Circle", ""},
373         {CU_NURBS|CU_PRIM_PATH, "PATH", ICON_CURVE_PATH, "Path", ""},
374         {0, NULL, 0, NULL, NULL}
375 };
376
377 static int object_add_curve_exec(bContext *C, wmOperator *op)
378 {
379         Object *obedit= CTX_data_edit_object(C);
380         ListBase *editnurb;
381         Nurb *nu;
382         int newob= 0;
383         
384         if(obedit==NULL || obedit->type!=OB_CURVE) {
385                 object_add_type(C, OB_CURVE);
386                 ED_object_enter_editmode(C, 0);
387                 newob = 1;
388         }
389         else DAG_id_flush_update(&obedit->id, OB_RECALC_DATA);
390         
391         obedit= CTX_data_edit_object(C);
392         nu= add_nurbs_primitive(C, RNA_enum_get(op->ptr, "type"), newob);
393         editnurb= curve_get_editcurve(obedit);
394         BLI_addtail(editnurb, nu);
395         
396         /* userdef */
397         if (newob && (U.flag & USER_ADD_EDITMODE)==0) {
398                 ED_object_exit_editmode(C, EM_FREEDATA);
399         }
400         
401         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit);
402         
403         return OPERATOR_FINISHED;
404 }
405
406 static int object_add_curve_invoke(bContext *C, wmOperator *op, wmEvent *event)
407 {
408         Object *obedit= CTX_data_edit_object(C);
409         uiPopupMenu *pup;
410         uiLayout *layout;
411
412         pup= uiPupMenuBegin(C, op->type->name, 0);
413         layout= uiPupMenuLayout(pup);
414         if(!obedit || obedit->type == OB_CURVE)
415                 uiItemsEnumO(layout, op->type->idname, "type");
416         else
417                 uiItemsEnumO(layout, "OBJECT_OT_surface_add", "type");
418         uiPupMenuEnd(C, pup);
419
420         return OPERATOR_CANCELLED;
421 }
422
423 void OBJECT_OT_curve_add(wmOperatorType *ot)
424 {
425         /* identifiers */
426         ot->name= "Add Curve";
427         ot->description = "Add a curve object to the scene.";
428         ot->idname= "OBJECT_OT_curve_add";
429         
430         /* api callbacks */
431         ot->invoke= object_add_curve_invoke;
432         ot->exec= object_add_curve_exec;
433         
434         ot->poll= ED_operator_scene_editable;
435         
436         /* flags */
437         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
438         
439         RNA_def_enum(ot->srna, "type", prop_curve_types, 0, "Primitive", "");
440 }
441
442 static EnumPropertyItem prop_surface_types[]= {
443         {CU_PRIM_CURVE|CU_NURBS, "NURBS_CURVE", ICON_SURFACE_NCURVE, "NURBS Curve", ""},
444         {CU_PRIM_CIRCLE|CU_NURBS, "NURBS_CIRCLE", ICON_SURFACE_NCIRCLE, "NURBS Circle", ""},
445         {CU_PRIM_PATCH|CU_NURBS, "NURBS_SURFACE", ICON_SURFACE_NSURFACE, "NURBS Surface", ""},
446         {CU_PRIM_TUBE|CU_NURBS, "NURBS_TUBE", ICON_SURFACE_NTUBE, "NURBS Tube", ""},
447         {CU_PRIM_SPHERE|CU_NURBS, "NURBS_SPHERE", ICON_SURFACE_NSPHERE, "NURBS Sphere", ""},
448         {CU_PRIM_DONUT|CU_NURBS, "NURBS_DONUT", ICON_SURFACE_NDONUT, "NURBS Donut", ""},
449         {0, NULL, 0, NULL, NULL}
450 };
451
452 static int object_add_surface_exec(bContext *C, wmOperator *op)
453 {
454         Object *obedit= CTX_data_edit_object(C);
455         ListBase *editnurb;
456         Nurb *nu;
457         int newob= 0;
458         
459         if(obedit==NULL || obedit->type!=OB_SURF) {
460                 object_add_type(C, OB_SURF);
461                 ED_object_enter_editmode(C, 0);
462                 newob = 1;
463         }
464         else DAG_id_flush_update(&obedit->id, OB_RECALC_DATA);
465         
466         obedit= CTX_data_edit_object(C);
467         nu= add_nurbs_primitive(C, RNA_enum_get(op->ptr, "type"), newob);
468         editnurb= curve_get_editcurve(obedit);
469         BLI_addtail(editnurb, nu);
470         
471         /* userdef */
472         if (newob && (U.flag & USER_ADD_EDITMODE)==0) {
473                 ED_object_exit_editmode(C, EM_FREEDATA);
474         }
475         
476         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit);
477         
478         return OPERATOR_FINISHED;
479 }
480
481 void OBJECT_OT_surface_add(wmOperatorType *ot)
482 {
483         /* identifiers */
484         ot->name= "Add Surface";
485         ot->description = "Add a surface object to the scene.";
486         ot->idname= "OBJECT_OT_surface_add";
487         
488         /* api callbacks */
489         ot->invoke= WM_menu_invoke;
490         ot->exec= object_add_surface_exec;
491         
492         ot->poll= ED_operator_scene_editable;
493         
494         /* flags */
495         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
496         
497         RNA_def_enum(ot->srna, "type", prop_surface_types, 0, "Primitive", "");
498 }
499
500 static EnumPropertyItem prop_metaball_types[]= {
501         {MB_BALL, "MBALL_BALL", ICON_META_BALL, "Meta Ball", ""},
502         {MB_TUBE, "MBALL_TUBE", ICON_META_TUBE, "Meta Tube", ""},
503         {MB_PLANE, "MBALL_PLANE", ICON_META_PLANE, "Meta Plane", ""},
504         {MB_CUBE, "MBALL_CUBE", ICON_META_CUBE, "Meta Cube", ""},
505         {MB_ELIPSOID, "MBALL_ELLIPSOID", ICON_META_ELLIPSOID, "Meta Ellipsoid", ""},
506         {0, NULL, 0, NULL, NULL}
507 };
508
509 static int object_metaball_add_exec(bContext *C, wmOperator *op)
510 {
511         Object *obedit= CTX_data_edit_object(C);
512         MetaBall *mball;
513         MetaElem *elem;
514         int newob= 0;
515         
516         if(obedit==NULL || obedit->type!=OB_MBALL) {
517                 object_add_type(C, OB_MBALL);
518                 ED_object_enter_editmode(C, 0);
519                 newob = 1;
520         }
521         else DAG_id_flush_update(&obedit->id, OB_RECALC_DATA);
522         
523         obedit= CTX_data_edit_object(C);
524         elem= (MetaElem*)add_metaball_primitive(C, RNA_enum_get(op->ptr, "type"), newob);
525         mball= (MetaBall*)obedit->data;
526         BLI_addtail(mball->editelems, elem);
527         
528         /* userdef */
529         if (newob && (U.flag & USER_ADD_EDITMODE)==0) {
530                 ED_object_exit_editmode(C, EM_FREEDATA);
531         }
532         
533         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit);
534         
535         return OPERATOR_FINISHED;
536 }
537
538 static int object_metaball_add_invoke(bContext *C, wmOperator *op, wmEvent *event)
539 {
540         Object *obedit= CTX_data_edit_object(C);
541         uiPopupMenu *pup;
542         uiLayout *layout;
543
544         pup= uiPupMenuBegin(C, op->type->name, 0);
545         layout= uiPupMenuLayout(pup);
546         if(!obedit || obedit->type == OB_MBALL)
547                 uiItemsEnumO(layout, op->type->idname, "type");
548         else
549                 uiItemsEnumO(layout, "OBJECT_OT_metaball_add", "type");
550         uiPupMenuEnd(C, pup);
551
552         return OPERATOR_CANCELLED;
553 }
554
555 void OBJECT_OT_metaball_add(wmOperatorType *ot)
556 {
557         /* identifiers */
558         ot->name= "Add Metaball";
559         ot->description= "Add an metaball object to the scene.";
560         ot->idname= "OBJECT_OT_metaball_add";
561
562         /* api callbacks */
563         ot->invoke= object_metaball_add_invoke;
564         ot->exec= object_metaball_add_exec;
565         ot->poll= ED_operator_scene_editable;
566
567         /* flags */
568         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
569         
570         RNA_def_enum(ot->srna, "type", prop_metaball_types, 0, "Primitive", "");
571 }
572 static int object_add_text_exec(bContext *C, wmOperator *op)
573 {
574         Object *obedit= CTX_data_edit_object(C);
575         
576         if(obedit && obedit->type==OB_FONT)
577                 return OPERATOR_CANCELLED;
578
579         object_add_type(C, OB_FONT);
580         obedit= CTX_data_active_object(C);
581
582         if(U.flag & USER_ADD_EDITMODE)
583                 ED_object_enter_editmode(C, 0);
584         
585         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit);
586         
587         return OPERATOR_FINISHED;
588 }
589
590 void OBJECT_OT_text_add(wmOperatorType *ot)
591 {
592         /* identifiers */
593         ot->name= "Add Text";
594         ot->description = "Add a text object to the scene";
595         ot->idname= "OBJECT_OT_text_add";
596         
597         /* api callbacks */
598         ot->exec= object_add_text_exec;
599         ot->poll= ED_operator_scene_editable;
600         
601         /* flags */
602         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
603 }
604
605 static int object_armature_add_exec(bContext *C, wmOperator *op)
606 {
607         Object *obedit= CTX_data_edit_object(C);
608         View3D *v3d= CTX_wm_view3d(C);
609         RegionView3D *rv3d= NULL;
610         int newob= 0;
611         
612         if ((obedit==NULL) || (obedit->type != OB_ARMATURE)) {
613                 object_add_type(C, OB_ARMATURE);
614                 ED_object_enter_editmode(C, 0);
615                 newob = 1;
616         }
617         else DAG_id_flush_update(&obedit->id, OB_RECALC_DATA);
618         
619         if(v3d) 
620                 rv3d= CTX_wm_region(C)->regiondata;
621         
622         /* v3d and rv3d are allowed to be NULL */
623         add_primitive_bone(CTX_data_scene(C), v3d, rv3d);
624
625         /* userdef */
626         if (newob && (U.flag & USER_ADD_EDITMODE)==0) {
627                 ED_object_exit_editmode(C, EM_FREEDATA);
628         }
629         
630         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit);
631         
632         return OPERATOR_FINISHED;
633 }
634
635 void OBJECT_OT_armature_add(wmOperatorType *ot)
636 {       
637         /* identifiers */
638         ot->name= "Add Armature";
639         ot->description = "Add an armature object to the scene.";
640         ot->idname= "OBJECT_OT_armature_add";
641         
642         /* api callbacks */
643         ot->exec= object_armature_add_exec;
644         ot->poll= ED_operator_scene_editable;
645         
646         /* flags */
647         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
648 }
649
650 static int object_lamp_add_exec(bContext *C, wmOperator *op)
651 {
652         Object *ob;
653         int type= RNA_enum_get(op->ptr, "type");
654
655         ob= object_add_type(C, OB_LAMP);
656         if(ob && ob->data)
657                 ((Lamp*)ob->data)->type= type;
658         
659         return OPERATOR_FINISHED;
660 }
661
662 void OBJECT_OT_lamp_add(wmOperatorType *ot)
663 {       
664         static EnumPropertyItem lamp_type_items[] = {
665                 {LA_LOCAL, "POINT", ICON_LAMP_POINT, "Point", "Omnidirectional point light source."},
666                 {LA_SUN, "SUN", ICON_LAMP_SUN, "Sun", "Constant direction parallel ray light source."},
667                 {LA_SPOT, "SPOT", ICON_LAMP_SPOT, "Spot", "Directional cone light source."},
668                 {LA_HEMI, "HEMI", ICON_LAMP_HEMI, "Hemi", "180 degree constant light source."},
669                 {LA_AREA, "AREA", ICON_LAMP_AREA, "Area", "Directional area light source."},
670                 {0, NULL, 0, NULL, NULL}};
671
672         /* identifiers */
673         ot->name= "Add Lamp";
674         ot->description = "Add a lamp object to the scene.";
675         ot->idname= "OBJECT_OT_lamp_add";
676         
677         /* api callbacks */
678         ot->invoke= WM_menu_invoke;
679         ot->exec= object_lamp_add_exec;
680         ot->poll= ED_operator_scene_editable;
681         
682         /* flags */
683         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
684
685         /* properties */
686         RNA_def_enum(ot->srna, "type", lamp_type_items, 0, "Type", "");
687 }
688
689 static int object_primitive_add_invoke(bContext *C, wmOperator *op, wmEvent *event)
690 {
691         uiPopupMenu *pup= uiPupMenuBegin(C, "Add Object", 0);
692         uiLayout *layout= uiPupMenuLayout(pup);
693         
694         uiItemMenuEnumO(layout, "Mesh", ICON_OUTLINER_OB_MESH, "OBJECT_OT_mesh_add", "type");
695         uiItemMenuEnumO(layout, "Curve", ICON_OUTLINER_OB_CURVE, "OBJECT_OT_curve_add", "type");
696         uiItemMenuEnumO(layout, "Surface", ICON_OUTLINER_OB_SURFACE, "OBJECT_OT_surface_add", "type");
697         uiItemMenuEnumO(layout, "Metaball", ICON_OUTLINER_OB_META, "OBJECT_OT_metaball_add", "type");
698         uiItemO(layout, "Text", ICON_OUTLINER_OB_FONT, "OBJECT_OT_text_add");
699         uiItemS(layout);
700         uiItemO(layout, "Armature", ICON_OUTLINER_OB_ARMATURE, "OBJECT_OT_armature_add");
701         uiItemEnumO(layout, NULL, ICON_OUTLINER_OB_LATTICE, "OBJECT_OT_add", "type", OB_LATTICE);
702         uiItemEnumO(layout, NULL, ICON_OUTLINER_OB_EMPTY, "OBJECT_OT_add", "type", OB_EMPTY);
703         uiItemS(layout);
704         uiItemEnumO(layout, NULL, ICON_OUTLINER_OB_CAMERA, "OBJECT_OT_add", "type", OB_CAMERA);
705         uiItemMenuEnumO(layout, "Lamp", ICON_OUTLINER_OB_LAMP, "OBJECT_OT_lamp_add", "type");
706         uiItemS(layout);
707         uiItemMenuEnumO(layout, "Force Field", ICON_OUTLINER_OB_EMPTY, "OBJECT_OT_effector_add", "type");
708         uiItemS(layout);
709         uiItemMenuEnumO(layout, "Group Instance", ICON_OUTLINER_OB_EMPTY, "OBJECT_OT_group_instance_add", "type");
710         
711         uiPupMenuEnd(C, pup);
712         
713         /* this operator is only for a menu, not used further */
714         return OPERATOR_CANCELLED;
715 }
716
717 /* only used as menu */
718 void OBJECT_OT_primitive_add(wmOperatorType *ot)
719 {
720         /* identifiers */
721         ot->name= "Add Primitive";
722         ot->description = "Add a primitive object.";
723         ot->idname= "OBJECT_OT_primitive_add";
724         
725         /* api callbacks */
726         ot->invoke= object_primitive_add_invoke;
727         
728         ot->poll= ED_operator_scene_editable;
729         
730         /* flags */
731         ot->flag= 0;
732 }
733
734 /* add dupligroup */
735 static EnumPropertyItem *add_dupligroup_itemf(bContext *C, PointerRNA *ptr, int *free)
736 {
737         EnumPropertyItem *item= NULL, item_tmp;
738         int totitem= 0;
739         int i= 0;
740         Group *group;
741
742         if(C==NULL)
743                 return NULL;
744
745         memset(&item_tmp, 0, sizeof(item_tmp));
746
747         for(group= CTX_data_main(C)->group.first; group; group= group->id.next) {
748                 item_tmp.identifier= item_tmp.name= group->id.name+2;
749                 item_tmp.value= i++;
750                 RNA_enum_item_add(&item, &totitem, &item_tmp);
751         }
752
753         if(i>0) {
754                 *free= 1;
755                 return item;
756         }
757         else {
758                 return NULL;
759         }
760 }
761
762 static int group_instance_add_exec(bContext *C, wmOperator *op)
763 {
764         /* XXX, using an enum for library lookups is a bit dodgy */
765         Group *group= BLI_findlink(&CTX_data_main(C)->group, RNA_enum_get(op->ptr, "type"));
766
767         if(group) {
768                 Object *ob= object_add_type(C, OB_EMPTY);
769                 rename_id(&ob->id, group->id.name+2);
770                 ob->dup_group= group;
771                 ob->transflag |= OB_DUPLIGROUP;
772                 id_us_plus(&group->id);
773
774
775                 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
776
777                 return OPERATOR_FINISHED;
778         }
779
780         return OPERATOR_CANCELLED;
781 }
782
783 /* only used as menu */
784 void OBJECT_OT_group_instance_add(wmOperatorType *ot)
785 {
786         PropertyRNA *prop;
787         static EnumPropertyItem prop_group_dummy_types[] = {
788                 {0, NULL, 0, NULL, NULL}
789         };
790
791         /* identifiers */
792         ot->name= "Add Group Instance";
793         ot->description = "Add a dupligroup instance.";
794         ot->idname= "OBJECT_OT_group_instance_add";
795
796         /* api callbacks */
797         ot->exec= group_instance_add_exec;
798
799         ot->poll= ED_operator_scene_editable;
800
801         /* flags */
802         ot->flag= 0;
803
804         /* properties */
805         prop= RNA_def_enum(ot->srna, "type", prop_group_dummy_types, 0, "Type", "");
806         RNA_def_enum_funcs(prop, add_dupligroup_itemf);
807 }
808
809 /**************************** Delete Object *************************/
810
811 /* remove base from a specific scene */
812 /* note: now unlinks constraints as well */
813 void ED_base_object_free_and_unlink(Scene *scene, Base *base)
814 {
815         BLI_remlink(&scene->base, base);
816         free_libblock_us(&G.main->object, base->object);
817         if(scene->basact==base) scene->basact= NULL;
818         MEM_freeN(base);
819 }
820
821 static int object_delete_exec(bContext *C, wmOperator *op)
822 {
823         Scene *scene= CTX_data_scene(C);
824         int islamp= 0;
825         
826         if(CTX_data_edit_object(C)) 
827                 return OPERATOR_CANCELLED;
828         
829         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
830
831                 if(base->object->type==OB_LAMP) islamp= 1;
832                 
833                 /* remove from current scene only */
834                 ED_base_object_free_and_unlink(scene, base);
835         }
836         CTX_DATA_END;
837
838         if(islamp) reshadeall_displist(scene);  /* only frees displist */
839         
840         DAG_scene_sort(scene);
841         ED_anim_dag_flush_update(C);
842         
843         WM_event_add_notifier(C, NC_SCENE|ND_OB_ACTIVE, CTX_data_scene(C));
844         
845         return OPERATOR_FINISHED;
846 }
847
848 void OBJECT_OT_delete(wmOperatorType *ot)
849 {
850         /* identifiers */
851         ot->name= "Delete";
852         ot->description = "Delete selected objects.";
853         ot->idname= "OBJECT_OT_delete";
854         
855         /* api callbacks */
856         ot->invoke= WM_operator_confirm;
857         ot->exec= object_delete_exec;
858         ot->poll= ED_operator_scene_editable;
859         
860         /* flags */
861         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
862 }
863
864 /**************************** Copy Utilities ******************************/
865
866 static void copy_object__forwardModifierLinks(void *userData, Object *ob,
867                                               ID **idpoin)
868 {
869         /* this is copied from ID_NEW; it might be better to have a macro */
870         if(*idpoin && (*idpoin)->newid) *idpoin = (*idpoin)->newid;
871 }
872
873 /* after copying objects, copied data should get new pointers */
874 static void copy_object_set_idnew(bContext *C, int dupflag)
875 {
876         Object *ob;
877         Material *ma, *mao;
878         ID *id;
879 #if 0 // XXX old animation system
880         Ipo *ipo;
881         bActionStrip *strip;
882 #endif // XXX old animation system
883         int a;
884         
885         /* XXX check object pointers */
886         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
887                 ob= base->object;
888                 relink_constraints(&ob->constraints);
889                 if (ob->pose){
890                         bPoseChannel *chan;
891                         for (chan = ob->pose->chanbase.first; chan; chan=chan->next){
892                                 relink_constraints(&chan->constraints);
893                         }
894                 }
895                 modifiers_foreachIDLink(ob, copy_object__forwardModifierLinks, NULL);
896                 ID_NEW(ob->parent);
897                 ID_NEW(ob->track);
898                 ID_NEW(ob->proxy);
899                 ID_NEW(ob->proxy_group);
900                 
901 #if 0 // XXX old animation system
902                 for(strip= ob->nlastrips.first; strip; strip= strip->next) {
903                         bActionModifier *amod;
904                         for(amod= strip->modifiers.first; amod; amod= amod->next)
905                                 ID_NEW(amod->ob);
906                 }
907 #endif // XXX old animation system
908         }
909         CTX_DATA_END;
910         
911         /* materials */
912         if( dupflag & USER_DUP_MAT) {
913                 mao= G.main->mat.first;
914                 while(mao) {
915                         if(mao->id.newid) {
916                                 
917                                 ma= (Material *)mao->id.newid;
918                                 
919                                 if(dupflag & USER_DUP_TEX) {
920                                         for(a=0; a<MAX_MTEX; a++) {
921                                                 if(ma->mtex[a]) {
922                                                         id= (ID *)ma->mtex[a]->tex;
923                                                         if(id) {
924                                                                 ID_NEW_US(ma->mtex[a]->tex)
925                                                                 else ma->mtex[a]->tex= copy_texture(ma->mtex[a]->tex);
926                                                                 id->us--;
927                                                         }
928                                                 }
929                                         }
930                                 }
931 #if 0 // XXX old animation system
932                                 id= (ID *)ma->ipo;
933                                 if(id) {
934                                         ID_NEW_US(ma->ipo)
935                                         else ma->ipo= copy_ipo(ma->ipo);
936                                         id->us--;
937                                 }
938 #endif // XXX old animation system
939                         }
940                         mao= mao->id.next;
941                 }
942         }
943         
944 #if 0 // XXX old animation system
945         /* lamps */
946         if( dupflag & USER_DUP_IPO) {
947                 Lamp *la= G.main->lamp.first;
948                 while(la) {
949                         if(la->id.newid) {
950                                 Lamp *lan= (Lamp *)la->id.newid;
951                                 id= (ID *)lan->ipo;
952                                 if(id) {
953                                         ID_NEW_US(lan->ipo)
954                                         else lan->ipo= copy_ipo(lan->ipo);
955                                         id->us--;
956                                 }
957                         }
958                         la= la->id.next;
959                 }
960         }
961         
962         /* ipos */
963         ipo= G.main->ipo.first;
964         while(ipo) {
965                 if(ipo->id.lib==NULL && ipo->id.newid) {
966                         Ipo *ipon= (Ipo *)ipo->id.newid;
967                         IpoCurve *icu;
968                         for(icu= ipon->curve.first; icu; icu= icu->next) {
969                                 if(icu->driver) {
970                                         ID_NEW(icu->driver->ob);
971                                 }
972                         }
973                 }
974                 ipo= ipo->id.next;
975         }
976 #endif // XXX old animation system
977         
978         set_sca_new_poins();
979         
980         clear_id_newpoins();
981 }
982
983 /********************* Make Duplicates Real ************************/
984
985 static void make_object_duplilist_real(bContext *C, Scene *scene, Base *base)
986 {
987         Base *basen;
988         Object *ob;
989         ListBase *lb;
990         DupliObject *dob;
991         
992         if(!base && !(base = BASACT))
993                 return;
994         
995         if(!(base->object->transflag & OB_DUPLI))
996                 return;
997         
998         lb= object_duplilist(scene, base->object);
999         
1000         for(dob= lb->first; dob; dob= dob->next) {
1001                 ob= copy_object(dob->ob);
1002                 /* font duplis can have a totcol without material, we get them from parent
1003                 * should be implemented better...
1004                 */
1005                 if(ob->mat==NULL) ob->totcol= 0;
1006                 
1007                 basen= MEM_dupallocN(base);
1008                 basen->flag &= ~OB_FROMDUPLI;
1009                 BLI_addhead(&scene->base, basen);       /* addhead: othwise eternal loop */
1010                 basen->object= ob;
1011                 ob->ipo= NULL;          /* make sure apply works */
1012                 ob->parent= ob->track= NULL;
1013                 ob->disp.first= ob->disp.last= NULL;
1014                 ob->transflag &= ~OB_DUPLI;     
1015                 
1016                 Mat4CpyMat4(ob->obmat, dob->mat);
1017                 ED_object_apply_obmat(ob);
1018         }
1019         
1020         copy_object_set_idnew(C, 0);
1021         
1022         free_object_duplilist(lb);
1023         
1024         base->object->transflag &= ~OB_DUPLI;   
1025 }
1026
1027 static int object_duplicates_make_real_exec(bContext *C, wmOperator *op)
1028 {
1029         Scene *scene= CTX_data_scene(C);
1030         
1031         clear_id_newpoins();
1032                 
1033         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
1034                 make_object_duplilist_real(C, scene, base);
1035         }
1036         CTX_DATA_END;
1037
1038         DAG_scene_sort(scene);
1039         ED_anim_dag_flush_update(C);    
1040         WM_event_add_notifier(C, NC_SCENE, scene);
1041         
1042         return OPERATOR_FINISHED;
1043 }
1044
1045 void OBJECT_OT_duplicates_make_real(wmOperatorType *ot)
1046 {
1047         
1048         /* identifiers */
1049         ot->name= "Make Duplicates Real";
1050         ot->description = "Make dupli objects attached to this object real.";
1051         ot->idname= "OBJECT_OT_duplicates_make_real";
1052         
1053         /* api callbacks */
1054         ot->invoke= WM_operator_confirm;
1055         ot->exec= object_duplicates_make_real_exec;
1056         
1057         ot->poll= ED_operator_scene_editable;
1058         
1059         /* flags */
1060         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1061 }
1062
1063 /**************************** Convert **************************/
1064
1065 static EnumPropertyItem convert_target_items[]= {
1066         {OB_CURVE, "CURVE", 0, "Curve", ""},
1067         {OB_MESH, "MESH", 0, "Mesh", ""},
1068         {0, NULL, 0, NULL, NULL}};
1069
1070 static void curvetomesh(Scene *scene, Object *ob) 
1071 {
1072         Curve *cu= ob->data;
1073         
1074         if(cu->disp.first==0)
1075                 makeDispListCurveTypes(scene, ob, 0); /* force creation */
1076
1077         nurbs_to_mesh(ob); /* also does users */
1078
1079         if(ob->type == OB_MESH)
1080                 object_free_modifiers(ob);
1081 }
1082
1083 static int convert_poll(bContext *C)
1084 {
1085         Object *obact= CTX_data_active_object(C);
1086         Scene *scene= CTX_data_scene(C);
1087
1088         return (!scene->id.lib && obact && scene->obedit != obact && (obact->flag & SELECT));
1089 }
1090
1091 static int convert_exec(bContext *C, wmOperator *op)
1092 {
1093         Scene *scene= CTX_data_scene(C);
1094         Base *basen=NULL, *basact=NULL, *basedel=NULL;
1095         Object *ob, *ob1, *obact= CTX_data_active_object(C);
1096         DerivedMesh *dm;
1097         Curve *cu;
1098         Nurb *nu;
1099         MetaBall *mb;
1100         Mesh *me;
1101         int target= RNA_enum_get(op->ptr, "target");
1102         int keep_original= RNA_boolean_get(op->ptr, "keep_original");
1103         int a;
1104         
1105         /* don't forget multiple users! */
1106
1107         /* reset flags */
1108         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
1109                 ob= base->object;
1110                 ob->flag &= ~OB_DONE;
1111         }
1112         CTX_DATA_END;
1113
1114         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
1115                 ob= base->object;
1116                 
1117                 if(ob->flag & OB_DONE)
1118                         continue;
1119                 else if(ob->type==OB_MESH && ob->modifiers.first) { /* converting a mesh with no modifiers causes a segfault */
1120                         ob->flag |= OB_DONE;
1121                         basedel = base;
1122
1123                         ob1= copy_object(ob);
1124                         ob1->recalc |= OB_RECALC;
1125
1126                         basen= MEM_mallocN(sizeof(Base), "duplibase");
1127                         *basen= *base;
1128                         BLI_addhead(&scene->base, basen);       /* addhead: otherwise eternal loop */
1129                         basen->object= ob1;
1130                         basen->flag |= SELECT;
1131                         base->flag &= ~SELECT;
1132                         ob->flag &= ~SELECT;
1133
1134                         /* decrement original mesh's usage count  */
1135                         me= ob1->data;
1136                         me->id.us--;
1137
1138                         /* make a new copy of the mesh */
1139                         ob1->data= copy_mesh(me);
1140
1141                         /* make new mesh data from the original copy */
1142                         dm= mesh_get_derived_final(scene, ob1, CD_MASK_MESH);
1143                         /* dm= mesh_create_derived_no_deform(ob1, NULL);        this was called original (instead of get_derived). man o man why! (ton) */
1144                         
1145                         DM_to_mesh(dm, ob1->data);
1146
1147                         dm->release(dm);
1148                         object_free_modifiers(ob1);     /* after derivedmesh calls! */
1149                 }
1150                 else if(ob->type==OB_FONT) {
1151                         ob->flag |= OB_DONE;
1152
1153                         ob->type= OB_CURVE;
1154                         cu= ob->data;
1155
1156                         if(cu->vfont) {
1157                                 cu->vfont->id.us--;
1158                                 cu->vfont= 0;
1159                         }
1160                         if(cu->vfontb) {
1161                                 cu->vfontb->id.us--;
1162                                 cu->vfontb= 0;
1163                         }
1164                         if(cu->vfonti) {
1165                                 cu->vfonti->id.us--;
1166                                 cu->vfonti= 0;
1167                         }
1168                         if(cu->vfontbi) {
1169                                 cu->vfontbi->id.us--;
1170                                 cu->vfontbi= 0;
1171                         }                                       
1172                         /* other users */
1173                         if(cu->id.us>1) {
1174                                 for(ob1= G.main->object.first; ob1; ob1=ob1->id.next) {
1175                                         if(ob1->data==cu) {
1176                                                 ob1->type= OB_CURVE;
1177                                                 ob1->recalc |= OB_RECALC;
1178                                         }
1179                                 }
1180                         }
1181
1182                         for(nu=cu->nurb.first; nu; nu=nu->next)
1183                                 nu->charidx= 0;
1184
1185                         if(target == OB_MESH)
1186                                 curvetomesh(scene, ob);
1187                 }
1188                 else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
1189                         ob->flag |= OB_DONE;
1190
1191                         if(target == OB_MESH)
1192                                 curvetomesh(scene, ob);
1193                 }
1194                 else if(ob->type==OB_MBALL) {
1195                         ob= find_basis_mball(scene, ob);
1196                         
1197                         if(ob->disp.first && !(ob->flag & OB_DONE)) {
1198                                 ob->flag |= OB_DONE;
1199                                 basedel = base;
1200
1201                                 ob1= copy_object(ob);
1202                                 ob1->recalc |= OB_RECALC;
1203
1204                                 basen= MEM_mallocN(sizeof(Base), "duplibase");
1205                                 *basen= *base;
1206                                 BLI_addhead(&scene->base, basen);       /* addhead: otherwise eternal loop */
1207                                 basen->object= ob1;
1208                                 basen->flag |= SELECT;
1209                                 basedel->flag &= ~SELECT;
1210                                 ob->flag &= ~SELECT;
1211                                 
1212                                 mb= ob1->data;
1213                                 mb->id.us--;
1214                                 
1215                                 ob1->data= add_mesh("Mesh");
1216                                 ob1->type= OB_MESH;
1217                                 
1218                                 me= ob1->data;
1219                                 me->totcol= mb->totcol;
1220                                 if(ob1->totcol) {
1221                                         me->mat= MEM_dupallocN(mb->mat);
1222                                         for(a=0; a<ob1->totcol; a++) id_us_plus((ID *)me->mat[a]);
1223                                 }
1224                                 
1225                                 mball_to_mesh(&ob->disp, ob1->data);
1226                                 
1227                                 /* So we can see the wireframe */
1228                                 BASACT= basen; // XXX hm
1229                         }
1230                         else
1231                                 continue;
1232                 }
1233                 else
1234                         continue;
1235
1236                 /* If the original object is active then make this object active */
1237                 if(basen) {
1238                         if(ob == obact) {
1239                                 ED_base_object_activate(C, basen);
1240                                 basact = basen;
1241                         }
1242
1243                         basen= NULL;
1244                 }
1245
1246                 /* delete original if needed */
1247                 if(basedel) {
1248                         if(!keep_original)
1249                                 ED_base_object_free_and_unlink(scene, basedel); 
1250
1251                         basedel = NULL;
1252                 }
1253         }
1254         CTX_DATA_END;
1255         
1256         /* delete object should renew depsgraph */
1257         if(!keep_original)
1258                 DAG_scene_sort(scene);
1259
1260         /* texspace and normals */
1261         if(!basen) BASACT= NULL; // XXX base;
1262
1263 // XXX  ED_object_enter_editmode(C, 0);
1264 // XXX  exit_editmode(C, EM_FREEDATA|EM_WAITCURSOR); /* freedata, but no undo */
1265         BASACT= basact;
1266
1267         DAG_scene_sort(scene);
1268         WM_event_add_notifier(C, NC_SCENE, scene);
1269
1270         return OPERATOR_FINISHED;
1271 }
1272
1273 static int convert_invoke(bContext *C, wmOperator *op, wmEvent *event)
1274 {
1275         Object *obact= CTX_data_active_object(C);
1276         uiPopupMenu *pup;
1277         uiLayout *layout;
1278         char *title;
1279
1280         if(obact->type==OB_FONT) {
1281                 pup= uiPupMenuBegin(C, "Convert Font to", 0);
1282                 layout= uiPupMenuLayout(pup);
1283
1284                 uiItemEnumO(layout, "Curve", 0, op->type->idname, "target", OB_CURVE);
1285         }
1286         else {
1287                 if(obact->type == OB_MBALL)
1288                         title= "Convert Metaball to";
1289                 else if(obact->type == OB_CURVE)
1290                         title= "Convert Curve to";
1291                 else if(obact->type == OB_SURF)
1292                         title= "Convert Nurbs Surface to";
1293                 else if(obact->type == OB_MESH)
1294                         title= "Convert Modifiers to";
1295                 else
1296                         return OPERATOR_CANCELLED;
1297
1298                 pup= uiPupMenuBegin(C, title, 0);
1299                 layout= uiPupMenuLayout(pup);
1300         }
1301
1302         uiItemBooleanO(layout, "Mesh (keep original)", 0, op->type->idname, "keep_original", 1);
1303         uiItemBooleanO(layout, "Mesh (delete original)", 0, op->type->idname, "keep_original", 0);
1304
1305         uiPupMenuEnd(C, pup);
1306
1307         return OPERATOR_CANCELLED;
1308 }
1309
1310 void OBJECT_OT_convert(wmOperatorType *ot)
1311 {
1312         /* identifiers */
1313         ot->name= "Convert";
1314         ot->description = "Convert selected objects to another type.";
1315         ot->idname= "OBJECT_OT_convert";
1316         
1317         /* api callbacks */
1318         ot->invoke= convert_invoke;
1319         ot->exec= convert_exec;
1320         ot->poll= convert_poll;
1321         
1322         /* flags */
1323         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1324
1325         /* properties */
1326         RNA_def_enum(ot->srna, "target", convert_target_items, OB_MESH, "Target", "Type of object to convert to.");
1327         RNA_def_boolean(ot->srna, "keep_original", 0, "Keep Original", "Keep original objects instead of replacing them.");
1328 }
1329
1330 /**************************** Duplicate ************************/
1331
1332 /* 
1333         dupflag: a flag made from constants declared in DNA_userdef_types.h
1334         The flag tells adduplicate() weather to copy data linked to the object, or to reference the existing data.
1335         U.dupflag for default operations or you can construct a flag as python does
1336         if the dupflag is 0 then no data will be copied (linked duplicate) */
1337
1338 /* used below, assumes id.new is correct */
1339 /* leaves selection of base/object unaltered */
1340 static Base *object_add_duplicate_internal(Scene *scene, Base *base, int dupflag)
1341 {
1342         Base *basen= NULL;
1343         Material ***matarar;
1344         Object *ob, *obn;
1345         ID *id;
1346         int a, didit;
1347
1348         ob= base->object;
1349         if(ob->mode & OB_MODE_POSE) {
1350                 ; /* nothing? */
1351         }
1352         else {
1353                 obn= copy_object(ob);
1354                 obn->recalc |= OB_RECALC;
1355                 
1356                 basen= MEM_mallocN(sizeof(Base), "duplibase");
1357                 *basen= *base;
1358                 BLI_addhead(&scene->base, basen);       /* addhead: prevent eternal loop */
1359                 basen->object= obn;
1360                 
1361                 if(basen->flag & OB_FROMGROUP) {
1362                         Group *group;
1363                         for(group= G.main->group.first; group; group= group->id.next) {
1364                                 if(object_in_group(ob, group))
1365                                         add_to_group(group, obn);
1366                         }
1367                         obn->flag |= OB_FROMGROUP; /* this flag is unset with copy_object() */
1368                 }
1369                 
1370                 /* duplicates using userflags */
1371 #if 0 // XXX old animation system                               
1372                 if(dupflag & USER_DUP_IPO) {
1373                         bConstraintChannel *chan;
1374                         id= (ID *)obn->ipo;
1375                         
1376                         if(id) {
1377                                 ID_NEW_US( obn->ipo)
1378                                 else obn->ipo= copy_ipo(obn->ipo);
1379                                 id->us--;
1380                         }
1381                         /* Handle constraint ipos */
1382                         for (chan=obn->constraintChannels.first; chan; chan=chan->next){
1383                                 id= (ID *)chan->ipo;
1384                                 if(id) {
1385                                         ID_NEW_US( chan->ipo)
1386                                         else chan->ipo= copy_ipo(chan->ipo);
1387                                         id->us--;
1388                                 }
1389                         }
1390                 }
1391                 if(dupflag & USER_DUP_ACT){ /* Not buttons in the UI to modify this, add later? */
1392                         id= (ID *)obn->action;
1393                         if (id){
1394                                 ID_NEW_US(obn->action)
1395                                 else{
1396                                         obn->action= copy_action(obn->action);
1397                                 }
1398                                 id->us--;
1399                         }
1400                 }
1401 #endif // XXX old animation system
1402                 if(dupflag & USER_DUP_MAT) {
1403                         for(a=0; a<obn->totcol; a++) {
1404                                 id= (ID *)obn->mat[a];
1405                                 if(id) {
1406                                         ID_NEW_US(obn->mat[a])
1407                                         else obn->mat[a]= copy_material(obn->mat[a]);
1408                                         id->us--;
1409                                 }
1410                         }
1411                 }
1412                 if(dupflag & USER_DUP_PSYS) {
1413                         ParticleSystem *psys;
1414                         for(psys=obn->particlesystem.first; psys; psys=psys->next) {
1415                                 id= (ID*) psys->part;
1416                                 if(id) {
1417                                         ID_NEW_US(psys->part)
1418                                         else psys->part= psys_copy_settings(psys->part);
1419                                         id->us--;
1420                                 }
1421                         }
1422                 }
1423                 
1424                 id= obn->data;
1425                 didit= 0;
1426                 
1427                 switch(obn->type) {
1428                         case OB_MESH:
1429                                 if(dupflag & USER_DUP_MESH) {
1430                                         ID_NEW_US2( obn->data )
1431                                         else {
1432                                                 obn->data= copy_mesh(obn->data);
1433                                                 
1434                                                 if(obn->fluidsimSettings) {
1435                                                         obn->fluidsimSettings->orgMesh = (Mesh *)obn->data;
1436                                                 }
1437                                                 
1438                                                 didit= 1;
1439                                         }
1440                                         id->us--;
1441                                 }
1442                                 break;
1443                         case OB_CURVE:
1444                                 if(dupflag & USER_DUP_CURVE) {
1445                                         ID_NEW_US2(obn->data )
1446                                         else {
1447                                                 obn->data= copy_curve(obn->data);
1448                                                 didit= 1;
1449                                         }
1450                                         id->us--;
1451                                 }
1452                                 break;
1453                         case OB_SURF:
1454                                 if(dupflag & USER_DUP_SURF) {
1455                                         ID_NEW_US2( obn->data )
1456                                         else {
1457                                                 obn->data= copy_curve(obn->data);
1458                                                 didit= 1;
1459                                         }
1460                                         id->us--;
1461                                 }
1462                                 break;
1463                         case OB_FONT:
1464                                 if(dupflag & USER_DUP_FONT) {
1465                                         ID_NEW_US2( obn->data )
1466                                         else {
1467                                                 obn->data= copy_curve(obn->data);
1468                                                 didit= 1;
1469                                         }
1470                                         id->us--;
1471                                 }
1472                                 break;
1473                         case OB_MBALL:
1474                                 if(dupflag & USER_DUP_MBALL) {
1475                                         ID_NEW_US2(obn->data )
1476                                         else {
1477                                                 obn->data= copy_mball(obn->data);
1478                                                 didit= 1;
1479                                         }
1480                                         id->us--;
1481                                 }
1482                                 break;
1483                         case OB_LAMP:
1484                                 if(dupflag & USER_DUP_LAMP) {
1485                                         ID_NEW_US2(obn->data )
1486                                         else obn->data= copy_lamp(obn->data);
1487                                         id->us--;
1488                                 }
1489                                 break;
1490                                 
1491                         case OB_ARMATURE:
1492                                 obn->recalc |= OB_RECALC_DATA;
1493                                 if(obn->pose) obn->pose->flag |= POSE_RECALC;
1494                                         
1495                                         if(dupflag & USER_DUP_ARM) {
1496                                                 ID_NEW_US2(obn->data )
1497                                                 else {
1498                                                         obn->data= copy_armature(obn->data);
1499                                                         armature_rebuild_pose(obn, obn->data);
1500                                                         didit= 1;
1501                                                 }
1502                                                 id->us--;
1503                                         }
1504                                                 
1505                                                 break;
1506                                 
1507                         case OB_LATTICE:
1508                                 if(dupflag!=0) {
1509                                         ID_NEW_US2(obn->data )
1510                                         else obn->data= copy_lattice(obn->data);
1511                                         id->us--;
1512                                 }
1513                                 break;
1514                         case OB_CAMERA:
1515                                 if(dupflag!=0) {
1516                                         ID_NEW_US2(obn->data )
1517                                         else obn->data= copy_camera(obn->data);
1518                                         id->us--;
1519                                 }
1520                                 break;
1521                 }
1522                 
1523                 if(dupflag & USER_DUP_MAT) {
1524                         matarar= give_matarar(obn);
1525                         if(didit && matarar) {
1526                                 for(a=0; a<obn->totcol; a++) {
1527                                         id= (ID *)(*matarar)[a];
1528                                         if(id) {
1529                                                 ID_NEW_US( (*matarar)[a] )
1530                                                 else (*matarar)[a]= copy_material((*matarar)[a]);
1531                                                 
1532                                                 id->us--;
1533                                         }
1534                                 }
1535                         }
1536                 }
1537         }
1538         return basen;
1539 }
1540
1541 /* single object duplicate, if dupflag==0, fully linked, else it uses the flags given */
1542 /* leaves selection of base/object unaltered */
1543 Base *ED_object_add_duplicate(Scene *scene, Base *base, int dupflag)
1544 {
1545         Base *basen;
1546
1547         clear_id_newpoins();
1548         clear_sca_new_poins();  /* sensor/contr/act */
1549         
1550         basen= object_add_duplicate_internal(scene, base, dupflag);
1551         
1552         DAG_scene_sort(scene);
1553         
1554         return basen;
1555 }
1556
1557 /* contextual operator dupli */
1558 static int duplicate_exec(bContext *C, wmOperator *op)
1559 {
1560         Scene *scene= CTX_data_scene(C);
1561         int linked= RNA_boolean_get(op->ptr, "linked");
1562         int dupflag= (linked)? 0: U.dupflag;
1563         
1564         clear_id_newpoins();
1565         clear_sca_new_poins();  /* sensor/contr/act */
1566         
1567         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
1568                 Base *basen= object_add_duplicate_internal(scene, base, dupflag);
1569                 
1570                 /* note that this is safe to do with this context iterator,
1571                    the list is made in advance */
1572                 ED_base_object_select(base, BA_DESELECT);
1573
1574                 /* new object becomes active */
1575                 if(BASACT==base)
1576                         ED_base_object_activate(C, basen);
1577                 
1578         }
1579         CTX_DATA_END;
1580
1581         copy_object_set_idnew(C, dupflag);
1582
1583         DAG_scene_sort(scene);
1584         ED_anim_dag_flush_update(C);    
1585
1586         WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene);
1587
1588         return OPERATOR_FINISHED;
1589 }
1590
1591 void OBJECT_OT_duplicate(wmOperatorType *ot)
1592 {
1593         /* identifiers */
1594         ot->name= "Duplicate";
1595         ot->description = "Duplicate selected objects.";
1596         ot->idname= "OBJECT_OT_duplicate";
1597         
1598         /* api callbacks */
1599         ot->exec= duplicate_exec;
1600         ot->poll= ED_operator_scene_editable;
1601         
1602         /* flags */
1603         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1604         
1605         /* to give to transform */
1606         RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data.");
1607         RNA_def_int(ot->srna, "mode", TFM_TRANSLATION, 0, INT_MAX, "Mode", "", 0, INT_MAX);
1608 }
1609
1610 /**************************** Join *************************/
1611
1612 static int join_exec(bContext *C, wmOperator *op)
1613 {
1614         Scene *scene= CTX_data_scene(C);
1615         Object *ob= CTX_data_active_object(C);
1616
1617         if(scene->obedit) {
1618                 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in editmode.");
1619                 return OPERATOR_CANCELLED;
1620         }
1621         else if(!ob) {
1622                 BKE_report(op->reports, RPT_ERROR, "Can't join unless there is an active object.");
1623                 return OPERATOR_CANCELLED;
1624         }
1625         else if(object_data_is_libdata(ob)) {
1626                 BKE_report(op->reports, RPT_ERROR, "Can't edit external libdata.");
1627                 return OPERATOR_CANCELLED;
1628         }
1629
1630         if(ob->type == OB_MESH)
1631                 return join_mesh_exec(C, op);
1632         else if(ELEM(ob->type, OB_CURVE, OB_SURF))
1633                 return join_curve_exec(C, op);
1634         else if(ob->type == OB_ARMATURE)
1635                 return join_armature_exec(C, op);
1636
1637         BKE_report(op->reports, RPT_ERROR, "This object type doesn't support joining.");
1638
1639         return OPERATOR_CANCELLED;
1640 }
1641
1642 void OBJECT_OT_join(wmOperatorType *ot)
1643 {
1644         /* identifiers */
1645         ot->name= "Join";
1646         ot->description = "Join selected objects into active object.";
1647         ot->idname= "OBJECT_OT_join";
1648         
1649         /* api callbacks */
1650         ot->exec= join_exec;
1651         ot->poll= ED_operator_scene_editable;
1652         
1653         /* flags */
1654         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1655 }
1656