Merged changes in the trunk up to revision 52340.
[blender.git] / source / blender / blenloader / intern / readfile.c
index 89f723cb94544b4853771742520a0d9bd92c4233..90fd6cad93cca0e7d52278bff8563b71cbe97b46 100644 (file)
@@ -70,6 +70,7 @@
 #include "DNA_key_types.h"
 #include "DNA_lattice_types.h"
 #include "DNA_lamp_types.h"
 #include "DNA_key_types.h"
 #include "DNA_lattice_types.h"
 #include "DNA_lamp_types.h"
+#include "DNA_linestyle_types.h"
 #include "DNA_meta_types.h"
 #include "DNA_material_types.h"
 #include "DNA_mesh_types.h"
 #include "DNA_meta_types.h"
 #include "DNA_material_types.h"
 #include "DNA_mesh_types.h"
@@ -821,7 +822,7 @@ static void decode_blender_header(FileData *fd)
                        /* is the file saved in a different endian
                         * than we need ?
                         */
                        /* is the file saved in a different endian
                         * than we need ?
                         */
-                       if (((((char *)&remove_this_endian_test)[0] == 1) ? L_ENDIAN : B_ENDIAN) != ((header[8] == 'v') ? L_ENDIAN : B_ENDIAN)) {
+                       if (((((char*)&remove_this_endian_test)[0]==1)?L_ENDIAN:B_ENDIAN) != ((header[8]=='v')?L_ENDIAN:B_ENDIAN)) {
                                fd->flags |= FD_FLAGS_SWITCH_ENDIAN;
                        }
                        
                                fd->flags |= FD_FLAGS_SWITCH_ENDIAN;
                        }
                        
@@ -943,7 +944,7 @@ static int fd_read_from_memfile(FileData *filedata, void *buffer, unsigned int s
                        if (chunkoffset+readsize > chunk->size)
                                readsize= chunk->size-chunkoffset;
                        
                        if (chunkoffset+readsize > chunk->size)
                                readsize= chunk->size-chunkoffset;
                        
-                       memcpy((char *)buffer + totread, chunk->buf + chunkoffset, readsize);
+                       memcpy((char*)buffer + totread, chunk->buf + chunkoffset, readsize);
                        totread += readsize;
                        filedata->seek += readsize;
                        seek += readsize;
                        totread += readsize;
                        filedata->seek += readsize;
                        seek += readsize;
@@ -985,7 +986,7 @@ static FileData *blo_decode_and_check(FileData *fd, ReportList *reports)
                        blo_freefiledata(fd);
                        fd = NULL;
                }
                        blo_freefiledata(fd);
                        fd = NULL;
                }
-       }
+       } 
        else {
                BKE_reportf(reports, RPT_ERROR, "Failed to read blend file '%s', not a blend file", fd->relabase);
                blo_freefiledata(fd);
        else {
                BKE_reportf(reports, RPT_ERROR, "Failed to read blend file '%s', not a blend file", fd->relabase);
                blo_freefiledata(fd);
@@ -1132,7 +1133,7 @@ int BLO_is_a_library(const char *path, char *dir, char *group)
                /* the last part of the dir is a .blend file, no group follows */
                *fd = '/'; /* put back the removed slash separating the dir and the .blend file name */
        }
                /* the last part of the dir is a .blend file, no group follows */
                *fd = '/'; /* put back the removed slash separating the dir and the .blend file name */
        }
