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