svn merge ^/trunk/blender -r55372:55392
[blender.git] / source / blender / blenloader / intern / readfile.c
index ea5610af3b5ad19aafabe18e5e6f8bacb8156723..0fa01b95bb2bf5efc908cc4a02ac70f66316bdf3 100644 (file)
@@ -71,6 +71,9 @@
 #include "DNA_key_types.h"
 #include "DNA_lattice_types.h"
 #include "DNA_lamp_types.h"
+#ifdef WITH_FREESTYLE
+#  include "DNA_linestyle_types.h"
+#endif
 #include "DNA_meta_types.h"
 #include "DNA_material_types.h"
 #include "DNA_mesh_types.h"
@@ -1067,10 +1070,10 @@ FileData *blo_openblendermemory(const void *mem, int memsize, ReportList *report
                        }
                }
                else
-                       fd->read = fd_read_from_memory;
+               fd->read = fd_read_from_memory;
                        
                fd->flags |= FD_FLAGS_NOT_MY_BUFFER;
-
+               
                return blo_decode_and_check(fd, reports);
        }
 }
@@ -1200,19 +1203,19 @@ static void *newdataadr(FileData *fd, void *adr)                /* only direct databocks */
        return oldnewmap_lookup_and_inc(fd->datamap, adr);
 }
 
-static void *newglobadr(FileData *fd, void *adr)           /* direct datablocks with global linking */
+static void *newglobadr(FileData *fd, void *adr)               /* direct datablocks with global linking */
 {
        return oldnewmap_lookup_and_inc(fd->globmap, adr);
 }
 
-static void *newimaadr(FileData *fd, void *adr)                    /* used to restore image data after undo */
+static void *newimaadr(FileData *fd, void *adr)                /* used to restore image data after undo */
 {
        if (fd->imamap && adr)
                return oldnewmap_lookup_and_inc(fd->imamap, adr);
        return NULL;
 }
 
