Merged changes in the trunk up to revision 48695.
[blender-staging.git] / source / blender / editors / object / object_relations.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_relations.c
27  *  \ingroup edobj
28  */
29
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include "MEM_guardedalloc.h"
36
37 #include "DNA_anim_types.h"
38 #include "DNA_mesh_types.h"
39 #include "DNA_constraint_types.h"
40 #include "DNA_group_types.h"
41 #include "DNA_lamp_types.h"
42 #include "DNA_lattice_types.h"
43 #include "DNA_material_types.h"
44 #include "DNA_meta_types.h"
45 #include "DNA_particle_types.h"
46 #include "DNA_scene_types.h"
47 #include "DNA_speaker_types.h"
48 #include "DNA_world_types.h"
49 #include "DNA_object_types.h"
50
51 #include "BLI_math.h"
52 #include "BLI_listbase.h"
53 #include "BLI_string.h"
54 #include "BLI_utildefines.h"
55
56 #include "BKE_action.h"
57 #include "BKE_animsys.h"
58 #include "BKE_armature.h"
59 #include "BKE_camera.h"
60 #include "BKE_context.h"
61 #include "BKE_constraint.h"
62 #include "BKE_curve.h"
63 #include "BKE_depsgraph.h"
64 #include "BKE_DerivedMesh.h"
65 #include "BKE_displist.h"
66 #include "BKE_global.h"
67 #include "BKE_fcurve.h"
68 #include "BKE_lamp.h"
69 #include "BKE_lattice.h"
70 #include "BKE_library.h"
71 #include "BKE_main.h"
72 #include "BKE_material.h"
73 #include "BKE_mball.h"
74 #include "BKE_mesh.h"
75 #include "BKE_modifier.h"
76 #include "BKE_object.h"
77 #include "BKE_report.h"
78 #include "BKE_sca.h"
79 #include "BKE_scene.h"
80 #include "BKE_speaker.h"
81 #include "BKE_texture.h"
82 #include "BKE_tessmesh.h"
83
84 #include "WM_api.h"
85 #include "WM_types.h"
86
87 #include "UI_interface.h"
88 #include "UI_resources.h"
89
90 #include "RNA_access.h"
91 #include "RNA_define.h"
92 #include "RNA_enum_types.h"
93
94 #include "ED_armature.h"
95 #include "ED_curve.h"
96 #include "ED_keyframing.h"
97 #include "ED_object.h"
98 #include "ED_mesh.h"
99 #include "ED_screen.h"
100 #include "ED_view3d.h"
101
102 #include "object_intern.h"
103
104 /*********************** Make Vertex Parent Operator ************************/
105
106 static int vertex_parent_set_poll(bContext *C)
107 {
108         return ED_operator_editmesh(C) || ED_operator_editsurfcurve(C) || ED_operator_editlattice(C);
109 }
110
111 static int vertex_parent_set_exec(bContext *C, wmOperator *op)
112 {
113         Main *bmain = CTX_data_main(C);
114         Scene *scene = CTX_data_scene(C);
115         Object *obedit = CTX_data_edit_object(C);
116         BMVert *eve;
117         BMIter iter;
118         Curve *cu;
119         Nurb *nu;
120         BezTriple *bezt;
121         BPoint *bp;
122         Object *par;
123         int a, v1 = 0, v2 = 0, v3 = 0, v4 = 0, nr = 1;
124         
125         /* we need 1 to 3 selected vertices */
126         
127         if (obedit->type == OB_MESH) {
128                 Mesh *me = obedit->data;
129                 BMEditMesh *em;
130
131                 EDBM_mesh_load(obedit);
132                 EDBM_mesh_make(scene->toolsettings, scene, obedit);
133
134                 em = me->edit_btmesh;
135
136                 /* derivedMesh might be needed for solving parenting,
137                  * so re-create it here */
138                 makeDerivedMesh(scene, obedit, em, CD_MASK_BAREMESH, 0);
139
140                 BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
141                         if (BM_elem_flag_test(eve, BM_ELEM_SELECT)) {
142                                 if (v1 == 0) v1 = nr;
143                                 else if (v2 == 0) v2 = nr;
144                                 else if (v3 == 0) v3 = nr;
145                                 else if (v4 == 0) v4 = nr;
146                                 else break;
147                         }
148                         nr++;
149                 }
150         }
151         else if (ELEM(obedit->type, OB_SURF, OB_CURVE)) {
152                 ListBase *editnurb = object_editcurve_get(obedit);
153                 
154                 cu = obedit->data;
155
156                 nu = editnurb->first;
157                 while (nu) {
158                         if (nu->type == CU_BEZIER) {
159                                 bezt = nu->bezt;
160                                 a = nu->pntsu;
161                                 while (a--) {
162                                         if (BEZSELECTED_HIDDENHANDLES(cu, bezt)) {
163                                                 if (v1 == 0) v1 = nr;
164                                                 else if (v2 == 0) v2 = nr;
165                                                 else if (v3 == 0) v3 = nr;
166                                                 else if (v4 == 0) v4 = nr;
167                                                 else break;
168                                         }
169                                         nr++;
170                                         bezt++;
171                                 }
172                         }
173                         else {
174                                 bp = nu->bp;
175                                 a = nu->pntsu * nu->pntsv;
176                                 while (a--) {
177                                         if (bp->f1 & SELECT) {
178                                                 if (v1 == 0) v1 = nr;
179                                                 else if (v2 == 0) v2 = nr;
180                                                 else if (v3 == 0) v3 = nr;
181                                                 else if (v4 == 0) v4 = nr;
182                                                 else break;
183                                         }
184                                         nr++;
185                                         bp++;
186                                 }
187                         }
188                         nu = nu->next;
189                 }
190         }
191         else if (obedit->type == OB_LATTICE) {
192                 Lattice *lt = obedit->data;
193                 
194                 a = lt->editlatt->latt->pntsu * lt->editlatt->latt->pntsv * lt->editlatt->latt->pntsw;
195                 bp = lt->editlatt->latt->def;
196                 while (a--) {
197                         if (bp->f1 & SELECT) {
198                                 if (v1 == 0) v1 = nr;
199                                 else if (v2 == 0) v2 = nr;
200                                 else if (v3 == 0) v3 = nr;
201                                 else if (v4 == 0) v4 = nr;
202                                 else break;
203                         }
204                         nr++;
205                         bp++;
206                 }
207         }
208         
209         if (v4 || !((v1 && v2 == 0 && v3 == 0) || (v1 && v2 && v3)) ) {
210                 BKE_report(op->reports, RPT_ERROR, "Select either 1 or 3 vertices to parent to");
211                 return OPERATOR_CANCELLED;
212         }
213         
214         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
215         {
216                 if (ob != obedit) {
217                         ob->recalc |= OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME;
218                         par = obedit->parent;
219                         
220                         while (par) {
221                                 if (par == ob) break;
222                                 par = par->parent;
223                         }
224                         if (par) {
225                                 BKE_report(op->reports, RPT_ERROR, "Loop in parents");
226                         }
227                         else {
228                                 Object workob;
229                                 
230                                 ob->parent = BASACT->object;
231                                 if (v3) {
232                                         ob->partype = PARVERT3;
233                                         ob->par1 = v1 - 1;
234                                         ob->par2 = v2 - 1;
235                                         ob->par3 = v3 - 1;
236
237                                         /* inverse parent matrix */
238                                         BKE_object_workob_calc_parent(scene, ob, &workob);
239                                         invert_m4_m4(ob->parentinv, workob.obmat);
240                                 }
241                                 else {
242                                         ob->partype = PARVERT1;
243                                         ob->par1 = v1 - 1;
244
245                                         /* inverse parent matrix */
246                                         BKE_object_workob_calc_parent(scene, ob, &workob);
247                                         invert_m4_m4(ob->parentinv, workob.obmat);
248                                 }
249                         }
250                 }
251         }
252         CTX_DATA_END;
253         
254         DAG_scene_sort(bmain, scene);
255
256         WM_event_add_notifier(C, NC_OBJECT, NULL);
257
258         return OPERATOR_FINISHED;
259 }
260
261 void OBJECT_OT_vertex_parent_set(wmOperatorType *ot)
262 {
263         /* identifiers */
264         ot->name = "Make Vertex Parent";
265         ot->description = "Parent selected objects to the selected vertices";
266         ot->idname = "OBJECT_OT_vertex_parent_set";
267         
268         /* api callbacks */
269         ot->invoke = WM_operator_confirm;
270         ot->poll = vertex_parent_set_poll;
271         ot->exec = vertex_parent_set_exec;
272         
273         /* flags */
274         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
275 }
276
277 /********************** Make Proxy Operator *************************/
278
279 /* set the object to proxify */
280 static int make_proxy_invoke(bContext *C, wmOperator *op, wmEvent *evt)
281 {
282         Scene *scene = CTX_data_scene(C);
283         Object *ob = ED_object_active_context(C);
284         
285         /* sanity checks */
286         if (!scene || scene->id.lib || !ob)
287                 return OPERATOR_CANCELLED;
288                 
289         /* Get object to work on - use a menu if we need to... */
290         if (ob->dup_group && ob->dup_group->id.lib) {
291                 /* gives menu with list of objects in group */
292                 //proxy_group_objects_menu(C, op, ob, ob->dup_group);
293                 WM_enum_search_invoke(C, op, evt);
294                 return OPERATOR_CANCELLED;
295
296         }
297         else if (ob->id.lib) {
298                 uiPopupMenu *pup = uiPupMenuBegin(C, "OK?", ICON_QUESTION);
299                 uiLayout *layout = uiPupMenuLayout(pup);
300                 
301                 /* create operator menu item with relevant properties filled in */
302                 uiItemFullO_ptr(layout, op->type, op->type->name, ICON_NONE, NULL, WM_OP_EXEC_REGION_WIN, UI_ITEM_O_RETURN_PROPS);
303                 
304                 /* present the menu and be done... */
305                 uiPupMenuEnd(C, pup);
306         }
307         else {
308                 /* error.. cannot continue */
309                 BKE_report(op->reports, RPT_ERROR, "Can only make proxy for a referenced object or group");
310         }
311         
312         /* this invoke just calls another instance of this operator... */
313         return OPERATOR_CANCELLED;
314 }
315
316 static int make_proxy_exec(bContext *C, wmOperator *op)
317 {
318         Main *bmain = CTX_data_main(C);
319         Object *ob, *gob = ED_object_active_context(C);
320         GroupObject *go;
321         Scene *scene = CTX_data_scene(C);
322
323         if (gob->dup_group != NULL) {
324                 go = BLI_findlink(&gob->dup_group->gobject, RNA_enum_get(op->ptr, "object"));
325                 ob = go->ob;
326         }
327         else {
328                 ob = gob;
329                 gob = NULL;
330         }
331         
332         if (ob) {
333                 Object *newob;
334                 Base *newbase, *oldbase = BASACT;
335                 char name[MAX_ID_NAME + 4];
336                 
337                 /* Add new object for the proxy */
338                 newob = BKE_object_add(scene, OB_EMPTY);
339
340                 BLI_snprintf(name, sizeof(name), "%s_proxy", ((ID *)(gob ? gob : ob))->name + 2);
341
342                 rename_id(&newob->id, name);
343                 
344                 /* set layers OK */
345                 newbase = BASACT;    /* BKE_object_add sets active... */
346                 newbase->lay = oldbase->lay;
347                 newob->lay = newbase->lay;
348                 
349                 /* remove base, leave user count of object, it gets linked in BKE_object_make_proxy */
350                 if (gob == NULL) {
351                         BLI_remlink(&scene->base, oldbase);
352                         MEM_freeN(oldbase);
353                 }
354                 
355                 BKE_object_make_proxy(newob, ob, gob);
356                 
357                 /* depsgraph flushes are needed for the new data */
358                 DAG_scene_sort(bmain, scene);
359                 DAG_id_tag_update(&newob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
360                 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, newob);
361         }
362         else {
363                 BKE_report(op->reports, RPT_ERROR, "No object to make proxy for");
364                 return OPERATOR_CANCELLED;
365         }
366         
367         return OPERATOR_FINISHED;
368 }
369
370 /* Generic itemf's for operators that take library args */
371 static EnumPropertyItem *proxy_group_object_itemf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), int *free)
372 {
373         EnumPropertyItem item_tmp = {0}, *item = NULL;
374         int totitem = 0;
375         int i = 0;
376         Object *ob = ED_object_active_context(C);
377         GroupObject *go;
378
379         if (!ob || !ob->dup_group)
380                 return DummyRNA_DEFAULT_items;
381
382         /* find the object to affect */
383         for (go = ob->dup_group->gobject.first; go; go = go->next) {
384                 item_tmp.identifier = item_tmp.name = go->ob->id.name + 2;
385                 item_tmp.value = i++;
386                 RNA_enum_item_add(&item, &totitem, &item_tmp);
387         }
388
389         RNA_enum_item_end(&item, &totitem);
390         *free = 1;
391
392         return item;
393 }
394
395 void OBJECT_OT_proxy_make(wmOperatorType *ot)
396 {
397         PropertyRNA *prop;
398
399         /* identifiers */
400         ot->name = "Make Proxy";
401         ot->idname = "OBJECT_OT_proxy_make";
402         ot->description = "Add empty object to become local replacement data of a library-linked object";
403         
404         /* callbacks */
405         ot->invoke = make_proxy_invoke;
406         ot->exec = make_proxy_exec;
407         ot->poll = ED_operator_object_active;
408         
409         /* flags */
410         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
411         
412         /* properties */
413         prop = RNA_def_enum(ot->srna, "object", DummyRNA_DEFAULT_items, 0, "Proxy Object", "Name of lib-linked/grouped object to make a proxy for"); /* XXX, relies on hard coded ID at the moment */
414         RNA_def_enum_funcs(prop, proxy_group_object_itemf);
415         ot->prop = prop;
416 }
417
418 /********************** Clear Parent Operator ******************* */
419
420 EnumPropertyItem prop_clear_parent_types[] = {
421         {0, "CLEAR", 0, "Clear Parent", ""},
422         {1, "CLEAR_KEEP_TRANSFORM", 0, "Clear and Keep Transformation", ""},
423         {2, "CLEAR_INVERSE", 0, "Clear Parent Inverse", ""},
424         {0, NULL, 0, NULL, NULL}
425 };
426
427 void ED_object_parent_clear(Object *ob, int type)
428 {
429
430         if (ob->parent == NULL)
431                 return;
432                 
433         if (type == 0) {
434                 ob->parent = NULL;
435         }
436         else if (type == 1) {
437                 ob->parent = NULL;
438                 BKE_object_apply_mat4(ob, ob->obmat, TRUE, FALSE);
439         }
440         else if (type == 2)
441                 unit_m4(ob->parentinv);
442                 
443         ob->recalc |= OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME;
444 }
445
446 /* note, poll should check for editable scene */
447 static int parent_clear_exec(bContext *C, wmOperator *op)
448 {
449         Main *bmain = CTX_data_main(C);
450         Scene *scene = CTX_data_scene(C);
451         int type = RNA_enum_get(op->ptr, "type");
452
453         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
454         {
455                 ED_object_parent_clear(ob, type);
456         }
457         CTX_DATA_END;
458
459         DAG_scene_sort(bmain, scene);
460         DAG_ids_flush_update(bmain, 0);
461         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
462         WM_event_add_notifier(C, NC_OBJECT | ND_PARENT, NULL);
463         return OPERATOR_FINISHED;
464 }
465
466 void OBJECT_OT_parent_clear(wmOperatorType *ot)
467 {
468         /* identifiers */
469         ot->name = "Clear Parent";
470         ot->description = "Clear the object's parenting";
471         ot->idname = "OBJECT_OT_parent_clear";
472         
473         /* api callbacks */
474         ot->invoke = WM_menu_invoke;
475         ot->exec = parent_clear_exec;
476         
477         ot->poll = ED_operator_object_active_editable;
478         
479         /* flags */
480         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
481         
482         ot->prop = RNA_def_enum(ot->srna, "type", prop_clear_parent_types, 0, "Type", "");
483 }
484
485 /* ******************** Make Parent Operator *********************** */
486
487 void ED_object_parent(Object *ob, Object *par, int type, const char *substr)
488 {
489         if (!par || BKE_object_parent_loop_check(par, ob)) {
490                 ob->parent = NULL;
491                 ob->partype = PAROBJECT;
492                 ob->parsubstr[0] = 0;
493                 return;
494         }
495
496         /* this could use some more checks */
497
498         ob->parent = par;
499         ob->partype &= ~PARTYPE;
500         ob->partype |= type;
501         BLI_strncpy(ob->parsubstr, substr, sizeof(ob->parsubstr));
502 }
503
504 /* Operator Property */
505 EnumPropertyItem prop_make_parent_types[] = {
506         {PAR_OBJECT, "OBJECT", 0, "Object", ""},
507         {PAR_ARMATURE, "ARMATURE", 0, "Armature Deform", ""},
508         {PAR_ARMATURE_NAME, "ARMATURE_NAME", 0, "   With Empty Groups", ""},
509         {PAR_ARMATURE_AUTO, "ARMATURE_AUTO", 0, "   With Automatic Weights", ""},
510         {PAR_ARMATURE_ENVELOPE, "ARMATURE_ENVELOPE", 0, "   With Envelope Weights", ""},
511         {PAR_BONE, "BONE", 0, "Bone", ""},
512         {PAR_CURVE, "CURVE", 0, "Curve Deform", ""},
513         {PAR_FOLLOW, "FOLLOW", 0, "Follow Path", ""},
514         {PAR_PATH_CONST, "PATH_CONST", 0, "Path Constraint", ""},
515         {PAR_LATTICE, "LATTICE", 0, "Lattice Deform", ""},
516         {PAR_VERTEX, "VERTEX", 0, "Vertex", ""},
517         {PAR_TRIA, "TRIA", 0, "Triangle", ""},
518         {0, NULL, 0, NULL, NULL}
519 };
520
521 int ED_object_parent_set(ReportList *reports, Main *bmain, Scene *scene, Object *ob, Object *par, int partype)
522 {
523         bPoseChannel *pchan = NULL;
524         int pararm = ELEM4(partype, PAR_ARMATURE, PAR_ARMATURE_NAME, PAR_ARMATURE_ENVELOPE, PAR_ARMATURE_AUTO);
525         
526         par->recalc |= OB_RECALC_OB;
527         
528         /* preconditions */
529         if (partype == PAR_FOLLOW || partype == PAR_PATH_CONST) {
530                 if (par->type != OB_CURVE)
531                         return 0;
532                 else {
533                         Curve *cu = par->data;
534                         
535                         if ((cu->flag & CU_PATH) == 0) {
536                                 cu->flag |= CU_PATH | CU_FOLLOW;
537                                 BKE_displist_make_curveTypes(scene, par, 0);  /* force creation of path data */
538                         }
539                         else cu->flag |= CU_FOLLOW;
540                         
541                         /* if follow, add F-Curve for ctime (i.e. "eval_time") so that path-follow works */
542                         if (partype == PAR_FOLLOW) {
543                                 /* get or create F-Curve */
544                                 bAction *act = verify_adt_action(&cu->id, 1);
545                                 FCurve *fcu = verify_fcurve(act, NULL, NULL, "eval_time", 0, 1);
546                                 
547                                 /* setup dummy 'generator' modifier here to get 1-1 correspondence still working */
548                                 if (!fcu->bezt && !fcu->fpt && !fcu->modifiers.first)
549                                         add_fmodifier(&fcu->modifiers, FMODIFIER_TYPE_GENERATOR);
550                         }
551                         
552                         /* fall back on regular parenting now (for follow only) */
553                         if (partype == PAR_FOLLOW)
554                                 partype = PAR_OBJECT;
555                 }               
556         }
557         else if (partype == PAR_BONE) {
558                 pchan = BKE_pose_channel_active(par);
559                 
560                 if (pchan == NULL) {
561                         BKE_report(reports, RPT_ERROR, "No active Bone");
562                         return 0;
563                 }
564         }
565         
566         if (ob != par) {
567                 if (BKE_object_parent_loop_check(par, ob)) {
568                         BKE_report(reports, RPT_ERROR, "Loop in parents");
569                         return 0;
570                 }
571                 else {
572                         Object workob;
573                         
574                         /* apply transformation of previous parenting */
575                         /* BKE_object_apply_mat4(ob, ob->obmat); */ /* removed because of bug [#23577] */
576                         
577                         /* set the parent (except for follow-path constraint option) */
578                         if (partype != PAR_PATH_CONST) {
579                                 ob->parent = par;
580                         }
581                         
582                         /* handle types */
583                         if (pchan)
584                                 BLI_strncpy(ob->parsubstr, pchan->name, sizeof(ob->parsubstr));
585                         else
586                                 ob->parsubstr[0] = 0;
587                                 
588                         if (partype == PAR_PATH_CONST) {
589                                 /* don't do anything here, since this is not technically "parenting" */
590                         }
591                         else if (ELEM(partype, PAR_CURVE, PAR_LATTICE) || (pararm)) {
592                                 /* partype is now set to PAROBJECT so that invisible 'virtual' modifiers don't need to be created
593                                  * NOTE: the old (2.4x) method was to set ob->partype = PARSKEL, creating the virtual modifiers
594                                  */
595                                 ob->partype = PAROBJECT; /* note, dna define, not operator property */
596                                 //ob->partype= PARSKEL; /* note, dna define, not operator property */
597                                 
598                                 /* BUT, to keep the deforms, we need a modifier, and then we need to set the object that it uses */
599                                 // XXX currently this should only happen for meshes, curves, surfaces, and lattices - this stuff isn't available for metas yet
600                                 if (ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_LATTICE)) {
601                                         ModifierData *md;
602                                         
603                                         switch (partype) {
604                                                 case PAR_CURVE: /* curve deform */
605                                                         md = ED_object_modifier_add(reports, bmain, scene, ob, NULL, eModifierType_Curve);
606                                                         ((CurveModifierData *)md)->object = par;
607                                                         break;
608                                                 case PAR_LATTICE: /* lattice deform */
609                                                         md = ED_object_modifier_add(reports, bmain, scene, ob, NULL, eModifierType_Lattice);
610                                                         ((LatticeModifierData *)md)->object = par;
611                                                         break;
612                                                 default: /* armature deform */
613                                                         md = ED_object_modifier_add(reports, bmain, scene, ob, NULL, eModifierType_Armature);
614                                                         ((ArmatureModifierData *)md)->object = par;
615                                                         break;
616                                         }
617                                 }
618                         }
619                         else if (partype == PAR_BONE)
620                                 ob->partype = PARBONE;  /* note, dna define, not operator property */
621                         else
622                                 ob->partype = PAROBJECT;  /* note, dna define, not operator property */
623                         
624                         /* constraint */
625                         if (partype == PAR_PATH_CONST) {
626                                 bConstraint *con;
627                                 bFollowPathConstraint *data;
628                                 float cmat[4][4], vec[3];
629                                 
630                                 con = add_ob_constraint(ob, "AutoPath", CONSTRAINT_TYPE_FOLLOWPATH);
631                                 
632                                 data = con->data;
633                                 data->tar = par;
634                                 
635                                 get_constraint_target_matrix(scene, con, 0, CONSTRAINT_OBTYPE_OBJECT, NULL, cmat, scene->r.cfra);
636                                 sub_v3_v3v3(vec, ob->obmat[3], cmat[3]);
637                                 
638                                 ob->loc[0] = vec[0];
639                                 ob->loc[1] = vec[1];
640                                 ob->loc[2] = vec[2];
641                         }
642                         else if (pararm && ob->type == OB_MESH && par->type == OB_ARMATURE) {
643                                 if (partype == PAR_ARMATURE_NAME)
644                                         create_vgroups_from_armature(reports, scene, ob, par, ARM_GROUPS_NAME, 0);
645                                 else if (partype == PAR_ARMATURE_ENVELOPE)
646                                         create_vgroups_from_armature(reports, scene, ob, par, ARM_GROUPS_ENVELOPE, 0);
647                                 else if (partype == PAR_ARMATURE_AUTO) {
648                                         WM_cursor_wait(1);
649                                         create_vgroups_from_armature(reports, scene, ob, par, ARM_GROUPS_AUTO, 0);
650                                         WM_cursor_wait(0);
651                                 }
652                                 /* get corrected inverse */
653                                 ob->partype = PAROBJECT;
654                                 BKE_object_workob_calc_parent(scene, ob, &workob);
655                                 
656                                 invert_m4_m4(ob->parentinv, workob.obmat);
657                         }
658                         else {
659                                 /* calculate inverse parent matrix */
660                                 BKE_object_workob_calc_parent(scene, ob, &workob);
661                                 invert_m4_m4(ob->parentinv, workob.obmat);
662                         }
663                         
664                         ob->recalc |= OB_RECALC_OB | OB_RECALC_DATA;
665                 }
666         }
667
668         return 1;
669 }
670
671 static int parent_set_exec(bContext *C, wmOperator *op)
672 {
673         Main *bmain = CTX_data_main(C);
674         Scene *scene = CTX_data_scene(C);
675         Object *par = ED_object_active_context(C);
676         int partype = RNA_enum_get(op->ptr, "type");
677         int ok = 1;
678
679         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
680         {
681                 if (!ED_object_parent_set(op->reports, bmain, scene, ob, par, partype)) {
682                         ok = 0;
683                         break;
684                 }
685         }
686         CTX_DATA_END;
687
688         if (!ok)
689                 return OPERATOR_CANCELLED;
690
691         DAG_scene_sort(bmain, scene);
692         DAG_ids_flush_update(bmain, 0);
693         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
694         WM_event_add_notifier(C, NC_OBJECT | ND_PARENT, NULL);
695
696         return OPERATOR_FINISHED;
697 }
698
699
700 static int parent_set_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *UNUSED(event))
701 {
702         Object *ob = ED_object_active_context(C);
703         uiPopupMenu *pup = uiPupMenuBegin(C, "Set Parent To", ICON_NONE);
704         uiLayout *layout = uiPupMenuLayout(pup);
705         
706         uiLayoutSetOperatorContext(layout, WM_OP_EXEC_DEFAULT);
707         uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_OBJECT);
708         
709         /* ob becomes parent, make the associated menus */
710         if (ob->type == OB_ARMATURE) {
711                 uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_ARMATURE);
712                 uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_ARMATURE_NAME);
713                 uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_ARMATURE_ENVELOPE);
714                 uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_ARMATURE_AUTO);
715                 uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_BONE);
716         }
717         else if (ob->type == OB_CURVE) {
718                 uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_CURVE);
719                 uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_FOLLOW);
720                 uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_PATH_CONST);
721         }
722         else if (ob->type == OB_LATTICE) {
723                 uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_LATTICE);
724         }
725         
726         uiPupMenuEnd(C, pup);
727         
728         return OPERATOR_CANCELLED;
729 }
730
731
732 void OBJECT_OT_parent_set(wmOperatorType *ot)
733 {
734         /* identifiers */
735         ot->name = "Make Parent";
736         ot->description = "Set the object's parenting";
737         ot->idname = "OBJECT_OT_parent_set";
738         
739         /* api callbacks */
740         ot->invoke = parent_set_invoke;
741         ot->exec = parent_set_exec;
742         
743         ot->poll = ED_operator_object_active;
744         
745         /* flags */
746         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
747         
748         RNA_def_enum(ot->srna, "type", prop_make_parent_types, 0, "Type", "");
749 }
750
751 /* ************ Make Parent Without Inverse Operator ******************* */
752
753 static int parent_noinv_set_exec(bContext *C, wmOperator *op)
754 {
755         Main *bmain = CTX_data_main(C);
756         Object *par = ED_object_active_context(C);
757         
758         par->recalc |= OB_RECALC_OB;
759         
760         /* context iterator */
761         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
762         {
763                 if (ob != par) {
764                         if (BKE_object_parent_loop_check(par, ob)) {
765                                 BKE_report(op->reports, RPT_ERROR, "Loop in parents");
766                         }
767                         else {
768                                 /* clear inverse matrix and also the object location */
769                                 unit_m4(ob->parentinv);
770                                 memset(ob->loc, 0, 3 * sizeof(float));
771                                 
772                                 /* set recalc flags */
773                                 ob->recalc |= OB_RECALC_OB | OB_RECALC_DATA;
774                                 
775                                 /* set parenting type for object - object only... */
776                                 ob->parent = par;
777                                 ob->partype = PAROBJECT; /* note, dna define, not operator property */
778                         }
779                 }
780         }
781         CTX_DATA_END;
782         
783         DAG_scene_sort(bmain, CTX_data_scene(C));
784         DAG_ids_flush_update(bmain, 0);
785         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
786         
787         return OPERATOR_FINISHED;
788 }
789
790 void OBJECT_OT_parent_no_inverse_set(wmOperatorType *ot)
791 {
792         /* identifiers */
793         ot->name = "Make Parent without Inverse";
794         ot->description = "Set the object's parenting without setting the inverse parent correction";
795         ot->idname = "OBJECT_OT_parent_no_inverse_set";
796         
797         /* api callbacks */
798         ot->invoke = WM_operator_confirm;
799         ot->exec = parent_noinv_set_exec;
800         ot->poll = ED_operator_object_active_editable;
801         
802         /* flags */
803         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
804 }
805
806 /************************ Clear Slow Parent Operator *********************/
807
808 static int object_slow_parent_clear_exec(bContext *C, wmOperator *UNUSED(op))
809 {
810         Main *bmain = CTX_data_main(C);
811         Scene *scene = CTX_data_scene(C);
812
813         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
814         {
815                 if (ob->parent) {
816                         if (ob->partype & PARSLOW) {
817                                 ob->partype -= PARSLOW;
818                                 BKE_object_where_is_calc(scene, ob);
819                                 ob->partype |= PARSLOW;
820                                 ob->recalc |= OB_RECALC_OB;
821                         }
822                 }
823         }
824         CTX_DATA_END;
825
826         DAG_ids_flush_update(bmain, 0);
827         WM_event_add_notifier(C, NC_SCENE, scene);
828         
829         return OPERATOR_FINISHED;
830 }
831
832 void OBJECT_OT_slow_parent_clear(wmOperatorType *ot)
833 {
834         
835         /* identifiers */
836         ot->name = "Clear Slow Parent";
837         ot->description = "Clear the object's slow parent";
838         ot->idname = "OBJECT_OT_slow_parent_clear";
839         
840         /* api callbacks */
841         ot->invoke = WM_operator_confirm;
842         ot->exec = object_slow_parent_clear_exec;
843         ot->poll = ED_operator_view3d_active;
844         
845         /* flags */
846         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
847 }
848
849 /********************** Make Slow Parent Operator *********************/
850
851 static int object_slow_parent_set_exec(bContext *C, wmOperator *UNUSED(op))
852 {
853         Main *bmain = CTX_data_main(C);
854         Scene *scene = CTX_data_scene(C);
855
856         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
857         {
858                 if (ob->parent)
859                         ob->partype |= PARSLOW;
860
861                 ob->recalc |= OB_RECALC_OB;
862                 
863         }
864         CTX_DATA_END;
865
866         DAG_ids_flush_update(bmain, 0);
867         WM_event_add_notifier(C, NC_SCENE, scene);
868         
869         return OPERATOR_FINISHED;
870 }
871
872 void OBJECT_OT_slow_parent_set(wmOperatorType *ot)
873 {
874         
875         /* identifiers */
876         ot->name = "Set Slow Parent";
877         ot->description = "Set the object's slow parent";
878         ot->idname = "OBJECT_OT_slow_parent_set";
879         
880         /* api callbacks */
881         ot->invoke = WM_operator_confirm;
882         ot->exec = object_slow_parent_set_exec;
883         ot->poll = ED_operator_view3d_active;
884         
885         /* flags */
886         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
887 }
888
889 /* ******************** Clear Track Operator ******************* */
890
891 static EnumPropertyItem prop_clear_track_types[] = {
892         {0, "CLEAR", 0, "Clear Track", ""},
893         {1, "CLEAR_KEEP_TRANSFORM", 0, "Clear and Keep Transformation (Clear Track)", ""},
894         {0, NULL, 0, NULL, NULL}
895 };
896
897 /* note, poll should check for editable scene */
898 static int object_track_clear_exec(bContext *C, wmOperator *op)
899 {
900         Main *bmain = CTX_data_main(C);
901         Scene *scene = CTX_data_scene(C);
902         int type = RNA_enum_get(op->ptr, "type");
903
904         if (CTX_data_edit_object(C)) {
905                 BKE_report(op->reports, RPT_ERROR, "Operation cannot be performed in EditMode");
906                 return OPERATOR_CANCELLED;
907         }
908         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
909         {
910                 bConstraint *con, *pcon;
911                 
912                 /* remove track-object for old track */
913                 ob->track = NULL;
914                 ob->recalc |= OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME;
915                 
916                 /* also remove all tracking constraints */
917                 for (con = ob->constraints.last; con; con = pcon) {
918                         pcon = con->prev;
919                         if (ELEM3(con->type, CONSTRAINT_TYPE_TRACKTO, CONSTRAINT_TYPE_LOCKTRACK, CONSTRAINT_TYPE_DAMPTRACK))
920                                 remove_constraint(&ob->constraints, con);
921                 }
922                 
923                 if (type == 1)
924                         BKE_object_apply_mat4(ob, ob->obmat, TRUE, TRUE);
925         }
926         CTX_DATA_END;
927
928         DAG_ids_flush_update(bmain, 0);
929         DAG_scene_sort(bmain, scene);
930         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
931
932         return OPERATOR_FINISHED;
933 }
934
935 void OBJECT_OT_track_clear(wmOperatorType *ot)
936 {
937         /* identifiers */
938         ot->name = "Clear track";
939         ot->description = "Clear tracking constraint or flag from object";
940         ot->idname = "OBJECT_OT_track_clear";
941         
942         /* api callbacks */
943         ot->invoke = WM_menu_invoke;
944         ot->exec = object_track_clear_exec;
945         
946         ot->poll = ED_operator_objectmode;
947         
948         /* flags */
949         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
950         
951         ot->prop = RNA_def_enum(ot->srna, "type", prop_clear_track_types, 0, "Type", "");
952 }
953
954 /************************** Make Track Operator *****************************/
955
956 static EnumPropertyItem prop_make_track_types[] = {
957         {1, "DAMPTRACK", 0, "Damped Track Constraint", ""},
958         {2, "TRACKTO", 0, "Track To Constraint", ""},
959         {3, "LOCKTRACK", 0, "Lock Track Constraint", ""},
960         {0, NULL, 0, NULL, NULL}
961 };
962
963 static int track_set_exec(bContext *C, wmOperator *op)
964 {
965         Main *bmain = CTX_data_main(C);
966         Scene *scene = CTX_data_scene(C);
967         Object *obact = ED_object_active_context(C);
968         
969         int type = RNA_enum_get(op->ptr, "type");
970         
971         if (type == 1) {
972                 bConstraint *con;
973                 bDampTrackConstraint *data;
974
975                 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
976                 {
977                         if (ob != obact) {
978                                 con = add_ob_constraint(ob, "AutoTrack", CONSTRAINT_TYPE_DAMPTRACK);
979
980                                 data = con->data;
981                                 data->tar = obact;
982                                 ob->recalc |= OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME;
983                                 
984                                 /* Lamp, Camera and Speaker track differently by default */
985                                 if (ob->type == OB_LAMP || ob->type == OB_CAMERA || ob->type == OB_SPEAKER)
986                                         data->trackflag = TRACK_nZ;
987                         }
988                 }
989                 CTX_DATA_END;
990         }
991         else if (type == 2) {
992                 bConstraint *con;
993                 bTrackToConstraint *data;
994
995                 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
996                 {
997                         if (ob != obact) {
998                                 con = add_ob_constraint(ob, "AutoTrack", CONSTRAINT_TYPE_TRACKTO);
999
1000                                 data = con->data;
1001                                 data->tar = obact;
1002                                 ob->recalc |= OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME;
1003                                 
1004                                 /* Lamp, Camera and Speaker track differently by default */
1005                                 if (ob->type == OB_LAMP || ob->type == OB_CAMERA || ob->type == OB_SPEAKER) {
1006                                         data->reserved1 = TRACK_nZ;
1007                                         data->reserved2 = UP_Y;
1008                                 }
1009                         }
1010                 }
1011                 CTX_DATA_END;
1012         }
1013         else if (type == 3) {
1014                 bConstraint *con;
1015                 bLockTrackConstraint *data;
1016
1017                 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1018                 {
1019                         if (ob != obact) {
1020                                 con = add_ob_constraint(ob, "AutoTrack", CONSTRAINT_TYPE_LOCKTRACK);
1021
1022                                 data = con->data;
1023                                 data->tar = obact;
1024                                 ob->recalc |= OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME;
1025                                 
1026                                 /* Lamp, Camera and Speaker track differently by default */
1027                                 if (ob->type == OB_LAMP || ob->type == OB_CAMERA || ob->type == OB_SPEAKER) {
1028                                         data->trackflag = TRACK_nZ;
1029                                         data->lockflag = LOCK_Y;
1030                                 }
1031                         }
1032                 }
1033                 CTX_DATA_END;
1034         }
1035         
1036         DAG_scene_sort(bmain, scene);
1037         DAG_ids_flush_update(bmain, 0);
1038         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1039         
1040         return OPERATOR_FINISHED;
1041 }
1042
1043 void OBJECT_OT_track_set(wmOperatorType *ot)
1044 {
1045         /* identifiers */
1046         ot->name = "Make Track";
1047         ot->description = "Make the object track another object, either by constraint or old way or locked track";
1048         ot->idname = "OBJECT_OT_track_set";
1049         
1050         /* api callbacks */
1051         ot->invoke = WM_menu_invoke;
1052         ot->exec = track_set_exec;
1053         
1054         ot->poll = ED_operator_objectmode;
1055         
1056         /* flags */
1057         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1058         
1059         /* properties */
1060         ot->prop = RNA_def_enum(ot->srna, "type", prop_make_track_types, 0, "Type", "");
1061 }
1062
1063 /************************** Move to Layer Operator *****************************/
1064
1065 static unsigned int move_to_layer_init(bContext *C, wmOperator *op)
1066 {
1067         int values[20], a;
1068         unsigned int lay = 0;
1069
1070         if (!RNA_struct_property_is_set(op->ptr, "layers")) {
1071                 /* note: layers are set in bases, library objects work for this */
1072                 CTX_DATA_BEGIN (C, Base *, base, selected_bases)
1073                 {
1074                         lay |= base->lay;
1075                 }
1076                 CTX_DATA_END;
1077
1078                 for (a = 0; a < 20; a++)
1079                         values[a] = (lay & (1 << a));
1080                 
1081                 RNA_boolean_set_array(op->ptr, "layers", values);
1082         }
1083         else {
1084                 RNA_boolean_get_array(op->ptr, "layers", values);
1085
1086                 for (a = 0; a < 20; a++)
1087                         if (values[a])
1088                                 lay |= (1 << a);
1089         }
1090
1091         return lay;
1092 }
1093
1094 static int move_to_layer_invoke(bContext *C, wmOperator *op, wmEvent *event)
1095 {
1096         View3D *v3d = CTX_wm_view3d(C);
1097         if (v3d && v3d->localvd) {
1098                 return WM_operator_confirm_message(C, op, "Move from localview");
1099         }
1100         else {
1101                 move_to_layer_init(C, op);
1102                 return WM_operator_props_popup(C, op, event);
1103         }
1104 }
1105
1106 static int move_to_layer_exec(bContext *C, wmOperator *op)
1107 {
1108         Main *bmain = CTX_data_main(C);
1109         Scene *scene = CTX_data_scene(C);
1110         View3D *v3d = CTX_wm_view3d(C);
1111         unsigned int lay, local;
1112         /* int is_lamp = FALSE; */ /* UNUSED */
1113         
1114         lay = move_to_layer_init(C, op);
1115         lay &= 0xFFFFFF;
1116
1117         if (lay == 0) return OPERATOR_CANCELLED;
1118         
1119         if (v3d && v3d->localvd) {
1120                 /* now we can move out of localview. */
1121                 /* note: layers are set in bases, library objects work for this */
1122                 CTX_DATA_BEGIN (C, Base *, base, selected_bases)
1123                 {
1124                         lay = base->lay & ~v3d->lay;
1125                         base->lay = lay;
1126                         base->object->lay = lay;
1127                         base->object->flag &= ~SELECT;
1128                         base->flag &= ~SELECT;
1129                         /* if (base->object->type==OB_LAMP) is_lamp = TRUE; */
1130                 }
1131                 CTX_DATA_END;
1132         }
1133         else {
1134                 /* normal non localview operation */
1135                 /* note: layers are set in bases, library objects work for this */
1136                 CTX_DATA_BEGIN (C, Base *, base, selected_bases)
1137                 {
1138                         /* upper byte is used for local view */
1139                         local = base->lay & 0xFF000000;
1140                         base->lay = lay + local;
1141                         base->object->lay = lay;
1142                         /* if (base->object->type==OB_LAMP) is_lamp = TRUE; */
1143                 }
1144                 CTX_DATA_END;
1145         }
1146         
1147         /* warning, active object may be hidden now */
1148         
1149         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, scene);
1150         WM_event_add_notifier(C, NC_SCENE | ND_LAYER_CONTENT, scene);
1151
1152         DAG_scene_sort(bmain, scene);
1153
1154         return OPERATOR_FINISHED;
1155 }
1156
1157 void OBJECT_OT_move_to_layer(wmOperatorType *ot)
1158 {
1159         /* identifiers */
1160         ot->name = "Move to Layer";
1161         ot->description = "Move the object to different layers";
1162         ot->idname = "OBJECT_OT_move_to_layer";
1163         
1164         /* api callbacks */
1165         ot->invoke = move_to_layer_invoke;
1166         ot->exec = move_to_layer_exec;
1167         ot->poll = ED_operator_objectmode;
1168         
1169         /* flags */
1170         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1171         
1172         /* properties */
1173         RNA_def_boolean_layer_member(ot->srna, "layers", 20, NULL, "Layer", "");
1174 }
1175
1176 /************************** Link to Scene Operator *****************************/
1177
1178 #if 0
1179 static void link_to_scene(Main *UNUSED(bmain), unsigned short UNUSED(nr))
1180 {
1181         Scene *sce = (Scene *) BLI_findlink(&bmain->scene, G.curscreen->scenenr - 1);
1182         Base *base, *nbase;
1183         
1184         if (sce == 0) return;
1185         if (sce->id.lib) return;
1186         
1187         for (base = FIRSTBASE; base; base = base->next) {
1188                 if (TESTBASE(v3d, base)) {
1189                         
1190                         nbase = MEM_mallocN(sizeof(Base), "newbase");
1191                         *nbase = *base;
1192                         BLI_addhead(&(sce->base), nbase);
1193                         id_us_plus((ID *)base->object);
1194                 }
1195         }
1196 }
1197 #endif
1198
1199 Base *ED_object_scene_link(Scene *scene, Object *ob)
1200 {
1201         Base *base;
1202
1203         if (BKE_scene_base_find(scene, ob)) {
1204                 return NULL;
1205         }
1206
1207         base = BKE_scene_base_add(scene, ob);
1208         id_us_plus(&ob->id);
1209
1210         return base;
1211 }
1212
1213 static int make_links_scene_exec(bContext *C, wmOperator *op)
1214 {
1215         Main *bmain = CTX_data_main(C);
1216         Scene *scene_to = BLI_findlink(&CTX_data_main(C)->scene, RNA_enum_get(op->ptr, "scene"));
1217
1218         if (scene_to == NULL) {
1219                 BKE_report(op->reports, RPT_ERROR, "Couldn't find scene");
1220                 return OPERATOR_CANCELLED;
1221         }
1222
1223         if (scene_to == CTX_data_scene(C)) {
1224                 BKE_report(op->reports, RPT_ERROR, "Can't link objects into the same scene");
1225                 return OPERATOR_CANCELLED;
1226         }
1227
1228         if (scene_to->id.lib) {
1229                 BKE_report(op->reports, RPT_ERROR, "Can't link objects into a linked scene");
1230                 return OPERATOR_CANCELLED;
1231         }
1232
1233         CTX_DATA_BEGIN (C, Base *, base, selected_bases)
1234         {
1235                 ED_object_scene_link(scene_to, base->object);
1236         }
1237         CTX_DATA_END;
1238
1239         DAG_ids_flush_update(bmain, 0);
1240
1241         /* one day multiple scenes will be visible, then we should have some update function for them */
1242         return OPERATOR_FINISHED;
1243 }
1244
1245 enum {
1246         MAKE_LINKS_OBDATA = 1,
1247         MAKE_LINKS_MATERIALS,
1248         MAKE_LINKS_ANIMDATA,
1249         MAKE_LINKS_DUPLIGROUP,
1250         MAKE_LINKS_MODIFIERS
1251 };
1252
1253 /* Return 1 if make link data is allow, zero otherwise */
1254 static int allow_make_links_data(int ev, Object *ob, Object *obt)
1255 {
1256         switch (ev) {
1257                 case MAKE_LINKS_OBDATA:
1258                         if (ob->type == obt->type && ob->type != OB_EMPTY)
1259                                 return 1;
1260                         break;
1261                 case MAKE_LINKS_MATERIALS:
1262                         if (OB_TYPE_SUPPORT_MATERIAL(ob->type) &&
1263                             OB_TYPE_SUPPORT_MATERIAL(obt->type))
1264                         {
1265                                 return 1;
1266                         }
1267                         break;
1268                 case MAKE_LINKS_ANIMDATA:
1269                 case MAKE_LINKS_DUPLIGROUP:
1270                         return 1;
1271                 case MAKE_LINKS_MODIFIERS:
1272                         if (ob->type != OB_EMPTY && obt->type != OB_EMPTY)
1273                                 return 1;
1274                         break;
1275         }
1276         return 0;
1277 }
1278
1279 static int make_links_data_exec(bContext *C, wmOperator *op)
1280 {
1281         Main *bmain = CTX_data_main(C);
1282         int event = RNA_enum_get(op->ptr, "type");
1283         Object *ob;
1284         ID *id;
1285         int a;
1286
1287         ob = ED_object_active_context(C);
1288
1289         CTX_DATA_BEGIN (C, Object *, obt, selected_editable_objects)
1290         {
1291                 if (ob != obt) {
1292                         if (allow_make_links_data(event, ob, obt)) {
1293                                 switch (event) {
1294                                         case MAKE_LINKS_OBDATA: /* obdata */
1295                                                 id = obt->data;
1296                                                 id->us--;
1297
1298                                                 id = ob->data;
1299                                                 id_us_plus(id);
1300                                                 obt->data = id;
1301
1302                                                 /* if amount of material indices changed: */
1303                                                 test_object_materials(obt->data);
1304
1305                                                 obt->recalc |= OB_RECALC_DATA;
1306                                                 break;
1307                                         case MAKE_LINKS_MATERIALS:
1308                                                 /* new approach, using functions from kernel */
1309                                                 for (a = 0; a < ob->totcol; a++) {
1310                                                         Material *ma = give_current_material(ob, a + 1);
1311                                                         assign_material(obt, ma, a + 1); /* also works with ma==NULL */
1312                                                 }
1313                                                 break;
1314                                         case MAKE_LINKS_ANIMDATA:
1315                                                 BKE_copy_animdata_id((ID *)obt, (ID *)ob, FALSE);
1316                                                 BKE_copy_animdata_id((ID *)obt->data, (ID *)ob->data, FALSE);
1317                                                 break;
1318                                         case MAKE_LINKS_DUPLIGROUP:
1319                                                 obt->dup_group = ob->dup_group;
1320                                                 if (obt->dup_group) {
1321                                                         id_lib_extern(&obt->dup_group->id);
1322                                                         obt->transflag |= OB_DUPLIGROUP;
1323                                                 }
1324                                                 break;
1325                                         case MAKE_LINKS_MODIFIERS:
1326                                                 BKE_object_link_modifiers(obt, ob);
1327                                                 obt->recalc |= OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME;
1328                                                 break;
1329                                 }
1330                         }
1331                 }
1332         }
1333         CTX_DATA_END;
1334
1335         DAG_scene_sort(bmain, CTX_data_scene(C));
1336         
1337         DAG_ids_flush_update(bmain, 0);
1338         WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, CTX_wm_view3d(C));
1339         return OPERATOR_FINISHED;
1340 }
1341
1342
1343 void OBJECT_OT_make_links_scene(wmOperatorType *ot)
1344 {
1345         PropertyRNA *prop;
1346
1347         /* identifiers */
1348         ot->name = "Link Objects to Scene";
1349         ot->description = "Link selection to another scene";
1350         ot->idname = "OBJECT_OT_make_links_scene";
1351
1352         /* api callbacks */
1353         ot->invoke = WM_enum_search_invoke;
1354         ot->exec = make_links_scene_exec;
1355         /* better not run the poll check */
1356
1357         /* flags */
1358         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1359
1360         /* properties */
1361         prop = RNA_def_enum(ot->srna, "scene", DummyRNA_NULL_items, 0, "Scene", "");
1362         RNA_def_enum_funcs(prop, RNA_scene_local_itemf);
1363         ot->prop = prop;
1364 }
1365
1366 void OBJECT_OT_make_links_data(wmOperatorType *ot)
1367 {
1368         static EnumPropertyItem make_links_items[] = {
1369                 {MAKE_LINKS_OBDATA,     "OBDATA", 0, "Object Data", ""},
1370                 {MAKE_LINKS_MATERIALS,  "MATERIAL", 0, "Materials", ""},
1371                 {MAKE_LINKS_ANIMDATA,   "ANIMATION", 0, "Animation Data", ""},
1372                 {MAKE_LINKS_DUPLIGROUP, "DUPLIGROUP", 0, "DupliGroup", ""},
1373                 {MAKE_LINKS_MODIFIERS,  "MODIFIERS", 0, "Modifiers", ""},
1374                 {0, NULL, 0, NULL, NULL}};
1375
1376         /* identifiers */
1377         ot->name = "Link Data";
1378         ot->description = "Make links from the active object to other selected objects";
1379         ot->idname = "OBJECT_OT_make_links_data";
1380
1381         /* api callbacks */
1382         ot->exec = make_links_data_exec;
1383         ot->poll = ED_operator_object_active;
1384
1385         /* flags */
1386         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1387
1388         /* properties */
1389         ot->prop = RNA_def_enum(ot->srna, "type", make_links_items, 0, "Type", "");
1390 }
1391
1392
1393 /**************************** Make Single User ********************************/
1394
1395 static void single_object_users(Scene *scene, View3D *v3d, int flag)    
1396 {
1397         Base *base;
1398         Object *ob, *obn;
1399         
1400         clear_sca_new_poins();  /* sensor/contr/act */
1401
1402         /* duplicate (must set newid) */
1403         for (base = FIRSTBASE; base; base = base->next) {
1404                 ob = base->object;
1405                 
1406                 /* newid may still have some trash from Outliner tree building,
1407                  * so clear that first to avoid errors [#26002]
1408                  */
1409                 ob->id.newid = NULL;
1410                 
1411                 if ( (base->flag & flag) == flag) {
1412                         if (ob->id.lib == NULL && ob->id.us > 1) {
1413                                 /* base gets copy of object */
1414                                 obn = BKE_object_copy(ob);
1415                                 base->object = obn;
1416                                 ob->id.us--;
1417                         }
1418                 }
1419         }
1420         
1421         ID_NEW(scene->camera);
1422         if (v3d) ID_NEW(v3d->camera);
1423         
1424         /* object pointers */
1425         for (base = FIRSTBASE; base; base = base->next) {
1426                 BKE_object_relink(base->object);
1427         }
1428
1429         set_sca_new_poins();
1430 }
1431
1432 /* not an especially efficient function, only added so the single user
1433  * button can be functional.*/
1434 void ED_object_single_user(Scene *scene, Object *ob)
1435 {
1436         Base *base;
1437
1438         for (base = FIRSTBASE; base; base = base->next) {
1439                 if (base->object == ob) base->flag |=  OB_DONE;
1440                 else base->flag &= ~OB_DONE;
1441         }
1442
1443         single_object_users(scene, NULL, OB_DONE);
1444 }
1445
1446 static void new_id_matar(Material **matar, int totcol)
1447 {
1448         ID *id;
1449         int a;
1450         
1451         for (a = 0; a < totcol; a++) {
1452                 id = (ID *)matar[a];
1453                 if (id && id->lib == NULL) {
1454                         if (id->newid) {
1455                                 matar[a] = (Material *)id->newid;
1456                                 id_us_plus(id->newid);
1457                                 id->us--;
1458                         }
1459                         else if (id->us > 1) {
1460                                 matar[a] = BKE_material_copy(matar[a]);
1461                                 id->us--;
1462                                 id->newid = (ID *)matar[a];
1463                         }
1464                 }
1465         }
1466 }
1467
1468 static void single_obdata_users(Main *bmain, Scene *scene, int flag)
1469 {
1470         Object *ob;
1471         Lamp *la;
1472         Curve *cu;
1473         //Camera *cam;
1474         Base *base;
1475         Mesh *me;
1476         ID *id;
1477         int a;
1478
1479         for (base = FIRSTBASE; base; base = base->next) {
1480                 ob = base->object;
1481                 if (ob->id.lib == NULL && (base->flag & flag) == flag) {
1482                         id = ob->data;
1483                         
1484                         if (id && id->us > 1 && id->lib == NULL) {
1485                                 ob->recalc = OB_RECALC_DATA;
1486                                 
1487                                 BKE_copy_animdata_id_action(id);
1488                                 
1489                                 switch (ob->type) {
1490                                         case OB_LAMP:
1491                                                 ob->data = la = BKE_lamp_copy(ob->data);
1492                                                 for (a = 0; a < MAX_MTEX; a++) {
1493                                                         if (la->mtex[a]) {
1494                                                                 ID_NEW(la->mtex[a]->object);
1495                                                         }
1496                                                 }
1497                                                 break;
1498                                         case OB_CAMERA:
1499                                                 ob->data = BKE_camera_copy(ob->data);
1500                                                 break;
1501                                         case OB_MESH:
1502                                                 ob->data = BKE_mesh_copy(ob->data);
1503                                                 //me= ob->data;
1504                                                 //if (me && me->key)
1505                                                 //      ipo_idnew(me->key->ipo);        /* drivers */
1506                                                 break;
1507                                         case OB_MBALL:
1508                                                 ob->data = BKE_mball_copy(ob->data);
1509                                                 break;
1510                                         case OB_CURVE:
1511                                         case OB_SURF:
1512                                         case OB_FONT:
1513                                                 ob->data = cu = BKE_curve_copy(ob->data);
1514                                                 ID_NEW(cu->bevobj);
1515                                                 ID_NEW(cu->taperobj);
1516                                                 break;
1517                                         case OB_LATTICE:
1518                                                 ob->data = BKE_lattice_copy(ob->data);
1519                                                 break;
1520                                         case OB_ARMATURE:
1521                                                 ob->recalc |= OB_RECALC_DATA;
1522                                                 ob->data = BKE_armature_copy(ob->data);
1523                                                 BKE_pose_rebuild(ob, ob->data);
1524                                                 break;
1525                                         case OB_SPEAKER:
1526                                                 ob->data = BKE_speaker_copy(ob->data);
1527                                                 break;
1528                                         default:
1529                                                 if (G.debug & G_DEBUG)
1530                                                         printf("ERROR %s: can't copy %s\n", __func__, id->name);
1531                                                 return;
1532                                 }
1533                                 
1534                                 id->us--;
1535                                 id->newid = ob->data;
1536                                 
1537                         }
1538                         
1539                 }
1540         }
1541         
1542         me = bmain->mesh.first;
1543         while (me) {
1544                 ID_NEW(me->texcomesh);
1545                 me = me->id.next;
1546         }
1547 }
1548
1549 static void single_object_action_users(Scene *scene, int flag)
1550 {
1551         Object *ob;
1552         Base *base;
1553         
1554         for (base = FIRSTBASE; base; base = base->next) {
1555                 ob = base->object;
1556                 if (ob->id.lib == NULL && (flag == 0 || (base->flag & SELECT)) ) {
1557                         ob->recalc = OB_RECALC_DATA;
1558                         BKE_copy_animdata_id_action(&ob->id);
1559                 }
1560         }
1561 }
1562
1563 static void single_mat_users(Scene *scene, int flag, int do_textures)
1564 {
1565         Object *ob;
1566         Base *base;
1567         Material *ma, *man;
1568         Tex *tex;
1569         int a, b;
1570         
1571         for (base = FIRSTBASE; base; base = base->next) {
1572                 ob = base->object;
1573                 if (ob->id.lib == NULL && (flag == 0 || (base->flag & SELECT)) ) {
1574         
1575                         for (a = 1; a <= ob->totcol; a++) {
1576                                 ma = give_current_material(ob, a);
1577                                 if (ma) {
1578                                         /* do not test for LIB_NEW: this functions guaranteed delivers single_users! */
1579                                         
1580                                         if (ma->id.us > 1) {
1581                                                 man = BKE_material_copy(ma);
1582                                                 BKE_copy_animdata_id_action(&man->id);
1583                                                 
1584                                                 man->id.us = 0;
1585                                                 assign_material(ob, man, a);
1586
1587                                                 if (do_textures) {
1588                                                         for (b = 0; b < MAX_MTEX; b++) {
1589                                                                 if (ma->mtex[b] && (tex = ma->mtex[b]->tex)) {
1590                                                                         if (tex->id.us > 1) {
1591                                                                                 tex->id.us--;
1592                                                                                 tex = BKE_texture_copy(tex);
1593                                                                                 BKE_copy_animdata_id_action(&tex->id);
1594                                                                                 man->mtex[b]->tex = tex;
1595                                                                         }
1596                                                                 }
1597                                                         }
1598                                                 }
1599                                         }
1600                                 }
1601                         }
1602                 }
1603         }
1604 }
1605
1606 static void do_single_tex_user(Tex **from)
1607 {
1608         Tex *tex, *texn;
1609         
1610         tex = *from;
1611         if (tex == NULL) return;
1612         
1613         if (tex->id.newid) {
1614                 *from = (Tex *)tex->id.newid;
1615                 id_us_plus(tex->id.newid);
1616                 tex->id.us--;
1617         }
1618         else if (tex->id.us > 1) {
1619                 texn = BKE_texture_copy(tex);
1620                 BKE_copy_animdata_id_action(&texn->id);
1621                 tex->id.newid = (ID *)texn;
1622                 tex->id.us--;
1623                 *from = texn;
1624         }
1625 }
1626
1627 static void single_tex_users_expand(Main *bmain)
1628 {
1629         /* only when 'parent' blocks are LIB_NEW */
1630         Material *ma;
1631         Lamp *la;
1632         World *wo;
1633         int b;
1634                 
1635         for (ma = bmain->mat.first; ma; ma = ma->id.next) {
1636                 if (ma->id.flag & LIB_NEW) {
1637                         for (b = 0; b < MAX_MTEX; b++) {
1638                                 if (ma->mtex[b] && ma->mtex[b]->tex) {
1639                                         do_single_tex_user(&(ma->mtex[b]->tex));
1640                                 }
1641                         }
1642                 }
1643         }
1644
1645         for (la = bmain->lamp.first; la; la = la->id.next) {
1646                 if (la->id.flag & LIB_NEW) {
1647                         for (b = 0; b < MAX_MTEX; b++) {
1648                                 if (la->mtex[b] && la->mtex[b]->tex) {
1649                                         do_single_tex_user(&(la->mtex[b]->tex));
1650                                 }
1651                         }
1652                 }
1653         }
1654
1655         for (wo = bmain->world.first; wo; wo = wo->id.next) {
1656                 if (wo->id.flag & LIB_NEW) {
1657                         for (b = 0; b < MAX_MTEX; b++) {
1658                                 if (wo->mtex[b] && wo->mtex[b]->tex) {
1659                                         do_single_tex_user(&(wo->mtex[b]->tex));
1660                                 }
1661                         }
1662                 }
1663         }
1664 }
1665
1666 static void single_mat_users_expand(Main *bmain)
1667 {
1668         /* only when 'parent' blocks are LIB_NEW */
1669         Object *ob;
1670         Mesh *me;
1671         Curve *cu;
1672         MetaBall *mb;
1673         Material *ma;
1674         int a;
1675         
1676         for (ob = bmain->object.first; ob; ob = ob->id.next)
1677                 if (ob->id.flag & LIB_NEW)
1678                         new_id_matar(ob->mat, ob->totcol);
1679
1680         for (me = bmain->mesh.first; me; me = me->id.next)
1681                 if (me->id.flag & LIB_NEW)
1682                         new_id_matar(me->mat, me->totcol);
1683
1684         for (cu = bmain->curve.first; cu; cu = cu->id.next)
1685                 if (cu->id.flag & LIB_NEW)
1686                         new_id_matar(cu->mat, cu->totcol);
1687
1688         for (mb = bmain->mball.first; mb; mb = mb->id.next)
1689                 if (mb->id.flag & LIB_NEW)
1690                         new_id_matar(mb->mat, mb->totcol);
1691
1692         /* material imats  */
1693         for (ma = bmain->mat.first; ma; ma = ma->id.next)
1694                 if (ma->id.flag & LIB_NEW)
1695                         for (a = 0; a < MAX_MTEX; a++)
1696                                 if (ma->mtex[a])
1697                                         ID_NEW(ma->mtex[a]->object);
1698 }
1699
1700 /* used for copying scenes */
1701 void ED_object_single_users(Main *bmain, Scene *scene, int full)
1702 {
1703         single_object_users(scene, NULL, 0);
1704
1705         if (full) {
1706                 single_obdata_users(bmain, scene, 0);
1707                 single_object_action_users(scene, 0);
1708                 single_mat_users_expand(bmain);
1709                 single_tex_users_expand(bmain);
1710         }
1711
1712         clear_id_newpoins();
1713 }
1714
1715 /******************************* Make Local ***********************************/
1716
1717 /* helper for below, ma was checked to be not NULL */
1718 static void make_local_makelocalmaterial(Material *ma)
1719 {
1720         AnimData *adt;
1721         int b;
1722         
1723         id_make_local(&ma->id, 0);
1724         
1725         for (b = 0; b < MAX_MTEX; b++)
1726                 if (ma->mtex[b] && ma->mtex[b]->tex)
1727                         id_make_local(&ma->mtex[b]->tex->id, 0);
1728         
1729         adt = BKE_animdata_from_id(&ma->id);
1730         if (adt) BKE_animdata_make_local(adt);
1731
1732         /* nodetree? XXX */
1733 }
1734
1735 enum {
1736         MAKE_LOCAL_SELECT_OB,
1737         MAKE_LOCAL_SELECT_OBDATA,
1738         MAKE_LOCAL_SELECT_OBDATA_MATERIAL,
1739         MAKE_LOCAL_ALL
1740 };
1741
1742 static int make_local_exec(bContext *C, wmOperator *op)
1743 {
1744         Main *bmain = CTX_data_main(C);
1745         AnimData *adt;
1746         ParticleSystem *psys;
1747         Material *ma, ***matarar;
1748         Lamp *la;
1749         ID *id;
1750         int a, b, mode = RNA_enum_get(op->ptr, "type");
1751         
1752         if (mode == MAKE_LOCAL_ALL) {
1753                 BKE_library_make_local(bmain, NULL, 0); /* NULL is all libs */
1754                 WM_event_add_notifier(C, NC_WINDOW, NULL);
1755                 return OPERATOR_FINISHED;
1756         }
1757
1758         clear_id_newpoins();
1759         
1760         CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
1761         {
1762                 if (ob->id.lib)
1763                         id_make_local(&ob->id, 0);
1764         }
1765         CTX_DATA_END;
1766         
1767         /* maybe object pointers */
1768         CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
1769         {
1770                 if (ob->id.lib == NULL) {
1771                         ID_NEW(ob->parent);
1772                 }
1773         }
1774         CTX_DATA_END;
1775
1776         CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
1777         {
1778                 id = ob->data;
1779                         
1780                 if (id && (ELEM(mode, MAKE_LOCAL_SELECT_OBDATA, MAKE_LOCAL_SELECT_OBDATA_MATERIAL))) {
1781                         id_make_local(id, 0);
1782                         adt = BKE_animdata_from_id(id);
1783                         if (adt) BKE_animdata_make_local(adt);
1784                         
1785                         /* tag indirect data direct */
1786                         matarar = (Material ***)give_matarar(ob);
1787                         if (matarar) {
1788                                 for (a = 0; a < ob->totcol; a++) {
1789                                         ma = (*matarar)[a];
1790                                         if (ma)
1791                                                 id_lib_extern(&ma->id);
1792                                 }
1793                         }
1794                 }
1795
1796                 for (psys = ob->particlesystem.first; psys; psys = psys->next)
1797                         id_make_local(&psys->part->id, 0);
1798
1799                 adt = BKE_animdata_from_id(&ob->id);
1800                 if (adt) BKE_animdata_make_local(adt);
1801         }
1802         CTX_DATA_END;
1803
1804         if (mode == MAKE_LOCAL_SELECT_OBDATA_MATERIAL) {
1805                 CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
1806                 {
1807                         if (ob->type == OB_LAMP) {
1808                                 la = ob->data;
1809
1810                                 for (b = 0; b < MAX_MTEX; b++)
1811                                         if (la->mtex[b] && la->mtex[b]->tex)
1812                                                 id_make_local(&la->mtex[b]->tex->id, 0);
1813                         }
1814                         else {
1815                                 for (a = 0; a < ob->totcol; a++) {
1816                                         ma = ob->mat[a];
1817                                         if (ma)
1818                                                 make_local_makelocalmaterial(ma);
1819                                 }
1820                                 
1821                                 matarar = (Material ***)give_matarar(ob);
1822                                 if (matarar) {
1823                                         for (a = 0; a < ob->totcol; a++) {
1824                                                 ma = (*matarar)[a];
1825                                                 if (ma)
1826                                                         make_local_makelocalmaterial(ma);
1827                                         }
1828                                 }
1829                         }
1830                 }
1831                 CTX_DATA_END;
1832         }
1833
1834         WM_event_add_notifier(C, NC_WINDOW, NULL);
1835
1836         return OPERATOR_FINISHED;
1837 }
1838
1839 void OBJECT_OT_make_local(wmOperatorType *ot)
1840 {
1841         static EnumPropertyItem type_items[] = {
1842                 {MAKE_LOCAL_SELECT_OB, "SELECT_OBJECT", 0, "Selected Objects", ""},
1843                 {MAKE_LOCAL_SELECT_OBDATA, "SELECT_OBDATA", 0, "Selected Objects and Data", ""},
1844                 {MAKE_LOCAL_SELECT_OBDATA_MATERIAL, "SELECT_OBDATA_MATERIAL", 0, "Selected Objects, Data and Materials", ""},
1845                 {MAKE_LOCAL_ALL, "ALL", 0, "All", ""},
1846                 {0, NULL, 0, NULL, NULL}
1847         };
1848
1849         /* identifiers */
1850         ot->name = "Make Local";
1851         ot->description = "Make library linked datablocks local to this file";
1852         ot->idname = "OBJECT_OT_make_local";
1853         
1854         /* api callbacks */
1855         ot->invoke = WM_menu_invoke;
1856         ot->exec = make_local_exec;
1857         ot->poll = ED_operator_objectmode;
1858         
1859         /* flags */
1860         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1861         
1862         /* properties */
1863         ot->prop = RNA_def_enum(ot->srna, "type", type_items, 0, "Type", "");
1864 }
1865
1866 static int make_single_user_exec(bContext *C, wmOperator *op)
1867 {
1868         Main *bmain = CTX_data_main(C);
1869         Scene *scene = CTX_data_scene(C);
1870         View3D *v3d = CTX_wm_view3d(C); /* ok if this is NULL */
1871         int flag = RNA_enum_get(op->ptr, "type"); /* 0==ALL, SELECTED==selected objecs */
1872
1873         if (RNA_boolean_get(op->ptr, "object"))
1874                 single_object_users(scene, v3d, flag);
1875
1876         if (RNA_boolean_get(op->ptr, "obdata"))
1877                 single_obdata_users(bmain, scene, flag);
1878
1879         if (RNA_boolean_get(op->ptr, "material"))
1880                 single_mat_users(scene, flag, RNA_boolean_get(op->ptr, "texture"));
1881
1882 #if 0 /* can't do this separate from materials */
1883         if (RNA_boolean_get(op->ptr, "texture"))
1884                 single_mat_users(scene, flag, TRUE);
1885 #endif
1886         if (RNA_boolean_get(op->ptr, "animation"))
1887                 single_object_action_users(scene, flag);
1888
1889         clear_id_newpoins();
1890
1891         WM_event_add_notifier(C, NC_WINDOW, NULL);
1892         return OPERATOR_FINISHED;
1893 }
1894
1895 void OBJECT_OT_make_single_user(wmOperatorType *ot)
1896 {
1897         static EnumPropertyItem type_items[] = {
1898                 {SELECT, "SELECTED_OBJECTS", 0, "Selected Objects", ""},
1899                 {0, "ALL", 0, "All", ""},
1900                 {0, NULL, 0, NULL, NULL}};
1901
1902         /* identifiers */
1903         ot->name = "Make Single User";
1904         ot->description = "Make linked data local to each object";
1905         ot->idname = "OBJECT_OT_make_single_user";
1906
1907         /* api callbacks */
1908         ot->invoke = WM_menu_invoke;
1909         ot->exec = make_single_user_exec;
1910         ot->poll = ED_operator_objectmode;
1911
1912         /* flags */
1913         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1914
1915         /* properties */
1916         ot->prop = RNA_def_enum(ot->srna, "type", type_items, SELECT, "Type", "");
1917
1918         RNA_def_boolean(ot->srna, "object", 0, "Object", "Make single user objects");
1919         RNA_def_boolean(ot->srna, "obdata", 0, "Object Data", "Make single user object data");
1920         RNA_def_boolean(ot->srna, "material", 0, "Materials", "Make materials local to each datablock");
1921         RNA_def_boolean(ot->srna, "texture", 0, "Textures", "Make textures local to each material");
1922         RNA_def_boolean(ot->srna, "animation", 0, "Object Animation", "Make animation data local to each object");
1923 }
1924
1925 static int drop_named_material_invoke(bContext *C, wmOperator *op, wmEvent *event)
1926 {
1927         Main *bmain = CTX_data_main(C);
1928         Base *base = ED_view3d_give_base_under_cursor(C, event->mval);
1929         Material *ma;
1930         char name[MAX_ID_NAME - 2];
1931         
1932         RNA_string_get(op->ptr, "name", name);
1933         ma = (Material *)BKE_libblock_find_name(ID_MA, name);
1934         if (base == NULL || ma == NULL)
1935                 return OPERATOR_CANCELLED;
1936         
1937         assign_material(base->object, ma, 1);
1938         
1939         DAG_ids_flush_update(bmain, 0);
1940         WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, CTX_wm_view3d(C));
1941         WM_event_add_notifier(C, NC_MATERIAL | ND_SHADING, ma);
1942         
1943         return OPERATOR_FINISHED;
1944 }
1945
1946 /* used for dropbox */
1947 /* assigns to object under cursor, only first material slot */
1948 void OBJECT_OT_drop_named_material(wmOperatorType *ot)
1949 {
1950
1951         /* identifiers */
1952         ot->name = "Drop Named Material on Object";
1953         ot->description = "";
1954         ot->idname = "OBJECT_OT_drop_named_material";
1955         
1956         /* api callbacks */
1957         ot->invoke = drop_named_material_invoke;
1958         ot->poll = ED_operator_objectmode;
1959         
1960         /* flags */
1961         ot->flag = OPTYPE_UNDO;
1962         
1963         /* properties */
1964         RNA_def_string(ot->srna, "name", "Material", MAX_ID_NAME - 2, "Name", "Material name to assign");
1965 }