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