-static void *newmclipadr(FileData *fd, void *adr)      /* used to restore movie clip data after undo */
+static void *newmclipadr(FileData *fd, void *adr)              /* used to restore movie clip data after undo */
 {
        if (fd->movieclipmap && adr)
                return oldnewmap_lookup_and_inc(fd->movieclipmap, adr);
@@ -1223,7 +1226,7 @@ static void *newpackedadr(FileData *fd, void *adr)      /* used to restore packe
 {
        if (fd->packedmap && adr)
                return oldnewmap_lookup_and_inc(fd->packedmap, adr);
-       
+
        return oldnewmap_lookup_and_inc(fd->datamap, adr);
 }
 
@@ -1401,7 +1404,7 @@ void blo_make_movieclip_pointer_map(FileData *fd, Main *oldmain)
        for (; sce; sce = sce->id.next) {
                if (sce->nodetree) {
                        bNode *node;
-                       for (node = sce->nodetree->nodes.first; node; node= node->next)
+                       for (node = sce->nodetree->nodes.first; node; node = node->next)
                                if (node->type == CMP_NODE_MOVIEDISTORTION)
                                        oldnewmap_insert(fd->movieclipmap, node->storage, node->storage, 0);
                }
@@ -2332,15 +2335,15 @@ static void lib_link_ntree(FileData *fd, ID *id, bNodeTree *ntree)
                
                node->id= newlibadr_us(fd, id->lib, node->id);
 
-               for (sock=node->inputs.first; sock; sock=sock->next)
+               for (sock = node->inputs.first; sock; sock = sock->next)
                        lib_link_node_socket(fd, id, sock);
-               for (sock=node->outputs.first; sock; sock=sock->next)
+               for (sock = node->outputs.first; sock; sock = sock->next)
                        lib_link_node_socket(fd, id, sock);
        }
        
-       for (sock=ntree->inputs.first; sock; sock=sock->next)
+       for (sock = ntree->inputs.first; sock; sock = sock->next)
                lib_link_node_socket(fd, id, sock);
-       for (sock=ntree->outputs.first; sock; sock=sock->next)
+       for (sock = ntree->outputs.first; sock; sock = sock->next)
                lib_link_node_socket(fd, id, sock);
 }
 
@@ -2367,7 +2370,7 @@ static void lib_node_do_versions_group_indices(bNode *gnode)
        bNodeSocket *sock;
        bNodeLink *link;
        
-       for (sock=gnode->outputs.first; sock; sock=sock->next) {
+       for (sock=gnode->outputs.first; sock; sock = sock->next) {
                int old_index = sock->to_index;
                
                for (link = ngroup->links.first; link; link = link->next) {
@@ -2380,7 +2383,7 @@ static void lib_node_do_versions_group_indices(bNode *gnode)
                        }
                }
        }
-       for (sock=gnode->inputs.first; sock; sock=sock->next) {
+       for (sock=gnode->inputs.first; sock; sock = sock->next) {
                int old_index = sock->to_index;
                
                for (link = ngroup->links.first; link; link = link->next) {
@@ -3415,7 +3418,7 @@ static void direct_link_texture(FileData *fd, Tex *tex)
        tex->env = newdataadr(fd, tex->env);
        if (tex->env) {
                tex->env->ima = NULL;
-               memset(tex->env->cube, 0, 6 * sizeof(void *));
+               memset(tex->env->cube, 0, 6*sizeof(void *));
                tex->env->ok= 0;
        }
        tex->pd = newdataadr(fd, tex->pd);
@@ -4245,9 +4248,9 @@ static void lib_link_object(FileData *fd, Main *main)
                                        ob->proxy = NULL;
                                        
                                        if (ob->id.lib)
-                                               printf("Proxy lost from  object %s lib %s\n", ob->id.name+2, ob->id.lib->name);
+                                               printf("Proxy lost from  object %s lib %s\n", ob->id.name + 2, ob->id.lib->name);
                                        else
-                                               printf("Proxy lost from  object %s lib <NONE>\n", ob->id.name+2);
+                                               printf("Proxy lost from  object %s lib <NONE>\n", ob->id.name + 2);
                                }
                                else {
                                        /* this triggers object_update to always use a copy */
@@ -4261,9 +4264,9 @@ static void lib_link_object(FileData *fd, Main *main)
                        
                        if (ob->data==NULL && poin!=NULL) {
                                if (ob->id.lib)
-                                       printf("Can't find obdata of %s lib %s\n", ob->id.name+2, ob->id.lib->name);
+                                       printf("Can't find obdata of %s lib %s\n", ob->id.name + 2, ob->id.lib->name);
                                else
-                                       printf("Object %s lost data.\n", ob->id.name+2);
+                                       printf("Object %s lost data.\n", ob->id.name + 2);
                                
                                ob->type = OB_EMPTY;
                                warn = 1;
@@ -4984,7 +4987,7 @@ static void composite_patch(bNodeTree *ntree, Scene *scene)
 {
        bNode *node;
        
-       for (node= ntree->nodes.first; node; node= node->next) {
+       for (node = ntree->nodes.first; node; node = node->next) {
                if (node->id==NULL && ELEM4(node->type, CMP_NODE_R_LAYERS, CMP_NODE_COMPOSITE, CMP_NODE_DEFOCUS, CMP_NODE_OUTPUT_FILE))
                        node->id = &scene->id;
        }
@@ -5129,6 +5132,16 @@ 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);
+#ifdef WITH_FREESTYLE
+                               {
+                                       FreestyleLineSet *fls;
+
+                                       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);
+                                       }
+                               }
+#endif
                        }
                        /*Game Settings: Dome Warp Text*/
                        sce->gm.dome.warptext = newlibadr(fd, sce->id.lib, sce->gm.dome.warptext);