-       else {
+       else {          
                char *gp = fd + 1; // in case we have a .blend file, gp points to the group
                
                /* Find the last slash */
                char *gp = fd + 1; // in case we have a .blend file, gp points to the group
                
                /* Find the last slash */
@@ -1841,7 +1842,7 @@ static void lib_link_fcurves(FileData *fd, ID *id, ListBase *list)
                        
                        for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
                                DRIVER_TARGETS_LOOPER(dvar)
                        
                        for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
                                DRIVER_TARGETS_LOOPER(dvar)
-                               {
+                               {       
                                        /* only relink if still used */
                                        if (tarIndex < dvar->num_targets)
                                                dtar->id = newlibadr(fd, id->lib, dtar->id); 
                                        /* only relink if still used */
                                        if (tarIndex < dvar->num_targets)
                                                dtar->id = newlibadr(fd, id->lib, dtar->id); 
@@ -2434,7 +2435,7 @@ static void direct_link_nodetree(FileData *fd, bNodeTree *ntree)
                        /* could be handlerized at some point */
                        if (ntree->type==NTREE_SHADER) {
                                if (node->type==SH_NODE_CURVE_VEC || node->type==SH_NODE_CURVE_RGB) {
                        /* could be handlerized at some point */
                        if (ntree->type==NTREE_SHADER) {
                                if (node->type==SH_NODE_CURVE_VEC || node->type==SH_NODE_CURVE_RGB) {
-                                       direct_link_curvemapping(fd, node->storage);
+                               direct_link_curvemapping(fd, node->storage);
                                }
                                else if (node->type==SH_NODE_SCRIPT) {
                                        NodeShaderScript *nss = (NodeShaderScript *) node->storage;
                                }
                                else if (node->type==SH_NODE_SCRIPT) {
                                        NodeShaderScript *nss = (NodeShaderScript *) node->storage;
@@ -2818,7 +2819,7 @@ static void switch_endian_keyblock(Key *key, KeyBlock *kb)
                        
                        cp += 2;
                }
                        
                        cp += 2;
                }
-               data += elemsize;
+               data+= elemsize;
        }
 }
 
        }
 }
 
@@ -2853,7 +2854,7 @@ static void lib_link_mball(FileData *fd, Main *main)
                        if (mb->adt) lib_link_animdata(fd, &mb->id, mb->adt);
                        
                        for (a = 0; a < mb->totcol; a++) 
                        if (mb->adt) lib_link_animdata(fd, &mb->id, mb->adt);
                        
                        for (a = 0; a < mb->totcol; a++) 
-                               mb->mat[a] = newlibadr_us(fd, mb->id.lib, mb->mat[a]);
+                               mb->mat[a]= newlibadr_us(fd, mb->id.lib, mb->mat[a]);
                        
                        mb->ipo = newlibadr_us(fd, mb->id.lib, mb->ipo); // XXX deprecated - old animation system
                        
                        
                        mb->ipo = newlibadr_us(fd, mb->id.lib, mb->ipo); // XXX deprecated - old animation system
                        
@@ -3043,7 +3044,7 @@ static void direct_link_image(FileData *fd, Image *ima)
                link_ibuf_list(fd, &ima->ibufs);
        else
                ima->ibufs.first = ima->ibufs.last = NULL;
                link_ibuf_list(fd, &ima->ibufs);
        else
                ima->ibufs.first = ima->ibufs.last = NULL;
-
+       
        /* if not restored, we keep the binded opengl index */
        if (ima->ibufs.first == NULL) {
                ima->bindcode = 0;
        /* if not restored, we keep the binded opengl index */
        if (ima->ibufs.first == NULL) {
                ima->bindcode = 0;
@@ -3091,7 +3092,7 @@ static void lib_link_curve(FileData *fd, Main *main)
                        cu->taperobj = newlibadr(fd, cu->id.lib, cu->taperobj);
                        cu->textoncurve = newlibadr(fd, cu->id.lib, cu->textoncurve);
                        cu->vfont = newlibadr_us(fd, cu->id.lib, cu->vfont);
                        cu->taperobj = newlibadr(fd, cu->id.lib, cu->taperobj);
                        cu->textoncurve = newlibadr(fd, cu->id.lib, cu->textoncurve);
                        cu->vfont = newlibadr_us(fd, cu->id.lib, cu->vfont);
-                       cu->vfontb = newlibadr_us(fd, cu->id.lib, cu->vfontb);
+                       cu->vfontb = newlibadr_us(fd, cu->id.lib, cu->vfontb);                  
                        cu->vfonti = newlibadr_us(fd, cu->id.lib, cu->vfonti);
                        cu->vfontbi = newlibadr_us(fd, cu->id.lib, cu->vfontbi);
                        
                        cu->vfonti = newlibadr_us(fd, cu->id.lib, cu->vfonti);
                        cu->vfontbi = newlibadr_us(fd, cu->id.lib, cu->vfontbi);
                        
@@ -3125,7 +3126,7 @@ static void direct_link_curve(FileData *fd, Curve *cu)
        cu->mat = newdataadr(fd, cu->mat);
        test_pointer_array(fd, (void **)&cu->mat);
        cu->str = newdataadr(fd, cu->str);
        cu->mat = newdataadr(fd, cu->mat);
        test_pointer_array(fd, (void **)&cu->mat);
        cu->str = newdataadr(fd, cu->str);
-       cu->strinfo= newdataadr(fd, cu->strinfo);
+       cu->strinfo= newdataadr(fd, cu->strinfo);       
        cu->tb = newdataadr(fd, cu->tb);
 
        if (cu->vfont == NULL) link_list(fd, &(cu->nurb));
        cu->tb = newdataadr(fd, cu->tb);
 
        if (cu->vfont == NULL) link_list(fd, &(cu->nurb));
@@ -3136,14 +3137,14 @@ static void direct_link_curve(FileData *fd, Curve *cu)
                if (cu->tb) {
                        memcpy(tb, cu->tb, cu->totbox*sizeof(TextBox));
                        MEM_freeN(cu->tb);
                if (cu->tb) {
                        memcpy(tb, cu->tb, cu->totbox*sizeof(TextBox));
                        MEM_freeN(cu->tb);
-                       cu->tb = tb;
+                       cu->tb = tb;                    
                }
                else {
                        cu->totbox = 1;
                        cu->actbox = 1;
                        cu->tb = tb;
                        cu->tb[0].w = cu->linewidth;
                }
                else {
                        cu->totbox = 1;
                        cu->actbox = 1;
                        cu->tb = tb;
                        cu->tb[0].w = cu->linewidth;
-               }
+               }               
                if (cu->wordspace == 0.0f) cu->wordspace = 1.0f;
        }
 
                if (cu->wordspace == 0.0f) cu->wordspace = 1.0f;
        }
 
@@ -3312,7 +3313,7 @@ static const char *ptcache_data_struct[] = {
        "", // BPHYS_DATA_ROTATION
        "", // BPHYS_DATA_AVELOCITY / BPHYS_DATA_XCONST */
        "", // BPHYS_DATA_SIZE:
        "", // BPHYS_DATA_ROTATION
        "", // BPHYS_DATA_AVELOCITY / BPHYS_DATA_XCONST */
        "", // BPHYS_DATA_SIZE:
-       "", // BPHYS_DATA_TIMES:
+       "", // BPHYS_DATA_TIMES:        
        "BoidData" // case BPHYS_DATA_BOIDS:
 };
 static void direct_link_pointcache(FileData *fd, PointCache *cache)
        "BoidData" // case BPHYS_DATA_BOIDS:
 };
 static void direct_link_pointcache(FileData *fd, PointCache *cache)
@@ -3421,7 +3422,7 @@ static void lib_link_particlesettings(FileData *fd, Main *main)
                                        /* special case for only one object in the group */
                                        index_ok = 1;
                                }
                                        /* special case for only one object in the group */
                                        index_ok = 1;
                                }
