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