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