-                               else {
+                               else { 
                                        for (dw = part->dupliweights.first; dw; dw = dw->next) {
                                                if (dw->index > 0) {
                                                        index_ok = 1;
                                        for (dw = part->dupliweights.first; dw; dw = dw->next) {
                                                if (dw->index > 0) {
                                                        index_ok = 1;
@@ -3429,7 +3430,7 @@ static void lib_link_particlesettings(FileData *fd, Main *main)
                                                }
                                        }
                                }
                                                }
                                        }
                                }
-
+                               
                                if (index_ok) {
                                        /* if we have indexes, let's use them */
                                        for (dw = part->dupliweights.first; dw; dw = dw->next) {
                                if (index_ok) {
                                        /* if we have indexes, let's use them */
                                        for (dw = part->dupliweights.first; dw; dw = dw->next) {
@@ -3441,9 +3442,9 @@ static void lib_link_particlesettings(FileData *fd, Main *main)
                                        /* otherwise try to get objects from own library (won't work on library linked groups) */
                                        for (dw = part->dupliweights.first; dw; dw = dw->next) {
                                                dw->ob = newlibadr(fd, part->id.lib, dw->ob);
                                        /* otherwise try to get objects from own library (won't work on library linked groups) */
                                        for (dw = part->dupliweights.first; dw; dw = dw->next) {
                                                dw->ob = newlibadr(fd, part->id.lib, dw->ob);
-                                       }
                                }
                        }
                                }
                        }
+                       }
                        else {
                                part->dupliweights.first = part->dupliweights.last = NULL;
                        }
                        else {
                                part->dupliweights.first = part->dupliweights.last = NULL;
                        }
@@ -3658,7 +3659,7 @@ static void lib_link_mtface(FileData *fd, Mesh *me, MTFace *mtface, int totface)
 
 static void lib_link_customdata_mtface(FileData *fd, Mesh *me, CustomData *fdata, int totface)
 {
 
 static void lib_link_customdata_mtface(FileData *fd, Mesh *me, CustomData *fdata, int totface)
 {
-       int i;
+       int i;  
        for (i = 0; i < fdata->totlayer; i++) {
                CustomDataLayer *layer = &fdata->layers[i];
                
        for (i = 0; i < fdata->totlayer; i++) {
                CustomDataLayer *layer = &fdata->layers[i];
                
@@ -3683,11 +3684,11 @@ static void lib_link_customdata_mtpoly(FileData *fd, Mesh *me, CustomData *pdata
                                tf->tpage = newlibadr(fd, me->id.lib, tf->tpage);
                                if (tf->tpage && tf->tpage->id.us == 0) {
                                        tf->tpage->id.us = 1;
                                tf->tpage = newlibadr(fd, me->id.lib, tf->tpage);
                                if (tf->tpage && tf->tpage->id.us == 0) {
                                        tf->tpage->id.us = 1;
-                               }
                        }
                }
        }
 }
                        }
                }
        }
 }
