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