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