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