+}
 
 static void lib_link_mesh(FileData *fd, Main *main)
 {
 
 static void lib_link_mesh(FileData *fd, Main *main)
 {
@@ -4107,7 +4108,7 @@ static void lib_link_object(FileData *fd, Main *main)
                                if (paf->type == EFF_PARTICLE) {
                                        paf->group = newlibadr_us(fd, ob->id.lib, paf->group);
                                }
                                if (paf->type == EFF_PARTICLE) {
                                        paf->group = newlibadr_us(fd, ob->id.lib, paf->group);
                                }
-                       }
+                       }                               
                        
                        for (sens = ob->sensors.first; sens; sens = sens->next) {
                                for (a = 0; a < sens->totlinks; a++)
                        
                        for (sens = ob->sensors.first; sens; sens = sens->next) {
                                for (a = 0; a < sens->totlinks; a++)
@@ -4639,7 +4640,7 @@ static void direct_link_object(FileData *fd, Object *ob)
        direct_link_partdeflect(ob->pd);
        ob->soft= newdataadr(fd, ob->soft);
        if (ob->soft) {
        direct_link_partdeflect(ob->pd);
        ob->soft= newdataadr(fd, ob->soft);
        if (ob->soft) {
-               SoftBody *sb = ob->soft;
+               SoftBody *sb = ob->soft;                
                
                sb->bpoint = NULL;      // init pointers so it gets rebuilt nicely
                sb->bspring = NULL;
                
                sb->bpoint = NULL;      // init pointers so it gets rebuilt nicely
                sb->bspring = NULL;
@@ -4786,6 +4787,7 @@ static void lib_link_scene(FileData *fd, Main *main)
        Base *base, *next;
        Sequence *seq;
        SceneRenderLayer *srl;
        Base *base, *next;
        Sequence *seq;
        SceneRenderLayer *srl;
+       FreestyleLineSet *fls;
        TimeMarker *marker;
        
        for (sce = main->scene.first; sce; sce = sce->id.next) {
        TimeMarker *marker;
        
        for (sce = main->scene.first; sce; sce = sce->id.next) {
@@ -4889,6 +4891,10 @@ static void lib_link_scene(FileData *fd, Main *main)
                        for (srl = sce->r.layers.first; srl; srl = srl->next) {
                                srl->mat_override = newlibadr_us(fd, sce->id.lib, srl->mat_override);
                                srl->light_override = newlibadr_us(fd, sce->id.lib, srl->light_override);
                        for (srl = sce->r.layers.first; srl; srl = srl->next) {
                                srl->mat_override = newlibadr_us(fd, sce->id.lib, srl->mat_override);
                                srl->light_override = newlibadr_us(fd, sce->id.lib, srl->light_override);
+                               for(fls=srl->freestyleConfig.linesets.first; fls; fls= fls->next) {
+                                       fls->linestyle= newlibadr_us(fd, sce->id.lib, fls->linestyle);
+                                       fls->group= newlibadr_us(fd, sce->id.lib, fls->group);
+                               }
                        }
                        /*Game Settings: Dome Warp Text*/
                        sce->gm.dome.warptext = newlibadr(fd, sce->id.lib, sce->gm.dome.warptext);
                        }
                        /*Game Settings: Dome Warp Text*/
                        sce->gm.dome.warptext = newlibadr(fd, sce->id.lib, sce->gm.dome.warptext);
@@ -4957,6 +4963,7 @@ static void direct_link_scene(FileData *fd, Scene *sce)
        Editing *ed;
        Sequence *seq;
        MetaStack *ms;
        Editing *ed;
        Sequence *seq;
        MetaStack *ms;
+       SceneRenderLayer *srl;
        
        sce->theDag = NULL;
        sce->dagisvalid = 0;
        
        sce->theDag = NULL;
        sce->dagisvalid = 0;
@@ -5082,7 +5089,7 @@ static void direct_link_scene(FileData *fd, Scene *sce)
                                        ed->seqbasep = (ListBase *)(poin+offset);
                                else
                                        ed->seqbasep = &ed->seqbase;
                                        ed->seqbasep = (ListBase *)(poin+offset);
                                else
                                        ed->seqbasep = &ed->seqbase;
-                       }
+                       }                       
                        /* stack */
                        link_list(fd, &(ed->metastack));
                        
                        /* stack */
                        link_list(fd, &(ed->metastack));
                        
@@ -5116,7 +5123,7 @@ static void direct_link_scene(FileData *fd, Scene *sce)
        }
        if (sce->r.ffcodecdata.properties) {
                sce->r.ffcodecdata.properties = newdataadr(fd, sce->r.ffcodecdata.properties);
        }
        if (sce->r.ffcodecdata.properties) {
                sce->r.ffcodecdata.properties = newdataadr(fd, sce->r.ffcodecdata.properties);
-               if (sce->r.ffcodecdata.properties) {
+               if (sce->r.ffcodecdata.properties) { 
                        IDP_DirectLinkProperty(sce->r.ffcodecdata.properties, 
                                (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
                }
                        IDP_DirectLinkProperty(sce->r.ffcodecdata.properties, 
                                (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
                }
@@ -5125,6 +5132,13 @@ static void direct_link_scene(FileData *fd, Scene *sce)
        link_list(fd, &(sce->markers));
        link_list(fd, &(sce->transform_spaces));
        link_list(fd, &(sce->r.layers));
        link_list(fd, &(sce->markers));
        link_list(fd, &(sce->transform_spaces));
        link_list(fd, &(sce->r.layers));
+
+       for(srl = sce->r.layers.first; srl; srl = srl->next) {
+               link_list(fd, &(srl->freestyleConfig.modules));
+       }
+       for(srl = sce->r.layers.first; srl; srl = srl->next) {
+               link_list(fd, &(srl->freestyleConfig.linesets));
+       }
        
        sce->nodetree = newdataadr(fd, sce->nodetree);
        if (sce->nodetree) {
        
        sce->nodetree = newdataadr(fd, sce->nodetree);
        if (sce->nodetree) {
@@ -6388,6 +6402,177 @@ static void lib_link_mask(FileData *fd, Main *main)
        }
 }
 
        }
 }
 
+/* ************ READ LINE STYLE ***************** */
+
+static void lib_link_linestyle(FileData *fd, Main *main)
+{
+       FreestyleLineStyle *linestyle;
+       LineStyleModifier *m;
+
+       linestyle = main->linestyle.first;
+       while (linestyle) {
+               if (linestyle->id.flag & LIB_NEED_LINK) {
+                       linestyle->id.flag -= LIB_NEED_LINK;
+
+                       if (linestyle->id.properties) IDP_LibLinkProperty(linestyle->id.properties, (fd->flags & FD_FLAGS_SWITCH_ENDIAN), fd);
+                       if (linestyle->adt) lib_link_animdata(fd, &linestyle->id, linestyle->adt);
+                       for (m = linestyle->color_modifiers.first; m; m = m->next) {
+                               switch (m->type) {
+                               case LS_MODIFIER_DISTANCE_FROM_OBJECT:
+                                       {
+                                               LineStyleColorModifier_DistanceFromObject *cm = (LineStyleColorModifier_DistanceFromObject *)m;
+                                               cm->target = newlibadr(fd, linestyle->id.lib, cm->target);
+                                       }
+                                       break;
+                               }
+                       }
+                       for (m = linestyle->alpha_modifiers.first; m; m = m->next){
+                               switch (m->type) {
+                               case LS_MODIFIER_DISTANCE_FROM_OBJECT:
+                                       {
+                                               LineStyleAlphaModifier_DistanceFromObject *am = (LineStyleAlphaModifier_DistanceFromObject *)m;
+                                               am->target = newlibadr(fd, linestyle->id.lib, am->target);
+                                       }
+                                       break;
+                               }
+                       }
+                       for (m = linestyle->thickness_modifiers.first; m; m = m->next){
+                               switch (m->type) {
+                               case LS_MODIFIER_DISTANCE_FROM_OBJECT:
+                                       {
+                                               LineStyleThicknessModifier_DistanceFromObject *tm = (LineStyleThicknessModifier_DistanceFromObject *)m;
+                                               tm->target = newlibadr(fd, linestyle->id.lib, tm->target);
+                                       }
+                                       break;
+                               }
+                       }
+               }
+               linestyle = linestyle->id.next;
+       }
+}
+
+static void direct_link_linestyle_color_modifier(FileData *fd, LineStyleModifier *modifier)
+{
+       switch (modifier->type) {
+       case LS_MODIFIER_ALONG_STROKE:
+               {
+                       LineStyleColorModifier_AlongStroke *m = (LineStyleColorModifier_AlongStroke *)modifier;
+                       m->color_ramp = newdataadr(fd, m->color_ramp);
+               }
+               break;
+       case LS_MODIFIER_DISTANCE_FROM_CAMERA:
+               {
+                       LineStyleColorModifier_DistanceFromCamera *m = (LineStyleColorModifier_DistanceFromCamera *)modifier;
+                       m->color_ramp = newdataadr(fd, m->color_ramp);
+               }
+               break;
+       case LS_MODIFIER_DISTANCE_FROM_OBJECT:
+               {
+                       LineStyleColorModifier_DistanceFromObject *m = (LineStyleColorModifier_DistanceFromObject *)modifier;
+                       m->color_ramp = newdataadr(fd, m->color_ramp);
+               }
+               break;
+       case LS_MODIFIER_MATERIAL:
+               {
+                       LineStyleColorModifier_Material *m = (LineStyleColorModifier_Material *)modifier;
+                       m->color_ramp = newdataadr(fd, m->color_ramp);
+               }
+               break;
+       }
+}
+
+static void direct_link_linestyle_alpha_modifier(FileData *fd, LineStyleModifier *modifier)
+{
+       switch (modifier->type) {
+       case LS_MODIFIER_ALONG_STROKE:
+               {
+                       LineStyleAlphaModifier_AlongStroke *m = (LineStyleAlphaModifier_AlongStroke *)modifier;
+                       m->curve = newdataadr(fd, m->curve);
+                       direct_link_curvemapping(fd, m->curve);
+               }
+               break;
+       case LS_MODIFIER_DISTANCE_FROM_CAMERA:
+               {
+                       LineStyleAlphaModifier_DistanceFromCamera *m = (LineStyleAlphaModifier_DistanceFromCamera *)modifier;
+                       m->curve = newdataadr(fd, m->curve);
+                       direct_link_curvemapping(fd, m->curve);
+               }
+               break;
+       case LS_MODIFIER_DISTANCE_FROM_OBJECT:
+               {
+                       LineStyleAlphaModifier_DistanceFromObject *m = (LineStyleAlphaModifier_DistanceFromObject *)modifier;
+                       m->curve = newdataadr(fd, m->curve);
+                       direct_link_curvemapping(fd, m->curve);
+               }
+               break;
+       case LS_MODIFIER_MATERIAL:
+               {
+                       LineStyleAlphaModifier_Material *m = (LineStyleAlphaModifier_Material *)modifier;
+                       m->curve = newdataadr(fd, m->curve);
+                       direct_link_curvemapping(fd, m->curve);
+               }
+               break;
+       }
+}
+
+static void direct_link_linestyle_thickness_modifier(FileData *fd, LineStyleModifier *modifier)
+{
+       switch (modifier->type) {
+       case LS_MODIFIER_ALONG_STROKE:
+               {
+                       LineStyleThicknessModifier_AlongStroke *m = (LineStyleThicknessModifier_AlongStroke *)modifier;
+                       m->curve = newdataadr(fd, m->curve);
+                       direct_link_curvemapping(fd, m->curve);
+               }
+               break;
+       case LS_MODIFIER_DISTANCE_FROM_CAMERA:
+               {
+                       LineStyleThicknessModifier_DistanceFromCamera *m = (LineStyleThicknessModifier_DistanceFromCamera *)modifier;
+                       m->curve = newdataadr(fd, m->curve);
+                       direct_link_curvemapping(fd, m->curve);
+               }
+               break;
+       case LS_MODIFIER_DISTANCE_FROM_OBJECT:
+               {
+                       LineStyleThicknessModifier_DistanceFromObject *m = (LineStyleThicknessModifier_DistanceFromObject *)modifier;
+                       m->curve = newdataadr(fd, m->curve);
+                       direct_link_curvemapping(fd, m->curve);
+               }
+               break;
+       case LS_MODIFIER_MATERIAL:
+               {
+                       LineStyleThicknessModifier_Material *m = (LineStyleThicknessModifier_Material *)modifier;
+                       m->curve = newdataadr(fd, m->curve);
+                       direct_link_curvemapping(fd, m->curve);
+               }
+               break;
+       }
+}
+
+static void direct_link_linestyle_geometry_modifier(FileData *fd, LineStyleModifier *modifier)
+{
+}
+
+static void direct_link_linestyle(FileData *fd, FreestyleLineStyle *linestyle)
+{
+       LineStyleModifier *modifier;
+
+       linestyle->adt= newdataadr(fd, linestyle->adt);
+       direct_link_animdata(fd, linestyle->adt);
+       link_list(fd, &linestyle->color_modifiers);
+       for(modifier=linestyle->color_modifiers.first; modifier; modifier= modifier->next)
+               direct_link_linestyle_color_modifier(fd, modifier);
+       link_list(fd, &linestyle->alpha_modifiers);
+       for(modifier=linestyle->alpha_modifiers.first; modifier; modifier= modifier->next)
+               direct_link_linestyle_alpha_modifier(fd, modifier);
+       link_list(fd, &linestyle->thickness_modifiers);
+       for(modifier=linestyle->thickness_modifiers.first; modifier; modifier= modifier->next)
+               direct_link_linestyle_thickness_modifier(fd, modifier);
+       link_list(fd, &linestyle->geometry_modifiers);
+       for(modifier=linestyle->geometry_modifiers.first; modifier; modifier= modifier->next)
+               direct_link_linestyle_geometry_modifier(fd, modifier);
+}
+
 /* ************** GENERAL & MAIN ******************** */
 
 
 /* ************** GENERAL & MAIN ******************** */
 
 
@@ -6422,6 +6607,7 @@ static const char *dataname(short id_code)
                case ID_PA: return "Data from PA";
                case ID_GD: return "Data from GD";
                case ID_MC: return "Data from MC";
                case ID_PA: return "Data from PA";
                case ID_GD: return "Data from GD";
                case ID_MC: return "Data from MC";
+               case ID_LS: return "Data from LS";
        }
        return "Data from Lib Block";
        
        }
        return "Data from Lib Block";
        
@@ -6598,6 +6784,9 @@ static BHead *read_libblock(FileData *fd, Main *main, BHead *bhead, int flag, ID
                case ID_MSK:
                        direct_link_mask(fd, (Mask *)id);
                        break;
                case ID_MSK:
                        direct_link_mask(fd, (Mask *)id);
                        break;
+               case ID_LS:
+                       direct_link_linestyle(fd, (FreestyleLineStyle *)id);
+                       break;
        }
        
        oldnewmap_free_unused(fd->datamap);
        }
        
        oldnewmap_free_unused(fd->datamap);
@@ -7150,7 +7339,7 @@ static void do_version_node_fix_internal_links_264(void *UNUSED(data), ID *UNUSE
                }
        }
 }
                }
        }
 }
