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