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