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