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