@@ -5235,7 +5248,7 @@ static void direct_link_scene(FileData *fd, Scene *sce)
                if (sce->toolsettings->vpaint) {
                        sce->toolsettings->vpaint->vpaint_prev = NULL;
                        sce->toolsettings->vpaint->tot = 0;
-               }
+       }
                if (sce->toolsettings->wpaint) {
                        sce->toolsettings->wpaint->wpaint_prev = NULL;
                        sce->toolsettings->wpaint->tot = 0;
@@ -5375,6 +5388,19 @@ 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));
+
+#ifdef WITH_FREESTYLE
+       {
+               SceneRenderLayer *srl;
+
+               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));
+               }
+       }
+#endif
        
        sce->nodetree = newdataadr(fd, sce->nodetree);
        if (sce->nodetree) {
@@ -5681,7 +5707,7 @@ static void lib_link_screen(FileData *fd, Main *main)
 
 static bool restore_pointer(ID *id, ID *newid, int user)
 {
-       if (strcmp(newid->name+2, id->name+2) == 0) {
+       if (strcmp(newid->name + 2, id->name + 2) == 0) {
                if (newid->lib == id->lib) {
                        if (user == 1) {
                                if (newid->us == 0) {
@@ -6027,7 +6053,7 @@ static void direct_link_region(FileData *fd, ARegion *ar, int spacetype)
                pa->activedata = NULL;
                pa->type = NULL;
        }
-
+       
        link_list(fd, &ar->ui_lists);
 
        for (ui_list = ar->ui_lists.first; ui_list; ui_list = ui_list->next) {
@@ -6342,7 +6368,7 @@ static void direct_link_screen(FileData *fd, bScreen *sc)
                                sclip->scopes.track_preview = NULL;
                                sclip->draw_context = NULL;
                                sclip->scopes.ok = 0;
-                       }
+               }
                }
                
                sa->actionzones.first = sa->actionzones.last = NULL;
@@ -6711,6 +6737,181 @@ static void lib_link_mask(FileData *fd, Main *main)
        }
 }
 
+#ifdef WITH_FREESTYLE
+/* ************ 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 *UNUSED(fd), LineStyleModifier *UNUSED(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);
+}
+#endif
+
 /* ************** GENERAL & MAIN ******************** */
 
 
@@ -6745,6 +6946,9 @@ 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";
+#ifdef WITH_FREESTYLE
+               case ID_LS: return "Data from LS";
+#endif
        }
        return "Data from Lib Block";
        
@@ -6921,6 +7125,11 @@ static BHead *read_libblock(FileData *fd, Main *main, BHead *bhead, int flag, ID
                case ID_MSK:
                        direct_link_mask(fd, (Mask *)id);
                        break;
+#ifdef WITH_FREESTYLE
+               case ID_LS:
+                       direct_link_linestyle(fd, (FreestyleLineStyle *)id);
+                       break;
+#endif
        }
        
        oldnewmap_free_unused(fd->datamap);
@@ -7036,7 +7245,7 @@ static void do_versions_nodetree_convert_angle(bNodeTree *ntree)
                        /* Convert degrees to radians. */
                        NodeDefocus *nqd = node->storage;
                        /* XXX DNA char to float conversion seems to map the char value into the [0.0f, 1.0f] range... */
-                       nqd->rotation = DEG2RADF(nqd->rotation * 255.0f);
+                       nqd->rotation = DEG2RADF(nqd->rotation*255.0f);
                }
                else if (node->type == CMP_NODE_CHROMA_MATTE) {
                        /* Convert degrees to radians. */
@@ -7048,7 +7257,7 @@ static void do_versions_nodetree_convert_angle(bNodeTree *ntree)
                        /* Convert degrees to radians. */
                        NodeGlare *ndg = node->storage;
                        /* XXX DNA char to float conversion seems to map the char value into the [0.0f, 1.0f] range... */
-                       ndg->angle_ofs = DEG2RADF(ndg->angle_ofs * 255.0f);
+                       ndg->angle_ofs = DEG2RADF(ndg->angle_ofs*255.0f);
                }
                /* XXX TexMapping struct is used by other nodes too (at least node_composite_mapValue),
                 *     but not the rot part...
@@ -7238,7 +7447,7 @@ static void do_versions_nodetree_multi_file_output_format_2_62_1(Scene *sce, bNo
                                nimf->format = sce->r.im_format;
                        
                        /* transfer render format toggle to node format toggle */
