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