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