4678416e1c917f730171bf11646e1bdd127325c2
[blender.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                                         what_does_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                                         what_does_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 = add_object(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;    /* add_object 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 object_make_proxy */
350                 if (gob == NULL) {
351                         BLI_remlink(&scene->base, oldbase);
352                         MEM_freeN(oldbase);
353                 }
354                 
355                 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(bContext *C, int type)
428 {
429         Main *bmain = CTX_data_main(C);
430         Scene *scene = CTX_data_scene(C);
431
432         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
433         {
434                 if (ob->parent == NULL)
435                         continue;
436                 
437                 if (type == 0) {
438                         ob->parent = NULL;
439                 }
440                 else if (type == 1) {
441                         ob->parent = NULL;
442                         object_apply_mat4(ob, ob->obmat, TRUE, FALSE);
443                 }
444                 else if (type == 2)
445                         unit_m4(ob->parentinv);
446                 
447                 ob->recalc |= OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME;
448         }
449         CTX_DATA_END;
450
451         DAG_scene_sort(bmain, scene);
452         DAG_ids_flush_update(bmain, 0);
453         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
454         WM_event_add_notifier(C, NC_OBJECT | ND_PARENT, NULL);
455 }
456
457 /* note, poll should check for editable scene */
458 static int parent_clear_exec(bContext *C, wmOperator *op)
459 {
460         ED_object_parent_clear(C, RNA_enum_get(op->ptr, "type"));
461
462         return OPERATOR_FINISHED;
463 }
464
465 void OBJECT_OT_parent_clear(wmOperatorType *ot)
466 {
467         /* identifiers */
468         ot->name = "Clear Parent";
469         ot->description = "Clear the object's parenting";
470         ot->idname = "OBJECT_OT_parent_clear";
471         
472         /* api callbacks */
473         ot->invoke = WM_menu_invoke;
474         ot->exec = parent_clear_exec;
475         
476         ot->poll = ED_operator_object_active_editable;
477         
478         /* flags */
479         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
480         
481         ot->prop = RNA_def_enum(ot->srna, "type", prop_clear_parent_types, 0, "Type", "");
482 }
483
484 /* ******************** Make Parent Operator *********************** */
485
486 void ED_object_parent(Object *ob, Object *par, int type, const char *substr)
487 {
488         if (!par || BKE_object_parent_loop_check(par, ob)) {
489                 ob->parent = NULL;
490                 ob->partype = PAROBJECT;
491                 ob->parsubstr[0] = 0;
492                 return;
493         }
494
495         /* this could use some more checks */
496
497         ob->parent = par;
498         ob->partype &= ~PARTYPE;
499         ob->partype |= type;
500         BLI_strncpy(ob->parsubstr, substr, sizeof(ob->parsubstr));
501 }
502
503 /* Operator Property */
504 EnumPropertyItem prop_make_parent_types[] = {
505         {PAR_OBJECT, "OBJECT", 0, "Object", ""},
506         {PAR_ARMATURE, "ARMATURE", 0, "Armature Deform", ""},
507         {PAR_ARMATURE_NAME, "ARMATURE_NAME", 0, "   With Empty Groups", ""},
508         {PAR_ARMATURE_AUTO, "ARMATURE_AUTO", 0, "   With Automatic Weights", ""},
509         {PAR_ARMATURE_ENVELOPE, "ARMATURE_ENVELOPE", 0, "   With Envelope Weights", ""},
510         {PAR_BONE, "BONE", 0, "Bone", ""},
511         {PAR_CURVE, "CURVE", 0, "Curve Deform", ""},
512         {PAR_FOLLOW, "FOLLOW", 0, "Follow Path", ""},
513         {PAR_PATH_CONST, "PATH_CONST", 0, "Path Constraint", ""},
514         {PAR_LATTICE, "LATTICE", 0, "Lattice Deform", ""},
515         {PAR_VERTEX, "VERTEX", 0, "Vertex", ""},
516         {PAR_TRIA, "TRIA", 0, "Triangle", ""},
517         {0, NULL, 0, NULL, NULL}
518 };
519
520 int ED_object_parent_set(ReportList *reports, Main *bmain, Scene *scene, Object *ob, Object *par, int partype)
521 {
522         bPoseChannel *pchan = NULL;
523         int pararm = ELEM4(partype, PAR_ARMATURE, PAR_ARMATURE_NAME, PAR_ARMATURE_ENVELOPE, PAR_ARMATURE_AUTO);
524         
525         par->recalc |= OB_RECALC_OB;
526         
527         /* preconditions */
528         if (partype == PAR_FOLLOW || partype == PAR_PATH_CONST) {
529                 if (par->type != OB_CURVE)
530                         return 0;
531                 else {
532                         Curve *cu = par->data;
533                         
534                         if ((cu->flag & CU_PATH) == 0) {
535                                 cu->flag |= CU_PATH | CU_FOLLOW;
536                                 makeDispListCurveTypes(scene, par, 0);  /* force creation of path data */
537                         }
538                         else cu->flag |= CU_FOLLOW;
539                         
540                         /* if follow, add F-Curve for ctime (i.e. "eval_time") so that path-follow works */
541                         if (partype == PAR_FOLLOW) {
542                                 /* get or create F-Curve */
543                                 bAction *act = verify_adt_action(&cu->id, 1);
544                                 FCurve *fcu = verify_fcurve(act, NULL, "eval_time", 0, 1);
545                                 
546                                 /* setup dummy 'generator' modifier here to get 1-1 correspondence still working */
547                                 if (!fcu->bezt && !fcu->fpt && !fcu->modifiers.first)
548                                         add_fmodifier(&fcu->modifiers, FMODIFIER_TYPE_GENERATOR);
549                         }
550                         
551                         /* fall back on regular parenting now (for follow only) */
552                         if (partype == PAR_FOLLOW)
553                                 partype = PAR_OBJECT;
554                 }               
555         }
556         else if (partype == PAR_BONE) {
557                 pchan = get_active_posechannel(par);
558                 
559                 if (pchan == NULL) {
560                         BKE_report(reports, RPT_ERROR, "No active Bone");
561                         return 0;
562                 }
563         }
564         
565         if (ob != par) {
566                 if (BKE_object_parent_loop_check(par, ob)) {
567                         BKE_report(reports, RPT_ERROR, "Loop in parents");
568                         return 0;
569                 }
570                 else {
571                         Object workob;
572                         
573                         /* apply transformation of previous parenting */
574                         /* object_apply_mat4(ob, ob->obmat); */ /* removed because of bug [#23577] */
575                         
576                         /* set the parent (except for follow-path constraint option) */
577                         if (partype != PAR_PATH_CONST) {
578                                 ob->parent = par;
579                         }
580                         
581                         /* handle types */
582                         if (pchan)
583                                 BLI_strncpy(ob->parsubstr, pchan->name, sizeof(ob->parsubstr));
584                         else
585                                 ob->parsubstr[0] = 0;
586                                 
587                         if (partype == PAR_PATH_CONST) {
588                                 /* don't do anything here, since this is not technically "parenting" */
589                         }
590                         else if (ELEM(partype, PAR_CURVE, PAR_LATTICE) || (pararm)) {
591                                 /* partype is now set to PAROBJECT so that invisible 'virtual' modifiers don't need to be created
592                                  * NOTE: the old (2.4x) method was to set ob->partype = PARSKEL, creating the virtual modifiers
593                                  */
594                                 ob->partype = PAROBJECT; /* note, dna define, not operator property */
595                                 //ob->partype= PARSKEL; /* note, dna define, not operator property */
596                                 
597                                 /* BUT, to keep the deforms, we need a modifier, and then we need to set the object that it uses */
598                                 // XXX currently this should only happen for meshes, curves, surfaces, and lattices - this stuff isn't available for metas yet
599                                 if (ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_LATTICE)) {
600                                         ModifierData *md;
601                                         
602                                         switch (partype) {
603                                                 case PAR_CURVE: /* curve deform */
604                                                         md = ED_object_modifier_add(reports, bmain, scene, ob, NULL, eModifierType_Curve);
605                                                         ((CurveModifierData *)md)->object = par;
606                                                         break;
607                                                 case PAR_LATTICE: /* lattice deform */
608                                                         md = ED_object_modifier_add(reports, bmain, scene, ob, NULL, eModifierType_Lattice);
609                                                         ((LatticeModifierData *)md)->object = par;
610                                                         break;
611                                                 default: /* armature deform */
612                                                         md = ED_object_modifier_add(reports, bmain, scene, ob, NULL, eModifierType_Armature);
613                                                         ((ArmatureModifierData *)md)->object = par;
614                                                         break;
615                                         }
616                                 }
617                         }
618                         else if (partype == PAR_BONE)
619                                 ob->partype = PARBONE;  /* note, dna define, not operator property */
620                         else
621                                 ob->partype = PAROBJECT;  /* note, dna define, not operator property */
622                         
623                         /* constraint */
624                         if (partype == PAR_PATH_CONST) {
625                                 bConstraint *con;
626                                 bFollowPathConstraint *data;
627                                 float cmat[4][4], vec[3];
628                                 
629                                 con = add_ob_constraint(ob, "AutoPath", CONSTRAINT_TYPE_FOLLOWPATH);
630                                 
631                                 data = con->data;
632                                 data->tar = par;
633                                 
634                                 get_constraint_target_matrix(scene, con, 0, CONSTRAINT_OBTYPE_OBJECT, NULL, cmat, scene->r.cfra);
635                                 sub_v3_v3v3(vec, ob->obmat[3], cmat[3]);
636                                 
637                                 ob->loc[0] = vec[0];
638                                 ob->loc[1] = vec[1];
639                                 ob->loc[2] = vec[2];
640                         }
641                         else if (pararm && ob->type == OB_MESH && par->type == OB_ARMATURE) {
642                                 if (partype == PAR_ARMATURE_NAME)
643                                         create_vgroups_from_armature(reports, scene, ob, par, ARM_GROUPS_NAME, 0);
644                                 else if (partype == PAR_ARMATURE_ENVELOPE)
645                                         create_vgroups_from_armature(reports, scene, ob, par, ARM_GROUPS_ENVELOPE, 0);
646                                 else if (partype == PAR_ARMATURE_AUTO) {
647                                         WM_cursor_wait(1);
648                                         create_vgroups_from_armature(reports, scene, ob, par, ARM_GROUPS_AUTO, 0);
649                                         WM_cursor_wait(0);
650                                 }
651                                 /* get corrected inverse */
652                                 ob->partype = PAROBJECT;
653                                 what_does_parent(scene, ob, &workob);
654                                 
655                                 invert_m4_m4(ob->parentinv, workob.obmat);
656                         }
657                         else {
658                                 /* calculate inverse parent matrix */
659                                 what_does_parent(scene, ob, &workob);
660                                 invert_m4_m4(ob->parentinv, workob.obmat);
661                         }
662                         
663                         ob->recalc |= OB_RECALC_OB | OB_RECALC_DATA;
664                 }
665         }
666
667         return 1;
668 }
669
670 static int parent_set_exec(bContext *C, wmOperator *op)
671 {
672         Main *bmain = CTX_data_main(C);
673         Scene *scene = CTX_data_scene(C);
674         Object *par = ED_object_active_context(C);
675         int partype = RNA_enum_get(op->ptr, "type");
676         int ok = 1;
677
678         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
679         {
680                 if (!ED_object_parent_set(op->reports, bmain, scene, ob, par, partype)) {
681                         ok = 0;
682                         break;
683                 }
684         }
685         CTX_DATA_END;
686
687         if (!ok)
688                 return OPERATOR_CANCELLED;
689
690         DAG_scene_sort(bmain, scene);
691         DAG_ids_flush_update(bmain, 0);
692         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
693         WM_event_add_notifier(C, NC_OBJECT | ND_PARENT, NULL);
694
695         return OPERATOR_FINISHED;
696 }
697
698
699 static int parent_set_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *UNUSED(event))
700 {
701         Object *ob = ED_object_active_context(C);
702         uiPopupMenu *pup = uiPupMenuBegin(C, "Set Parent To", ICON_NONE);
703         uiLayout *layout = uiPupMenuLayout(pup);
704         
705         uiLayoutSetOperatorContext(layout, WM_OP_EXEC_DEFAULT);
706         uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_OBJECT);
707         
708         /* ob becomes parent, make the associated menus */
709         if (ob->type == OB_ARMATURE) {
710                 uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_ARMATURE);
711                 uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_ARMATURE_NAME);
712                 uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_ARMATURE_ENVELOPE);
713                 uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_ARMATURE_AUTO);
714                 uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_BONE);
715         }
716         else if (ob->type == OB_CURVE) {
717                 uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_CURVE);
718                 uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_FOLLOW);
719                 uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_PATH_CONST);
720         }
721         else if (ob->type == OB_LATTICE) {
722                 uiItemEnumO(layout, "OBJECT_OT_parent_set", NULL, 0, "type", PAR_LATTICE);
723         }
724         
725         uiPupMenuEnd(C, pup);
726         
727         return OPERATOR_CANCELLED;
728 }
729
730
731 void OBJECT_OT_parent_set(wmOperatorType *ot)
732 {
733         /* identifiers */
734         ot->name = "Make Parent";
735         ot->description = "Set the object's parenting";
736         ot->idname = "OBJECT_OT_parent_set";
737         
738         /* api callbacks */
739         ot->invoke = parent_set_invoke;
740         ot->exec = parent_set_exec;
741         
742         ot->poll = ED_operator_object_active;
743         
744         /* flags */
745         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
746         
747         RNA_def_enum(ot->srna, "type", prop_make_parent_types, 0, "Type", "");
748 }
749
750 /* ************ Make Parent Without Inverse Operator ******************* */
751
752 static int parent_noinv_set_exec(bContext *C, wmOperator *op)
753 {
754         Main *bmain = CTX_data_main(C);
755         Object *par = ED_object_active_context(C);
756         
757         par->recalc |= OB_RECALC_OB;
758         
759         /* context iterator */
760         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
761         {
762                 if (ob != par) {
763                         if (BKE_object_parent_loop_check(par, ob)) {
764                                 BKE_report(op->reports, RPT_ERROR, "Loop in parents");
765                         }
766                         else {
767                                 /* clear inverse matrix and also the object location */
768                                 unit_m4(ob->parentinv);
769                                 memset(ob->loc, 0, 3 * sizeof(float));
770                                 
771                                 /* set recalc flags */
772                                 ob->recalc |= OB_RECALC_OB | OB_RECALC_DATA;
773                                 
774                                 /* set parenting type for object - object only... */
775                                 ob->parent = par;
776                                 ob->partype = PAROBJECT; /* note, dna define, not operator property */
777                         }
778                 }
779         }
780         CTX_DATA_END;
781         
782         DAG_scene_sort(bmain, CTX_data_scene(C));
783         DAG_ids_flush_update(bmain, 0);
784         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
785         
786         return OPERATOR_FINISHED;
787 }
788
789 void OBJECT_OT_parent_no_inverse_set(wmOperatorType *ot)
790 {
791         /* identifiers */
792         ot->name = "Make Parent without Inverse";
793         ot->description = "Set the object's parenting without setting the inverse parent correction";
794         ot->idname = "OBJECT_OT_parent_no_inverse_set";
795         
796         /* api callbacks */
797         ot->invoke = WM_operator_confirm;
798         ot->exec = parent_noinv_set_exec;
799         ot->poll = ED_operator_object_active_editable;
800         
801         /* flags */
802         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
803 }
804
805 /************************ Clear Slow Parent Operator *********************/
806
807 static int object_slow_parent_clear_exec(bContext *C, wmOperator *UNUSED(op))
808 {
809         Main *bmain = CTX_data_main(C);
810         Scene *scene = CTX_data_scene(C);
811
812         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
813         {
814                 if (ob->parent) {
815                         if (ob->partype & PARSLOW) {
816                                 ob->partype -= PARSLOW;
817                                 where_is_object(scene, ob);
818                                 ob->partype |= PARSLOW;
819                                 ob->recalc |= OB_RECALC_OB;
820                         }
821                 }
822         }
823         CTX_DATA_END;
824
825         DAG_ids_flush_update(bmain, 0);
826         WM_event_add_notifier(C, NC_SCENE, scene);
827         
828         return OPERATOR_FINISHED;
829 }
830
831 void OBJECT_OT_slow_parent_clear(wmOperatorType *ot)
832 {
833         
834         /* identifiers */
835         ot->name = "Clear Slow Parent";
836         ot->description = "Clear the object's slow parent";
837         ot->idname = "OBJECT_OT_slow_parent_clear";
838         
839         /* api callbacks */
840         ot->invoke = WM_operator_confirm;
841         ot->exec = object_slow_parent_clear_exec;
842         ot->poll = ED_operator_view3d_active;
843         
844         /* flags */
845         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
846 }
847
848 /********************** Make Slow Parent Operator *********************/
849
850 static int object_slow_parent_set_exec(bContext *C, wmOperator *UNUSED(op))
851 {
852         Main *bmain = CTX_data_main(C);
853         Scene *scene = CTX_data_scene(C);
854
855         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
856         {
857                 if (ob->parent)
858                         ob->partype |= PARSLOW;
859
860                 ob->recalc |= OB_RECALC_OB;
861                 
862         }
863         CTX_DATA_END;
864
865         DAG_ids_flush_update(bmain, 0);
866         WM_event_add_notifier(C, NC_SCENE, scene);
867         
868         return OPERATOR_FINISHED;
869 }
870
871 void OBJECT_OT_slow_parent_set(wmOperatorType *ot)
872 {
873         
874         /* identifiers */
875         ot->name = "Set Slow Parent";
876         ot->description = "Set the object's slow parent";
877         ot->idname = "OBJECT_OT_slow_parent_set";
878         
879         /* api callbacks */
880         ot->invoke = WM_operator_confirm;
881         ot->exec = object_slow_parent_set_exec;
882         ot->poll = ED_operator_view3d_active;
883         
884         /* flags */
885         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
886 }
887
888 /* ******************** Clear Track Operator ******************* */
889
890 static EnumPropertyItem prop_clear_track_types[] = {
891         {0, "CLEAR", 0, "Clear Track", ""},
892         {1, "CLEAR_KEEP_TRANSFORM", 0, "Clear and Keep Transformation (Clear Track)", ""},
893         {0, NULL, 0, NULL, NULL}
894 };
895
896 /* note, poll should check for editable scene */
897 static int object_track_clear_exec(bContext *C, wmOperator *op)
898 {
899         Main *bmain = CTX_data_main(C);
900         Scene *scene = CTX_data_scene(C);
901         int type = RNA_enum_get(op->ptr, "type");
902
903         if (CTX_data_edit_object(C)) {
904                 BKE_report(op->reports, RPT_ERROR, "Operation cannot be performed in EditMode");
905                 return OPERATOR_CANCELLED;
906         }
907         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
908         {
909                 bConstraint *con, *pcon;
910                 
911                 /* remove track-object for old track */
912                 ob->track = NULL;
913                 ob->recalc |= OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME;
914                 
915                 /* also remove all tracking constraints */
916                 for (con = ob->constraints.last; con; con = pcon) {
917                         pcon = con->prev;
918                         if (ELEM3(con->type, CONSTRAINT_TYPE_TRACKTO, CONSTRAINT_TYPE_LOCKTRACK, CONSTRAINT_TYPE_DAMPTRACK))
919                                 remove_constraint(&ob->constraints, con);
920                 }
921                 
922                 if (type == 1)
923                         object_apply_mat4(ob, ob->obmat, TRUE, TRUE);
924         }
925         CTX_DATA_END;
926
927         DAG_ids_flush_update(bmain, 0);
928         DAG_scene_sort(bmain, scene);
929         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
930
931         return OPERATOR_FINISHED;
932 }
933
934 void OBJECT_OT_track_clear(wmOperatorType *ot)
935 {
936         /* identifiers */
937         ot->name = "Clear track";
938         ot->description = "Clear tracking constraint or flag from object";
939         ot->idname = "OBJECT_OT_track_clear";
940         
941         /* api callbacks */
942         ot->invoke = WM_menu_invoke;
943         ot->exec = object_track_clear_exec;
944         
945         ot->poll = ED_operator_objectmode;
946         
947         /* flags */
948         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
949         
950         ot->prop = RNA_def_enum(ot->srna, "type", prop_clear_track_types, 0, "Type", "");
951 }
952
953 /************************** Make Track Operator *****************************/
954
955 static EnumPropertyItem prop_make_track_types[] = {
956         {1, "DAMPTRACK", 0, "Damped Track Constraint", ""},
957         {2, "TRACKTO", 0, "Track To Constraint", ""},
958         {3, "LOCKTRACK", 0, "Lock Track Constraint", ""},
959         {0, NULL, 0, NULL, NULL}
960 };
961
962 static int track_set_exec(bContext *C, wmOperator *op)
963 {
964         Main *bmain = CTX_data_main(C);
965         Scene *scene = CTX_data_scene(C);
966         Object *obact = ED_object_active_context(C);
967         
968         int type = RNA_enum_get(op->ptr, "type");
969         
970         if (type == 1) {
971                 bConstraint *con;
972                 bDampTrackConstraint *data;
973
974                 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
975                 {
976                         if (ob != obact) {
977                                 con = add_ob_constraint(ob, "AutoTrack", CONSTRAINT_TYPE_DAMPTRACK);
978
979                                 data = con->data;
980                                 data->tar = obact;
981                                 ob->recalc |= OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME;
982                                 
983                                 /* Lamp, Camera and Speaker track differently by default */
984                                 if (ob->type == OB_LAMP || ob->type == OB_CAMERA || ob->type == OB_SPEAKER)
985                                         data->trackflag = TRACK_nZ;
986                         }
987                 }
988                 CTX_DATA_END;
989         }
990         else if (type == 2) {
991                 bConstraint *con;
992                 bTrackToConstraint *data;
993
994                 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
995                 {
996                         if (ob != obact) {
997                                 con = add_ob_constraint(ob, "AutoTrack", CONSTRAINT_TYPE_TRACKTO);
998
999                                 data = con->data;
1000                                 data->tar = obact;
1001                                 ob->recalc |= OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME;
1002                                 
1003                                 /* Lamp, Camera and Speaker track differently by default */
1004                                 if (ob->type == OB_LAMP || ob->type == OB_CAMERA || ob->type == OB_SPEAKER) {
1005                                         data->reserved1 = TRACK_nZ;
1006                                         data->reserved2 = UP_Y;
1007                                 }
1008                         }
1009                 }
1010                 CTX_DATA_END;
1011         }
1012         else if (type == 3) {
1013                 bConstraint *con;
1014                 bLockTrackConstraint *data;
1015
1016                 CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
1017                 {
1018                         if (ob != obact) {
1019                                 con = add_ob_constraint(ob, "AutoTrack", CONSTRAINT_TYPE_LOCKTRACK);
1020
1021                                 data = con->data;
1022                                 data->tar = obact;
1023                                 ob->recalc |= OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME;
1024                                 
1025                                 /* Lamp, Camera and Speaker track differently by default */
1026                                 if (ob->type == OB_LAMP || ob->type == OB_CAMERA || ob->type == OB_SPEAKER) {
1027                                         data->trackflag = TRACK_nZ;
1028                                         data->lockflag = LOCK_Y;
1029                                 }
1030                         }
1031                 }
1032                 CTX_DATA_END;
1033         }
1034         
1035         DAG_scene_sort(bmain, scene);
1036         DAG_ids_flush_update(bmain, 0);
1037         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
1038         
1039         return OPERATOR_FINISHED;
1040 }
1041
1042 void OBJECT_OT_track_set(wmOperatorType *ot)
1043 {
1044         /* identifiers */
1045         ot->name = "Make Track";
1046         ot->description = "Make the object track another object, either by constraint or old way or locked track";
1047         ot->idname = "OBJECT_OT_track_set";
1048         
1049         /* api callbacks */
1050         ot->invoke = WM_menu_invoke;
1051         ot->exec = track_set_exec;
1052         
1053         ot->poll = ED_operator_objectmode;
1054         
1055         /* flags */
1056         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1057         
1058         /* properties */
1059         ot->prop = RNA_def_enum(ot->srna, "type", prop_make_track_types, 0, "Type", "");
1060 }
1061
1062 /************************** Move to Layer Operator *****************************/
1063
1064 static unsigned int move_to_layer_init(bContext *C, wmOperator *op)
1065 {
1066         int values[20], a;
1067         unsigned int lay = 0;
1068
1069         if (!RNA_struct_property_is_set(op->ptr, "layers")) {
1070                 /* note: layers are set in bases, library objects work for this */
1071                 CTX_DATA_BEGIN (C, Base *, base, selected_bases)
1072                 {
1073                         lay |= base->lay;
1074                 }
1075                 CTX_DATA_END;
1076
1077                 for (a = 0; a < 20; a++)
1078                         values[a] = (lay & (1 << a));
1079                 
1080                 RNA_boolean_set_array(op->ptr, "layers", values);
1081         }
1082         else {
1083                 RNA_boolean_get_array(op->ptr, "layers", values);
1084
1085                 for (a = 0; a < 20; a++)
1086                         if (values[a])
1087                                 lay |= (1 << a);
1088         }
1089
1090         return lay;
1091 }
1092
1093 static int move_to_layer_invoke(bContext *C, wmOperator *op, wmEvent *event)
1094 {
1095         View3D *v3d = CTX_wm_view3d(C);
1096         if (v3d && v3d->localvd) {
1097                 return WM_operator_confirm_message(C, op, "Move from localview");
1098         }
1099         else {
1100                 move_to_layer_init(C, op);
1101                 return WM_operator_props_popup(C, op, event);
1102         }
1103 }
1104
1105 static int move_to_layer_exec(bContext *C, wmOperator *op)
1106 {
1107         Main *bmain = CTX_data_main(C);
1108         Scene *scene = CTX_data_scene(C);
1109         View3D *v3d = CTX_wm_view3d(C);
1110         unsigned int lay, local;
1111         /* int islamp= 0; */ /* UNUSED */
1112         
1113         lay = move_to_layer_init(C, op);
1114         lay &= 0xFFFFFF;
1115
1116         if (lay == 0) return OPERATOR_CANCELLED;
1117         
1118         if (v3d && v3d->localvd) {
1119                 /* now we can move out of localview. */
1120                 /* note: layers are set in bases, library objects work for this */
1121                 CTX_DATA_BEGIN (C, Base *, base, selected_bases)
1122                 {
1123                         lay = base->lay & ~v3d->lay;
1124                         base->lay = lay;
1125                         base->object->lay = lay;
1126                         base->object->flag &= ~SELECT;
1127                         base->flag &= ~SELECT;
1128                         /* if (base->object->type==OB_LAMP) islamp= 1; */
1129                 }
1130                 CTX_DATA_END;
1131         }
1132         else {
1133                 /* normal non localview operation */
1134                 /* note: layers are set in bases, library objects work for this */
1135                 CTX_DATA_BEGIN (C, Base *, base, selected_bases)
1136                 {
1137                         /* upper byte is used for local view */
1138                         local = base->lay & 0xFF000000;
1139                         base->lay = lay + local;
1140                         base->object->lay = lay;
1141                         /* if (base->object->type==OB_LAMP) islamp= 1; */
1142                 }
1143                 CTX_DATA_END;
1144         }
1145         
1146         /* warning, active object may be hidden now */
1147         
1148         WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, scene);
1149         WM_event_add_notifier(C, NC_SCENE | ND_LAYER_CONTENT, scene);
1150
1151         DAG_scene_sort(bmain, scene);
1152
1153         return OPERATOR_FINISHED;
1154 }
1155
1156 void OBJECT_OT_move_to_layer(wmOperatorType *ot)
1157 {
1158         /* identifiers */
1159         ot->name = "Move to Layer";
1160         ot->description = "Move the object to different layers";
1161         ot->idname = "OBJECT_OT_move_to_layer";
1162         
1163         /* api callbacks */
1164         ot->invoke = move_to_layer_invoke;
1165         ot->exec = move_to_layer_exec;
1166         ot->poll = ED_operator_objectmode;
1167         
1168         /* flags */
1169         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1170         
1171         /* properties */
1172         RNA_def_boolean_layer_member(ot->srna, "layers", 20, NULL, "Layer", "");
1173 }
1174
1175 /************************** Link to Scene Operator *****************************/
1176
1177 #if 0
1178 static void link_to_scene(Main *UNUSED(bmain), unsigned short UNUSED(nr))
1179 {
1180         Scene *sce = (Scene *) BLI_findlink(&bmain->scene, G.curscreen->scenenr - 1);
1181         Base *base, *nbase;
1182         
1183         if (sce == 0) return;
1184         if (sce->id.lib) return;
1185         
1186         for (base = FIRSTBASE; base; base = base->next) {
1187                 if (TESTBASE(v3d, base)) {
1188                         
1189                         nbase = MEM_mallocN(sizeof(Base), "newbase");
1190                         *nbase = *base;
1191                         BLI_addhead(&(sce->base), nbase);
1192                         id_us_plus((ID *)base->object);
1193                 }
1194         }
1195 }
1196 #endif
1197
1198 static int make_links_scene_exec(bContext *C, wmOperator *op)
1199 {
1200         Main *bmain = CTX_data_main(C);
1201         Scene *scene_to = BLI_findlink(&CTX_data_main(C)->scene, RNA_enum_get(op->ptr, "scene"));
1202
1203         if (scene_to == NULL) {
1204                 BKE_report(op->reports, RPT_ERROR, "Scene not found");
1205                 return OPERATOR_CANCELLED;
1206         }
1207
1208         if (scene_to == CTX_data_scene(C)) {
1209                 BKE_report(op->reports, RPT_ERROR, "Can't link objects into the same scene");
1210                 return OPERATOR_CANCELLED;
1211         }
1212
1213         if (scene_to->id.lib) {
1214                 BKE_report(op->reports, RPT_ERROR, "Can't link objects into a linked scene");
1215                 return OPERATOR_CANCELLED;
1216         }
1217
1218         CTX_DATA_BEGIN (C, Base *, base, selected_bases)
1219         {
1220                 if (!object_in_scene(base->object, scene_to)) {
1221                         Base *nbase = MEM_mallocN(sizeof(Base), "newbase");
1222                         *nbase = *base;
1223                         BLI_addhead(&(scene_to->base), nbase);
1224                         id_us_plus((ID *)base->object);
1225                 }
1226         }
1227         CTX_DATA_END;
1228
1229         DAG_ids_flush_update(bmain, 0);
1230
1231         /* one day multiple scenes will be visible, then we should have some update function for them */
1232         return OPERATOR_FINISHED;
1233 }
1234
1235 enum {
1236         MAKE_LINKS_OBDATA = 1,
1237         MAKE_LINKS_MATERIALS,
1238         MAKE_LINKS_ANIMDATA,
1239         MAKE_LINKS_DUPLIGROUP,
1240         MAKE_LINKS_MODIFIERS
1241 };
1242
1243 /* Return 1 if make link data is allow, zero otherwise */
1244 static int allow_make_links_data(int ev, Object *ob, Object *obt)
1245 {
1246         switch (ev) {
1247                 case MAKE_LINKS_OBDATA:
1248                         if (ob->type == obt->type && ob->type != OB_EMPTY)
1249                                 return 1;
1250                         break;
1251                 case MAKE_LINKS_MATERIALS:
1252                         if (OB_TYPE_SUPPORT_MATERIAL(ob->type) &&
1253                             OB_TYPE_SUPPORT_MATERIAL(obt->type))
1254                         {
1255                                 return 1;
1256                         }
1257                         break;
1258                 case MAKE_LINKS_ANIMDATA:
1259                 case MAKE_LINKS_DUPLIGROUP:
1260                         return 1;
1261                 case MAKE_LINKS_MODIFIERS:
1262                         if (ob->type != OB_EMPTY && obt->type != OB_EMPTY)
1263                                 return 1;
1264                         break;
1265         }
1266         return 0;
1267 }
1268
1269 static int make_links_data_exec(bContext *C, wmOperator *op)
1270 {
1271         Main *bmain = CTX_data_main(C);
1272         int event = RNA_enum_get(op->ptr, "type");
1273         Object *ob;
1274         ID *id;
1275         int a;
1276
1277         ob = ED_object_active_context(C);
1278
1279         CTX_DATA_BEGIN (C, Object *, obt, selected_editable_objects)
1280         {
1281                 if (ob != obt) {
1282                         if (allow_make_links_data(event, ob, obt)) {
1283                                 switch (event) {
1284                                         case MAKE_LINKS_OBDATA: /* obdata */
1285                                                 id = obt->data;
1286                                                 id->us--;
1287
1288                                                 id = ob->data;
1289                                                 id_us_plus(id);
1290                                                 obt->data = id;
1291
1292                                                 /* if amount of material indices changed: */
1293                                                 test_object_materials(obt->data);
1294
1295                                                 obt->recalc |= OB_RECALC_DATA;
1296                                                 break;
1297                                         case MAKE_LINKS_MATERIALS:
1298                                                 /* new approach, using functions from kernel */
1299                                                 for (a = 0; a < ob->totcol; a++) {
1300                                                         Material *ma = give_current_material(ob, a + 1);
1301                                                         assign_material(obt, ma, a + 1); /* also works with ma==NULL */
1302                                                 }
1303                                                 break;
1304                                         case MAKE_LINKS_ANIMDATA:
1305                                                 BKE_copy_animdata_id((ID *)obt, (ID *)ob, FALSE);
1306                                                 BKE_copy_animdata_id((ID *)obt->data, (ID *)ob->data, FALSE);
1307                                                 break;
1308                                         case MAKE_LINKS_DUPLIGROUP:
1309                                                 obt->dup_group = ob->dup_group;
1310                                                 if (obt->dup_group) {
1311                                                         id_lib_extern(&obt->dup_group->id);
1312                                                         obt->transflag |= OB_DUPLIGROUP;
1313                                                 }
1314                                                 break;
1315                                         case MAKE_LINKS_MODIFIERS:
1316                                                 object_link_modifiers(obt, ob);
1317                                                 obt->recalc |= OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME;
1318                                                 break;
1319                                 }
1320                         }
1321                 }
1322         }
1323         CTX_DATA_END;
1324
1325         DAG_scene_sort(bmain, CTX_data_scene(C));
1326         
1327         DAG_ids_flush_update(bmain, 0);
1328         WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, CTX_wm_view3d(C));
1329         return OPERATOR_FINISHED;
1330 }
1331
1332
1333 void OBJECT_OT_make_links_scene(wmOperatorType *ot)
1334 {
1335         PropertyRNA *prop;
1336
1337         /* identifiers */
1338         ot->name = "Link Objects to Scene";
1339         ot->description = "Link selection to another scene";
1340         ot->idname = "OBJECT_OT_make_links_scene";
1341
1342         /* api callbacks */
1343         ot->invoke = WM_enum_search_invoke;
1344         ot->exec = make_links_scene_exec;
1345         /* better not run the poll check */
1346
1347         /* flags */
1348         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1349
1350         /* properties */
1351         prop = RNA_def_enum(ot->srna, "scene", DummyRNA_NULL_items, 0, "Scene", "");
1352         RNA_def_enum_funcs(prop, RNA_scene_local_itemf);
1353         ot->prop = prop;
1354 }
1355
1356 void OBJECT_OT_make_links_data(wmOperatorType *ot)
1357 {
1358         static EnumPropertyItem make_links_items[] = {
1359                 {MAKE_LINKS_OBDATA,     "OBDATA", 0, "Object Data", ""},
1360                 {MAKE_LINKS_MATERIALS,  "MATERIAL", 0, "Materials", ""},
1361                 {MAKE_LINKS_ANIMDATA,   "ANIMATION", 0, "Animation Data", ""},
1362                 {MAKE_LINKS_DUPLIGROUP, "DUPLIGROUP", 0, "DupliGroup", ""},
1363                 {MAKE_LINKS_MODIFIERS,  "MODIFIERS", 0, "Modifiers", ""},
1364                 {0, NULL, 0, NULL, NULL}};
1365
1366         /* identifiers */
1367         ot->name = "Link Data";
1368         ot->description = "Make links from the active object to other selected objects";
1369         ot->idname = "OBJECT_OT_make_links_data";
1370
1371         /* api callbacks */
1372         ot->exec = make_links_data_exec;
1373         ot->poll = ED_operator_object_active;
1374
1375         /* flags */
1376         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1377
1378         /* properties */
1379         ot->prop = RNA_def_enum(ot->srna, "type", make_links_items, 0, "Type", "");
1380 }
1381
1382
1383 /**************************** Make Single User ********************************/
1384
1385 static void single_object_users(Scene *scene, View3D *v3d, int flag)    
1386 {
1387         Base *base;
1388         Object *ob, *obn;
1389         
1390         clear_sca_new_poins();  /* sensor/contr/act */
1391
1392         /* duplicate (must set newid) */
1393         for (base = FIRSTBASE; base; base = base->next) {
1394                 ob = base->object;
1395                 
1396                 /* newid may still have some trash from Outliner tree building,
1397                  * so clear that first to avoid errors [#26002]
1398                  */
1399                 ob->id.newid = NULL;
1400                 
1401                 if ( (base->flag & flag) == flag) {
1402                         if (ob->id.lib == NULL && ob->id.us > 1) {
1403                                 /* base gets copy of object */
1404                                 obn = copy_object(ob);
1405                                 base->object = obn;
1406                                 ob->id.us--;
1407                         }
1408                 }
1409         }
1410         
1411         ID_NEW(scene->camera);
1412         if (v3d) ID_NEW(v3d->camera);
1413         
1414         /* object pointers */
1415         for (base = FIRSTBASE; base; base = base->next) {
1416                 object_relink(base->object);
1417         }
1418
1419         set_sca_new_poins();
1420 }
1421
1422 /* not an especially efficient function, only added so the single user
1423  * button can be functional.*/
1424 void ED_object_single_user(Scene *scene, Object *ob)
1425 {
1426         Base *base;
1427
1428         for (base = FIRSTBASE; base; base = base->next) {
1429                 if (base->object == ob) base->flag |=  OB_DONE;
1430                 else base->flag &= ~OB_DONE;
1431         }
1432
1433         single_object_users(scene, NULL, OB_DONE);
1434 }
1435
1436 static void new_id_matar(Material **matar, int totcol)
1437 {
1438         ID *id;
1439         int a;
1440         
1441         for (a = 0; a < totcol; a++) {
1442                 id = (ID *)matar[a];
1443                 if (id && id->lib == NULL) {
1444                         if (id->newid) {
1445                                 matar[a] = (Material *)id->newid;
1446                                 id_us_plus(id->newid);
1447                                 id->us--;
1448                         }
1449                         else if (id->us > 1) {
1450                                 matar[a] = copy_material(matar[a]);
1451                                 id->us--;
1452                                 id->newid = (ID *)matar[a];
1453                         }
1454                 }
1455         }
1456 }
1457
1458 static void single_obdata_users(Main *bmain, Scene *scene, int flag)
1459 {
1460         Object *ob;
1461         Lamp *la;
1462         Curve *cu;
1463         //Camera *cam;
1464         Base *base;
1465         Mesh *me;
1466         ID *id;
1467         int a;
1468
1469         for (base = FIRSTBASE; base; base = base->next) {
1470                 ob = base->object;
1471                 if (ob->id.lib == NULL && (base->flag & flag) == flag) {
1472                         id = ob->data;
1473                         
1474                         if (id && id->us > 1 && id->lib == NULL) {
1475                                 ob->recalc = OB_RECALC_DATA;
1476                                 
1477                                 BKE_copy_animdata_id_action(id);
1478                                 
1479                                 switch (ob->type) {
1480                                         case OB_LAMP:
1481                                                 ob->data = la = copy_lamp(ob->data);
1482                                                 for (a = 0; a < MAX_MTEX; a++) {
1483                                                         if (la->mtex[a]) {
1484                                                                 ID_NEW(la->mtex[a]->object);
1485                                                         }
1486                                                 }
1487                                                 break;
1488                                         case OB_CAMERA:
1489                                                 ob->data = BKE_camera_copy(ob->data);
1490                                                 break;
1491                                         case OB_MESH:
1492                                                 ob->data = copy_mesh(ob->data);
1493                                                 //me= ob->data;
1494                                                 //if (me && me->key)
1495                                                 //      ipo_idnew(me->key->ipo);        /* drivers */
1496                                                 break;
1497                                         case OB_MBALL:
1498                                                 ob->data = BKE_metaball_copy(ob->data);
1499                                                 break;
1500                                         case OB_CURVE:
1501                                         case OB_SURF:
1502                                         case OB_FONT:
1503                                                 ob->data = cu = BKE_curve_copy(ob->data);
1504                                                 ID_NEW(cu->bevobj);
1505                                                 ID_NEW(cu->taperobj);
1506                                                 break;
1507                                         case OB_LATTICE:
1508                                                 ob->data = copy_lattice(ob->data);
1509                                                 break;
1510                                         case OB_ARMATURE:
1511                                                 ob->recalc |= OB_RECALC_DATA;
1512                                                 ob->data = copy_armature(ob->data);
1513                                                 armature_rebuild_pose(ob, ob->data);
1514                                                 break;
1515                                         case OB_SPEAKER:
1516                                                 ob->data = copy_speaker(ob->data);
1517                                                 break;
1518                                         default:
1519                                                 if (G.debug & G_DEBUG)
1520                                                         printf("ERROR %s: can't copy %s\n", __func__, id->name);
1521                                                 return;
1522                                 }
1523                                 
1524                                 id->us--;
1525                                 id->newid = ob->data;
1526                                 
1527                         }
1528                         
1529                 }
1530         }
1531         
1532         me = bmain->mesh.first;
1533         while (me) {
1534                 ID_NEW(me->texcomesh);
1535                 me = me->id.next;
1536         }
1537 }
1538
1539 static void single_object_action_users(Scene *scene, int flag)
1540 {
1541         Object *ob;
1542         Base *base;
1543         
1544         for (base = FIRSTBASE; base; base = base->next) {
1545                 ob = base->object;
1546                 if (ob->id.lib == NULL && (flag == 0 || (base->flag & SELECT)) ) {
1547                         ob->recalc = OB_RECALC_DATA;
1548                         BKE_copy_animdata_id_action(&ob->id);
1549                 }
1550         }
1551 }
1552
1553 static void single_mat_users(Scene *scene, int flag, int do_textures)
1554 {
1555         Object *ob;
1556         Base *base;
1557         Material *ma, *man;
1558         Tex *tex;
1559         int a, b;
1560         
1561         for (base = FIRSTBASE; base; base = base->next) {
1562                 ob = base->object;
1563                 if (ob->id.lib == NULL && (flag == 0 || (base->flag & SELECT)) ) {
1564         
1565                         for (a = 1; a <= ob->totcol; a++) {
1566                                 ma = give_current_material(ob, a);
1567                                 if (ma) {
1568                                         /* do not test for LIB_NEW: this functions guaranteed delivers single_users! */
1569                                         
1570                                         if (ma->id.us > 1) {
1571                                                 man = copy_material(ma);
1572                                                 BKE_copy_animdata_id_action(&man->id);
1573                                                 
1574                                                 man->id.us = 0;
1575                                                 assign_material(ob, man, a);
1576
1577                                                 if (do_textures) {
1578                                                         for (b = 0; b < MAX_MTEX; b++) {
1579                                                                 if (ma->mtex[b] && (tex = ma->mtex[b]->tex)) {
1580                                                                         if (tex->id.us > 1) {
1581                                                                                 tex->id.us--;
1582                                                                                 tex = copy_texture(tex);
1583                                                                                 BKE_copy_animdata_id_action(&tex->id);
1584                                                                                 man->mtex[b]->tex = tex;
1585                                                                         }
1586                                                                 }
1587                                                         }
1588                                                 }
1589                                         }
1590                                 }
1591                         }
1592                 }
1593         }
1594 }
1595
1596 static void do_single_tex_user(Tex **from)
1597 {
1598         Tex *tex, *texn;
1599         
1600         tex = *from;
1601         if (tex == NULL) return;
1602         
1603         if (tex->id.newid) {
1604                 *from = (Tex *)tex->id.newid;
1605                 id_us_plus(tex->id.newid);
1606                 tex->id.us--;
1607         }
1608         else if (tex->id.us > 1) {
1609                 texn = copy_texture(tex);
1610                 BKE_copy_animdata_id_action(&texn->id);
1611                 tex->id.newid = (ID *)texn;
1612                 tex->id.us--;
1613                 *from = texn;
1614         }
1615 }
1616
1617 static void single_tex_users_expand(Main *bmain)
1618 {
1619         /* only when 'parent' blocks are LIB_NEW */
1620         Material *ma;
1621         Lamp *la;
1622         World *wo;
1623         int b;
1624                 
1625         for (ma = bmain->mat.first; ma; ma = ma->id.next) {
1626                 if (ma->id.flag & LIB_NEW) {
1627                         for (b = 0; b < MAX_MTEX; b++) {
1628                                 if (ma->mtex[b] && ma->mtex[b]->tex) {
1629                                         do_single_tex_user(&(ma->mtex[b]->tex));
1630                                 }
1631                         }
1632                 }
1633         }
1634
1635         for (la = bmain->lamp.first; la; la = la->id.next) {
1636                 if (la->id.flag & LIB_NEW) {
1637                         for (b = 0; b < MAX_MTEX; b++) {
1638                                 if (la->mtex[b] && la->mtex[b]->tex) {
1639                                         do_single_tex_user(&(la->mtex[b]->tex));
1640                                 }
1641                         }
1642                 }
1643         }
1644
1645         for (wo = bmain->world.first; wo; wo = wo->id.next) {
1646                 if (wo->id.flag & LIB_NEW) {
1647                         for (b = 0; b < MAX_MTEX; b++) {
1648                                 if (wo->mtex[b] && wo->mtex[b]->tex) {
1649                                         do_single_tex_user(&(wo->mtex[b]->tex));
1650                                 }
1651                         }
1652                 }
1653         }
1654 }
1655
1656 static void single_mat_users_expand(Main *bmain)
1657 {
1658         /* only when 'parent' blocks are LIB_NEW */
1659         Object *ob;
1660         Mesh *me;
1661         Curve *cu;
1662         MetaBall *mb;
1663         Material *ma;
1664         int a;
1665         
1666         for (ob = bmain->object.first; ob; ob = ob->id.next)
1667                 if (ob->id.flag & LIB_NEW)
1668                         new_id_matar(ob->mat, ob->totcol);
1669
1670         for (me = bmain->mesh.first; me; me = me->id.next)
1671                 if (me->id.flag & LIB_NEW)
1672                         new_id_matar(me->mat, me->totcol);
1673
1674         for (cu = bmain->curve.first; cu; cu = cu->id.next)
1675                 if (cu->id.flag & LIB_NEW)
1676                         new_id_matar(cu->mat, cu->totcol);
1677
1678         for (mb = bmain->mball.first; mb; mb = mb->id.next)
1679                 if (mb->id.flag & LIB_NEW)
1680                         new_id_matar(mb->mat, mb->totcol);
1681
1682         /* material imats  */
1683         for (ma = bmain->mat.first; ma; ma = ma->id.next)
1684                 if (ma->id.flag & LIB_NEW)
1685                         for (a = 0; a < MAX_MTEX; a++)
1686                                 if (ma->mtex[a])
1687                                         ID_NEW(ma->mtex[a]->object);
1688 }
1689
1690 /* used for copying scenes */
1691 void ED_object_single_users(Main *bmain, Scene *scene, int full)
1692 {
1693         single_object_users(scene, NULL, 0);
1694
1695         if (full) {
1696                 single_obdata_users(bmain, scene, 0);
1697                 single_object_action_users(scene, 0);
1698                 single_mat_users_expand(bmain);
1699                 single_tex_users_expand(bmain);
1700         }
1701
1702         clear_id_newpoins();
1703 }
1704
1705 /******************************* Make Local ***********************************/
1706
1707 /* helper for below, ma was checked to be not NULL */
1708 static void make_local_makelocalmaterial(Material *ma)
1709 {
1710         AnimData *adt;
1711         int b;
1712         
1713         id_make_local(&ma->id, 0);
1714         
1715         for (b = 0; b < MAX_MTEX; b++)
1716                 if (ma->mtex[b] && ma->mtex[b]->tex)
1717                         id_make_local(&ma->mtex[b]->tex->id, 0);
1718         
1719         adt = BKE_animdata_from_id(&ma->id);
1720         if (adt) BKE_animdata_make_local(adt);
1721
1722         /* nodetree? XXX */
1723 }
1724
1725 static int make_local_exec(bContext *C, wmOperator *op)
1726 {
1727         Main *bmain = CTX_data_main(C);
1728         AnimData *adt;
1729         ParticleSystem *psys;
1730         Material *ma, ***matarar;
1731         Lamp *la;
1732         ID *id;
1733         int a, b, mode = RNA_enum_get(op->ptr, "type");
1734         
1735         if (mode == 3) {
1736                 BKE_library_make_local(bmain, NULL, 0); /* NULL is all libs */
1737                 WM_event_add_notifier(C, NC_WINDOW, NULL);
1738                 return OPERATOR_FINISHED;
1739         }
1740
1741         clear_id_newpoins();
1742         
1743         CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
1744         {
1745                 if (ob->id.lib)
1746                         id_make_local(&ob->id, 0);
1747         }
1748         CTX_DATA_END;
1749         
1750         /* maybe object pointers */
1751         CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
1752         {
1753                 if (ob->id.lib == NULL) {
1754                         ID_NEW(ob->parent);
1755                 }
1756         }
1757         CTX_DATA_END;
1758
1759         CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
1760         {
1761                 id = ob->data;
1762                         
1763                 if (id && mode > 1) {
1764                         id_make_local(id, 0);
1765                         adt = BKE_animdata_from_id(id);
1766                         if (adt) BKE_animdata_make_local(adt);
1767                         
1768                         /* tag indirect data direct */
1769                         matarar = (Material ***)give_matarar(ob);
1770                         if (matarar) {
1771                                 for (a = 0; a < ob->totcol; a++) {
1772                                         ma = (*matarar)[a];
1773                                         if (ma)
1774                                                 id_lib_extern(&ma->id);
1775                                 }
1776                         }
1777                 }
1778
1779                 for (psys = ob->particlesystem.first; psys; psys = psys->next)
1780                         id_make_local(&psys->part->id, 0);
1781
1782                 adt = BKE_animdata_from_id(&ob->id);
1783                 if (adt) BKE_animdata_make_local(adt);
1784         }
1785         CTX_DATA_END;
1786
1787         if (mode > 1) {
1788                 CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
1789                 {
1790                         if (ob->type == OB_LAMP) {
1791                                 la = ob->data;
1792
1793                                 for (b = 0; b < MAX_MTEX; b++)
1794                                         if (la->mtex[b] && la->mtex[b]->tex)
1795                                                 id_make_local(&la->mtex[b]->tex->id, 0);
1796                         }
1797                         else {
1798                                 for (a = 0; a < ob->totcol; a++) {
1799                                         ma = ob->mat[a];
1800                                         if (ma)
1801                                                 make_local_makelocalmaterial(ma);
1802                                 }
1803                                 
1804                                 matarar = (Material ***)give_matarar(ob);
1805                                 if (matarar) {
1806                                         for (a = 0; a < ob->totcol; a++) {
1807                                                 ma = (*matarar)[a];
1808                                                 if (ma)
1809                                                         make_local_makelocalmaterial(ma);
1810                                         }
1811                                 }
1812                         }
1813                 }
1814                 CTX_DATA_END;
1815         }
1816
1817         WM_event_add_notifier(C, NC_WINDOW, NULL);
1818
1819         return OPERATOR_FINISHED;
1820 }
1821
1822 void OBJECT_OT_make_local(wmOperatorType *ot)
1823 {
1824         static EnumPropertyItem type_items[] = {
1825                 {1, "SELECTED_OBJECTS", 0, "Selected Objects", ""},
1826                 {2, "SELECTED_OBJECTS_DATA", 0, "Selected Objects and Data", ""},
1827                 {3, "ALL", 0, "All", ""},
1828                 {0, NULL, 0, NULL, NULL}};
1829
1830         /* identifiers */
1831         ot->name = "Make Local";
1832         ot->description = "Make library linked datablocks local to this file";
1833         ot->idname = "OBJECT_OT_make_local";
1834         
1835         /* api callbacks */
1836         ot->invoke = WM_menu_invoke;
1837         ot->exec = make_local_exec;
1838         ot->poll = ED_operator_objectmode;
1839         
1840         /* flags */
1841         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1842         
1843         /* properties */
1844         ot->prop = RNA_def_enum(ot->srna, "type", type_items, 0, "Type", "");
1845 }
1846
1847 static int make_single_user_exec(bContext *C, wmOperator *op)
1848 {
1849         Main *bmain = CTX_data_main(C);
1850         Scene *scene = CTX_data_scene(C);
1851         View3D *v3d = CTX_wm_view3d(C); /* ok if this is NULL */
1852         int flag = RNA_enum_get(op->ptr, "type"); /* 0==ALL, SELECTED==selected objecs */
1853
1854         if (RNA_boolean_get(op->ptr, "object"))
1855                 single_object_users(scene, v3d, flag);
1856
1857         if (RNA_boolean_get(op->ptr, "obdata"))
1858                 single_obdata_users(bmain, scene, flag);
1859
1860         if (RNA_boolean_get(op->ptr, "material"))
1861                 single_mat_users(scene, flag, RNA_boolean_get(op->ptr, "texture"));
1862
1863 #if 0 /* can't do this separate from materials */
1864         if (RNA_boolean_get(op->ptr, "texture"))
1865                 single_mat_users(scene, flag, TRUE);
1866 #endif
1867         if (RNA_boolean_get(op->ptr, "animation"))
1868                 single_object_action_users(scene, flag);
1869
1870         clear_id_newpoins();
1871
1872         WM_event_add_notifier(C, NC_WINDOW, NULL);
1873         return OPERATOR_FINISHED;
1874 }
1875
1876 void OBJECT_OT_make_single_user(wmOperatorType *ot)
1877 {
1878         static EnumPropertyItem type_items[] = {
1879                 {SELECT, "SELECTED_OBJECTS", 0, "Selected Objects", ""},
1880                 {0, "ALL", 0, "All", ""},
1881                 {0, NULL, 0, NULL, NULL}};
1882
1883         /* identifiers */
1884         ot->name = "Make Single User";
1885         ot->description = "Make linked data local to each object";
1886         ot->idname = "OBJECT_OT_make_single_user";
1887
1888         /* api callbacks */
1889         ot->invoke = WM_menu_invoke;
1890         ot->exec = make_single_user_exec;
1891         ot->poll = ED_operator_objectmode;
1892
1893         /* flags */
1894         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1895
1896         /* properties */
1897         ot->prop = RNA_def_enum(ot->srna, "type", type_items, SELECT, "Type", "");
1898
1899         RNA_def_boolean(ot->srna, "object", 0, "Object", "Make single user objects");
1900         RNA_def_boolean(ot->srna, "obdata", 0, "Object Data", "Make single user object data");
1901         RNA_def_boolean(ot->srna, "material", 0, "Materials", "Make materials local to each datablock");
1902         RNA_def_boolean(ot->srna, "texture", 0, "Textures", "Make textures local to each material");
1903         RNA_def_boolean(ot->srna, "animation", 0, "Object Animation", "Make animation data local to each object");
1904 }
1905
1906 static int drop_named_material_invoke(bContext *C, wmOperator *op, wmEvent *event)
1907 {
1908         Main *bmain = CTX_data_main(C);
1909         Base *base = ED_view3d_give_base_under_cursor(C, event->mval);
1910         Material *ma;
1911         char name[MAX_ID_NAME - 2];
1912         
1913         RNA_string_get(op->ptr, "name", name);
1914         ma = (Material *)find_id("MA", name);
1915         if (base == NULL || ma == NULL)
1916                 return OPERATOR_CANCELLED;
1917         
1918         assign_material(base->object, ma, 1);
1919         
1920         DAG_ids_flush_update(bmain, 0);
1921         WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, CTX_wm_view3d(C));
1922         
1923         return OPERATOR_FINISHED;
1924 }
1925
1926 /* used for dropbox */
1927 /* assigns to object under cursor, only first material slot */
1928 void OBJECT_OT_drop_named_material(wmOperatorType *ot)
1929 {
1930
1931         /* identifiers */
1932         ot->name = "Drop Named Material on Object";
1933         ot->description = "";
1934         ot->idname = "OBJECT_OT_drop_named_material";
1935         
1936         /* api callbacks */
1937         ot->invoke = drop_named_material_invoke;
1938         ot->poll = ED_operator_objectmode;
1939         
1940         /* flags */
1941         ot->flag = OPTYPE_UNDO;
1942         
1943         /* properties */
1944         RNA_def_string(ot->srna, "name", "Material", MAX_ID_NAME - 2, "Name", "Material name to assign");
1945 }