-       
+
 static void do_version_logic_264(ListBase *regionbase)
 {
        ARegion *ar;
 static void do_version_logic_264(ListBase *regionbase)
 {
        ARegion *ar;
@@ -7398,10 +7587,10 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                        if ( (ob->dsize[i] == 0.0f) || /* simple case, user never touched dsize */
                                             (ob->size[i]  == 0.0f))   /* cant scale the dsize to give a non zero result, so fallback to 1.0f */
                                        {
                                        if ( (ob->dsize[i] == 0.0f) || /* simple case, user never touched dsize */
                                             (ob->size[i]  == 0.0f))   /* cant scale the dsize to give a non zero result, so fallback to 1.0f */
                                        {
-                                               ob->dscale[i] = 1.0f;
+                                               ob->dscale[i]= 1.0f;
                                        }
                                        else {
                                        }
                                        else {
-                                               ob->dscale[i] = (ob->size[i] + ob->dsize[i]) / ob->size[i];
+                                               ob->dscale[i]= (ob->size[i] + ob->dsize[i]) / ob->size[i];
                                        }
                                }
                        }
                                        }
                                }
                        }
@@ -8287,7 +8476,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                        do_version_node_fix_internal_links_264(NULL, NULL, ntree);
                
        }
                        do_version_node_fix_internal_links_264(NULL, NULL, ntree);
                
        }
