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