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