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