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