-       
+
        if (main->versionfile < 264 || (main->versionfile == 264 && main->subversionfile < 6)) {
                bScreen *sc;
                
        if (main->versionfile < 264 || (main->versionfile == 264 && main->subversionfile < 6)) {
                bScreen *sc;
                
@@ -8375,6 +8564,39 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                }
        }
 
                }
        }
 
+       /* default values in Freestyle settings */
+       {
+               Scene *sce;
+               SceneRenderLayer *srl;
+               FreestyleLineStyle *linestyle;
+
+               for(sce = main->scene.first; sce; sce = sce->id.next) {
+                       if (sce->r.line_thickness_mode == 0) {
+                               sce->r.line_thickness_mode= R_LINE_THICKNESS_ABSOLUTE;
+                               sce->r.unit_line_thickness= 1.f;
+                       }
+                       for(srl= sce->r.layers.first; srl; srl= srl->next) {
+                               if (srl->freestyleConfig.mode == 0)
+                                       srl->freestyleConfig.mode= FREESTYLE_CONTROL_EDITOR_MODE;
+                               if (srl->freestyleConfig.raycasting_algorithm == FREESTYLE_ALGO_CULLED_ADAPTIVE_CUMULATIVE ||
+                                       srl->freestyleConfig.raycasting_algorithm == FREESTYLE_ALGO_CULLED_ADAPTIVE_TRADITIONAL) {
+                                       srl->freestyleConfig.raycasting_algorithm= 0; /* deprecated */
+                                       srl->freestyleConfig.flags |= FREESTYLE_CULLING;
+                               }
+                       }
+               }
+               for(linestyle = main->linestyle.first; linestyle; linestyle = linestyle->id.next) {
+                       if (linestyle->thickness_position == 0) {
+                               linestyle->thickness_position= LS_THICKNESS_CENTER;
+                               linestyle->thickness_ratio= 0.5f;
+                       }
+                       if (linestyle->chaining == 0)
+                               linestyle->chaining= LS_CHAINING_PLAIN;
+                       if (linestyle->rounds == 0)
+                               linestyle->rounds= 3;
+               }
+       }
+
        /* WATCH IT!!!: pointers from libdata have not been converted yet here! */
        /* WATCH IT 2!: Userdef struct init has to be in editors/interface/resources.c! */
 
        /* WATCH IT!!!: pointers from libdata have not been converted yet here! */
        /* WATCH IT 2!: Userdef struct init has to be in editors/interface/resources.c! */
 
