47053ea6d9339d66b6be6fe9d2540cc63f8f3dac
[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 /* add dupligroup */
690 static EnumPropertyItem *add_dupligroup_itemf(bContext *C, PointerRNA *ptr, int *free)
691 {
692         EnumPropertyItem *item= NULL, item_tmp;
693         int totitem= 0;
694         int i= 0;
695         Group *group;
696
697         if(C==NULL)
698                 return NULL;
699
700         memset(&item_tmp, 0, sizeof(item_tmp));
701
702         for(group= CTX_data_main(C)->group.first; group; group= group->id.next) {
703                 item_tmp.identifier= item_tmp.name= group->id.name+2;
704                 item_tmp.value= i++;
705                 RNA_enum_item_add(&item, &totitem, &item_tmp);
706         }
707
708         if(i>0) {
709                 *free= 1;
710                 return item;
711         }
712         else {
713                 return NULL;
714         }
715 }
716
717 static int group_instance_add_exec(bContext *C, wmOperator *op)
718 {
719         /* XXX, using an enum for library lookups is a bit dodgy */
720         Group *group= BLI_findlink(&CTX_data_main(C)->group, RNA_enum_get(op->ptr, "type"));
721
722         if(group) {
723                 Object *ob= object_add_type(C, OB_EMPTY);
724                 rename_id(&ob->id, group->id.name+2);
725                 ob->dup_group= group;
726                 ob->transflag |= OB_DUPLIGROUP;
727                 id_us_plus(&group->id);
728
729
730                 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
731
732                 return OPERATOR_FINISHED;
733         }
734
735         return OPERATOR_CANCELLED;
736 }
737
738 /* only used as menu */
739 void OBJECT_OT_group_instance_add(wmOperatorType *ot)
740 {
741         PropertyRNA *prop;
742         static EnumPropertyItem prop_group_dummy_types[] = {
743                 {0, NULL, 0, NULL, NULL}
744         };
745
746         /* identifiers */
747         ot->name= "Add Group Instance";
748         ot->description = "Add a dupligroup instance.";
749         ot->idname= "OBJECT_OT_group_instance_add";
750
751         /* api callbacks */
752         ot->exec= group_instance_add_exec;
753
754         ot->poll= ED_operator_scene_editable;
755
756         /* flags */
757         ot->flag= 0;
758
759         /* properties */
760         prop= RNA_def_enum(ot->srna, "type", prop_group_dummy_types, 0, "Type", "");
761         RNA_def_enum_funcs(prop, add_dupligroup_itemf);
762 }
763
764 /**************************** Delete Object *************************/
765
766 /* remove base from a specific scene */
767 /* note: now unlinks constraints as well */
768 void ED_base_object_free_and_unlink(Scene *scene, Base *base)
769 {
770         BLI_remlink(&scene->base, base);
771         free_libblock_us(&G.main->object, base->object);
772         if(scene->basact==base) scene->basact= NULL;
773         MEM_freeN(base);
774 }
775
776 static int object_delete_exec(bContext *C, wmOperator *op)
777 {
778         Scene *scene= CTX_data_scene(C);
779         int islamp= 0;
780         
781         if(CTX_data_edit_object(C)) 
782                 return OPERATOR_CANCELLED;
783         
784         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
785
786                 if(base->object->type==OB_LAMP) islamp= 1;
787                 
788                 /* remove from current scene only */
789                 ED_base_object_free_and_unlink(scene, base);
790         }
791         CTX_DATA_END;
792
793         if(islamp) reshadeall_displist(scene);  /* only frees displist */
794         
795         DAG_scene_sort(scene);
796         ED_anim_dag_flush_update(C);
797         
798         WM_event_add_notifier(C, NC_SCENE|ND_OB_ACTIVE, CTX_data_scene(C));
799         
800         return OPERATOR_FINISHED;
801 }
802
803 void OBJECT_OT_delete(wmOperatorType *ot)
804 {
805         /* identifiers */
806         ot->name= "Delete";
807         ot->description = "Delete selected objects.";
808         ot->idname= "OBJECT_OT_delete";
809         
810         /* api callbacks */
811         ot->invoke= WM_operator_confirm;
812         ot->exec= object_delete_exec;
813         ot->poll= ED_operator_scene_editable;
814         
815         /* flags */
816         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
817 }
818
819 /**************************** Copy Utilities ******************************/
820
821 static void copy_object__forwardModifierLinks(void *userData, Object *ob,
822                                               ID **idpoin)
823 {
824         /* this is copied from ID_NEW; it might be better to have a macro */
825         if(*idpoin && (*idpoin)->newid) *idpoin = (*idpoin)->newid;
826 }
827
828 /* after copying objects, copied data should get new pointers */
829 static void copy_object_set_idnew(bContext *C, int dupflag)
830 {
831         Object *ob;
832         Material *ma, *mao;
833         ID *id;
834 #if 0 // XXX old animation system
835         Ipo *ipo;
836         bActionStrip *strip;
837 #endif // XXX old animation system
838         int a;
839         
840         /* XXX check object pointers */
841         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
842                 ob= base->object;
843                 relink_constraints(&ob->constraints);
844                 if (ob->pose){
845                         bPoseChannel *chan;
846                         for (chan = ob->pose->chanbase.first; chan; chan=chan->next){
847                                 relink_constraints(&chan->constraints);
848                         }
849                 }
850                 modifiers_foreachIDLink(ob, copy_object__forwardModifierLinks, NULL);
851                 ID_NEW(ob->parent);
852                 ID_NEW(ob->track);
853                 ID_NEW(ob->proxy);
854                 ID_NEW(ob->proxy_group);
855                 
856 #if 0 // XXX old animation system
857                 for(strip= ob->nlastrips.first; strip; strip= strip->next) {
858                         bActionModifier *amod;
859                         for(amod= strip->modifiers.first; amod; amod= amod->next)
860                                 ID_NEW(amod->ob);
861                 }
862 #endif // XXX old animation system
863         }
864         CTX_DATA_END;
865         
866         /* materials */
867         if( dupflag & USER_DUP_MAT) {
868                 mao= G.main->mat.first;
869                 while(mao) {
870                         if(mao->id.newid) {
871                                 
872                                 ma= (Material *)mao->id.newid;
873                                 
874                                 if(dupflag & USER_DUP_TEX) {
875                                         for(a=0; a<MAX_MTEX; a++) {
876                                                 if(ma->mtex[a]) {
877                                                         id= (ID *)ma->mtex[a]->tex;
878                                                         if(id) {
879                                                                 ID_NEW_US(ma->mtex[a]->tex)
880                                                                 else ma->mtex[a]->tex= copy_texture(ma->mtex[a]->tex);
881                                                                 id->us--;
882                                                         }
883                                                 }
884                                         }
885                                 }
886 #if 0 // XXX old animation system
887                                 id= (ID *)ma->ipo;
888                                 if(id) {
889                                         ID_NEW_US(ma->ipo)
890                                         else ma->ipo= copy_ipo(ma->ipo);
891                                         id->us--;
892                                 }
893 #endif // XXX old animation system
894                         }
895                         mao= mao->id.next;
896                 }
897         }
898         
899 #if 0 // XXX old animation system
900         /* lamps */
901         if( dupflag & USER_DUP_IPO) {
902                 Lamp *la= G.main->lamp.first;
903                 while(la) {
904                         if(la->id.newid) {
905                                 Lamp *lan= (Lamp *)la->id.newid;
906                                 id= (ID *)lan->ipo;
907                                 if(id) {
908                                         ID_NEW_US(lan->ipo)
909                                         else lan->ipo= copy_ipo(lan->ipo);
910                                         id->us--;
911                                 }
912                         }
913                         la= la->id.next;
914                 }
915         }
916         
917         /* ipos */
918         ipo= G.main->ipo.first;
919         while(ipo) {
920                 if(ipo->id.lib==NULL && ipo->id.newid) {
921                         Ipo *ipon= (Ipo *)ipo->id.newid;
922                         IpoCurve *icu;
923                         for(icu= ipon->curve.first; icu; icu= icu->next) {
924                                 if(icu->driver) {
925                                         ID_NEW(icu->driver->ob);
926                                 }
927                         }
928                 }
929                 ipo= ipo->id.next;
930         }
931 #endif // XXX old animation system
932         
933         set_sca_new_poins();
934         
935         clear_id_newpoins();
936 }
937
938 /********************* Make Duplicates Real ************************/
939
940 static void make_object_duplilist_real(bContext *C, Scene *scene, Base *base)
941 {
942         Base *basen;
943         Object *ob;
944         ListBase *lb;
945         DupliObject *dob;
946         
947         if(!base && !(base = BASACT))
948                 return;
949         
950         if(!(base->object->transflag & OB_DUPLI))
951                 return;
952         
953         lb= object_duplilist(scene, base->object);
954         
955         for(dob= lb->first; dob; dob= dob->next) {
956                 ob= copy_object(dob->ob);
957                 /* font duplis can have a totcol without material, we get them from parent
958                 * should be implemented better...
959                 */
960                 if(ob->mat==NULL) ob->totcol= 0;
961                 
962                 basen= MEM_dupallocN(base);
963                 basen->flag &= ~OB_FROMDUPLI;
964                 BLI_addhead(&scene->base, basen);       /* addhead: othwise eternal loop */
965                 basen->object= ob;
966                 ob->ipo= NULL;          /* make sure apply works */
967                 ob->parent= ob->track= NULL;
968                 ob->disp.first= ob->disp.last= NULL;
969                 ob->transflag &= ~OB_DUPLI;     
970                 
971                 Mat4CpyMat4(ob->obmat, dob->mat);
972                 ED_object_apply_obmat(ob);
973         }
974         
975         copy_object_set_idnew(C, 0);
976         
977         free_object_duplilist(lb);
978         
979         base->object->transflag &= ~OB_DUPLI;   
980 }
981
982 static int object_duplicates_make_real_exec(bContext *C, wmOperator *op)
983 {
984         Scene *scene= CTX_data_scene(C);
985         
986         clear_id_newpoins();
987                 
988         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
989                 make_object_duplilist_real(C, scene, base);
990         }
991         CTX_DATA_END;
992
993         DAG_scene_sort(scene);
994         ED_anim_dag_flush_update(C);    
995         WM_event_add_notifier(C, NC_SCENE, scene);
996         
997         return OPERATOR_FINISHED;
998 }
999
1000 void OBJECT_OT_duplicates_make_real(wmOperatorType *ot)
1001 {
1002         
1003         /* identifiers */
1004         ot->name= "Make Duplicates Real";
1005         ot->description = "Make dupli objects attached to this object real.";
1006         ot->idname= "OBJECT_OT_duplicates_make_real";
1007         
1008         /* api callbacks */
1009         ot->invoke= WM_operator_confirm;
1010         ot->exec= object_duplicates_make_real_exec;
1011         
1012         ot->poll= ED_operator_scene_editable;
1013         
1014         /* flags */
1015         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1016 }
1017
1018 /**************************** Convert **************************/
1019
1020 static EnumPropertyItem convert_target_items[]= {
1021         {OB_CURVE, "CURVE", 0, "Curve", ""},
1022         {OB_MESH, "MESH", 0, "Mesh", ""},
1023         {0, NULL, 0, NULL, NULL}};
1024
1025 static void curvetomesh(Scene *scene, Object *ob) 
1026 {
1027         Curve *cu= ob->data;
1028         
1029         if(cu->disp.first==0)
1030                 makeDispListCurveTypes(scene, ob, 0); /* force creation */
1031
1032         nurbs_to_mesh(ob); /* also does users */
1033
1034         if(ob->type == OB_MESH)
1035                 object_free_modifiers(ob);
1036 }
1037
1038 static void meshtocurve(Scene *scene, Object *ob)
1039 {
1040         mesh_to_curve(scene, ob);
1041
1042         if(ob->type == OB_CURVE)
1043                 object_free_modifiers(ob);
1044 }
1045
1046 static int convert_poll(bContext *C)
1047 {
1048         Object *obact= CTX_data_active_object(C);
1049         Scene *scene= CTX_data_scene(C);
1050
1051         return (!scene->id.lib && obact && scene->obedit != obact && (obact->flag & SELECT));
1052 }
1053
1054 static int convert_exec(bContext *C, wmOperator *op)
1055 {
1056         Scene *scene= CTX_data_scene(C);
1057         Base *basen=NULL, *basact=NULL, *basedel=NULL;
1058         Object *ob, *ob1, *obact= CTX_data_active_object(C);
1059         DerivedMesh *dm;
1060         Curve *cu;
1061         Nurb *nu;
1062         MetaBall *mb;
1063         Mesh *me;
1064         int target= RNA_enum_get(op->ptr, "target");
1065         int keep_original= RNA_boolean_get(op->ptr, "keep_original");
1066         int a;
1067         
1068         /* don't forget multiple users! */
1069
1070         /* reset flags */
1071         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
1072                 ob= base->object;
1073                 ob->flag &= ~OB_DONE;
1074         }
1075         CTX_DATA_END;
1076
1077         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
1078                 ob= base->object;
1079                 
1080                 if(ob->flag & OB_DONE)
1081                         continue;
1082                 else if (ob->type==OB_MESH && target == OB_CURVE) {
1083                         ob->flag |= OB_DONE;
1084                         meshtocurve(scene, ob);
1085                         ob->recalc |= OB_RECALC;
1086                 }
1087                 else if(ob->type==OB_MESH && ob->modifiers.first) { /* converting a mesh with no modifiers causes a segfault */
1088                         ob->flag |= OB_DONE;
1089                         basedel = base;
1090
1091                         ob1= copy_object(ob);
1092                         ob1->recalc |= OB_RECALC;
1093
1094                         basen= MEM_mallocN(sizeof(Base), "duplibase");
1095                         *basen= *base;
1096                         BLI_addhead(&scene->base, basen);       /* addhead: otherwise eternal loop */
1097                         basen->object= ob1;
1098                         basen->flag |= SELECT;
1099                         base->flag &= ~SELECT;
1100                         ob->flag &= ~SELECT;
1101
1102                         /* decrement original mesh's usage count  */
1103                         me= ob1->data;
1104                         me->id.us--;
1105
1106                         /* make a new copy of the mesh */
1107                         ob1->data= copy_mesh(me);
1108
1109                         /* make new mesh data from the original copy */
1110                         dm= mesh_get_derived_final(scene, ob1, CD_MASK_MESH);
1111                         /* dm= mesh_create_derived_no_deform(ob1, NULL);        this was called original (instead of get_derived). man o man why! (ton) */
1112
1113                         DM_to_mesh(dm, ob1->data);
1114
1115                         dm->release(dm);
1116                         object_free_modifiers(ob1);     /* after derivedmesh calls! */
1117                 }
1118                 else if(ob->type==OB_FONT) {
1119                         ob->flag |= OB_DONE;
1120
1121                         ob->type= OB_CURVE;
1122                         cu= ob->data;
1123
1124                         if(cu->vfont) {
1125                                 cu->vfont->id.us--;
1126                                 cu->vfont= 0;
1127                         }
1128                         if(cu->vfontb) {
1129                                 cu->vfontb->id.us--;
1130                                 cu->vfontb= 0;
1131                         }
1132                         if(cu->vfonti) {
1133                                 cu->vfonti->id.us--;
1134                                 cu->vfonti= 0;
1135                         }
1136                         if(cu->vfontbi) {
1137                                 cu->vfontbi->id.us--;
1138                                 cu->vfontbi= 0;
1139                         }                                       
1140                         /* other users */
1141                         if(cu->id.us>1) {
1142                                 for(ob1= G.main->object.first; ob1; ob1=ob1->id.next) {
1143                                         if(ob1->data==cu) {
1144                                                 ob1->type= OB_CURVE;
1145                                                 ob1->recalc |= OB_RECALC;
1146                                         }
1147                                 }
1148                         }
1149
1150                         for(nu=cu->nurb.first; nu; nu=nu->next)
1151                                 nu->charidx= 0;
1152
1153                         if(target == OB_MESH)
1154                                 curvetomesh(scene, ob);
1155                 }
1156                 else if(ELEM(ob->type, OB_CURVE, OB_SURF)) {
1157                         ob->flag |= OB_DONE;
1158
1159                         if(target == OB_MESH)
1160                                 curvetomesh(scene, ob);
1161                 }
1162                 else if(ob->type==OB_MBALL) {
1163                         ob= find_basis_mball(scene, ob);
1164                         
1165                         if(ob->disp.first && !(ob->flag & OB_DONE)) {
1166                                 ob->flag |= OB_DONE;
1167                                 basedel = base;
1168
1169                                 ob1= copy_object(ob);
1170                                 ob1->recalc |= OB_RECALC;
1171
1172                                 basen= MEM_mallocN(sizeof(Base), "duplibase");
1173                                 *basen= *base;
1174                                 BLI_addhead(&scene->base, basen);       /* addhead: otherwise eternal loop */
1175                                 basen->object= ob1;
1176                                 basen->flag |= SELECT;
1177                                 basedel->flag &= ~SELECT;
1178                                 ob->flag &= ~SELECT;
1179                                 
1180                                 mb= ob1->data;
1181                                 mb->id.us--;
1182                                 
1183                                 ob1->data= add_mesh("Mesh");
1184                                 ob1->type= OB_MESH;
1185                                 
1186                                 me= ob1->data;
1187                                 me->totcol= mb->totcol;
1188                                 if(ob1->totcol) {
1189                                         me->mat= MEM_dupallocN(mb->mat);
1190                                         for(a=0; a<ob1->totcol; a++) id_us_plus((ID *)me->mat[a]);
1191                                 }
1192                                 
1193                                 mball_to_mesh(&ob->disp, ob1->data);
1194                                 
1195                                 /* So we can see the wireframe */
1196                                 BASACT= basen; // XXX hm
1197                         }
1198                         else
1199                                 continue;
1200                 }
1201                 else
1202                         continue;
1203
1204                 /* If the original object is active then make this object active */
1205                 if(basen) {
1206                         if(ob == obact) {
1207                                 ED_base_object_activate(C, basen);
1208                                 basact = basen;
1209                         }
1210
1211                         basen= NULL;
1212                 }
1213
1214                 /* delete original if needed */
1215                 if(basedel) {
1216                         if(!keep_original)
1217                                 ED_base_object_free_and_unlink(scene, basedel); 
1218
1219                         basedel = NULL;
1220                 }
1221         }
1222         CTX_DATA_END;
1223         
1224         /* delete object should renew depsgraph */
1225         if(!keep_original)
1226                 DAG_scene_sort(scene);
1227
1228         /* texspace and normals */
1229         if(!basen) BASACT= NULL; // XXX base;
1230
1231 // XXX  ED_object_enter_editmode(C, 0);
1232 // XXX  exit_editmode(C, EM_FREEDATA|EM_WAITCURSOR); /* freedata, but no undo */
1233         BASACT= basact;
1234
1235         DAG_scene_sort(scene);
1236         WM_event_add_notifier(C, NC_SCENE, scene);
1237
1238         return OPERATOR_FINISHED;
1239 }
1240
1241 static int convert_invoke(bContext *C, wmOperator *op, wmEvent *event)
1242 {
1243         Object *obact= CTX_data_active_object(C);
1244         uiPopupMenu *pup;
1245         uiLayout *layout;
1246         char *title;
1247
1248         if(obact->type==OB_FONT) {
1249                 pup= uiPupMenuBegin(C, "Convert Font to", 0);
1250                 layout= uiPupMenuLayout(pup);
1251
1252                 uiItemEnumO(layout, "Curve", 0, op->type->idname, "target", OB_CURVE);
1253         }
1254         else {
1255                 if(obact->type == OB_MBALL)
1256                         title= "Convert Metaball to";
1257                 else if(obact->type == OB_CURVE)
1258                         title= "Convert Curve to";
1259                 else if(obact->type == OB_SURF)
1260                         title= "Convert Nurbs Surface to";
1261                 else if(obact->type == OB_MESH)
1262                         title= "Convert Modifiers to";
1263                 else
1264                         return OPERATOR_CANCELLED;
1265
1266                 pup= uiPupMenuBegin(C, title, 0);
1267                 layout= uiPupMenuLayout(pup);
1268         }
1269
1270         uiItemBooleanO(layout, "Mesh (keep original)", 0, op->type->idname, "keep_original", 1);
1271         uiItemBooleanO(layout, "Mesh (delete original)", 0, op->type->idname, "keep_original", 0);
1272
1273         uiPupMenuEnd(C, pup);
1274
1275         return OPERATOR_CANCELLED;
1276 }
1277
1278 void OBJECT_OT_convert(wmOperatorType *ot)
1279 {
1280         /* identifiers */
1281         ot->name= "Convert";
1282         ot->description = "Convert selected objects to another type.";
1283         ot->idname= "OBJECT_OT_convert";
1284         
1285         /* api callbacks */
1286         ot->invoke= convert_invoke;
1287         ot->exec= convert_exec;
1288         ot->poll= convert_poll;
1289         
1290         /* flags */
1291         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1292
1293         /* properties */
1294         RNA_def_enum(ot->srna, "target", convert_target_items, OB_MESH, "Target", "Type of object to convert to.");
1295         RNA_def_boolean(ot->srna, "keep_original", 0, "Keep Original", "Keep original objects instead of replacing them.");
1296 }
1297
1298 /**************************** Duplicate ************************/
1299
1300 /* 
1301         dupflag: a flag made from constants declared in DNA_userdef_types.h
1302         The flag tells adduplicate() weather to copy data linked to the object, or to reference the existing data.
1303         U.dupflag for default operations or you can construct a flag as python does
1304         if the dupflag is 0 then no data will be copied (linked duplicate) */
1305
1306 /* used below, assumes id.new is correct */
1307 /* leaves selection of base/object unaltered */
1308 static Base *object_add_duplicate_internal(Scene *scene, Base *base, int dupflag)
1309 {
1310         Base *basen= NULL;
1311         Material ***matarar;
1312         Object *ob, *obn;
1313         ID *id;
1314         int a, didit;
1315
1316         ob= base->object;
1317         if(ob->mode & OB_MODE_POSE) {
1318                 ; /* nothing? */
1319         }
1320         else {
1321                 obn= copy_object(ob);
1322                 obn->recalc |= OB_RECALC;
1323                 
1324                 basen= MEM_mallocN(sizeof(Base), "duplibase");
1325                 *basen= *base;
1326                 BLI_addhead(&scene->base, basen);       /* addhead: prevent eternal loop */
1327                 basen->object= obn;
1328                 
1329                 if(basen->flag & OB_FROMGROUP) {
1330                         Group *group;
1331                         for(group= G.main->group.first; group; group= group->id.next) {
1332                                 if(object_in_group(ob, group))
1333                                         add_to_group(group, obn);
1334                         }
1335                         obn->flag |= OB_FROMGROUP; /* this flag is unset with copy_object() */
1336                 }
1337                 
1338                 /* duplicates using userflags */
1339 #if 0 // XXX old animation system                               
1340                 if(dupflag & USER_DUP_IPO) {
1341                         bConstraintChannel *chan;
1342                         id= (ID *)obn->ipo;
1343                         
1344                         if(id) {
1345                                 ID_NEW_US( obn->ipo)
1346                                 else obn->ipo= copy_ipo(obn->ipo);
1347                                 id->us--;
1348                         }
1349                         /* Handle constraint ipos */
1350                         for (chan=obn->constraintChannels.first; chan; chan=chan->next){
1351                                 id= (ID *)chan->ipo;
1352                                 if(id) {
1353                                         ID_NEW_US( chan->ipo)
1354                                         else chan->ipo= copy_ipo(chan->ipo);
1355                                         id->us--;
1356                                 }
1357                         }
1358                 }
1359                 if(dupflag & USER_DUP_ACT){ /* Not buttons in the UI to modify this, add later? */
1360                         id= (ID *)obn->action;
1361                         if (id){
1362                                 ID_NEW_US(obn->action)
1363                                 else{
1364                                         obn->action= copy_action(obn->action);
1365                                 }
1366                                 id->us--;
1367                         }
1368                 }
1369 #endif // XXX old animation system
1370                 if(dupflag & USER_DUP_MAT) {
1371                         for(a=0; a<obn->totcol; a++) {
1372                                 id= (ID *)obn->mat[a];
1373                                 if(id) {
1374                                         ID_NEW_US(obn->mat[a])
1375                                         else obn->mat[a]= copy_material(obn->mat[a]);
1376                                         id->us--;
1377                                 }
1378                         }
1379                 }
1380                 if(dupflag & USER_DUP_PSYS) {
1381                         ParticleSystem *psys;
1382                         for(psys=obn->particlesystem.first; psys; psys=psys->next) {
1383                                 id= (ID*) psys->part;
1384                                 if(id) {
1385                                         ID_NEW_US(psys->part)
1386                                         else psys->part= psys_copy_settings(psys->part);
1387                                         id->us--;
1388                                 }
1389                         }
1390                 }
1391                 
1392                 id= obn->data;
1393                 didit= 0;
1394                 
1395                 switch(obn->type) {
1396                         case OB_MESH:
1397                                 if(dupflag & USER_DUP_MESH) {
1398                                         ID_NEW_US2( obn->data )
1399                                         else {
1400                                                 obn->data= copy_mesh(obn->data);
1401                                                 
1402                                                 if(obn->fluidsimSettings) {
1403                                                         obn->fluidsimSettings->orgMesh = (Mesh *)obn->data;
1404                                                 }
1405                                                 
1406                                                 didit= 1;
1407                                         }
1408                                         id->us--;
1409                                 }
1410                                 break;
1411                         case OB_CURVE:
1412                                 if(dupflag & USER_DUP_CURVE) {
1413                                         ID_NEW_US2(obn->data )
1414                                         else {
1415                                                 obn->data= copy_curve(obn->data);
1416                                                 didit= 1;
1417                                         }
1418                                         id->us--;
1419                                 }
1420                                 break;
1421                         case OB_SURF:
1422                                 if(dupflag & USER_DUP_SURF) {
1423                                         ID_NEW_US2( obn->data )
1424                                         else {
1425                                                 obn->data= copy_curve(obn->data);
1426                                                 didit= 1;
1427                                         }
1428                                         id->us--;
1429                                 }
1430                                 break;
1431                         case OB_FONT:
1432                                 if(dupflag & USER_DUP_FONT) {
1433                                         ID_NEW_US2( obn->data )
1434                                         else {
1435                                                 obn->data= copy_curve(obn->data);
1436                                                 didit= 1;
1437                                         }
1438                                         id->us--;
1439                                 }
1440                                 break;
1441                         case OB_MBALL:
1442                                 if(dupflag & USER_DUP_MBALL) {
1443                                         ID_NEW_US2(obn->data )
1444                                         else {
1445                                                 obn->data= copy_mball(obn->data);
1446                                                 didit= 1;
1447                                         }
1448                                         id->us--;
1449                                 }
1450                                 break;
1451                         case OB_LAMP:
1452                                 if(dupflag & USER_DUP_LAMP) {
1453                                         ID_NEW_US2(obn->data )
1454                                         else obn->data= copy_lamp(obn->data);
1455                                         id->us--;
1456                                 }
1457                                 break;
1458                                 
1459                         case OB_ARMATURE:
1460                                 obn->recalc |= OB_RECALC_DATA;
1461                                 if(obn->pose) obn->pose->flag |= POSE_RECALC;
1462                                         
1463                                         if(dupflag & USER_DUP_ARM) {
1464                                                 ID_NEW_US2(obn->data )
1465                                                 else {
1466                                                         obn->data= copy_armature(obn->data);
1467                                                         armature_rebuild_pose(obn, obn->data);
1468                                                         didit= 1;
1469                                                 }
1470                                                 id->us--;
1471                                         }
1472                                                 
1473                                                 break;
1474                                 
1475                         case OB_LATTICE:
1476                                 if(dupflag!=0) {
1477                                         ID_NEW_US2(obn->data )
1478                                         else obn->data= copy_lattice(obn->data);
1479                                         id->us--;
1480                                 }
1481                                 break;
1482                         case OB_CAMERA:
1483                                 if(dupflag!=0) {
1484                                         ID_NEW_US2(obn->data )
1485                                         else obn->data= copy_camera(obn->data);
1486                                         id->us--;
1487                                 }
1488                                 break;
1489                 }
1490                 
1491                 if(dupflag & USER_DUP_MAT) {
1492                         matarar= give_matarar(obn);
1493                         if(didit && matarar) {
1494                                 for(a=0; a<obn->totcol; a++) {
1495                                         id= (ID *)(*matarar)[a];
1496                                         if(id) {
1497                                                 ID_NEW_US( (*matarar)[a] )
1498                                                 else (*matarar)[a]= copy_material((*matarar)[a]);
1499                                                 
1500                                                 id->us--;
1501                                         }
1502                                 }
1503                         }
1504                 }
1505         }
1506         return basen;
1507 }
1508
1509 /* single object duplicate, if dupflag==0, fully linked, else it uses the flags given */
1510 /* leaves selection of base/object unaltered */
1511 Base *ED_object_add_duplicate(Scene *scene, Base *base, int dupflag)
1512 {
1513         Base *basen;
1514
1515         clear_id_newpoins();
1516         clear_sca_new_poins();  /* sensor/contr/act */
1517         
1518         basen= object_add_duplicate_internal(scene, base, dupflag);
1519         
1520         DAG_scene_sort(scene);
1521         
1522         return basen;
1523 }
1524
1525 /* contextual operator dupli */
1526 static int duplicate_exec(bContext *C, wmOperator *op)
1527 {
1528         Scene *scene= CTX_data_scene(C);
1529         int linked= RNA_boolean_get(op->ptr, "linked");
1530         int dupflag= (linked)? 0: U.dupflag;
1531         
1532         clear_id_newpoins();
1533         clear_sca_new_poins();  /* sensor/contr/act */
1534         
1535         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
1536                 Base *basen= object_add_duplicate_internal(scene, base, dupflag);
1537                 
1538                 /* note that this is safe to do with this context iterator,
1539                    the list is made in advance */
1540                 ED_base_object_select(base, BA_DESELECT);
1541
1542                 /* new object becomes active */
1543                 if(BASACT==base)
1544                         ED_base_object_activate(C, basen);
1545                 
1546         }
1547         CTX_DATA_END;
1548
1549         copy_object_set_idnew(C, dupflag);
1550
1551         DAG_scene_sort(scene);
1552         ED_anim_dag_flush_update(C);    
1553
1554         WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene);
1555
1556         return OPERATOR_FINISHED;
1557 }
1558
1559 void OBJECT_OT_duplicate(wmOperatorType *ot)
1560 {
1561         /* identifiers */
1562         ot->name= "Duplicate";
1563         ot->description = "Duplicate selected objects.";
1564         ot->idname= "OBJECT_OT_duplicate";
1565         
1566         /* api callbacks */
1567         ot->exec= duplicate_exec;
1568         ot->poll= ED_operator_scene_editable;
1569         
1570         /* flags */
1571         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1572         
1573         /* to give to transform */
1574         RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data.");
1575         RNA_def_int(ot->srna, "mode", TFM_TRANSLATION, 0, INT_MAX, "Mode", "", 0, INT_MAX);
1576 }
1577
1578 /**************************** Join *************************/
1579
1580 static int join_exec(bContext *C, wmOperator *op)
1581 {
1582         Scene *scene= CTX_data_scene(C);
1583         Object *ob= CTX_data_active_object(C);
1584
1585         if(scene->obedit) {
1586                 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in editmode.");
1587                 return OPERATOR_CANCELLED;
1588         }
1589         else if(!ob) {
1590                 BKE_report(op->reports, RPT_ERROR, "Can't join unless there is an active object.");
1591                 return OPERATOR_CANCELLED;
1592         }
1593         else if(object_data_is_libdata(ob)) {
1594                 BKE_report(op->reports, RPT_ERROR, "Can't edit external libdata.");
1595                 return OPERATOR_CANCELLED;
1596         }
1597
1598         if(ob->type == OB_MESH)
1599                 return join_mesh_exec(C, op);
1600         else if(ELEM(ob->type, OB_CURVE, OB_SURF))
1601                 return join_curve_exec(C, op);
1602         else if(ob->type == OB_ARMATURE)
1603                 return join_armature_exec(C, op);
1604
1605         BKE_report(op->reports, RPT_ERROR, "This object type doesn't support joining.");
1606
1607         return OPERATOR_CANCELLED;
1608 }
1609
1610 void OBJECT_OT_join(wmOperatorType *ot)
1611 {
1612         /* identifiers */
1613         ot->name= "Join";
1614         ot->description = "Join selected objects into active object.";
1615         ot->idname= "OBJECT_OT_join";
1616         
1617         /* api callbacks */
1618         ot->exec= join_exec;
1619         ot->poll= ED_operator_scene_editable;
1620         
1621         /* flags */
1622         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1623 }
1624