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