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