@@ -8421,6 +8643,7 @@ static void lib_link_all(FileData *fd, Main *main)
        lib_link_particlesettings(fd, main);
        lib_link_movieclip(fd, main);
        lib_link_mask(fd, main);
        lib_link_particlesettings(fd, main);
        lib_link_movieclip(fd, main);
        lib_link_mask(fd, main);
+       lib_link_linestyle(fd, main);
 
        lib_link_mesh(fd, main);                /* as last: tpage images with users at zero */
        
 
        lib_link_mesh(fd, main);                /* as last: tpage images with users at zero */
        
@@ -9031,7 +9254,7 @@ static void expand_curve(FileData *fd, Main *mainvar, Curve *cu)
        }
        
        expand_doit(fd, mainvar, cu->vfont);
        }
        
        expand_doit(fd, mainvar, cu->vfont);
-       expand_doit(fd, mainvar, cu->vfontb);
+       expand_doit(fd, mainvar, cu->vfontb);   
        expand_doit(fd, mainvar, cu->vfonti);
        expand_doit(fd, mainvar, cu->vfontbi);
        expand_doit(fd, mainvar, cu->key);
        expand_doit(fd, mainvar, cu->vfonti);
        expand_doit(fd, mainvar, cu->vfontbi);
        expand_doit(fd, mainvar, cu->key);
