Merged changes in the trunk up to revision 54802.
[blender.git] / source / blender / blenloader / intern / readfile.c
index 426d15c82e36b1fae7921738f86bf68b7c074945..44a73fa14a07e07e440ccf2f0a7f973644aa7bfe 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"
@@ -1072,10 +1075,10 @@ FileData *blo_openblendermemory(void *mem, int memsize, ReportList *reports)
                        }
                }
                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);
        }
 }
@@ -1205,19 +1208,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);
@@ -1228,7 +1231,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);
 }
 
@@ -3334,7 +3337,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);
@@ -5046,6 +5049,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);
@@ -5152,7 +5165,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;
@@ -5292,6 +5305,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) {
@@ -5904,7 +5930,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) {
@@ -6216,7 +6242,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;
@@ -6585,6 +6611,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 ******************** */
 
 
@@ -6619,6 +6820,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";
        
@@ -6795,6 +6999,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);
@@ -6927,7 +7136,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. */
@@ -6939,7 +7148,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...
@@ -8665,7 +8874,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);
@@ -8820,6 +9029,47 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
 
        // if (main->versionfile < 265 || (main->versionfile == 265 && main->subversionfile < 7)) {
 
+#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;
+               }
+       }
+#endif
+
        /* 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! */
 
@@ -8841,8 +9091,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);
@@ -8869,6 +9119,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 */
        
@@ -8932,7 +9185,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) {
@@ -9152,7 +9405,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);
@@ -9829,6 +10082,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)
@@ -9927,6 +10192,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;
@@ -10017,6 +10304,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;
@@ -10437,9 +10729,9 @@ static void read_libraries(FileData *basefd, ListBase *mainlist)
                                                BLI_strncpy(fd->relabase, mainptr->curlib->filepath, sizeof(fd->relabase));
                                        }
                                        else {
-                                               BKE_reportf_wrap(basefd->reports, RPT_INFO, TIP_("Read library:  '%s', '%s'"),
-                                                                                mainptr->curlib->filepath, mainptr->curlib->name);
-                                               fd = blo_openblenderfile(mainptr->curlib->filepath, basefd->reports);
+                                       BKE_reportf_wrap(basefd->reports, RPT_INFO, TIP_("Read library:  '%s', '%s'"),
+                                                        mainptr->curlib->filepath, mainptr->curlib->name);
+                                       fd = blo_openblenderfile(mainptr->curlib->filepath, basefd->reports);
                                        }
                                        /* allow typing in a new lib path */
                                        if (G.debug_value == -666) {