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