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