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