Remove direct displist creation from BKE_vfont_to_curve_ex()
[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_object_types.h"
47 #include "DNA_scene_types.h"
48 #include "DNA_speaker_types.h"
49 #include "DNA_vfont_types.h"
50 #include "DNA_actuator_types.h"
51
52 #include "BLI_utildefines.h"
53 #include "BLI_ghash.h"
54 #include "BLI_listbase.h"
55 #include "BLI_math.h"
56 #include "BLI_string.h"
57
58 #include "BLF_translation.h"
59
60 #include "BKE_anim.h"
61 #include "BKE_animsys.h"
62 #include "BKE_armature.h"
63 #include "BKE_camera.h"
64 #include "BKE_constraint.h"
65 #include "BKE_context.h"
66 #include "BKE_curve.h"
67 #include "BKE_depsgraph.h"
68 #include "BKE_DerivedMesh.h"
69 #include "BKE_displist.h"
70 #include "BKE_effect.h"
71 #include "BKE_font.h"
72 #include "BKE_group.h"
73 #include "BKE_image.h"
74 #include "BKE_lamp.h"
75 #include "BKE_lattice.h"
76 #include "BKE_library.h"
77 #include "BKE_key.h"
78 #include "BKE_main.h"
79 #include "BKE_material.h"
80 #include "BKE_mball.h"
81 #include "BKE_mesh.h"
82 #include "BKE_modifier.h"
83 #include "BKE_nla.h"
84 #include "BKE_object.h"
85 #include "BKE_particle.h"
86 #include "BKE_report.h"
87 #include "BKE_sca.h"
88 #include "BKE_scene.h"
89 #include "BKE_speaker.h"
90 #include "BKE_texture.h"
91
92 #include "RNA_access.h"
93 #include "RNA_define.h"
94 #include "RNA_enum_types.h"
95
96 #include "WM_api.h"
97 #include "WM_types.h"
98
99 #include "ED_armature.h"
100 #include "ED_curve.h"
101 #include "ED_mball.h"
102 #include "ED_mesh.h"
103 #include "ED_node.h"
104 #include "ED_object.h"
105 #include "ED_physics.h"
106 #include "ED_render.h"
107 #include "ED_screen.h"
108 #include "ED_transform.h"
109 #include "ED_view3d.h"
110
111 #include "UI_interface.h"
112 #include "UI_resources.h"
113
114 #include "GPU_material.h"
115
116 #include "object_intern.h"
117
118 /* this is an exact copy of the define in rna_lamp.c
119  * kept here because of linking order.
120  * Icons are only defined here */
121 EnumPropertyItem lamp_type_items[] = {
122         {LA_LOCAL, "POINT", ICON_LAMP_POINT, "Point", "Omnidirectional point light source"},
123         {LA_SUN, "SUN", ICON_LAMP_SUN, "Sun", "Constant direction parallel ray light source"},
124         {LA_SPOT, "SPOT", ICON_LAMP_SPOT, "Spot", "Directional cone light source"},
125         {LA_HEMI, "HEMI", ICON_LAMP_HEMI, "Hemi", "180 degree constant light source"},
126         {LA_AREA, "AREA", ICON_LAMP_AREA, "Area", "Directional area light source"},
127         {0, NULL, 0, NULL, NULL}
128 };
129
130 /* copy from rna_object_force.c */
131 static EnumPropertyItem field_type_items[] = {
132         {PFIELD_FORCE, "FORCE", ICON_FORCE_FORCE, "Force", ""},
133         {PFIELD_WIND, "WIND", ICON_FORCE_WIND, "Wind", ""},
134         {PFIELD_VORTEX, "VORTEX", ICON_FORCE_VORTEX, "Vortex", ""},
135         {PFIELD_MAGNET, "MAGNET", ICON_FORCE_MAGNETIC, "Magnetic", ""},
136         {PFIELD_HARMONIC, "HARMONIC", ICON_FORCE_HARMONIC, "Harmonic", ""},
137         {PFIELD_CHARGE, "CHARGE", ICON_FORCE_CHARGE, "Charge", ""},
138         {PFIELD_LENNARDJ, "LENNARDJ", ICON_FORCE_LENNARDJONES, "Lennard-Jones", ""},
139         {PFIELD_TEXTURE, "TEXTURE", ICON_FORCE_TEXTURE, "Texture", ""},
140         {PFIELD_GUIDE, "GUIDE", ICON_FORCE_CURVE, "Curve Guide", ""},
141         {PFIELD_BOID, "BOID", ICON_FORCE_BOID, "Boid", ""},
142         {PFIELD_TURBULENCE, "TURBULENCE", ICON_FORCE_TURBULENCE, "Turbulence", ""},
143         {PFIELD_DRAG, "DRAG", ICON_FORCE_DRAG, "Drag", ""},
144         {PFIELD_SMOKEFLOW, "SMOKE", ICON_FORCE_SMOKEFLOW, "Smoke Flow", ""},
145         {0, NULL, 0, NULL, NULL}
146 };
147
148 /************************** Exported *****************************/
149
150 void ED_object_location_from_view(bContext *C, float loc[3])
151 {
152         View3D *v3d = CTX_wm_view3d(C);
153         Scene *scene = CTX_data_scene(C);
154         const float *cursor;
155
156         cursor = ED_view3d_cursor3d_get(scene, v3d);
157
158         copy_v3_v3(loc, cursor);
159 }
160
161 void ED_object_rotation_from_view(bContext *C, float rot[3])
162 {
163         RegionView3D *rv3d = CTX_wm_region_view3d(C);
164         if (rv3d) {
165                 float quat[4];
166                 copy_qt_qt(quat, rv3d->viewquat);
167                 quat[0] = -quat[0];
168                 quat_to_eul(rot, quat);
169         }
170         else {
171                 zero_v3(rot);
172         }
173 }
174
175 void ED_object_base_init_transform(bContext *C, Base *base, const float loc[3], const float rot[3])
176 {
177         Object *ob = base->object;
178         Scene *scene = CTX_data_scene(C);
179
180         if (!scene) return;
181
182         if (loc)
183                 copy_v3_v3(ob->loc, loc);
184
185         if (rot)
186                 copy_v3_v3(ob->rot, rot);
187
188         BKE_object_where_is_calc(scene, ob);
189 }
190
191 /* Uses context to figure out transform for primitive.
192  * Returns standard diameter. */
193 float ED_object_new_primitive_matrix(bContext *C, Object *obedit,
194                                      const float loc[3], const float rot[3], float primmat[4][4],
195                                      bool apply_diameter)
196 {
197         Scene *scene = CTX_data_scene(C);
198         View3D *v3d = CTX_wm_view3d(C);
199         float mat[3][3], rmat[3][3], cmat[3][3], imat[3][3];
200
201         unit_m4(primmat);
202
203         eul_to_mat3(rmat, rot);
204         invert_m3(rmat);
205
206         /* inverse transform for initial rotation and object */
207         copy_m3_m4(mat, obedit->obmat);
208         mul_m3_m3m3(cmat, rmat, mat);
209         invert_m3_m3(imat, cmat);
210         copy_m4_m3(primmat, imat);
211
212         /* center */
213         copy_v3_v3(primmat[3], loc);
214         sub_v3_v3v3(primmat[3], primmat[3], obedit->obmat[3]);
215         invert_m3_m3(imat, mat);
216         mul_m3_v3(imat, primmat[3]);
217
218         {
219                 const float dia = v3d ? ED_view3d_grid_scale(scene, v3d, NULL) : ED_scene_grid_scale(scene, NULL);
220
221                 if (apply_diameter) {
222                         primmat[0][0] *= dia;
223                         primmat[1][1] *= dia;
224                         primmat[2][2] *= dia;
225                 }
226
227                 return dia;
228         }
229
230         // return 1.0f;
231 }
232
233 /********************* Add Object Operator ********************/
234
235 static void view_align_update(struct Main *UNUSED(main), struct Scene *UNUSED(scene), struct PointerRNA *ptr)
236 {
237         RNA_struct_idprops_unset(ptr, "rotation");
238 }
239
240 void ED_object_add_unit_props(wmOperatorType *ot)
241 {
242         PropertyRNA *prop;
243
244         prop = RNA_def_float(ot->srna, "radius", 1.0f, 0.0, FLT_MAX, "Radius", "", 0.001, 100.00);
245         RNA_def_property_subtype(prop, PROP_DISTANCE);
246 }
247
248 void ED_object_add_generic_props(wmOperatorType *ot, bool do_editmode)
249 {
250         PropertyRNA *prop;
251
252         /* note: this property gets hidden for add-camera operator */
253         prop = RNA_def_boolean(ot->srna, "view_align", 0, "Align to View", "Align the new object to the view");
254         RNA_def_property_update_runtime(prop, view_align_update);
255
256         if (do_editmode) {
257                 prop = RNA_def_boolean(ot->srna, "enter_editmode", 0, "Enter Editmode",
258                                        "Enter editmode when adding this object");
259                 RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
260         }
261
262         prop = RNA_def_float_vector_xyz(ot->srna, "location", 3, NULL, -FLT_MAX, FLT_MAX, "Location",
263                                         "Location for the newly added object", -FLT_MAX, FLT_MAX);
264         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
265         prop = RNA_def_float_rotation(ot->srna, "rotation", 3, NULL, -FLT_MAX, FLT_MAX, "Rotation",
266                                       "Rotation for the newly added object", (float)-M_PI * 2.0f, (float)M_PI * 2.0f);
267         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
268
269         prop = RNA_def_boolean_layer_member(ot->srna, "layers", 20, NULL, "Layer", "");
270         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
271 }
272
273 bool ED_object_add_generic_get_opts(bContext *C, wmOperator *op, float loc[3], float rot[3],
274                                     bool *enter_editmode, unsigned int *layer, bool *is_view_aligned)
275 {
276         View3D *v3d = CTX_wm_view3d(C);
277         unsigned int _layer;
278
279         /* Switch to Edit mode? */
280         if (RNA_struct_find_property(op->ptr, "enter_editmode")) { /* optional */
281                 bool _enter_editmode;
282                 if (!enter_editmode)
283                         enter_editmode = &_enter_editmode;
284
285                 if (RNA_struct_property_is_set(op->ptr, "enter_editmode") && enter_editmode)
286                         *enter_editmode = RNA_boolean_get(op->ptr, "enter_editmode");
287                 else {
288                         *enter_editmode = (U.flag & USER_ADD_EDITMODE) != 0;
289                         RNA_boolean_set(op->ptr, "enter_editmode", *enter_editmode);
290                 }
291         }
292
293         /* Get layers! */
294         {
295                 int a, layer_values[20];
296                 if (!layer)
297                         layer = &_layer;
298
299                 if (RNA_struct_property_is_set(op->ptr, "layers")) {
300                         RNA_boolean_get_array(op->ptr, "layers", layer_values);
301                         *layer = 0;
302                         for (a = 0; a < 20; a++) {
303                                 if (layer_values[a])
304                                         *layer |= (1 << a);
305                         }
306                 }
307                 else {
308                         Scene *scene = CTX_data_scene(C);
309                         if (v3d)
310                                 *layer = (v3d->scenelock && !v3d->localvd) ? scene->layact : v3d->layact;
311                         else
312                                 *layer = scene->layact;
313                         for (a = 0; a < 20; a++) {
314                                 layer_values[a] = *layer & (1 << a);
315                         }
316                         RNA_boolean_set_array(op->ptr, "layers", layer_values);
317                 }
318
319                 /* in local view we additionally add local view layers,
320                  * not part of operator properties */
321                 if (v3d && v3d->localvd)
322                         *layer |= v3d->lay;
323         }
324
325         /* Location! */
326         {
327                 float _loc[3];
328                 if (!loc)
329                         loc = _loc;
330
331                 if (RNA_struct_property_is_set(op->ptr, "location")) {
332                         RNA_float_get_array(op->ptr, "location", loc);
333                 }
334                 else {
335                         ED_object_location_from_view(C, loc);
336                         RNA_float_set_array(op->ptr, "location", loc);
337                 }
338         }
339
340         /* Rotation! */
341         {
342                 bool _is_view_aligned;
343                 float _rot[3];
344                 if (!is_view_aligned)
345                         is_view_aligned = &_is_view_aligned;
346                 if (!rot)
347                         rot = _rot;
348
349                 if (RNA_struct_property_is_set(op->ptr, "rotation"))
350                         *is_view_aligned = false;
351                 else if (RNA_struct_property_is_set(op->ptr, "view_align"))
352                         *is_view_aligned = RNA_boolean_get(op->ptr, "view_align");
353                 else {
354                         *is_view_aligned = (U.flag & USER_ADD_VIEWALIGNED) != 0;
355                         RNA_boolean_set(op->ptr, "view_align", *is_view_aligned);
356                 }
357
358                 if (*is_view_aligned) {
359                         ED_object_rotation_from_view(C, rot);
360                         RNA_float_set_array(op->ptr, "rotation", rot);
361                 }
362                 else
363                         RNA_float_get_array(op->ptr, "rotation", rot);
364         }
365
366         if (layer && *layer == 0) {
367                 BKE_report(op->reports, RPT_ERROR, "Property 'layer' has no values set");
368                 return false;
369         }
370
371         return true;
372 }
373
374 /* For object add primitive operators.
375  * Do not call undo push in this function (users of this function have to). */
376 Object *ED_object_add_type(bContext *C, int type, const float loc[3], const float rot[3],
377                            bool enter_editmode, unsigned int layer)
378 {
379         Main *bmain = CTX_data_main(C);
380         Scene *scene = CTX_data_scene(C);
381         Object *ob;
382
383         /* for as long scene has editmode... */
384         if (CTX_data_edit_object(C)) 
385                 ED_object_editmode_exit(C, EM_FREEDATA | EM_FREEUNDO | EM_WAITCURSOR | EM_DO_UNDO);  /* freedata, and undo */
386
387         /* deselects all, sets scene->basact */
388         ob = BKE_object_add(bmain, scene, type);
389         BASACT->lay = ob->lay = layer;
390         /* editor level activate, notifiers */
391         ED_base_object_activate(C, BASACT);
392
393         /* more editor stuff */
394         ED_object_base_init_transform(C, BASACT, loc, rot);
395
396         /* Ignore collisions by default for non-mesh objects */
397         if (type != OB_MESH) {
398                 ob->body_type = OB_BODY_TYPE_NO_COLLISION;
399                 ob->gameflag &= ~(OB_SENSOR | OB_RIGID_BODY | OB_SOFT_BODY | OB_COLLISION | OB_CHARACTER | OB_OCCLUDER | OB_DYNAMIC | OB_NAVMESH); /* copied from rna_object.c */
400         }
401
402         DAG_id_type_tag(bmain, ID_OB);
403         DAG_relations_tag_update(bmain);
404         if (ob->data) {
405                 ED_render_id_flush_update(bmain, ob->data);
406         }
407
408         if (enter_editmode)
409                 ED_object_editmode_enter(C, EM_IGNORE_LAYER);
410
411         WM_event_add_notifier(C, NC_SCENE | ND_LAYER_CONTENT, scene);
412
413         return ob;
414 }
415
416 /* for object add operator */
417 static int object_add_exec(bContext *C, wmOperator *op)
418 {
419         bool enter_editmode;
420         unsigned int layer;
421         float loc[3], rot[3];
422
423         if (!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer, NULL))
424                 return OPERATOR_CANCELLED;
425
426         ED_object_add_type(C, RNA_enum_get(op->ptr, "type"), loc, rot, enter_editmode, layer);
427
428         return OPERATOR_FINISHED;
429 }
430
431 void OBJECT_OT_add(wmOperatorType *ot)
432 {
433         /* identifiers */
434         ot->name = "Add Object";
435         ot->description = "Add an object to the scene";
436         ot->idname = "OBJECT_OT_add";
437
438         /* api callbacks */
439         ot->exec = object_add_exec;
440         ot->poll = ED_operator_objectmode;
441
442         /* flags */
443         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
444
445         RNA_def_enum(ot->srna, "type", object_type_items, 0, "Type", "");
446
447         ED_object_add_generic_props(ot, TRUE);
448 }
449
450 /********************* Add Effector Operator ********************/
451
452 /* for object add operator */
453 static int effector_add_exec(bContext *C, wmOperator *op)
454 {
455         Object *ob;
456         int type;
457         bool enter_editmode;
458         unsigned int layer;
459         float loc[3], rot[3];
460         float mat[4][4];
461
462         if (!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer, NULL))
463                 return OPERATOR_CANCELLED;
464
465         type = RNA_enum_get(op->ptr, "type");
466
467         if (type == PFIELD_GUIDE) {
468                 ob = ED_object_add_type(C, OB_CURVE, loc, rot, FALSE, layer);
469                 if (!ob)
470                         return OPERATOR_CANCELLED;
471
472                 rename_id(&ob->id, CTX_DATA_(BLF_I18NCONTEXT_ID_OBJECT, "CurveGuide"));
473                 ((Curve *)ob->data)->flag |= CU_PATH | CU_3D;
474                 ED_object_editmode_enter(C, 0);
475                 ED_object_new_primitive_matrix(C, ob, loc, rot, mat, FALSE);
476                 BLI_addtail(object_editcurve_get(ob), add_nurbs_primitive(C, ob, mat, CU_NURBS | CU_PRIM_PATH, 1));
477                 if (!enter_editmode)
478                         ED_object_editmode_exit(C, EM_FREEDATA);
479         }
480         else {
481                 ob = ED_object_add_type(C, OB_EMPTY, loc, rot, FALSE, layer);
482                 if (!ob)
483                         return OPERATOR_CANCELLED;
484
485                 rename_id(&ob->id, CTX_DATA_(BLF_I18NCONTEXT_ID_OBJECT, "Field"));
486                 if (ELEM(type, PFIELD_WIND, PFIELD_VORTEX))
487                         ob->empty_drawtype = OB_SINGLE_ARROW;
488         }
489
490         ob->pd = object_add_collision_fields(type);
491
492         DAG_relations_tag_update(CTX_data_main(C));
493
494         return OPERATOR_FINISHED;
495 }
496
497 void OBJECT_OT_effector_add(wmOperatorType *ot)
498 {
499         /* identifiers */
500         ot->name = "Add Effector";
501         ot->description = "Add an empty object with a physics effector to the scene";
502         ot->idname = "OBJECT_OT_effector_add";
503
504         /* api callbacks */
505         ot->exec = effector_add_exec;
506         ot->poll = ED_operator_objectmode;
507
508         /* flags */
509         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
510
511         ot->prop = RNA_def_enum(ot->srna, "type", field_type_items, 0, "Type", "");
512
513         ED_object_add_generic_props(ot, TRUE);
514 }
515
516 /********************* Add Camera Operator ********************/
517
518 static int object_camera_add_exec(bContext *C, wmOperator *op)
519 {
520         View3D *v3d = CTX_wm_view3d(C);
521         Scene *scene = CTX_data_scene(C);
522         Object *ob;
523         bool enter_editmode;
524         unsigned int layer;
525         float loc[3], rot[3];
526
527         /* force view align for cameras */
528         RNA_boolean_set(op->ptr, "view_align", TRUE);
529
530         if (!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer, NULL))
531                 return OPERATOR_CANCELLED;
532
533         ob = ED_object_add_type(C, OB_CAMERA, loc, rot, FALSE, layer);
534
535         if (v3d) {
536                 if (v3d->camera == NULL)
537                         v3d->camera = ob;
538                 if (v3d->scenelock && scene->camera == NULL) {
539                         scene->camera = ob;
540                 }
541         }
542
543         return OPERATOR_FINISHED;
544 }
545
546 void OBJECT_OT_camera_add(wmOperatorType *ot)
547 {
548         PropertyRNA *prop;
549
550         /* identifiers */
551         ot->name = "Add Camera";
552         ot->description = "Add a camera object to the scene";
553         ot->idname = "OBJECT_OT_camera_add";
554
555         /* api callbacks */
556         ot->exec = object_camera_add_exec;
557         ot->poll = ED_operator_objectmode;
558
559         /* flags */
560         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
561
562         ED_object_add_generic_props(ot, TRUE);
563
564         /* hide this for cameras, default */
565         prop = RNA_struct_type_find_property(ot->srna, "view_align");
566         RNA_def_property_flag(prop, PROP_HIDDEN);
567 }
568
569
570 /********************* Add Metaball Operator ********************/
571
572 static int object_metaball_add_exec(bContext *C, wmOperator *op)
573 {
574         Object *obedit = CTX_data_edit_object(C);
575         bool newob = false;
576         bool enter_editmode;
577         unsigned int layer;
578         float loc[3], rot[3];
579         float mat[4][4];
580         float dia;
581
582         WM_operator_view3d_unit_defaults(C, op);
583         if (!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer, NULL))
584                 return OPERATOR_CANCELLED;
585
586         if (obedit == NULL || obedit->type != OB_MBALL) {
587                 obedit = ED_object_add_type(C, OB_MBALL, loc, rot, TRUE, layer);
588                 newob = true;
589         }
590         else {
591                 DAG_id_tag_update(&obedit->id, OB_RECALC_DATA);
592         }
593
594         ED_object_new_primitive_matrix(C, obedit, loc, rot, mat, false);
595         dia = RNA_float_get(op->ptr, "radius");
596
597         add_metaball_primitive(C, obedit, mat, dia, RNA_enum_get(op->ptr, "type"));
598
599         /* userdef */
600         if (newob && !enter_editmode) {
601                 ED_object_editmode_exit(C, EM_FREEDATA);
602         }
603
604         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, obedit);
605
606         return OPERATOR_FINISHED;
607 }
608
609 void OBJECT_OT_metaball_add(wmOperatorType *ot)
610 {
611         /* identifiers */
612         ot->name = "Add Metaball";
613         ot->description = "Add an metaball object to the scene";
614         ot->idname = "OBJECT_OT_metaball_add";
615
616         /* api callbacks */
617         ot->invoke = WM_menu_invoke;
618         ot->exec = object_metaball_add_exec;
619         ot->poll = ED_operator_scene_editable;
620
621         /* flags */
622         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
623
624         ot->prop = RNA_def_enum(ot->srna, "type", metaelem_type_items, 0, "Primitive", "");
625
626         ED_object_add_unit_props(ot);
627         ED_object_add_generic_props(ot, TRUE);
628 }
629
630 /********************* Add Text Operator ********************/
631
632 static int object_add_text_exec(bContext *C, wmOperator *op)
633 {
634         Object *obedit = CTX_data_edit_object(C);
635         bool enter_editmode;
636         unsigned int layer;
637         float loc[3], rot[3];
638
639         if (!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer, NULL))
640                 return OPERATOR_CANCELLED;
641
642         if (obedit && obedit->type == OB_FONT)
643                 return OPERATOR_CANCELLED;
644
645         obedit = ED_object_add_type(C, OB_FONT, loc, rot, enter_editmode, layer);
646
647         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, obedit);
648
649         return OPERATOR_FINISHED;
650 }
651
652 void OBJECT_OT_text_add(wmOperatorType *ot)
653 {
654         /* identifiers */
655         ot->name = "Add Text";
656         ot->description = "Add a text object to the scene";
657         ot->idname = "OBJECT_OT_text_add";
658
659         /* api callbacks */
660         ot->exec = object_add_text_exec;
661         ot->poll = ED_operator_objectmode;
662
663         /* flags */
664         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
665         ED_object_add_generic_props(ot, TRUE);
666 }
667
668 /********************* Add Armature Operator ********************/
669
670 static int object_armature_add_exec(bContext *C, wmOperator *op)
671 {
672         Object *obedit = CTX_data_edit_object(C);
673         RegionView3D *rv3d = CTX_wm_region_view3d(C);
674         bool newob = false;
675         bool enter_editmode;
676         unsigned int layer;
677         float loc[3], rot[3];
678         bool view_aligned = rv3d && (U.flag & USER_ADD_VIEWALIGNED);
679
680         if (!ED_object_add_generic_get_opts(C, op, loc, rot, &enter_editmode, &layer, NULL))
681                 return OPERATOR_CANCELLED;
682
683         if ((obedit == NULL) || (obedit->type != OB_ARMATURE)) {
684                 obedit = ED_object_add_type(C, OB_ARMATURE, loc, rot, TRUE, layer);
685                 ED_object_editmode_enter(C, 0);
686                 newob = true;
687         }
688         else {
689                 DAG_id_tag_update(&obedit->id, OB_RECALC_DATA);
690         }
691
692         if (obedit == NULL) {
693                 BKE_report(op->reports, RPT_ERROR, "Cannot create editmode armature");
694                 return OPERATOR_CANCELLED;
695         }
696
697         add_primitive_bone(obedit, view_aligned);
698
699         /* userdef */
700         if (newob && !enter_editmode)
701                 ED_object_editmode_exit(C, EM_FREEDATA);
702
703         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, obedit);
704
705         return OPERATOR_FINISHED;
706 }
707
708 void OBJECT_OT_armature_add(wmOperatorType *ot)
709 {
710         /* identifiers */
711         ot->name = "Add Armature";
712         ot->description = "Add an armature object to the scene";
713         ot->idname = "OBJECT_OT_armature_add";
714
715         /* api callbacks */
716         ot->exec = object_armature_add_exec;
717         ot->poll = ED_operator_objectmode;
718
719         /* flags */
720         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
721         ED_object_add_generic_props(ot, TRUE);
722 }
723
724 /********************* Add Empty Operator ********************/
725
726 static int object_empty_add_exec(bContext *C, wmOperator *op)
727 {
728         Object *ob;
729         int type = RNA_enum_get(op->ptr, "type");
730         unsigned int layer;
731         float loc[3], rot[3];
732
733         if (!ED_object_add_generic_get_opts(C, op, loc, rot, NULL, &layer, NULL))
734                 return OPERATOR_CANCELLED;
735
736         ob = ED_object_add_type(C, OB_EMPTY, loc, rot, FALSE, layer);
737         ob->empty_drawtype = type;
738
739         return OPERATOR_FINISHED;
740 }
741
742 void OBJECT_OT_empty_add(wmOperatorType *ot)
743 {
744         /* identifiers */
745         ot->name = "Add Empty";
746         ot->description = "Add an empty object to the scene";
747         ot->idname = "OBJECT_OT_empty_add";
748
749         /* api callbacks */
750         ot->invoke = WM_menu_invoke;
751         ot->exec = object_empty_add_exec;
752         ot->poll = ED_operator_objectmode;
753
754         /* flags */
755         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
756
757         /* properties */
758         ot->prop = RNA_def_enum(ot->srna, "type", object_empty_drawtype_items, 0, "Type", "");
759
760         ED_object_add_generic_props(ot, FALSE);
761 }
762
763 static int empty_drop_named_image_invoke(bContext *C, wmOperator *op, const wmEvent *event)
764 {
765         Base *base = NULL;
766         Image *ima = NULL;
767         Object *ob = NULL;
768
769         /* check image input variables */
770         if (RNA_struct_property_is_set(op->ptr, "filepath")) {
771                 char path[FILE_MAX];
772
773                 RNA_string_get(op->ptr, "filepath", path);
774                 ima = BKE_image_load_exists(path);
775         }
776         else if (RNA_struct_property_is_set(op->ptr, "name")) {
777                 char name[MAX_ID_NAME - 2];
778
779                 RNA_string_get(op->ptr, "name", name);
780                 ima = (Image *)BKE_libblock_find_name(ID_IM, name);
781         }
782
783         if (ima == NULL) {
784                 BKE_report(op->reports, RPT_ERROR, "Not an image");
785                 return OPERATOR_CANCELLED;
786         }
787
788         base = ED_view3d_give_base_under_cursor(C, event->mval);
789
790         /* if empty under cursor, then set object */
791         if (base && base->object->type == OB_EMPTY) {
792                 ob = base->object;
793                 WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, CTX_data_scene(C));
794         }
795         else {
796                 /* add new empty */
797                 unsigned int layer;
798                 float rot[3];
799
800                 if (!ED_object_add_generic_get_opts(C, op, NULL, rot, NULL, &layer, NULL))
801                         return OPERATOR_CANCELLED;
802
803                 ob = ED_object_add_type(C, OB_EMPTY, NULL, rot, FALSE, layer);
804
805                 /* add under the mouse */
806                 ED_object_location_from_view(C, ob->loc);
807                 ED_view3d_cursor3d_position(C, ob->loc, event->mval);
808         }
809
810         ob->empty_drawtype = OB_EMPTY_IMAGE;
811         ob->data = ima;
812
813         return OPERATOR_FINISHED;
814 }
815
816 void OBJECT_OT_drop_named_image(wmOperatorType *ot)
817 {
818         PropertyRNA *prop;
819
820         /* identifiers */
821         ot->name = "Add Empty Image/Drop Image To Empty";
822         ot->description = "Add an empty image type to scene with data";
823         ot->idname = "OBJECT_OT_drop_named_image";
824
825         /* api callbacks */
826         ot->invoke = empty_drop_named_image_invoke;
827         ot->poll = ED_operator_objectmode;
828
829         /* flags */
830         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
831
832         /* properties */
833         prop = RNA_def_string(ot->srna, "filepath", "", FILE_MAX, "Filepath", "Path to image file");
834         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
835         prop = RNA_def_string(ot->srna, "name", "", MAX_ID_NAME - 2, "Name", "Image name to assign");
836         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
837         ED_object_add_generic_props(ot, FALSE);
838 }
839
840 /********************* Add Lamp Operator ********************/
841
842 static const char *get_lamp_defname(int type)
843 {
844         switch (type) {
845                 case LA_LOCAL: return CTX_DATA_(BLF_I18NCONTEXT_ID_LAMP, "Point");
846                 case LA_SUN: return CTX_DATA_(BLF_I18NCONTEXT_ID_LAMP, "Sun");
847                 case LA_SPOT: return CTX_DATA_(BLF_I18NCONTEXT_ID_LAMP, "Spot");
848                 case LA_HEMI: return CTX_DATA_(BLF_I18NCONTEXT_ID_LAMP, "Hemi");
849                 case LA_AREA: return CTX_DATA_(BLF_I18NCONTEXT_ID_LAMP, "Area");
850                 default:
851                         return CTX_DATA_(BLF_I18NCONTEXT_ID_LAMP, "Lamp");
852         }
853 }
854
855 static int object_lamp_add_exec(bContext *C, wmOperator *op)
856 {
857         Scene *scene = CTX_data_scene(C);
858         Object *ob;
859         Lamp *la;
860         int type = RNA_enum_get(op->ptr, "type");
861         unsigned int layer;
862         float loc[3], rot[3];
863
864         if (!ED_object_add_generic_get_opts(C, op, loc, rot, NULL, &layer, NULL))
865                 return OPERATOR_CANCELLED;
866
867         ob = ED_object_add_type(C, OB_LAMP, loc, rot, FALSE, layer);
868         la = (Lamp *)ob->data;
869
870         la->type = type;
871         rename_id(&ob->id, get_lamp_defname(type));
872         rename_id(&la->id, get_lamp_defname(type));
873
874         if (BKE_scene_use_new_shading_nodes(scene)) {
875                 ED_node_shader_default(C, &la->id);
876                 la->use_nodes = TRUE;
877         }
878
879         return OPERATOR_FINISHED;
880 }
881
882 void OBJECT_OT_lamp_add(wmOperatorType *ot)
883 {
884         /* identifiers */
885         ot->name = "Add Lamp";
886         ot->description = "Add a lamp object to the scene";
887         ot->idname = "OBJECT_OT_lamp_add";
888
889         /* api callbacks */
890         ot->invoke = WM_menu_invoke;
891         ot->exec = object_lamp_add_exec;
892         ot->poll = ED_operator_objectmode;
893
894         /* flags */
895         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
896
897         /* properties */
898         ot->prop = RNA_def_enum(ot->srna, "type", lamp_type_items, 0, "Type", "");
899         RNA_def_property_translation_context(ot->prop, BLF_I18NCONTEXT_ID_LAMP);
900
901         ED_object_add_generic_props(ot, FALSE);
902 }
903
904 /********************* Add Group Instance Operator ********************/
905
906 static int group_instance_add_exec(bContext *C, wmOperator *op)
907 {
908         Group *group;
909         unsigned int layer;
910         float loc[3], rot[3];
911         
912         if (RNA_struct_property_is_set(op->ptr, "name")) {
913                 char name[MAX_ID_NAME - 2];
914                 
915                 RNA_string_get(op->ptr, "name", name);
916                 group = (Group *)BKE_libblock_find_name(ID_GR, name);
917                 
918                 if (0 == RNA_struct_property_is_set(op->ptr, "location")) {
919                         wmEvent *event = CTX_wm_window(C)->eventstate;
920                         ED_object_location_from_view(C, loc);
921                         ED_view3d_cursor3d_position(C, loc, event->mval);
922                         RNA_float_set_array(op->ptr, "location", loc);
923                 }
924         }
925         else
926                 group = BLI_findlink(&CTX_data_main(C)->group, RNA_enum_get(op->ptr, "group"));
927
928         if (!ED_object_add_generic_get_opts(C, op, loc, rot, NULL, &layer, NULL))
929                 return OPERATOR_CANCELLED;
930
931         if (group) {
932                 Main *bmain = CTX_data_main(C);
933                 Scene *scene = CTX_data_scene(C);
934                 Object *ob = ED_object_add_type(C, OB_EMPTY, loc, rot, FALSE, layer);
935                 rename_id(&ob->id, group->id.name + 2);
936                 ob->dup_group = group;
937                 ob->transflag |= OB_DUPLIGROUP;
938                 id_lib_extern(&group->id);
939
940                 /* works without this except if you try render right after, see: 22027 */
941                 DAG_relations_tag_update(bmain);
942
943                 WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
944
945                 return OPERATOR_FINISHED;
946         }
947
948         return OPERATOR_CANCELLED;
949 }
950
951 /* only used as menu */
952 void OBJECT_OT_group_instance_add(wmOperatorType *ot)
953 {
954         /* identifiers */
955         ot->name = "Add Group Instance";
956         ot->description = "Add a dupligroup instance";
957         ot->idname = "OBJECT_OT_group_instance_add";
958
959         /* api callbacks */
960         ot->invoke = WM_enum_search_invoke;
961         ot->exec = group_instance_add_exec;
962         ot->poll = ED_operator_objectmode;
963
964         /* flags */
965         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
966
967         /* properties */
968         RNA_def_string(ot->srna, "name", "Group", MAX_ID_NAME - 2, "Name", "Group name to add");
969         ot->prop = RNA_def_enum(ot->srna, "group", DummyRNA_NULL_items, 0, "Group", "");
970         RNA_def_enum_funcs(ot->prop, RNA_group_itemf);
971         ED_object_add_generic_props(ot, FALSE);
972 }
973
974 /********************* Add Speaker Operator ********************/
975
976 static int object_speaker_add_exec(bContext *C, wmOperator *op)
977 {
978         Object *ob;
979         unsigned int layer;
980         float loc[3], rot[3];
981         Scene *scene = CTX_data_scene(C);
982
983         if (!ED_object_add_generic_get_opts(C, op, loc, rot, NULL, &layer, NULL))
984                 return OPERATOR_CANCELLED;
985
986         ob = ED_object_add_type(C, OB_SPEAKER, loc, rot, FALSE, layer);
987
988         /* to make it easier to start using this immediately in NLA, a default sound clip is created
989          * ready to be moved around to retime the sound and/or make new sound clips
990          */
991         {
992                 /* create new data for NLA hierarchy */
993                 AnimData *adt = BKE_id_add_animdata(&ob->id);
994                 NlaTrack *nlt = add_nlatrack(adt, NULL);
995                 NlaStrip *strip = add_nla_soundstrip(CTX_data_scene(C), ob->data);
996                 strip->start = CFRA;
997                 strip->end += strip->start;
998
999                 /* hook them up */
1000                 BKE_nlatrack_add_strip(nlt, strip);
1001
1002                 /* auto-name the strip, and give the track an interesting name  */
1003                 BLI_strncpy(nlt->name, DATA_("SoundTrack"), sizeof(nlt->name));
1004                 BKE_nlastrip_validate_name(adt, strip);
1005
1006                 WM_event_add_notifier(C, NC_ANIMATION | ND_NLA | NA_EDITED, NULL);
1007         }
1008
1009         return OPERATOR_FINISHED;
1010 }
1011
1012 void OBJECT_OT_speaker_add(wmOperatorType *ot)
1013 {
1014         /* identifiers */
1015         ot->name = "Add Speaker";
1016         ot->description = "Add a speaker object to the scene";
1017         ot->idname = "OBJECT_OT_speaker_add";
1018
1019         /* api callbacks */
1020         ot->exec = object_speaker_add_exec;
1021         ot->poll = ED_operator_objectmode;
1022
1023         /* flags */
1024         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1025
1026         ED_object_add_generic_props(ot, TRUE);
1027 }
1028
1029 /**************************** Delete Object *************************/
1030
1031 static void object_delete_check_glsl_update(Object *ob)
1032 {
1033         /* some objects could affect on GLSL shading, make sure GLSL settings
1034          * are being tagged to be updated when object is removing from scene
1035          */
1036         if (ob->type == OB_LAMP) {
1037                 if (ob->gpulamp.first)
1038                         GPU_lamp_free(ob);
1039         }
1040 }
1041
1042 /* remove base from a specific scene */
1043 /* note: now unlinks constraints as well */
1044 void ED_base_object_free_and_unlink(Main *bmain, Scene *scene, Base *base)
1045 {
1046         DAG_id_type_tag(bmain, ID_OB);
1047         BKE_scene_base_unlink(scene, base);
1048         object_delete_check_glsl_update(base->object);
1049         BKE_libblock_free_us(&bmain->object, base->object);
1050         if (scene->basact == base) scene->basact = NULL;
1051         MEM_freeN(base);
1052 }
1053
1054 static int object_delete_exec(bContext *C, wmOperator *op)
1055 {
1056         Main *bmain = CTX_data_main(C);
1057         Scene *scene = CTX_data_scene(C);
1058         wmWindowManager *wm = CTX_wm_manager(C);
1059         wmWindow *win;
1060         const short use_global = RNA_boolean_get(op->ptr, "use_global");
1061         bool changed = false;
1062
1063         if (CTX_data_edit_object(C)) 
1064                 return OPERATOR_CANCELLED;
1065
1066         CTX_DATA_BEGIN (C, Base *, base, selected_bases)
1067         {
1068                 /* deselect object -- it could be used in other scenes */
1069                 base->object->flag &= ~SELECT;
1070
1071                 /* remove from current scene only */
1072                 ED_base_object_free_and_unlink(bmain, scene, base);
1073                 changed = true;
1074
1075                 if (use_global) {
1076                         Scene *scene_iter;
1077                         Base *base_other;
1078
1079                         for (scene_iter = bmain->scene.first; scene_iter; scene_iter = scene_iter->id.next) {
1080                                 if (scene_iter != scene && !(scene_iter->id.lib)) {
1081                                         base_other = BKE_scene_base_find(scene_iter, base->object);
1082                                         if (base_other) {
1083                                                 ED_base_object_free_and_unlink(bmain, scene_iter, base_other);
1084                                         }
1085                                 }
1086                         }
1087                 }
1088                 /* end global */
1089
1090         }
1091         CTX_DATA_END;
1092
1093         if (!changed)
1094                 return OPERATOR_CANCELLED;
1095
1096         /* delete has to handle all open scenes */
1097         BKE_main_id_flag_listbase(&bmain->scene, LIB_DOIT, 1);
1098         for (win = wm->windows.first; win; win = win->next) {
1099                 scene = win->screen->scene;
1100                 
1101                 if (scene->id.flag & LIB_DOIT) {
1102                         scene->id.flag &= ~LIB_DOIT;
1103                         
1104                         DAG_relations_tag_update(bmain);
1105
1106                         WM_event_add_notifier(C, NC_SCENE | ND_OB_ACTIVE, scene);
1107                         WM_event_add_notifier(C, NC_SCENE | ND_LAYER_CONTENT, scene);
1108                 }
1109         }
1110
1111         return OPERATOR_FINISHED;
1112 }
1113
1114 void OBJECT_OT_delete(wmOperatorType *ot)
1115 {
1116         /* identifiers */
1117         ot->name = "Delete";
1118         ot->description = "Delete selected objects";
1119         ot->idname = "OBJECT_OT_delete";
1120
1121         /* api callbacks */
1122         ot->invoke = WM_operator_confirm;
1123         ot->exec = object_delete_exec;
1124         ot->poll = ED_operator_objectmode;
1125
1126         /* flags */
1127         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1128
1129         RNA_def_boolean(ot->srna, "use_global", 0, "Delete Globally", "Remove object from all scenes");
1130 }
1131
1132 /**************************** Copy Utilities ******************************/
1133
1134 /* after copying objects, copied data should get new pointers */
1135 static void copy_object_set_idnew(bContext *C, int dupflag)
1136 {
1137         Main *bmain = CTX_data_main(C);
1138         Material *ma, *mao;
1139         ID *id;
1140         int a;
1141
1142         /* XXX check object pointers */
1143         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1144         {
1145                 BKE_object_relink(ob);
1146         }
1147         CTX_DATA_END;
1148
1149         /* materials */
1150         if (dupflag & USER_DUP_MAT) {
1151                 mao = bmain->mat.first;
1152                 while (mao) {
1153                         if (mao->id.newid) {
1154                                 ma = (Material *)mao->id.newid;
1155
1156                                 if (dupflag & USER_DUP_TEX) {
1157                                         for (a = 0; a < MAX_MTEX; a++) {
1158                                                 if (ma->mtex[a]) {
1159                                                         id = (ID *)ma->mtex[a]->tex;
1160                                                         if (id) {
1161                                                                 ID_NEW_US(ma->mtex[a]->tex)
1162                                                                 else
1163                                                                         ma->mtex[a]->tex = BKE_texture_copy(ma->mtex[a]->tex);
1164                                                                 id->us--;
1165                                                         }
1166                                                 }
1167                                         }
1168                                 }
1169 #if 0 // XXX old animation system
1170                                 id = (ID *)ma->ipo;
1171                                 if (id) {
1172                                         ID_NEW_US(ma->ipo)
1173                                         else
1174                                                 ma->ipo = copy_ipo(ma->ipo);
1175                                         id->us--;
1176                                 }
1177 #endif // XXX old animation system
1178                         }
1179                         mao = mao->id.next;
1180                 }
1181         }
1182
1183 #if 0 // XXX old animation system
1184           /* lamps */
1185         if (dupflag & USER_DUP_IPO) {
1186                 Lamp *la = bmain->lamp.first;
1187                 while (la) {
1188                         if (la->id.newid) {
1189                                 Lamp *lan = (Lamp *)la->id.newid;
1190                                 id = (ID *)lan->ipo;
1191                                 if (id) {
1192                                         ID_NEW_US(lan->ipo)
1193                                         else
1194                                                 lan->ipo = copy_ipo(lan->ipo);
1195                                         id->us--;
1196                                 }
1197                         }
1198                         la = la->id.next;
1199                 }
1200         }
1201
1202         /* ipos */
1203         ipo = bmain->ipo.first;
1204         while (ipo) {
1205                 if (ipo->id.lib == NULL && ipo->id.newid) {
1206                         Ipo *ipon = (Ipo *)ipo->id.newid;
1207                         IpoCurve *icu;
1208                         for (icu = ipon->curve.first; icu; icu = icu->next) {
1209                                 if (icu->driver) {
1210                                         ID_NEW(icu->driver->ob);
1211                                 }
1212                         }
1213                 }
1214                 ipo = ipo->id.next;
1215         }
1216 #endif // XXX old animation system
1217
1218         set_sca_new_poins();
1219
1220         BKE_main_id_clear_newpoins(bmain);
1221 }
1222
1223 /********************* Make Duplicates Real ************************/
1224
1225 static void make_object_duplilist_real(bContext *C, Scene *scene, Base *base,
1226                                        const short use_base_parent,
1227                                        const short use_hierarchy)
1228 {
1229         Main *bmain = CTX_data_main(C);
1230         ListBase *lb;
1231         DupliObject *dob;
1232         GHash *dupli_gh = NULL, *parent_gh = NULL;
1233         Object *object;
1234
1235         if (!(base->object->transflag & OB_DUPLI))
1236                 return;
1237
1238         lb = object_duplilist(bmain->eval_ctx, scene, base->object);
1239
1240         if (use_hierarchy || use_base_parent) {
1241                 dupli_gh = BLI_ghash_ptr_new("make_object_duplilist_real dupli_gh");
1242                 parent_gh = BLI_ghash_pair_new("make_object_duplilist_real parent_gh");
1243         }
1244
1245         for (dob = lb->first; dob; dob = dob->next) {
1246                 Base *basen;
1247                 Object *ob = BKE_object_copy(dob->ob);
1248
1249                 /* font duplis can have a totcol without material, we get them from parent
1250                  * should be implemented better...
1251                  */
1252                 if (ob->mat == NULL) ob->totcol = 0;
1253
1254                 basen = MEM_dupallocN(base);
1255                 basen->flag &= ~(OB_FROMDUPLI | OB_FROMGROUP);
1256                 ob->flag = basen->flag;
1257                 basen->lay = base->lay;
1258                 BLI_addhead(&scene->base, basen);   /* addhead: othwise eternal loop */
1259                 basen->object = ob;
1260
1261                 /* make sure apply works */
1262                 BKE_free_animdata(&ob->id);
1263                 ob->adt = NULL;
1264
1265                 ob->parent = NULL;
1266                 ob->constraints.first = ob->constraints.last = NULL;
1267                 ob->curve_cache = NULL;
1268                 ob->transflag &= ~OB_DUPLI;
1269                 ob->lay = base->lay;
1270
1271                 copy_m4_m4(ob->obmat, dob->mat);
1272                 BKE_object_apply_mat4(ob, ob->obmat, FALSE, FALSE);
1273
1274                 if (dupli_gh)
1275                         BLI_ghash_insert(dupli_gh, dob, ob);
1276                 if (parent_gh)
1277                         BLI_ghash_insert(parent_gh, BLI_ghashutil_pairalloc(dob->ob, SET_INT_IN_POINTER(dob->persistent_id[0])), ob);
1278         }
1279
1280         if (use_hierarchy) {
1281                 for (dob = lb->first; dob; dob = dob->next) {
1282                         /* original parents */
1283                         Object *ob_src =     dob->ob;
1284                         Object *ob_src_par = ob_src->parent;
1285
1286                         Object *ob_dst =     BLI_ghash_lookup(dupli_gh, dob);
1287                         Object *ob_dst_par = NULL;
1288
1289                         /* find parent that was also made real */
1290                         if (ob_src_par) {
1291                                 GHashPair *pair = BLI_ghashutil_pairalloc(ob_src_par, SET_INT_IN_POINTER(dob->persistent_id[0]));
1292                                 ob_dst_par = BLI_ghash_lookup(parent_gh, pair);
1293                                 BLI_ghashutil_pairfree(pair);
1294                         }
1295
1296                         if (ob_dst_par) {
1297                                 /* allow for all possible parent types */
1298                                 ob_dst->partype = ob_src->partype;
1299                                 BLI_strncpy(ob_dst->parsubstr, ob_src->parsubstr, sizeof(ob_dst->parsubstr));
1300                                 ob_dst->par1 = ob_src->par1;
1301                                 ob_dst->par2 = ob_src->par2;
1302                                 ob_dst->par3 = ob_src->par3;
1303
1304                                 copy_m4_m4(ob_dst->parentinv, ob_src->parentinv);
1305
1306                                 ob_dst->parent = ob_dst_par;
1307                         }
1308                         else if (use_base_parent) {
1309                                 ob_dst->parent = base->object;
1310                                 ob_dst->partype = PAROBJECT;
1311                         }
1312
1313                         if (ob_dst->parent) {
1314                                 invert_m4_m4(ob_dst->parentinv, dob->mat);
1315
1316                                 /* note, this may be the parent of other objects, but it should
1317                                  * still work out ok */
1318                                 BKE_object_apply_mat4(ob_dst, dob->mat, FALSE, TRUE);
1319
1320                                 /* to set ob_dst->orig and in case theres any other discrepicies */
1321                                 DAG_id_tag_update(&ob_dst->id, OB_RECALC_OB);
1322                         }
1323                 }
1324         }
1325         else if (use_base_parent) {
1326                 /* since we are ignoring the internal hierarchy - parent all to the
1327                  * base object */
1328                 for (dob = lb->first; dob; dob = dob->next) {
1329                         /* original parents */
1330                         Object *ob_dst = BLI_ghash_lookup(dupli_gh, dob);
1331
1332                         ob_dst->parent = base->object;
1333                         ob_dst->partype = PAROBJECT;
1334
1335                         /* similer to the code above, see comments */
1336                         invert_m4_m4(ob_dst->parentinv, dob->mat);
1337                         BKE_object_apply_mat4(ob_dst, dob->mat, FALSE, TRUE);
1338                         DAG_id_tag_update(&ob_dst->id, OB_RECALC_OB);
1339                 }
1340         }
1341
1342         /* The same how BKE_object_unlink detects which object proxies to clear. */
1343         if (base->object->transflag & OB_DUPLIGROUP && base->object->dup_group) {
1344                 for (object = bmain->object.first; object; object = object->id.next) {
1345                         if (object->proxy_group == base->object) {
1346                                 object->proxy = NULL;
1347                                 object->proxy_from = NULL;
1348                                 DAG_id_tag_update(&object->id, OB_RECALC_OB);
1349                         }
1350                 }
1351         }
1352
1353         if (dupli_gh)
1354                 BLI_ghash_free(dupli_gh, NULL, NULL);
1355         if (parent_gh)
1356                 BLI_ghash_free(parent_gh, BLI_ghashutil_pairfree, NULL);
1357
1358         copy_object_set_idnew(C, 0);
1359
1360         free_object_duplilist(lb);
1361
1362         base->object->transflag &= ~OB_DUPLI;
1363 }
1364
1365 static int object_duplicates_make_real_exec(bContext *C, wmOperator *op)
1366 {
1367         Main *bmain = CTX_data_main(C);
1368         Scene *scene = CTX_data_scene(C);
1369
1370         const short use_base_parent = RNA_boolean_get(op->ptr, "use_base_parent");
1371         const short use_hierarchy = RNA_boolean_get(op->ptr, "use_hierarchy");
1372
1373         BKE_main_id_clear_newpoins(bmain);
1374
1375         CTX_DATA_BEGIN (C, Base *, base, selected_editable_bases)
1376         {
1377                 make_object_duplilist_real(C, scene, base, use_base_parent, use_hierarchy);
1378
1379                 /* dependencies were changed */
1380                 WM_event_add_notifier(C, NC_OBJECT | ND_PARENT, base->object);
1381         }
1382         CTX_DATA_END;
1383
1384         DAG_relations_tag_update(bmain);
1385         WM_event_add_notifier(C, NC_SCENE, scene);
1386         WM_main_add_notifier(NC_OBJECT | ND_DRAW, NULL);
1387
1388         return OPERATOR_FINISHED;
1389 }
1390
1391 void OBJECT_OT_duplicates_make_real(wmOperatorType *ot)
1392 {
1393         /* identifiers */
1394         ot->name = "Make Duplicates Real";
1395         ot->description = "Make dupli objects attached to this object real";
1396         ot->idname = "OBJECT_OT_duplicates_make_real";
1397
1398         /* api callbacks */
1399         ot->exec = object_duplicates_make_real_exec;
1400
1401         ot->poll = ED_operator_objectmode;
1402
1403         /* flags */
1404         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1405
1406         RNA_def_boolean(ot->srna, "use_base_parent", 0, "Parent", "Parent newly created objects to the original duplicator");
1407         RNA_def_boolean(ot->srna, "use_hierarchy", 0, "Keep Hierarchy", "Maintain parent child relationships");
1408 }
1409
1410 /**************************** Convert **************************/
1411
1412 static EnumPropertyItem convert_target_items[] = {
1413         {OB_CURVE, "CURVE", ICON_OUTLINER_OB_CURVE, "Curve from Mesh/Text", ""},
1414         {OB_MESH, "MESH", ICON_OUTLINER_OB_MESH, "Mesh from Curve/Meta/Surf/Text", ""},
1415         {0, NULL, 0, NULL, NULL}
1416 };
1417
1418 static void curvetomesh(Object *ob) 
1419 {
1420         BLI_assert(ob->curve_cache != NULL);
1421
1422         BKE_mesh_from_nurbs(ob); /* also does users */
1423
1424         if (ob->type == OB_MESH)
1425                 BKE_object_free_modifiers(ob);
1426 }
1427
1428 static int convert_poll(bContext *C)
1429 {
1430         Object *obact = CTX_data_active_object(C);
1431         Scene *scene = CTX_data_scene(C);
1432
1433         return (!scene->id.lib && obact && scene->obedit != obact && (obact->flag & SELECT) && !(obact->id.lib));
1434 }
1435
1436 /* Helper for convert_exec */
1437 static Base *duplibase_for_convert(Scene *scene, Base *base, Object *ob)
1438 {
1439         Object *obn;
1440         Base *basen;
1441
1442         if (ob == NULL) {
1443                 ob = base->object;
1444         }
1445
1446         obn = BKE_object_copy(ob);
1447         DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1448
1449         basen = MEM_mallocN(sizeof(Base), "duplibase");
1450         *basen = *base;
1451         BLI_addhead(&scene->base, basen);   /* addhead: otherwise eternal loop */
1452         basen->object = obn;
1453         basen->flag |= SELECT;
1454         obn->flag |= SELECT;
1455         base->flag &= ~SELECT;
1456         ob->flag &= ~SELECT;
1457
1458         return basen;
1459 }
1460
1461 static int convert_exec(bContext *C, wmOperator *op)
1462 {
1463         Main *bmain = CTX_data_main(C);
1464         Scene *scene = CTX_data_scene(C);
1465         Base *basen = NULL, *basact = NULL;
1466         Object *ob, *ob1, *newob, *obact = CTX_data_active_object(C);
1467         DerivedMesh *dm;
1468         Curve *cu;
1469         Nurb *nu;
1470         MetaBall *mb;
1471         Mesh *me;
1472         const short target = RNA_enum_get(op->ptr, "target");
1473         const short keep_original = RNA_boolean_get(op->ptr, "keep_original");
1474         int a, mballConverted = 0;
1475
1476         /* don't forget multiple users! */
1477
1478         /* reset flags */
1479         CTX_DATA_BEGIN (C, Base *, base, selected_editable_bases)
1480         {
1481                 ob = base->object;
1482                 ob->flag &= ~OB_DONE;
1483
1484                 /* flag data thats not been edited (only needed for !keep_original) */
1485                 if (ob->data) {
1486                         ((ID *)ob->data)->flag |= LIB_DOIT;
1487                 }
1488         }
1489         CTX_DATA_END;
1490
1491         CTX_DATA_BEGIN (C, Base *, base, selected_editable_bases)
1492         {
1493                 ob = base->object;
1494
1495                 if (ob->flag & OB_DONE || !IS_TAGGED(ob->data)) {
1496                         if (ob->type != target) {
1497                                 base->flag &= ~SELECT;
1498                                 ob->flag &= ~SELECT;
1499                         }
1500
1501                         /* obdata already modified */
1502                         if (!IS_TAGGED(ob->data)) {
1503                                 /* When 2 objects with linked data are selected, converting both
1504                                  * would keep modifiers on all but the converted object [#26003] */
1505                                 if (ob->type == OB_MESH) {
1506                                         BKE_object_free_modifiers(ob);  /* after derivedmesh calls! */
1507                                 }
1508                         }
1509                 }
1510                 else if (ob->type == OB_MESH && target == OB_CURVE) {
1511                         ob->flag |= OB_DONE;
1512
1513                         if (keep_original) {
1514                                 basen = duplibase_for_convert(scene, base, NULL);
1515                                 newob = basen->object;
1516
1517                                 /* decrement original mesh's usage count  */
1518                                 me = newob->data;
1519                                 me->id.us--;
1520
1521                                 /* make a new copy of the mesh */
1522                                 newob->data = BKE_mesh_copy(me);
1523                         }
1524                         else {
1525                                 newob = ob;
1526                         }
1527
1528                         BKE_mesh_to_curve(scene, newob);
1529
1530                         if (newob->type == OB_CURVE) {
1531                                 BKE_object_free_modifiers(newob);   /* after derivedmesh calls! */
1532                                 ED_rigidbody_object_remove(scene, newob);
1533                         }
1534                 }
1535                 else if (ob->type == OB_MESH && ob->modifiers.first) { /* converting a mesh with no modifiers causes a segfault */
1536                         ob->flag |= OB_DONE;
1537
1538                         if (keep_original) {
1539                                 basen = duplibase_for_convert(scene, base, NULL);
1540                                 newob = basen->object;
1541
1542                                 /* decrement original mesh's usage count  */
1543                                 me = newob->data;
1544                                 me->id.us--;
1545
1546                                 /* make a new copy of the mesh */
1547                                 newob->data = BKE_mesh_copy(me);
1548                         }
1549                         else {
1550                                 newob = ob;
1551                                 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1552                         }
1553
1554                         /* make new mesh data from the original copy */
1555                         /* note: get the mesh from the original, not from the copy in some
1556                          * cases this doesnt give correct results (when MDEF is used for eg)
1557                          */
1558                         dm = mesh_get_derived_final(scene, newob, CD_MASK_MESH);
1559                         // dm = mesh_create_derived_no_deform(ob1, NULL);  /* this was called original (instead of get_derived). man o man why! (ton) */
1560
1561                         DM_to_mesh(dm, newob->data, newob, CD_MASK_MESH);
1562
1563                         /* re-tessellation is called by DM_to_mesh */
1564
1565                         dm->release(dm);
1566                         BKE_object_free_modifiers(newob);   /* after derivedmesh calls! */
1567                 }
1568                 else if (ob->type == OB_FONT) {
1569                         ob->flag |= OB_DONE;
1570
1571                         if (keep_original) {
1572                                 basen = duplibase_for_convert(scene, base, NULL);
1573                                 newob = basen->object;
1574
1575                                 /* decrement original curve's usage count  */
1576                                 ((Curve *)newob->data)->id.us--;
1577
1578                                 /* make a new copy of the curve */
1579                                 newob->data = BKE_curve_copy(ob->data);
1580                         }
1581                         else {
1582                                 newob = ob;
1583                         }
1584
1585                         cu = newob->data;
1586
1587                         /* TODO(sergey): Ideally DAG will create nurbs list for a curve data
1588                          *               datablock, but for until we've got granular update
1589                          *               lets take care by selves.
1590                          */
1591                         BKE_vfont_to_curve(bmain, newob, FO_EDIT);
1592
1593                         newob->type = OB_CURVE;
1594                         cu->type = OB_CURVE;
1595
1596                         if (cu->vfont) {
1597                                 cu->vfont->id.us--;
1598                                 cu->vfont = NULL;
1599                         }
1600                         if (cu->vfontb) {
1601                                 cu->vfontb->id.us--;
1602                                 cu->vfontb = NULL;
1603                         }
1604                         if (cu->vfonti) {
1605                                 cu->vfonti->id.us--;
1606                                 cu->vfonti = NULL;
1607                         }
1608                         if (cu->vfontbi) {
1609                                 cu->vfontbi->id.us--;
1610                                 cu->vfontbi = NULL;
1611                         }
1612
1613                         if (!keep_original) {
1614                                 /* other users */
1615                                 if (cu->id.us > 1) {
1616                                         for (ob1 = bmain->object.first; ob1; ob1 = ob1->id.next) {
1617                                                 if (ob1->data == ob->data) {
1618                                                         ob1->type = OB_CURVE;
1619                                                         DAG_id_tag_update(&ob1->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1620                                                 }
1621                                         }
1622                                 }
1623                         }
1624
1625                         for (nu = cu->nurb.first; nu; nu = nu->next)
1626                                 nu->charidx = 0;
1627
1628                         cu->flag &= ~CU_3D;
1629                         BKE_curve_curve_dimension_update(cu);
1630
1631                         if (target == OB_MESH) {
1632                                 curvetomesh(newob);
1633
1634                                 /* meshes doesn't use displist */
1635                                 BKE_object_free_curve_cache(newob);
1636                         }
1637                 }
1638                 else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
1639                         ob->flag |= OB_DONE;
1640
1641                         if (target == OB_MESH) {
1642                                 if (keep_original) {
1643                                         basen = duplibase_for_convert(scene, base, NULL);
1644                                         newob = basen->object;
1645
1646                                         /* decrement original curve's usage count  */
1647                                         ((Curve *)newob->data)->id.us--;
1648
1649                                         /* make a new copy of the curve */
1650                                         newob->data = BKE_curve_copy(ob->data);
1651                                 }
1652                                 else {
1653                                         newob = ob;
1654                                 }
1655
1656                                 curvetomesh(newob);
1657
1658                                 /* meshes doesn't use displist */
1659                                 BKE_object_free_curve_cache(newob);
1660                         }
1661                 }
1662                 else if (ob->type == OB_MBALL && target == OB_MESH) {
1663                         Object *baseob;
1664
1665                         base->flag &= ~SELECT;
1666                         ob->flag &= ~SELECT;
1667
1668                         baseob = BKE_mball_basis_find(scene, ob);
1669
1670                         if (ob != baseob) {
1671                                 /* if motherball is converting it would be marked as done later */
1672                                 ob->flag |= OB_DONE;
1673                         }
1674
1675                         if (!baseob->curve_cache || !baseob->curve_cache->disp.first) {
1676                                 BKE_displist_make_mball(bmain->eval_ctx, scene, baseob);
1677                         }
1678
1679                         if (!(baseob->flag & OB_DONE)) {
1680                                 baseob->flag |= OB_DONE;
1681
1682                                 basen = duplibase_for_convert(scene, base, baseob);
1683                                 newob = basen->object;
1684
1685                                 mb = newob->data;
1686                                 mb->id.us--;
1687
1688                                 newob->data = BKE_mesh_add(bmain, "Mesh");
1689                                 newob->type = OB_MESH;
1690
1691                                 me = newob->data;
1692                                 me->totcol = mb->totcol;
1693                                 if (newob->totcol) {
1694                                         me->mat = MEM_dupallocN(mb->mat);
1695                                         for (a = 0; a < newob->totcol; a++) id_us_plus((ID *)me->mat[a]);
1696                                 }
1697
1698                                 BKE_mesh_from_metaball(&baseob->curve_cache->disp, newob->data);
1699
1700                                 if (obact->type == OB_MBALL) {
1701                                         basact = basen;
1702                                 }
1703
1704                                 mballConverted = 1;
1705                         }
1706                 }
1707                 else {
1708                         continue;
1709                 }
1710
1711                 /* tag obdata if it was been changed */
1712
1713                 /* If the original object is active then make this object active */
1714                 if (basen) {
1715                         if (ob == obact) {
1716                                 /* store new active base to update BASACT */
1717                                 basact = basen;
1718                         }
1719
1720                         basen = NULL;
1721                 }
1722
1723                 if (!keep_original && (ob->flag & OB_DONE)) {
1724                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1725                         ((ID *)ob->data)->flag &= ~LIB_DOIT; /* flag not to convert this datablock again */
1726                 }
1727         }
1728         CTX_DATA_END;
1729
1730         if (!keep_original) {
1731                 if (mballConverted) {
1732                         Base *base = scene->base.first, *tmpbase;
1733                         while (base) {
1734                                 ob = base->object;
1735                                 tmpbase = base;
1736                                 base = base->next;
1737
1738                                 if (ob->type == OB_MBALL) {
1739                                         ED_base_object_free_and_unlink(bmain, scene, tmpbase);
1740                                 }
1741                         }
1742                 }
1743
1744                 /* delete object should renew depsgraph */
1745                 DAG_relations_tag_update(bmain);
1746         }
1747
1748 // XXX  ED_object_editmode_enter(C, 0);
1749 // XXX  exit_editmode(C, EM_FREEDATA|EM_WAITCURSOR); /* freedata, but no undo */
1750
1751         if (basact) {
1752                 /* active base was changed */
1753                 ED_base_object_activate(C, basact);
1754                 BASACT = basact;
1755         }
1756         else if (BASACT->object->flag & OB_DONE) {
1757                 WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, BASACT->object);
1758                 WM_event_add_notifier(C, NC_OBJECT | ND_DATA, BASACT->object);
1759         }
1760
1761         DAG_relations_tag_update(bmain);
1762         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, scene);
1763         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
1764
1765         return OPERATOR_FINISHED;
1766 }
1767
1768
1769 void OBJECT_OT_convert(wmOperatorType *ot)
1770 {
1771         /* identifiers */
1772         ot->name = "Convert to";
1773         ot->description = "Convert selected objects to another type";
1774         ot->idname = "OBJECT_OT_convert";
1775
1776         /* api callbacks */
1777         ot->invoke = WM_menu_invoke;
1778         ot->exec = convert_exec;
1779         ot->poll = convert_poll;
1780
1781         /* flags */
1782         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1783
1784         /* properties */
1785         ot->prop = RNA_def_enum(ot->srna, "target", convert_target_items, OB_MESH, "Target", "Type of object to convert to");
1786         RNA_def_boolean(ot->srna, "keep_original", 0, "Keep Original", "Keep original objects instead of replacing them");
1787 }
1788
1789 /**************************** Duplicate ************************/
1790
1791 /* 
1792  * dupflag: a flag made from constants declared in DNA_userdef_types.h
1793  * The flag tells adduplicate() whether to copy data linked to the object, or to reference the existing data.
1794  * U.dupflag for default operations or you can construct a flag as python does
1795  * if the dupflag is 0 then no data will be copied (linked duplicate) */
1796
1797 /* used below, assumes id.new is correct */
1798 /* leaves selection of base/object unaltered */
1799 static Base *object_add_duplicate_internal(Main *bmain, Scene *scene, Base *base, int dupflag)
1800 {
1801         Base *basen = NULL;
1802         Material ***matarar;
1803         Object *ob, *obn;
1804         ID *id;
1805         int a, didit;
1806
1807         ob = base->object;
1808         if (ob->mode & OB_MODE_POSE) {
1809                 ; /* nothing? */
1810         }
1811         else {
1812                 obn = BKE_object_copy(ob);
1813                 DAG_id_tag_update(&obn->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1814
1815                 basen = MEM_mallocN(sizeof(Base), "duplibase");
1816                 *basen = *base;
1817                 BLI_addhead(&scene->base, basen);   /* addhead: prevent eternal loop */
1818                 basen->object = obn;
1819
1820                 /* 1) duplis should end up in same group as the original
1821                  * 2) Rigid Body sim participants MUST always be part of a group...
1822                  */
1823                 // XXX: is 2) really a good measure here?
1824                 if ((basen->flag & OB_FROMGROUP) || ob->rigidbody_object || ob->rigidbody_constraint) {
1825                         Group *group;
1826                         for (group = bmain->group.first; group; group = group->id.next) {
1827                                 if (BKE_group_object_exists(group, ob))
1828                                         BKE_group_object_add(group, obn, scene, basen);
1829                         }
1830                 }
1831
1832                 /* duplicates using userflags */
1833                 if (dupflag & USER_DUP_ACT) {
1834                         BKE_copy_animdata_id_action(&obn->id);
1835                 }
1836
1837                 if (dupflag & USER_DUP_MAT) {
1838                         for (a = 0; a < obn->totcol; a++) {
1839                                 id = (ID *)obn->mat[a];
1840                                 if (id) {
1841                                         ID_NEW_US(obn->mat[a])
1842                                         else
1843                                                 obn->mat[a] = BKE_material_copy(obn->mat[a]);
1844                                         id->us--;
1845
1846                                         if (dupflag & USER_DUP_ACT) {
1847                                                 BKE_copy_animdata_id_action(&obn->mat[a]->id);
1848                                         }
1849                                 }
1850                         }
1851                 }
1852                 if (dupflag & USER_DUP_PSYS) {
1853                         ParticleSystem *psys;
1854                         for (psys = obn->particlesystem.first; psys; psys = psys->next) {
1855                                 id = (ID *) psys->part;
1856                                 if (id) {
1857                                         ID_NEW_US(psys->part)
1858                                         else
1859                                                 psys->part = BKE_particlesettings_copy(psys->part);
1860
1861                                         if (dupflag & USER_DUP_ACT) {
1862                                                 BKE_copy_animdata_id_action(&psys->part->id);
1863                                         }
1864
1865                                         id->us--;
1866                                 }
1867                         }
1868                 }
1869
1870                 id = obn->data;
1871                 didit = 0;
1872
1873                 switch (obn->type) {
1874                         case OB_MESH:
1875                                 if (dupflag & USER_DUP_MESH) {
1876                                         ID_NEW_US2(obn->data)
1877                                         else {
1878                                                 obn->data = BKE_mesh_copy(obn->data);
1879                                                 if (obn->fluidsimSettings) {
1880                                                         obn->fluidsimSettings->orgMesh = (Mesh *)obn->data;
1881                                                 }
1882                                                 didit = 1;
1883                                         }
1884                                         id->us--;
1885                                 }
1886                                 break;
1887                         case OB_CURVE:
1888                                 if (dupflag & USER_DUP_CURVE) {
1889                                         ID_NEW_US2(obn->data)
1890                                         else {
1891                                                 obn->data = BKE_curve_copy(obn->data);
1892                                                 didit = 1;
1893                                         }
1894                                         id->us--;
1895                                 }
1896                                 break;
1897                         case OB_SURF:
1898                                 if (dupflag & USER_DUP_SURF) {
1899                                         ID_NEW_US2(obn->data)
1900                                         else {
1901                                                 obn->data = BKE_curve_copy(obn->data);
1902                                                 didit = 1;
1903                                         }
1904                                         id->us--;
1905                                 }
1906                                 break;
1907                         case OB_FONT:
1908                                 if (dupflag & USER_DUP_FONT) {
1909                                         ID_NEW_US2(obn->data)
1910                                         else {
1911                                                 obn->data = BKE_curve_copy(obn->data);
1912                                                 didit = 1;
1913                                         }
1914                                         id->us--;
1915                                 }
1916                                 break;
1917                         case OB_MBALL:
1918                                 if (dupflag & USER_DUP_MBALL) {
1919                                         ID_NEW_US2(obn->data)
1920                                         else {
1921                                                 obn->data = BKE_mball_copy(obn->data);
1922                                                 didit = 1;
1923                                         }
1924                                         id->us--;
1925                                 }
1926                                 break;
1927                         case OB_LAMP:
1928                                 if (dupflag & USER_DUP_LAMP) {
1929                                         ID_NEW_US2(obn->data)
1930                                         else {
1931                                                 obn->data = BKE_lamp_copy(obn->data);
1932                                                 didit = 1;
1933                                         }
1934                                         id->us--;
1935                                 }
1936                                 break;
1937                         case OB_ARMATURE:
1938                                 DAG_id_tag_update(&obn->id, OB_RECALC_DATA);
1939                                 if (obn->pose)
1940                                         obn->pose->flag |= POSE_RECALC;
1941                                 if (dupflag & USER_DUP_ARM) {
1942                                         ID_NEW_US2(obn->data)
1943                                         else {
1944                                                 obn->data = BKE_armature_copy(obn->data);
1945                                                 BKE_pose_rebuild(obn, obn->data);
1946                                                 didit = 1;
1947                                         }
1948                                         id->us--;
1949                                 }
1950                                 break;
1951                         case OB_LATTICE:
1952                                 if (dupflag != 0) {
1953                                         ID_NEW_US2(obn->data)
1954                                         else {
1955                                                 obn->data = BKE_lattice_copy(obn->data);
1956                                                 didit = 1;
1957                                         }
1958                                         id->us--;
1959                                 }
1960                                 break;
1961                         case OB_CAMERA:
1962                                 if (dupflag != 0) {
1963                                         ID_NEW_US2(obn->data)
1964                                         else {
1965                                                 obn->data = BKE_camera_copy(obn->data);
1966                                                 didit = 1;
1967                                         }
1968                                         id->us--;
1969                                 }
1970                                 break;
1971                         case OB_SPEAKER:
1972                                 if (dupflag != 0) {
1973                                         ID_NEW_US2(obn->data)
1974                                         else {
1975                                                 obn->data = BKE_speaker_copy(obn->data);
1976                                                 didit = 1;
1977                                         }
1978                                         id->us--;
1979                                 }
1980                                 break;
1981                 }
1982
1983                 /* check if obdata is copied */
1984                 if (didit) {
1985                         Key *key = BKE_key_from_object(obn);
1986
1987                         if (dupflag & USER_DUP_ACT) {
1988                                 bActuator *act;
1989
1990                                 BKE_copy_animdata_id_action((ID *)obn->data);
1991                                 if (key) {
1992                                         BKE_copy_animdata_id_action((ID *)key);
1993                                 }
1994
1995                                 /* Update the duplicated action in the action actuators */
1996                                 for (act = obn->actuators.first; act; act = act->next) {
1997                                         if (act->type == ACT_ACTION) {
1998                                                 bActionActuator *actact = (bActionActuator *) act->data;
1999                                                 if (ob->adt && actact->act == ob->adt->action) {
2000                                                         actact->act = obn->adt->action;
2001                                                 }
2002                                         }
2003                                 }
2004                         }
2005
2006                         if (dupflag & USER_DUP_MAT) {
2007                                 matarar = give_matarar(obn);
2008                                 if (matarar) {
2009                                         for (a = 0; a < obn->totcol; a++) {
2010                                                 id = (ID *)(*matarar)[a];
2011                                                 if (id) {
2012                                                         ID_NEW_US((*matarar)[a])
2013                                                         else
2014                                                                 (*matarar)[a] = BKE_material_copy((*matarar)[a]);
2015                                                         id->us--;
2016                                                 }
2017                                         }
2018                                 }
2019                         }
2020                 }
2021         }
2022         return basen;
2023 }
2024
2025 /* single object duplicate, if dupflag==0, fully linked, else it uses the flags given */
2026 /* leaves selection of base/object unaltered.
2027  * note: don't call this within a loop since clear_* funcs loop over the entire database.
2028  * note: caller must do DAG_relations_tag_update(bmain);
2029  *       this is not done automatic since we may duplicate many objects in a batch */
2030 Base *ED_object_add_duplicate(Main *bmain, Scene *scene, Base *base, int dupflag)
2031 {
2032         Base *basen;
2033         Object *ob;
2034
2035         BKE_main_id_clear_newpoins(bmain);
2036         clear_sca_new_poins();  /* sensor/contr/act */
2037
2038         basen = object_add_duplicate_internal(bmain, scene, base, dupflag);
2039         if (basen == NULL) {
2040                 return NULL;
2041         }
2042
2043         ob = basen->object;
2044
2045         /* link own references to the newly duplicated data [#26816] */
2046         BKE_object_relink(ob);
2047         set_sca_new_poins_ob(ob);
2048
2049         /* DAG_relations_tag_update(bmain); */ /* caller must do */
2050
2051         if (ob->data) {
2052                 ED_render_id_flush_update(bmain, ob->data);
2053         }
2054
2055         return basen;
2056 }
2057
2058 /* contextual operator dupli */
2059 static int duplicate_exec(bContext *C, wmOperator *op)
2060 {
2061         Main *bmain = CTX_data_main(C);
2062         Scene *scene = CTX_data_scene(C);
2063         int linked = RNA_boolean_get(op->ptr, "linked");
2064         int dupflag = (linked) ? 0 : U.dupflag;
2065
2066         BKE_main_id_clear_newpoins(bmain);
2067         clear_sca_new_poins();  /* sensor/contr/act */
2068
2069         CTX_DATA_BEGIN (C, Base *, base, selected_bases)
2070         {
2071                 Base *basen = object_add_duplicate_internal(bmain, scene, base, dupflag);
2072
2073                 /* note that this is safe to do with this context iterator,
2074                  * the list is made in advance */
2075                 ED_base_object_select(base, BA_DESELECT);
2076
2077                 if (basen == NULL) {
2078                         continue;
2079                 }
2080
2081                 /* new object becomes active */
2082                 if (BASACT == base)
2083                         ED_base_object_activate(C, basen);
2084
2085                 if (basen->object->data) {
2086                         DAG_id_tag_update(basen->object->data, 0);
2087                 }
2088         }
2089         CTX_DATA_END;
2090
2091         copy_object_set_idnew(C, dupflag);
2092
2093         DAG_relations_tag_update(bmain);
2094
2095         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
2096
2097         return OPERATOR_FINISHED;
2098 }
2099
2100 void OBJECT_OT_duplicate(wmOperatorType *ot)
2101 {
2102         PropertyRNA *prop;
2103
2104         /* identifiers */
2105         ot->name = "Duplicate Objects";
2106         ot->description = "Duplicate selected objects";
2107         ot->idname = "OBJECT_OT_duplicate";
2108
2109         /* api callbacks */
2110         ot->exec = duplicate_exec;
2111         ot->poll = ED_operator_objectmode;
2112
2113         /* flags */
2114         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2115
2116         /* to give to transform */
2117         RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data");
2118         prop = RNA_def_enum(ot->srna, "mode", transform_mode_types, TFM_TRANSLATION, "Mode", "");
2119         RNA_def_property_flag(prop, PROP_HIDDEN);
2120 }
2121
2122 /* **************** add named object, for dragdrop ************* */
2123
2124 static int add_named_exec(bContext *C, wmOperator *op)
2125 {
2126         wmWindow *win = CTX_wm_window(C);
2127         const wmEvent *event = win ? win->eventstate : NULL;
2128         Main *bmain = CTX_data_main(C);
2129         Scene *scene = CTX_data_scene(C);
2130         Base *basen, *base;
2131         Object *ob;
2132         int linked = RNA_boolean_get(op->ptr, "linked");
2133         int dupflag = (linked) ? 0 : U.dupflag;
2134         char name[MAX_ID_NAME - 2];
2135
2136         /* find object, create fake base */
2137         RNA_string_get(op->ptr, "name", name);
2138         ob = (Object *)BKE_libblock_find_name(ID_OB, name);
2139         if (ob == NULL)
2140                 return OPERATOR_CANCELLED;
2141
2142         base = MEM_callocN(sizeof(Base), "duplibase");
2143         base->object = ob;
2144         base->flag = ob->flag;
2145
2146         /* prepare dupli */
2147         BKE_main_id_clear_newpoins(bmain);
2148         clear_sca_new_poins();  /* sensor/contr/act */
2149
2150         basen = object_add_duplicate_internal(bmain, scene, base, dupflag);
2151
2152         if (basen == NULL) {
2153                 MEM_freeN(base);
2154                 return OPERATOR_CANCELLED;
2155         }
2156
2157         basen->lay = basen->object->lay = scene->lay;
2158         basen->object->restrictflag &= ~OB_RESTRICT_VIEW;
2159
2160         if (event) {
2161                 ARegion *ar = CTX_wm_region(C);
2162                 const int mval[2] = {event->x - ar->winrct.xmin,
2163                                      event->y - ar->winrct.ymin};
2164                 ED_object_location_from_view(C, basen->object->loc);
2165                 ED_view3d_cursor3d_position(C, basen->object->loc, mval);
2166         }
2167         
2168         ED_base_object_select(basen, BA_SELECT);
2169         ED_base_object_activate(C, basen);
2170
2171         copy_object_set_idnew(C, dupflag);
2172
2173         DAG_relations_tag_update(bmain);
2174
2175         MEM_freeN(base);
2176
2177         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
2178
2179         return OPERATOR_FINISHED;
2180 }
2181
2182 void OBJECT_OT_add_named(wmOperatorType *ot)
2183 {
2184         /* identifiers */
2185         ot->name = "Add Named Object";
2186         ot->description = "Add named object";
2187         ot->idname = "OBJECT_OT_add_named";
2188
2189         /* api callbacks */
2190         ot->exec = add_named_exec;
2191         ot->poll = ED_operator_objectmode;
2192
2193         /* flags */
2194         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2195
2196         RNA_def_boolean(ot->srna, "linked", 0, "Linked", "Duplicate object but not object data, linking to the original data");
2197         RNA_def_string(ot->srna, "name", "Cube", MAX_ID_NAME - 2, "Name", "Object name to add");
2198 }
2199
2200 /**************************** Join *************************/
2201
2202 static int join_poll(bContext *C)
2203 {
2204         Object *ob = CTX_data_active_object(C);
2205
2206         if (!ob || ob->id.lib) return 0;
2207
2208         if (ELEM4(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_ARMATURE))
2209                 return ED_operator_screenactive(C);
2210         else
2211                 return 0;
2212 }
2213
2214 static int join_exec(bContext *C, wmOperator *op)
2215 {
2216         Scene *scene = CTX_data_scene(C);
2217         Object *ob = CTX_data_active_object(C);
2218
2219         if (scene->obedit) {
2220                 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in edit mode");
2221                 return OPERATOR_CANCELLED;
2222         }
2223         else if (BKE_object_obdata_is_libdata(ob)) {
2224                 BKE_report(op->reports, RPT_ERROR, "Cannot edit external libdata");
2225                 return OPERATOR_CANCELLED;
2226         }
2227
2228         if (ob->type == OB_MESH)
2229                 return join_mesh_exec(C, op);
2230         else if (ELEM(ob->type, OB_CURVE, OB_SURF))
2231                 return join_curve_exec(C, op);
2232         else if (ob->type == OB_ARMATURE)
2233                 return join_armature_exec(C, op);
2234
2235         return OPERATOR_CANCELLED;
2236 }
2237
2238 void OBJECT_OT_join(wmOperatorType *ot)
2239 {
2240         /* identifiers */
2241         ot->name = "Join";
2242         ot->description = "Join selected objects into active object";
2243         ot->idname = "OBJECT_OT_join";
2244
2245         /* api callbacks */
2246         ot->exec = join_exec;
2247         ot->poll = join_poll;
2248
2249         /* flags */
2250         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2251 }
2252
2253 /**************************** Join as Shape Key*************************/
2254
2255 static int join_shapes_poll(bContext *C)
2256 {
2257         Object *ob = CTX_data_active_object(C);
2258
2259         if (!ob || ob->id.lib) return 0;
2260
2261         /* only meshes supported at the moment */
2262         if (ob->type == OB_MESH)
2263                 return ED_operator_screenactive(C);
2264         else
2265                 return 0;
2266 }
2267
2268 static int join_shapes_exec(bContext *C, wmOperator *op)
2269 {
2270         Scene *scene = CTX_data_scene(C);
2271         Object *ob = CTX_data_active_object(C);
2272
2273         if (scene->obedit) {
2274                 BKE_report(op->reports, RPT_ERROR, "This data does not support joining in edit mode");
2275                 return OPERATOR_CANCELLED;
2276         }
2277         else if (BKE_object_obdata_is_libdata(ob)) {
2278                 BKE_report(op->reports, RPT_ERROR, "Cannot edit external libdata");
2279                 return OPERATOR_CANCELLED;
2280         }
2281
2282         if (ob->type == OB_MESH)
2283                 return join_mesh_shapes_exec(C, op);
2284
2285         return OPERATOR_CANCELLED;
2286 }
2287
2288 void OBJECT_OT_join_shapes(wmOperatorType *ot)
2289 {
2290         /* identifiers */
2291         ot->name = "Join as Shapes";
2292         ot->description = "Merge selected objects to shapes of active object";
2293         ot->idname = "OBJECT_OT_join_shapes";
2294
2295         /* api callbacks */
2296         ot->exec = join_shapes_exec;
2297         ot->poll = join_shapes_poll;
2298
2299         /* flags */
2300         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
2301 }