@@ -9335,6 +9558,7 @@ static void expand_scene(FileData *fd, Main *mainvar, Scene *sce)
 {
        Base *base;
        SceneRenderLayer *srl;
 {
        Base *base;
        SceneRenderLayer *srl;
+       FreestyleLineSet *lineset;
        
        for (base = sce->base.first; base; base = base->next) {
                expand_doit(fd, mainvar, base->object);
        
        for (base = sce->base.first; base; base = base->next) {
                expand_doit(fd, mainvar, base->object);
@@ -9355,6 +9579,12 @@ static void expand_scene(FileData *fd, Main *mainvar, Scene *sce)
        for (srl = sce->r.layers.first; srl; srl = srl->next) {
                expand_doit(fd, mainvar, srl->mat_override);
                expand_doit(fd, mainvar, srl->light_override);
        for (srl = sce->r.layers.first; srl; srl = srl->next) {
                expand_doit(fd, mainvar, srl->mat_override);
                expand_doit(fd, mainvar, srl->light_override);
+
+               for (lineset = srl->freestyleConfig.linesets.first; lineset; lineset = lineset->next) {
+                       if (lineset->group)
+                               expand_doit(fd, mainvar, lineset->group);
+                       expand_doit(fd, mainvar, lineset->linestyle);
+               }
        }
        
        if (sce->r.dometext)
        }
        
        if (sce->r.dometext)
@@ -9448,6 +9678,26 @@ static void expand_mask(FileData *fd, Main *mainvar, Mask *mask)
        }
 }
 
        }
 }
 
+static void expand_linestyle(FileData *fd, Main *mainvar, FreestyleLineStyle *linestyle)
+{
+       LineStyleModifier *m;
+
+       if (linestyle->adt)
+               expand_animdata(fd, mainvar, linestyle->adt);
+       for (m = linestyle->color_modifiers.first; m; m = m->next) {
+               if (m->type == LS_MODIFIER_DISTANCE_FROM_OBJECT)
+                       expand_doit(fd, mainvar, ((LineStyleColorModifier_DistanceFromObject *)m)->target);
+       }
+       for (m = linestyle->alpha_modifiers.first; m; m = m->next){
+               if (m->type == LS_MODIFIER_DISTANCE_FROM_OBJECT)
+                       expand_doit(fd, mainvar, ((LineStyleAlphaModifier_DistanceFromObject *)m)->target);
+       }
+       for (m = linestyle->thickness_modifiers.first; m; m = m->next){
+               if (m->type == LS_MODIFIER_DISTANCE_FROM_OBJECT)
+                       expand_doit(fd, mainvar, ((LineStyleThicknessModifier_DistanceFromObject *)m)->target);
+       }
+}
+
 static void expand_main(FileData *fd, Main *mainvar)
 {
        ListBase *lbarray[MAX_LIBARRAY];
 static void expand_main(FileData *fd, Main *mainvar)
 {
        ListBase *lbarray[MAX_LIBARRAY];
@@ -9534,6 +9784,9 @@ static void expand_main(FileData *fd, Main *mainvar)
                                        case ID_MSK:
                                                expand_mask(fd, mainvar, (Mask *)id);
                                                break;
                                        case ID_MSK:
                                                expand_mask(fd, mainvar, (Mask *)id);
                                                break;
+                                       case ID_LS:
+                                               expand_linestyle(fd, mainvar, (FreestyleLineStyle *)id);
+                                               break;
                                        }
                                        
                                        do_it = TRUE;
                                        }
                                        
                                        do_it = TRUE;
@@ -9853,7 +10106,7 @@ static void library_append_end(const bContext *C, Main *mainl, FileData **fd, in
        if ((*fd)->flags & FD_FLAGS_SWITCH_ENDIAN) {
                blo_freefiledata(*fd);
                *fd = NULL;
        if ((*fd)->flags & FD_FLAGS_SWITCH_ENDIAN) {
                blo_freefiledata(*fd);
                *fd = NULL;
-       }
+       }       
 }
 
 void BLO_library_append_end(const bContext *C, struct Main *mainl, BlendHandle** bh, int idcode, short flag)
 }
 
 void BLO_library_append_end(const bContext *C, struct Main *mainl, BlendHandle** bh, int idcode, short flag)