Audaspace:
[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_ALL;
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_ALL);
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_ALL;
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_ALL;
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_ALL;
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_ALL;
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_ALL;
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, "scene"));
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 /* Return 1 if make link data is allow, zero otherwise */
1192 static int allow_make_links_data(int ev, Object *ob, Object *obt)
1193 {
1194         switch(ev) {
1195                 case MAKE_LINKS_OBDATA:
1196                         if (ob->type == obt->type && ob->type != OB_EMPTY)
1197                                 return 1;
1198                         break;
1199                 case MAKE_LINKS_MATERIALS:
1200                         if (ELEM5(ob->type, OB_MESH, OB_CURVE, OB_FONT, OB_SURF, OB_MBALL) &&
1201                                 ELEM5(obt->type, OB_MESH, OB_CURVE, OB_FONT, OB_SURF, OB_MBALL))
1202                                 return 1;
1203                         break;
1204                 case MAKE_LINKS_ANIMDATA:
1205                 case MAKE_LINKS_DUPLIGROUP:
1206                         return 1;
1207                 case MAKE_LINKS_MODIFIERS:
1208                         if (ob->type != OB_EMPTY && obt->type != OB_EMPTY)
1209                                 return 1;
1210                         break;
1211         }
1212         return 0;
1213 }
1214
1215 static int make_links_data_exec(bContext *C, wmOperator *op)
1216 {
1217         int event = RNA_int_get(op->ptr, "type");
1218         Object *ob;
1219         ID *id;
1220         int a;
1221
1222         ob= CTX_data_active_object(C);
1223
1224         CTX_DATA_BEGIN(C, Object*, obt, selected_editable_objects) {
1225                 if(ob != obt) {
1226                         if (allow_make_links_data(event, ob, obt)) {
1227                                 switch(event) {
1228                                 case MAKE_LINKS_OBDATA: /* obdata */
1229                                         id= obt->data;
1230                                         id->us--;
1231
1232                                         id= ob->data;
1233                                         id_us_plus(id);
1234                                         obt->data= id;
1235
1236                                         /* if amount of material indices changed: */
1237                                         test_object_materials(obt->data);
1238
1239                                         obt->recalc |= OB_RECALC_DATA;
1240                                         break;
1241                                 case MAKE_LINKS_MATERIALS:
1242                                         /* new approach, using functions from kernel */
1243                                         for(a=0; a<ob->totcol; a++) {
1244                                                 Material *ma= give_current_material(ob, a+1);
1245                                                 assign_material(obt, ma, a+1);  /* also works with ma==NULL */
1246                                         }
1247                                         break;
1248                                 case MAKE_LINKS_ANIMDATA:
1249                                         BKE_copy_animdata_id((ID *)obt, (ID *)ob);
1250                                         BKE_copy_animdata_id((ID *)obt->data, (ID *)ob->data);
1251                                         break;
1252                                 case MAKE_LINKS_DUPLIGROUP:
1253                                         obt->dup_group= ob->dup_group;
1254                                         if(obt->dup_group) {
1255                                                 id_lib_extern(&obt->dup_group->id);
1256                                                 obt->transflag |= OB_DUPLIGROUP;
1257                                         }
1258                                         break;
1259                                 case MAKE_LINKS_MODIFIERS:
1260                                         object_link_modifiers(obt, ob);
1261                                         obt->recalc |= OB_RECALC_ALL;
1262                                         break;
1263                                 }
1264                         }
1265                 }
1266         }
1267         CTX_DATA_END;
1268
1269         DAG_ids_flush_update(0);
1270         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, CTX_wm_view3d(C));
1271         return OPERATOR_FINISHED;
1272 }
1273
1274
1275 void OBJECT_OT_make_links_scene(wmOperatorType *ot)
1276 {
1277         PropertyRNA *prop;
1278
1279         /* identifiers */
1280         ot->name= "Link Objects to Scene";
1281         ot->description = "Make linked data local to each object";
1282         ot->idname= "OBJECT_OT_make_links_scene";
1283
1284         /* api callbacks */
1285         ot->exec= make_links_scene_exec;
1286         /* better not run the poll check */
1287
1288         /* flags */
1289         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1290
1291         /* properties */
1292         prop= RNA_def_enum(ot->srna, "scene", DummyRNA_NULL_items, 0, "Scene", "");
1293         RNA_def_enum_funcs(prop, RNA_scene_local_itemf);
1294         ot->prop= prop;
1295 }
1296
1297 void OBJECT_OT_make_links_data(wmOperatorType *ot)
1298 {
1299         static EnumPropertyItem make_links_items[]= {
1300                 {MAKE_LINKS_OBDATA,             "OBDATA", 0, "Object Data", ""},
1301                 {MAKE_LINKS_MATERIALS,  "MATERIAL", 0, "Materials", ""},
1302                 {MAKE_LINKS_ANIMDATA,   "ANIMATION", 0, "Animation Data", ""},
1303                 {MAKE_LINKS_DUPLIGROUP, "DUPLIGROUP", 0, "DupliGroup", ""},
1304                 {MAKE_LINKS_MODIFIERS,  "MODIFIERS", 0, "Modifiers", ""},
1305                 {0, NULL, 0, NULL, NULL}};
1306
1307         PropertyRNA *prop;
1308
1309         /* identifiers */
1310         ot->name= "Link Data";
1311         ot->description = "Make links from the active object to other selected objects";
1312         ot->idname= "OBJECT_OT_make_links_data";
1313
1314         /* api callbacks */
1315         ot->exec= make_links_data_exec;
1316         ot->poll= ED_operator_scene_editable;
1317
1318         /* flags */
1319         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1320
1321         /* properties */
1322         prop= RNA_def_enum(ot->srna, "type", make_links_items, 0, "Type", "");
1323 }
1324
1325
1326 /**************************** Make Single User ********************************/
1327
1328 static void single_object_users__forwardModifierLinks(void *userData, Object *ob, Object **obpoin)
1329 {
1330         ID_NEW(*obpoin);
1331 }
1332
1333 void single_object_users(Scene *scene, View3D *v3d, int flag)   
1334 {
1335         Base *base;
1336         Object *ob, *obn;
1337         
1338         clear_sca_new_poins();  /* sensor/contr/act */
1339
1340         /* duplicate (must set newid) */
1341         for(base= FIRSTBASE; base; base= base->next) {
1342                 ob= base->object;
1343                 
1344                 if( (base->flag & flag)==flag ) {
1345                         if(ob->id.lib==NULL && ob->id.us>1) {
1346                                 /* base gets copy of object */
1347                                 obn= copy_object(ob);
1348                                 base->object= obn;
1349                                 ob->id.us--;
1350                         }
1351                 }
1352         }
1353         
1354         ID_NEW(scene->camera);
1355         if(v3d) ID_NEW(v3d->camera);
1356         
1357         /* object pointers */
1358         for(base= FIRSTBASE; base; base= base->next) {
1359                 ob= base->object;
1360                 if(ob->id.lib==NULL) {
1361                         relink_constraints(&base->object->constraints);
1362                         if (base->object->pose){
1363                                 bPoseChannel *chan;
1364                                 for (chan = base->object->pose->chanbase.first; chan; chan=chan->next){
1365                                         relink_constraints(&chan->constraints);
1366                                 }
1367                         }
1368                         modifiers_foreachObjectLink(base->object, single_object_users__forwardModifierLinks, NULL);
1369                         
1370                         ID_NEW(ob->parent);
1371                 }
1372         }
1373
1374         set_sca_new_poins();
1375 }
1376
1377 void new_id_matar(Material **matar, int totcol)
1378 {
1379         ID *id;
1380         int a;
1381         
1382         for(a=0; a<totcol; a++) {
1383                 id= (ID *)matar[a];
1384                 if(id && id->lib==0) {
1385                         if(id->newid) {
1386                                 matar[a]= (Material *)id->newid;
1387                                 id_us_plus(id->newid);
1388                                 id->us--;
1389                         }
1390                         else if(id->us>1) {
1391                                 matar[a]= copy_material(matar[a]);
1392                                 id->us--;
1393                                 id->newid= (ID *)matar[a];
1394                         }
1395                 }
1396         }
1397 }
1398
1399 void single_obdata_users(Scene *scene, int flag)
1400 {
1401         Object *ob;
1402         Lamp *la;
1403         Curve *cu;
1404         //Camera *cam;
1405         Base *base;
1406         Mesh *me;
1407         ID *id;
1408         int a;
1409
1410         for(base= FIRSTBASE; base; base= base->next) {
1411                 ob= base->object;
1412                 if(ob->id.lib==NULL && (base->flag & flag)==flag ) {
1413                         id= ob->data;
1414                         
1415                         if(id && id->us>1 && id->lib==0) {
1416                                 ob->recalc= OB_RECALC_DATA;
1417                                 
1418                                 switch(ob->type) {
1419                                 case OB_LAMP:
1420                                         if(id && id->us>1 && id->lib==NULL) {
1421                                                 ob->data= la= copy_lamp(ob->data);
1422                                                 for(a=0; a<MAX_MTEX; a++) {
1423                                                         if(la->mtex[a]) {
1424                                                                 ID_NEW(la->mtex[a]->object);
1425                                                         }
1426                                                 }
1427                                         }
1428                                         break;
1429                                 case OB_CAMERA:
1430                                         ob->data= copy_camera(ob->data);
1431                                         break;
1432                                 case OB_MESH:
1433                                         me= ob->data= copy_mesh(ob->data);
1434                                         //if(me && me->key)
1435                                         //      ipo_idnew(me->key->ipo);        /* drivers */
1436                                         break;
1437                                 case OB_MBALL:
1438                                         ob->data= copy_mball(ob->data);
1439                                         break;
1440                                 case OB_CURVE:
1441                                 case OB_SURF:
1442                                 case OB_FONT:
1443                                         ob->data= cu= copy_curve(ob->data);
1444                                         ID_NEW(cu->bevobj);
1445                                         ID_NEW(cu->taperobj);
1446                                         break;
1447                                 case OB_LATTICE:
1448                                         ob->data= copy_lattice(ob->data);
1449                                         break;
1450                                 case OB_ARMATURE:
1451                                         ob->recalc |= OB_RECALC_DATA;
1452                                         ob->data= copy_armature(ob->data);
1453                                         armature_rebuild_pose(ob, ob->data);
1454                                         break;
1455                                 default:
1456                                         if (G.f & G_DEBUG)
1457                                                 printf("ERROR single_obdata_users: can't copy %s\n", id->name);
1458                                         return;
1459                                 }
1460                                 
1461                                 id->us--;
1462                                 id->newid= ob->data;
1463                                 
1464                         }
1465                         
1466 #if 0 // XXX old animation system
1467                         id= (ID *)ob->action;
1468                         if (id && id->us>1 && id->lib==NULL){
1469                                 if(id->newid){
1470                                         ob->action= (bAction *)id->newid;
1471                                         id_us_plus(id->newid);
1472                                 }
1473                                 else {
1474                                         ob->action= copy_action(ob->action);
1475                                         id->us--;
1476                                         id->newid=(ID *)ob->action;
1477                                 }
1478                         }
1479                         id= (ID *)ob->ipo;
1480                         if(id && id->us>1 && id->lib==NULL) {
1481                                 if(id->newid) {
1482                                         ob->ipo= (Ipo *)id->newid;
1483                                         id_us_plus(id->newid);
1484                                 }
1485                                 else {
1486                                         ob->ipo= copy_ipo(ob->ipo);
1487                                         id->us--;
1488                                         id->newid= (ID *)ob->ipo;
1489                                 }
1490                                 ipo_idnew(ob->ipo);     /* drivers */
1491                         }
1492                         /* other ipos */
1493                         switch(ob->type) {
1494                         case OB_LAMP:
1495                                 la= ob->data;
1496                                 if(la->ipo && la->ipo->id.us>1) {
1497                                         la->ipo->id.us--;
1498                                         la->ipo= copy_ipo(la->ipo);
1499                                         ipo_idnew(la->ipo);     /* drivers */
1500                                 }
1501                                 break;
1502                         case OB_CAMERA:
1503                                 cam= ob->data;
1504                                 if(cam->ipo && cam->ipo->id.us>1) {
1505                                         cam->ipo->id.us--;
1506                                         cam->ipo= copy_ipo(cam->ipo);
1507                                         ipo_idnew(cam->ipo);    /* drivers */
1508                                 }
1509                                 break;
1510                         }
1511 #endif // XXX old animation system
1512                 }
1513         }
1514         
1515         me= G.main->mesh.first;
1516         while(me) {
1517                 ID_NEW(me->texcomesh);
1518                 me= me->id.next;
1519         }
1520 }
1521
1522 void single_ipo_users(Scene *scene, int flag)
1523 {
1524 #if 0 // XXX old animation system
1525         Object *ob;
1526         Base *base;
1527         ID *id;
1528         
1529         for(base= FIRSTBASE; base; base= base->next) {
1530                 ob= base->object;
1531                 if(ob->id.lib==NULL && (flag==0 || (base->flag & SELECT)) ) {
1532                         ob->recalc= OB_RECALC_DATA;
1533                         
1534                         id= (ID *)ob->ipo;
1535                         if(id && id->us>1 && id->lib==NULL) {
1536                                 ob->ipo= copy_ipo(ob->ipo);
1537                                 id->us--;
1538                                 ipo_idnew(ob->ipo);     /* drivers */
1539                         }
1540                 }
1541         }
1542 #endif // XXX old animation system
1543 }
1544
1545 static void single_mat_users(Scene *scene, int flag, int do_textures)
1546 {
1547         Object *ob;
1548         Base *base;
1549         Material *ma, *man;
1550         Tex *tex;
1551         int a, b;
1552         
1553         for(base= FIRSTBASE; base; base= base->next) {
1554                 ob= base->object;
1555                 if(ob->id.lib==NULL && (flag==0 || (base->flag & SELECT)) ) {
1556         
1557                         for(a=1; a<=ob->totcol; a++) {
1558                                 ma= give_current_material(ob, a);
1559                                 if(ma) {
1560                                         /* do not test for LIB_NEW: this functions guaranteed delivers single_users! */
1561                                         
1562                                         if(ma->id.us>1) {
1563                                                 man= copy_material(ma);
1564                                         
1565                                                 man->id.us= 0;
1566                                                 assign_material(ob, man, a);
1567         
1568 #if 0 // XXX old animation system                                               
1569                                                 if(ma->ipo) {
1570                                                         man->ipo= copy_ipo(ma->ipo);
1571                                                         ma->ipo->id.us--;
1572                                                         ipo_idnew(ma->ipo);     /* drivers */
1573                                                 }
1574 #endif // XXX old animation system
1575                                                 if(do_textures) {
1576                                                         for(b=0; b<MAX_MTEX; b++) {
1577                                                                 if(ma->mtex[b] && ma->mtex[b]->tex) {
1578                                                                         tex= ma->mtex[b]->tex;
1579                                                                         if(tex->id.us>1) {
1580                                                                                 ma->mtex[b]->tex= copy_texture(tex);
1581                                                                                 tex->id.us--;
1582                                                                         }
1583                                                                 }
1584                                                         }
1585                                                 }
1586                                         }
1587                                 }
1588                         }
1589                 }
1590         }
1591 }
1592
1593 void do_single_tex_user(Tex **from)
1594 {
1595         Tex *tex, *texn;
1596         
1597         tex= *from;
1598         if(tex==0) return;
1599         
1600         if(tex->id.newid) {
1601                 *from= (Tex *)tex->id.newid;
1602                 id_us_plus(tex->id.newid);
1603                 tex->id.us--;
1604         }
1605         else if(tex->id.us>1) {
1606                 texn= copy_texture(tex);
1607                 tex->id.newid= (ID *)texn;
1608                 tex->id.us--;
1609                 *from= texn;
1610         }
1611 }
1612
1613 void single_tex_users_expand()
1614 {
1615         /* only when 'parent' blocks are LIB_NEW */
1616         Main *bmain= G.main;
1617         Material *ma;
1618         Lamp *la;
1619         World *wo;
1620         int b;
1621                 
1622         for(ma= bmain->mat.first; ma; ma=ma->id.next) {
1623                 if(ma->id.flag & LIB_NEW) {
1624                         for(b=0; b<MAX_MTEX; b++) {
1625                                 if(ma->mtex[b] && ma->mtex[b]->tex) {
1626                                         do_single_tex_user( &(ma->mtex[b]->tex) );
1627                                 }
1628                         }
1629                 }
1630         }
1631
1632         for(la= bmain->lamp.first; la; la=la->id.next) {
1633                 if(la->id.flag & LIB_NEW) {
1634                         for(b=0; b<MAX_MTEX; b++) {
1635                                 if(la->mtex[b] && la->mtex[b]->tex) {
1636                                         do_single_tex_user( &(la->mtex[b]->tex) );
1637                                 }
1638                         }
1639                 }
1640         }
1641
1642         for(wo= bmain->world.first; wo; wo=wo->id.next) {
1643                 if(wo->id.flag & LIB_NEW) {
1644                         for(b=0; b<MAX_MTEX; b++) {
1645                                 if(wo->mtex[b] && wo->mtex[b]->tex) {
1646                                         do_single_tex_user( &(wo->mtex[b]->tex) );
1647                                 }
1648                         }
1649                 }
1650         }
1651 }
1652
1653 static void single_mat_users_expand(void)
1654 {
1655         /* only when 'parent' blocks are LIB_NEW */
1656         Main *bmain= G.main;
1657         Object *ob;
1658         Mesh *me;
1659         Curve *cu;
1660         MetaBall *mb;
1661         Material *ma;
1662         int a;
1663         
1664         for(ob=bmain->object.first; ob; ob=ob->id.next)
1665                 if(ob->id.flag & LIB_NEW)
1666                         new_id_matar(ob->mat, ob->totcol);
1667
1668         for(me=bmain->mesh.first; me; me=me->id.next)
1669                 if(me->id.flag & LIB_NEW)
1670                         new_id_matar(me->mat, me->totcol);
1671
1672         for(cu=bmain->curve.first; cu; cu=cu->id.next)
1673                 if(cu->id.flag & LIB_NEW)
1674                         new_id_matar(cu->mat, cu->totcol);
1675
1676         for(mb=bmain->mball.first; mb; mb=mb->id.next)
1677                 if(mb->id.flag & LIB_NEW)
1678                         new_id_matar(mb->mat, mb->totcol);
1679
1680         /* material imats  */
1681         for(ma=bmain->mat.first; ma; ma=ma->id.next)
1682                 if(ma->id.flag & LIB_NEW)
1683                         for(a=0; a<MAX_MTEX; a++)
1684                                 if(ma->mtex[a])
1685                                         ID_NEW(ma->mtex[a]->object);
1686 }
1687
1688 /* used for copying scenes */
1689 void ED_object_single_users(Scene *scene, int full)
1690 {
1691         single_object_users(scene, NULL, 0);
1692
1693         if(full) {
1694                 single_obdata_users(scene, 0);
1695                 single_mat_users_expand();
1696                 single_tex_users_expand();
1697         }
1698
1699         clear_id_newpoins();
1700 }
1701
1702 /******************************* Make Local ***********************************/
1703
1704 /* helper for below, ma was checked to be not NULL */
1705 static void make_local_makelocalmaterial(Material *ma)
1706 {
1707         AnimData *adt;
1708         int b;
1709         
1710         id_make_local(&ma->id, 0);
1711         
1712         for(b=0; b<MAX_MTEX; b++)
1713                 if(ma->mtex[b] && ma->mtex[b]->tex)
1714                         id_make_local(&ma->mtex[b]->tex->id, 0);
1715         
1716         adt= BKE_animdata_from_id(&ma->id);
1717         if(adt) BKE_animdata_make_local(adt);
1718
1719         /* nodetree? XXX */
1720 }
1721
1722 static int make_local_exec(bContext *C, wmOperator *op)
1723 {
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                 all_local(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
1762                 for(psys=ob->particlesystem.first; psys; psys=psys->next)
1763                         id_make_local(&psys->part->id, 0);
1764
1765                 adt= BKE_animdata_from_id(&ob->id);
1766                 if(adt) BKE_animdata_make_local(adt);
1767         }
1768         CTX_DATA_END;
1769
1770         if(mode>1) {
1771                 CTX_DATA_BEGIN(C, Object*, ob, selected_objects) {
1772                         if(ob->type==OB_LAMP) {
1773                                 la= ob->data;
1774
1775                                 for(b=0; b<MAX_MTEX; b++)
1776                                         if(la->mtex[b] && la->mtex[b]->tex)
1777                                                 id_make_local(&la->mtex[b]->tex->id, 0);
1778                         }
1779                         else {
1780                                 for(a=0; a<ob->totcol; a++) {
1781                                         ma= ob->mat[a];
1782                                         if(ma)
1783                                                 make_local_makelocalmaterial(ma);
1784                                 }
1785                                 
1786                                 matarar= (Material ***)give_matarar(ob);
1787                                 if(matarar) {
1788                                         for(a=0; a<ob->totcol; a++) {
1789                                                 ma= (*matarar)[a];
1790                                                 if(ma)
1791                                                         make_local_makelocalmaterial(ma);
1792                                         }
1793                                 }
1794                         }
1795                 }
1796                 CTX_DATA_END;
1797         }
1798
1799         WM_event_add_notifier(C, NC_WINDOW, NULL);
1800
1801         return OPERATOR_FINISHED;
1802 }
1803
1804 void OBJECT_OT_make_local(wmOperatorType *ot)
1805 {
1806         static EnumPropertyItem type_items[]= {
1807                 {1, "SELECTED_OBJECTS", 0, "Selected Objects", ""},
1808                 {2, "SELECTED_OBJECTS_DATA", 0, "Selected Objects and Data", ""},
1809                 {3, "ALL", 0, "All", ""},
1810                 {0, NULL, 0, NULL, NULL}};
1811
1812         /* identifiers */
1813         ot->name= "Make Local";
1814         ot->description = "Make library linked datablocks local to this file";
1815         ot->idname= "OBJECT_OT_make_local";
1816         
1817         /* api callbacks */
1818         ot->invoke= WM_menu_invoke;
1819         ot->exec= make_local_exec;
1820         ot->poll= ED_operator_scene_editable;
1821         
1822         /* flags */
1823         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1824         
1825         /* properties */
1826         ot->prop= RNA_def_enum(ot->srna, "type", type_items, 0, "Type", "");
1827 }
1828
1829 static int make_single_user_exec(bContext *C, wmOperator *op)
1830 {
1831         Scene *scene= CTX_data_scene(C);
1832         View3D *v3d= CTX_wm_view3d(C); /* ok if this is NULL */
1833         int flag= RNA_enum_get(op->ptr, "type"); /* 0==ALL, SELECTED==selected objecs */
1834
1835         if(RNA_boolean_get(op->ptr, "object"))
1836                 single_object_users(scene, v3d, flag);
1837
1838         if(RNA_boolean_get(op->ptr, "obdata"))
1839                 single_obdata_users(scene, flag);
1840
1841         if(RNA_boolean_get(op->ptr, "material"))
1842                 single_mat_users(scene, flag, FALSE);
1843
1844         if(RNA_boolean_get(op->ptr, "texture"))
1845                 single_mat_users(scene, flag, TRUE);
1846
1847         if(RNA_boolean_get(op->ptr, "animation"))
1848                 single_ipo_users(scene, flag);
1849
1850         clear_id_newpoins();
1851
1852         WM_event_add_notifier(C, NC_WINDOW, NULL);
1853         return OPERATOR_FINISHED;
1854 }
1855
1856 void OBJECT_OT_make_single_user(wmOperatorType *ot)
1857 {
1858         static EnumPropertyItem type_items[]= {
1859                 {SELECT, "SELECTED_OBJECTS", 0, "Selected Objects", ""},
1860                 {0, "ALL", 0, "All", ""},
1861                 {0, NULL, 0, NULL, NULL}};
1862
1863         /* identifiers */
1864         ot->name= "Make Single User";
1865         ot->description = "Make linked data local to each object";
1866         ot->idname= "OBJECT_OT_make_single_user";
1867
1868         /* api callbacks */
1869         ot->invoke= WM_menu_invoke;
1870         ot->exec= make_single_user_exec;
1871         ot->poll= ED_operator_scene_editable;
1872
1873         /* flags */
1874         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1875
1876         /* properties */
1877         ot->prop= RNA_def_enum(ot->srna, "type", type_items, 0, "Type", "");
1878
1879         RNA_def_boolean(ot->srna, "object", 0, "Object", "Make single user objects");
1880         RNA_def_boolean(ot->srna, "obdata", 0, "Object Data", "Make single user object data");
1881         RNA_def_boolean(ot->srna, "material", 0, "Materials", "Make materials local to each datablock");
1882         RNA_def_boolean(ot->srna, "texture", 0, "Textures", "Make textures local to each material");
1883         RNA_def_boolean(ot->srna, "animation", 0, "Animation Data", "Make animation data local to each object");
1884 }
1885
1886 static int drop_named_material_invoke(bContext *C, wmOperator *op, wmEvent *event)
1887 {
1888         Base *base= ED_view3d_give_base_under_cursor(C, event->mval);
1889         Material *ma;
1890         char name[32];
1891         
1892         RNA_string_get(op->ptr, "name", name);
1893         ma= (Material *)find_id("MA", name);
1894         if(base==NULL || ma==NULL) 
1895                 return OPERATOR_CANCELLED;
1896         
1897         assign_material(base->object, ma, 1);
1898         
1899         DAG_ids_flush_update(0);
1900         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, CTX_wm_view3d(C));
1901         
1902         return OPERATOR_FINISHED;
1903 }
1904
1905 /* used for dropbox */
1906 /* assigns to object under cursor, only first material slot */
1907 void OBJECT_OT_drop_named_material(wmOperatorType *ot)
1908 {
1909
1910         /* identifiers */
1911         ot->name= "Drop Named Material on Object";
1912         ot->description = "";
1913         ot->idname= "OBJECT_OT_drop_named_material";
1914         
1915         /* api callbacks */
1916         ot->invoke= drop_named_material_invoke;
1917         ot->poll= ED_operator_scene_editable;
1918         
1919         /* flags */
1920         ot->flag= OPTYPE_UNDO;
1921         
1922         /* properties */
1923         RNA_def_string(ot->srna, "name", "Material", 24, "Name", "Material name to assign.");
1924 }