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