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