-                       for (sock=node->inputs.first; sock; sock=sock->next) {
+                       for (sock = node->inputs.first; sock; sock = sock->next) {
                                NodeImageMultiFileSocket *simf = sock->storage;
                                simf->use_node_format = simf->use_render_format;
                        }
@@ -7292,7 +7501,7 @@ static void do_versions_nodetree_file_output_layers_2_64_5(bNodeTree *ntree)
        for (node=ntree->nodes.first; node; node=node->next) {
                if (node->type == CMP_NODE_OUTPUT_FILE) {
                        bNodeSocket *sock;
-                       for (sock=node->inputs.first; sock; sock=sock->next) {
+                       for (sock = node->inputs.first; sock; sock = sock->next) {
                                NodeImageMultiFileSocket *input = sock->storage;
                                
                                /* multilayer names are stored as separate strings now,
@@ -7487,15 +7696,15 @@ static void do_versions_nodetree_customnodes(bNodeTree *ntree, int UNUSED(is_gro
                        node->flag |= NODE_INIT;
                        
                        /* sockets idname */
-                       for (sock=node->inputs.first; sock; sock=sock->next)
+                       for (sock = node->inputs.first; sock; sock = sock->next)
                                BLI_strncpy(sock->idname, node_socket_get_static_idname(sock), sizeof(sock->idname));
-                       for (sock=node->outputs.first; sock; sock=sock->next)
+                       for (sock = node->outputs.first; sock; sock = sock->next)
                                BLI_strncpy(sock->idname, node_socket_get_static_idname(sock), sizeof(sock->idname));
                }
                /* tree sockets idname */
-               for (sock=ntree->inputs.first; sock; sock=sock->next)
+               for (sock = ntree->inputs.first; sock; sock = sock->next)
                        BLI_strncpy(sock->idname, node_socket_get_static_idname(sock), sizeof(sock->idname));
-               for (sock=ntree->outputs.first; sock; sock=sock->next)
+               for (sock = ntree->outputs.first; sock; sock = sock->next)
                        BLI_strncpy(sock->idname, node_socket_get_static_idname(sock), sizeof(sock->idname));
        }
        
@@ -7505,14 +7714,14 @@ static void do_versions_nodetree_customnodes(bNodeTree *ntree, int UNUSED(is_gro
                bNodeSocket *sock;
                
                for (node=ntree->nodes.first; node; node=node->next) {
-                       for (sock=node->inputs.first; sock; sock=sock->next)
+                       for (sock = node->inputs.first; sock; sock = sock->next)
                                sock->in_out = SOCK_IN;
-                       for (sock=node->outputs.first; sock; sock=sock->next)
+                       for (sock = node->outputs.first; sock; sock = sock->next)
                                sock->in_out = SOCK_OUT;
                }
-               for (sock=ntree->inputs.first; sock; sock=sock->next)
+               for (sock = ntree->inputs.first; sock; sock = sock->next)
                        sock->in_out = SOCK_IN;
-               for (sock=ntree->outputs.first; sock; sock=sock->next)
+               for (sock = ntree->outputs.first; sock; sock = sock->next)
                        sock->in_out = SOCK_OUT;
        }
        
@@ -7522,20 +7731,20 @@ static void do_versions_nodetree_customnodes(bNodeTree *ntree, int UNUSED(is_gro
                bNodeSocket *sock;
                
                for (node=ntree->nodes.first; node; node=node->next) {
-                       for (sock=node->inputs.first; sock; sock=sock->next) {
+                       for (sock = node->inputs.first; sock; sock = sock->next) {
                                BLI_strncpy(sock->identifier, sock->name, sizeof(sock->identifier));
                                BLI_uniquename(&node->inputs, sock, sock->identifier, '.', offsetof(bNodeSocket, identifier), sizeof(sock->identifier));
                        }
-                       for (sock=node->outputs.first; sock; sock=sock->next) {
+                       for (sock = node->outputs.first; sock; sock = sock->next) {
                                BLI_strncpy(sock->identifier, sock->name, sizeof(sock->identifier));
                                BLI_uniquename(&node->outputs, sock, sock->identifier, '.', offsetof(bNodeSocket, identifier), sizeof(sock->identifier));
                        }
                }
-               for (sock=ntree->inputs.first; sock; sock=sock->next) {
+               for (sock = ntree->inputs.first; sock; sock = sock->next) {
                        BLI_strncpy(sock->identifier, sock->name, sizeof(sock->identifier));
                        BLI_uniquename(&ntree->inputs, sock, sock->identifier, '.', offsetof(bNodeSocket, identifier), sizeof(sock->identifier));
                }
-               for (sock=ntree->outputs.first; sock; sock=sock->next) {
+               for (sock = ntree->outputs.first; sock; sock = sock->next) {
                        BLI_strncpy(sock->identifier, sock->name, sizeof(sock->identifier));
                        BLI_uniquename(&ntree->outputs, sock, sock->identifier, '.', offsetof(bNodeSocket, identifier), sizeof(sock->identifier));
                }
@@ -8838,7 +9047,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                        SEQ_END
 
                        if (scene->r.bake_samples == 0)
-                       scene->r.bake_samples = 256;
+                               scene->r.bake_samples = 256;
 
                        if (scene->world) {
                                World *world = blo_do_versions_newlibadr(fd, scene->id.lib, scene->world);
@@ -8980,7 +9189,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                        for (sa= sc->areabase.first; sa; sa= sa->next) {
                                SpaceLink *sl;
                                for (sl= sa->spacedata.first; sl; sl= sl->next) {
-                                       if(sl->spacetype==SPACE_NODE) {
+                                       if (sl->spacetype==SPACE_NODE) {
                                                SpaceNode *snode = (SpaceNode *)sl;
                                                
                                                /* reset pointers to force tree path update from context */
@@ -9038,6 +9247,105 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                
        }
        
+#ifdef WITH_FREESTYLE
+       /* 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.0f;
+                       }
+                       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 1
+                       /* disable the Misc panel for now */
+                       if (linestyle->panel == LS_PANEL_MISC) {
+                               linestyle->panel = LS_PANEL_STROKES;
+                       }
+#endif
+                       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;
+               }
+       }
+       /* The code segment below will be removed when the trunk merger is done.
+          For now it is kept for backward compatibility, giving branch users time
+          to migrate to the new CustomData-based edge/face marks. */
+       {
+               Mesh *me;
+               MEdge *medge;
+               MPoly *mpoly;
+               int i, found;
+
+               for (me = main->mesh.first; me; me = me->id.next) {
+                       /* Freestyle edge marks */
+                       found = 0;
+                       medge = me->medge;
+                       for (i = 0; i < me->totedge; i++) {
+                               if (medge->flag & ME_FREESTYLE_EDGE) {
+                                       found = 1;
+                                       break;
+                               }
+                               medge++;
+                       }
+                       if (found) {
+                               FreestyleEdge *fed = CustomData_add_layer(&me->edata, CD_FREESTYLE_EDGE, CD_CALLOC, NULL, me->totedge);
+                               medge = me->medge;
+                               for (i = 0; i < me->totedge; i++) {
+                                       if (medge->flag & ME_FREESTYLE_EDGE) {
+                                               medge->flag &= ~ME_FREESTYLE_EDGE;
+                                               fed->flag |= FREESTYLE_EDGE_MARK;
+                                       }
+                                       medge++;
+                                       fed++;
+                               }
+                               printf("Migrated to CustomData-based Freestyle edge marks\n");
+                       }
+                       /* Freestyle face marks */
+                       found = 0;
+                       mpoly = me->mpoly;
+                       for (i = 0; i < me->totpoly; i++) {
+                               if (mpoly->flag & ME_FREESTYLE_FACE) {
+                                       found = 1;
+                                       break;
+                               }
+                               mpoly++;
+                       }
+                       if (found) {
+                               FreestyleFace *ffa = CustomData_add_layer(&me->pdata, CD_FREESTYLE_FACE, CD_CALLOC, NULL, me->totpoly);
+                               mpoly = me->mpoly;
+                               for (i = 0; i < me->totpoly; i++) {
+                                       if (mpoly->flag & ME_FREESTYLE_FACE) {
+                                               mpoly->flag &= ~ME_FREESTYLE_FACE;
+                                               ffa->flag |= FREESTYLE_FACE_MARK;
+                                       }
+                                       mpoly++;
+                                       ffa++;
+                               }
+                               printf("Migrated to CustomData-based Freestyle face marks\n");
+                       }
+               }
+       }
+#endif
+
        /* WATCH IT!!!: pointers from libdata have not been converted yet here! */
        /* WATCH IT 2!: Userdef struct init see do_versions_userdef() above! */
 
@@ -9059,8 +9367,8 @@ static void lib_link_all(FileData *fd, Main *main)
        
        /* No load UI for undo memfiles */
        if (fd->memfile == NULL) {
-               lib_link_windowmanager(fd, main);
-               lib_link_screen(fd, main);
+       lib_link_windowmanager(fd, main);
+       lib_link_screen(fd, main);
        }
        lib_link_scene(fd, main);
        lib_link_object(fd, main);
@@ -9087,6 +9395,9 @@ static void lib_link_all(FileData *fd, Main *main)
        lib_link_particlesettings(fd, main);
        lib_link_movieclip(fd, main);
        lib_link_mask(fd, main);
+#ifdef WITH_FREESTYLE
+       lib_link_linestyle(fd, main);
+#endif
 
        lib_link_mesh(fd, main);                /* as last: tpage images with users at zero */
        
@@ -9150,7 +9461,7 @@ static BHead *read_userdef(BlendFileData *bfd, FileData *fd, BHead *bhead)
                for (kmi=keymap->items.first; kmi; kmi=kmi->next)
                        direct_link_keymapitem(fd, kmi);
        }
-
+       
        for (addon = user->addons.first; addon; addon = addon->next) {
                addon->prop = newdataadr(fd, addon->prop);
                if (addon->prop) {
@@ -9372,7 +9683,7 @@ static void expand_doit_library(void *fdhandle, Main *mainvar, void *old)
                                        return;
                                }
                                else
-                                       id = is_yet_read(fd, ptr, bhead);
+                               id = is_yet_read(fd, ptr, bhead);
                                
                                if (id == NULL) {
                                        read_libblock(fd, ptr, bhead, LIB_READ+LIB_INDIRECT, NULL);
@@ -10049,6 +10360,18 @@ 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);
+
+#ifdef WITH_FREESTYLE
+               {
+                       FreestyleLineSet *lineset;
+
+                       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);
+                       }
+               }
+#endif
        }
        
        if (sce->r.dometext)
@@ -10147,6 +10470,28 @@ static void expand_mask(FileData *fd, Main *mainvar, Mask *mask)
        }
 }
 
+#ifdef WITH_FREESTYLE
+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);
+       }
+}
+#endif
+
 void BLO_main_expander(void (*expand_doit_func)(void *, Main *, void *))
 {
        expand_doit = expand_doit_func;
@@ -10237,6 +10582,11 @@ void BLO_expand_main(void *fdhandle, Main *mainvar)
                                        case ID_MSK:
                                                expand_mask(fd, mainvar, (Mask *)id);
                                                break;
+#ifdef WITH_FREESTYLE
+                                       case ID_LS:
+                                               expand_linestyle(fd, mainvar, (FreestyleLineStyle *)id);
+                                               break;
+#endif
                                        }
                                        
                                        do_it = TRUE;
@@ -10331,7 +10681,7 @@ static void give_base_to_groups(Main *mainvar, Scene *scene)
                        Base *base;
                        
                        /* BKE_object_add(...) messes with the selection */
-                       Object *ob = BKE_object_add_only_object(mainvar, OB_EMPTY, group->id.name+2);
+                       Object *ob = BKE_object_add_only_object(mainvar, OB_EMPTY, group->id.name + 2);
                        ob->type = OB_EMPTY;
                        ob->lay = scene->lay;
                        
@@ -10345,7 +10695,7 @@ static void give_base_to_groups(Main *mainvar, Scene *scene)
                        /* assign the group */
                        ob->dup_group = group;
                        ob->transflag |= OB_DUPLIGROUP;
-                       rename_id(&ob->id, group->id.name+2);
+                       rename_id(&ob->id, group->id.name + 2);
                        copy_v3_v3(ob->loc, scene->cursor);
                }
        }
@@ -10733,7 +11083,7 @@ static void read_libraries(FileData *basefd, ListBase *mainlist)
                                                                        BKE_reportf_wrap(fd->reports, RPT_WARNING,
                                                                                         TIP_("LIB ERROR: %s: '%s' missing from '%s'"),
                                                                                         BKE_idcode_to_name(GS(id->name)),
-                                                                                        id->name+2, mainptr->curlib->filepath);
+                                                                                        id->name + 2, mainptr->curlib->filepath);
                                                                }
                                                                
                                                                change_idid_adr(mainlist, basefd, id, realid);