Merged changes in the trunk up to revision 46557.
authorTamito Kajiyama <rd6t-kjym@asahi-net.or.jp>
Fri, 11 May 2012 22:48:41 +0000 (22:48 +0000)
committerTamito Kajiyama <rd6t-kjym@asahi-net.or.jp>
Fri, 11 May 2012 22:48:41 +0000 (22:48 +0000)
Conflicts resolved:
source/blender/blenkernel/intern/material.c
source/blender/blenkernel/intern/subsurf_ccg.c
source/blender/blenloader/intern/readfile.c
source/blender/editors/animation/anim_channels_defines.c
source/blender/makesrna/intern/rna_scene.c

Additional changes:
* Fix for recent changes of BKE_* function renaming.
* Fix for an "attempt to free NULL pointer" in BlenderStrokeRenderer::RenderStrokeRepBasic().

46 files changed:
1  2 
release/scripts/startup/bl_ui/space_view3d.py
source/blender/blenkernel/intern/anim_sys.c
source/blender/blenkernel/intern/group.c
source/blender/blenkernel/intern/library.c
source/blender/blenkernel/intern/linestyle.c
source/blender/blenkernel/intern/material.c
source/blender/blenkernel/intern/object.c
source/blender/blenkernel/intern/scene.c
source/blender/blenkernel/intern/subsurf_ccg.c
source/blender/blenloader/intern/readfile.c
source/blender/blenloader/intern/writefile.c
source/blender/bmesh/intern/bmesh_construct.c
source/blender/editors/animation/anim_channels_defines.c
source/blender/editors/animation/anim_channels_edit.c
source/blender/editors/animation/anim_filter.c
source/blender/editors/interface/interface_templates.c
source/blender/editors/mesh/editmesh_select.c
source/blender/editors/mesh/editmesh_tools.c
source/blender/editors/mesh/mesh_intern.h
source/blender/editors/mesh/mesh_ops.c
source/blender/editors/render/render_shading.c
source/blender/editors/space_buttons/space_buttons.c
source/blender/editors/space_nla/nla_buttons.c
source/blender/editors/space_nla/nla_channels.c
source/blender/editors/space_view3d/drawobject.c
source/blender/freestyle/intern/blender_interface/BlenderStrokeRenderer.cpp
source/blender/freestyle/intern/blender_interface/BlenderStyleModule.h
source/blender/freestyle/intern/blender_interface/FRS_freestyle.cpp
source/blender/freestyle/intern/system/PythonInterpreter.h
source/blender/makesdna/DNA_action_types.h
source/blender/makesdna/DNA_meshdata_types.h
source/blender/makesdna/DNA_scene_types.h
source/blender/makesrna/RNA_access.h
source/blender/makesrna/intern/rna_ID.c
source/blender/makesrna/intern/rna_internal.h
source/blender/makesrna/intern/rna_main_api.c
source/blender/makesrna/intern/rna_mesh.c
source/blender/makesrna/intern/rna_scene.c
source/blender/python/SConscript
source/blender/render/intern/source/convertblender.c
source/blender/render/intern/source/pipeline.c
source/blender/render/intern/source/render_result.c
source/blender/windowmanager/intern/wm_files.c
source/blenderplayer/bad_level_call_stubs/stubs.c
source/creator/CMakeLists.txt
source/creator/creator.c

@@@ -79,36 -79,30 +79,36 @@@ void BKE_group_unlink(Group *group
        Object *ob;
        Scene *sce;
        SceneRenderLayer *srl;
 +      FreestyleLineSet *lineset;
        ParticleSystem *psys;
        
-       for (ma= bmain->mat.first; ma; ma= ma->id.next) {
-               if (ma->group==group)
-                       ma->group= NULL;
+       for (ma = bmain->mat.first; ma; ma = ma->id.next) {
+               if (ma->group == group)
+                       ma->group = NULL;
        }
-       for (ma= bmain->mat.first; ma; ma= ma->id.next) {
-               if (ma->group==group)
-                       ma->group= NULL;
+       for (ma = bmain->mat.first; ma; ma = ma->id.next) {
+               if (ma->group == group)
+                       ma->group = NULL;
        }
-       for (sce= bmain->scene.first; sce; sce= sce->id.next) {
-               Base *base= sce->base.first;
+       for (sce = bmain->scene.first; sce; sce = sce->id.next) {
+               Base *base = sce->base.first;
                
                /* ensure objects are not in this group */
-               for (; base; base= base->next) {
-                       if (rem_from_group(group, base->object, sce, base) && find_group(base->object, NULL)==NULL) {
+               for (; base; base = base->next) {
+                       if (rem_from_group(group, base->object, sce, base) && find_group(base->object, NULL) == NULL) {
                                base->object->flag &= ~OB_FROMGROUP;
                                base->flag &= ~OB_FROMGROUP;
                        }
                }                       
                
-               for (srl= sce->r.layers.first; srl; srl= srl->next) {
-                       if (srl->light_override==group)
-                               srl->light_override= NULL;
+               for (srl = sce->r.layers.first; srl; srl = srl->next) {
+                       if (srl->light_override == group)
+                               srl->light_override = NULL;
 +
 +                      for(lineset= srl->freestyleConfig.linesets.first; lineset; lineset= lineset->next) {
 +                              if (lineset->group == group)
 +                                      lineset->group= NULL;
 +                      }
                }
        }
        
@@@ -552,34 -544,33 +552,34 @@@ int set_listbasepointers(Main *main, Li
         * the specific data should be inserted here 
         */
  
-       lb[a++]= &(main->armature);
-       lb[a++]= &(main->mesh);
-       lb[a++]= &(main->curve);
-       lb[a++]= &(main->mball);
-       lb[a++]= &(main->latt);
-       lb[a++]= &(main->lamp);
-       lb[a++]= &(main->camera);
-       lb[a++]= &(main->text);
-       lb[a++]= &(main->sound);
-       lb[a++]= &(main->group);
-       lb[a++]= &(main->brush);
-       lb[a++]= &(main->script);
-       lb[a++]= &(main->particle);
-       lb[a++]= &(main->speaker);
-       lb[a++]= &(main->world);
-       lb[a++]= &(main->screen);
-       lb[a++]= &(main->object);
-       lb[a++]= &(main->scene);
-       lb[a++]= &(main->library);
-       lb[a++]= &(main->wm);
-       lb[a++]= &(main->movieclip);
+       lb[a++] = &(main->armature);
+       lb[a++] = &(main->mesh);
+       lb[a++] = &(main->curve);
+       lb[a++] = &(main->mball);
+       lb[a++] = &(main->latt);
+       lb[a++] = &(main->lamp);
+       lb[a++] = &(main->camera);
+       lb[a++] = &(main->text);
+       lb[a++] = &(main->sound);
+       lb[a++] = &(main->group);
+       lb[a++] = &(main->brush);
+       lb[a++] = &(main->script);
+       lb[a++] = &(main->particle);
+       lb[a++] = &(main->speaker);
+       lb[a++] = &(main->world);
+       lb[a++] = &(main->screen);
+       lb[a++] = &(main->object);
+       lb[a++] = &(main->scene);
+       lb[a++] = &(main->library);
+       lb[a++] = &(main->wm);
+       lb[a++] = &(main->movieclip);
 +      lb[a++]= &(main->linestyle);
        
-       lb[a]= NULL;
+       lb[a] = NULL;
  
        return a;
  }
index b2c2cb2,0000000..0ed1b02
mode 100644,000000..100644
--- /dev/null
@@@ -1,1005 -1,0 +1,1005 @@@
-       linestyle = (FreestyleLineStyle *)alloc_libblock(&main->linestyle, ID_LS, name);
 +/* linestyle.c
 + *
 + * $Id$
 + *
 + * ***** BEGIN GPL LICENSE BLOCK *****
 + *
 + * This program is free software; you can redistribute it and/or
 + * modify it under the terms of the GNU General Public License
 + * as published by the Free Software Foundation; either version 2
 + * of the License, or (at your option) any later version.
 + *
 + * This program is distributed in the hope that it will be useful,
 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 + * GNU General Public License for more details.
 + *
 + * You should have received a copy of the GNU General Public License
 + * along with this program; if not, write to the Free Software Foundation,
 + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 + *
 + * The Original Code is Copyright (C) 2010 Blender Foundation.
 + * All rights reserved.
 + *
 + * The Original Code is: all of this file.
 + *
 + * Contributor(s): none yet.
 + *
 + * ***** END GPL LICENSE BLOCK *****
 + */
 +
 +#include <stdio.h>
 +#include <stdlib.h>
 +#include <string.h>
 +
 +#include "MEM_guardedalloc.h"
 +
 +#include "DNA_object_types.h"
 +#include "DNA_material_types.h" /* for ramp blend */
 +#include "DNA_texture_types.h"
 +
 +#include "BKE_global.h"
 +#include "BKE_library.h"
 +#include "BKE_linestyle.h"
 +#include "BKE_main.h"
 +#include "BKE_texture.h"
 +#include "BKE_colortools.h"
 +#include "BKE_animsys.h"
 +
 +#include "BLI_blenlib.h"
 +
 +static char *modifier_name[LS_MODIFIER_NUM] = {
 +      NULL,
 +      "Along Stroke",
 +      "Distance from Camera",
 +      "Distance from Object",
 +      "Material",
 +      "Sampling",
 +      "Bezier Curve",
 +      "Sinus Displacement",
 +      "Spatial Noise",
 +      "Perlin Noise 1D",
 +      "Perlin Noise 2D",
 +      "Backbone Stretcher",
 +      "Tip Remover",
 +      "Calligraphy",
 +      "Polygonalization",
 +      "Guiding Lines",
 +      "Blueprint",
 +      "2D Offset",
 +      "2D Transform"};
 +
 +static void default_linestyle_settings(FreestyleLineStyle *linestyle)
 +{
 +      linestyle->panel = LS_PANEL_STROKES;
 +      linestyle->r = linestyle->g = linestyle->b = 0.0;
 +      linestyle->alpha = 1.0;
 +      linestyle->thickness = 1.0;
 +      linestyle->thickness_position = LS_THICKNESS_CENTER;
 +      linestyle->thickness_ratio = 0.5f;
 +      linestyle->chaining = LS_CHAINING_PLAIN;
 +      linestyle->rounds = 3;
 +      linestyle->min_angle = 0.0f;
 +      linestyle->max_angle = 0.0f;
 +      linestyle->min_length = 0.0f;
 +      linestyle->max_length = 10000.0f;
 +      linestyle->split_length = 100;
 +
 +      linestyle->color_modifiers.first = linestyle->color_modifiers.last = NULL;
 +      linestyle->alpha_modifiers.first = linestyle->alpha_modifiers.last = NULL;
 +      linestyle->thickness_modifiers.first = linestyle->thickness_modifiers.last = NULL;
 +      linestyle->geometry_modifiers.first = linestyle->geometry_modifiers.last = NULL;
 +
 +      FRS_add_linestyle_geometry_modifier(linestyle, LS_MODIFIER_SAMPLING);
 +
 +      linestyle->caps = LS_CAPS_BUTT;
 +}
 +
 +FreestyleLineStyle *FRS_new_linestyle(char *name, struct Main *main)
 +{
 +      FreestyleLineStyle *linestyle;
 +
 +      if (!main)
 +              main = G.main;
 +
++      linestyle = (FreestyleLineStyle *)BKE_libblock_alloc(&main->linestyle, ID_LS, name);
 +      
 +      default_linestyle_settings(linestyle);
 +
 +      return linestyle;
 +}
 +
 +void FRS_free_linestyle(FreestyleLineStyle *linestyle)
 +{
 +      LineStyleModifier *m;
 +
 +      BKE_free_animdata(&linestyle->id);
 +      while ((m = (LineStyleModifier *)linestyle->color_modifiers.first))
 +              FRS_remove_linestyle_color_modifier(linestyle, m);
 +      while ((m = (LineStyleModifier *)linestyle->alpha_modifiers.first))
 +              FRS_remove_linestyle_alpha_modifier(linestyle, m);
 +      while ((m = (LineStyleModifier *)linestyle->thickness_modifiers.first))
 +              FRS_remove_linestyle_thickness_modifier(linestyle, m);
 +      while ((m = (LineStyleModifier *)linestyle->geometry_modifiers.first))
 +              FRS_remove_linestyle_geometry_modifier(linestyle, m);
 +}
 +
 +FreestyleLineStyle *FRS_copy_linestyle(FreestyleLineStyle *linestyle)
 +{
 +      FreestyleLineStyle *new_linestyle;
 +      LineStyleModifier *m;
 +
 +      new_linestyle = FRS_new_linestyle(linestyle->id.name+2, NULL);
 +      FRS_free_linestyle(new_linestyle);
 +
 +      new_linestyle->r = linestyle->r;
 +      new_linestyle->g = linestyle->g;
 +      new_linestyle->b = linestyle->b;
 +      new_linestyle->alpha = linestyle->alpha;
 +      new_linestyle->thickness = linestyle->thickness;
 +      new_linestyle->thickness_position = linestyle->thickness_position;
 +      new_linestyle->thickness_ratio = linestyle->thickness_ratio;
 +      new_linestyle->flag = linestyle->flag;
 +      new_linestyle->caps = linestyle->caps;
 +      new_linestyle->chaining = linestyle->chaining;
 +      new_linestyle->rounds = linestyle->rounds;
 +      new_linestyle->min_angle = linestyle->min_angle;
 +      new_linestyle->max_angle = linestyle->max_angle;
 +      new_linestyle->min_length = linestyle->min_length;
 +      new_linestyle->max_length = linestyle->max_length;
 +      new_linestyle->split_length = linestyle->split_length;
 +      new_linestyle->dash1 = linestyle->dash1;
 +      new_linestyle->gap1 = linestyle->gap1;
 +      new_linestyle->dash2 = linestyle->dash2;
 +      new_linestyle->gap2 = linestyle->gap2;
 +      new_linestyle->dash3 = linestyle->dash3;
 +      new_linestyle->gap3 = linestyle->gap3;
 +      new_linestyle->panel = linestyle->panel;
 +      for (m = (LineStyleModifier *)linestyle->color_modifiers.first; m; m = m->next)
 +              FRS_copy_linestyle_color_modifier(new_linestyle, m);
 +      for (m = (LineStyleModifier *)linestyle->alpha_modifiers.first; m; m = m->next)
 +              FRS_copy_linestyle_alpha_modifier(new_linestyle, m);
 +      for (m = (LineStyleModifier *)linestyle->thickness_modifiers.first; m; m = m->next)
 +              FRS_copy_linestyle_thickness_modifier(new_linestyle, m);
 +      for (m = (LineStyleModifier *)linestyle->geometry_modifiers.first; m; m = m->next)
 +              FRS_copy_linestyle_geometry_modifier(new_linestyle, m);
 +      return new_linestyle;
 +}
 +
 +static LineStyleModifier *new_modifier(int type, size_t size)
 +{
 +      LineStyleModifier *m;
 +
 +      m = (LineStyleModifier *)MEM_callocN(size, "line style modifier");
 +      if (m) {
 +              m->type = type;
 +              strcpy(m->name, modifier_name[type]);
 +              m->influence = 1.0f;
 +              m->flags = LS_MODIFIER_ENABLED | LS_MODIFIER_EXPANDED;
 +      }
 +      return m;
 +}
 +
 +static void add_to_modifier_list(ListBase *lb, LineStyleModifier *m)
 +{
 +      BLI_addtail(lb, (void *)m);
 +      BLI_uniquename(lb, m, modifier_name[m->type], '.', offsetof(LineStyleModifier, name), sizeof(m->name));
 +}
 +
 +static LineStyleModifier *alloc_color_modifier(int type)
 +{
 +      size_t size;
 +
 +      switch (type) {
 +      case LS_MODIFIER_ALONG_STROKE:
 +              size = sizeof(LineStyleColorModifier_AlongStroke);
 +              break;
 +      case LS_MODIFIER_DISTANCE_FROM_CAMERA:
 +              size = sizeof(LineStyleColorModifier_DistanceFromCamera);
 +              break;
 +      case LS_MODIFIER_DISTANCE_FROM_OBJECT:
 +              size = sizeof(LineStyleColorModifier_DistanceFromObject);
 +              break;
 +      case LS_MODIFIER_MATERIAL:
 +              size = sizeof(LineStyleColorModifier_Material);
 +              break;
 +      default:
 +              return NULL; /* unknown modifier type */
 +      }
 +      return new_modifier(type, size);
 +}
 +
 +LineStyleModifier *FRS_add_linestyle_color_modifier(FreestyleLineStyle *linestyle, int type)
 +{
 +      LineStyleModifier *m;
 +      
 +      m = alloc_color_modifier(type);
 +      if (!m)
 +              return NULL;
 +      m->blend = MA_RAMP_BLEND;
 +      switch (type) {
 +      case LS_MODIFIER_ALONG_STROKE:
 +              ((LineStyleColorModifier_AlongStroke *)m)->color_ramp = add_colorband(1);
 +              break;
 +      case LS_MODIFIER_DISTANCE_FROM_CAMERA:
 +              ((LineStyleColorModifier_DistanceFromCamera *)m)->color_ramp = add_colorband(1);
 +              ((LineStyleColorModifier_DistanceFromCamera *)m)->range_min = 0.0f;
 +              ((LineStyleColorModifier_DistanceFromCamera *)m)->range_max = 10000.0f;
 +              break;
 +      case LS_MODIFIER_DISTANCE_FROM_OBJECT:
 +              ((LineStyleColorModifier_DistanceFromObject *)m)->target = NULL;
 +              ((LineStyleColorModifier_DistanceFromObject *)m)->color_ramp = add_colorband(1);
 +              ((LineStyleColorModifier_DistanceFromObject *)m)->range_min = 0.0f;
 +              ((LineStyleColorModifier_DistanceFromObject *)m)->range_max = 10000.0f;
 +              break;
 +      case LS_MODIFIER_MATERIAL:
 +              ((LineStyleColorModifier_Material *)m)->color_ramp = add_colorband(1);
 +              ((LineStyleColorModifier_Material *)m)->mat_attr = LS_MODIFIER_MATERIAL_DIFF;
 +              break;
 +      default:
 +              return NULL; /* unknown modifier type */
 +      }
 +      add_to_modifier_list(&linestyle->color_modifiers, m);
 +
 +      return m;
 +}
 +
 +LineStyleModifier *FRS_copy_linestyle_color_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *m)
 +{
 +      LineStyleModifier *new_m;
 +      
 +      new_m = alloc_color_modifier(m->type);
 +      if (!new_m)
 +              return NULL;
 +      new_m->influence = m->influence;
 +      new_m->flags = m->flags;
 +      new_m->blend = m->blend;
 +      switch (m->type) {
 +      case LS_MODIFIER_ALONG_STROKE:
 +              {
 +                      LineStyleColorModifier_AlongStroke *p = (LineStyleColorModifier_AlongStroke *)m;
 +                      ((LineStyleColorModifier_AlongStroke *)new_m)->color_ramp = MEM_dupallocN(p->color_ramp);
 +              }
 +              break;
 +      case LS_MODIFIER_DISTANCE_FROM_CAMERA:
 +              {
 +                      LineStyleColorModifier_DistanceFromCamera *p = (LineStyleColorModifier_DistanceFromCamera *)m;
 +                      ((LineStyleColorModifier_DistanceFromCamera *)new_m)->color_ramp = MEM_dupallocN(p->color_ramp);
 +                      ((LineStyleColorModifier_DistanceFromCamera *)new_m)->range_min = p->range_min;
 +                      ((LineStyleColorModifier_DistanceFromCamera *)new_m)->range_max = p->range_max;
 +              }
 +              break;
 +      case LS_MODIFIER_DISTANCE_FROM_OBJECT:
 +              {
 +                      LineStyleColorModifier_DistanceFromObject *p = (LineStyleColorModifier_DistanceFromObject *)m;
 +                      if (p->target)
 +                              p->target->id.us++;
 +                      ((LineStyleColorModifier_DistanceFromObject *)new_m)->target = p->target;
 +                      ((LineStyleColorModifier_DistanceFromObject *)new_m)->color_ramp = MEM_dupallocN(p->color_ramp);
 +                      ((LineStyleColorModifier_DistanceFromObject *)new_m)->range_min = p->range_min;
 +                      ((LineStyleColorModifier_DistanceFromObject *)new_m)->range_max = p->range_max;
 +              }
 +              break;
 +      case LS_MODIFIER_MATERIAL:
 +              {
 +                      LineStyleColorModifier_Material *p = (LineStyleColorModifier_Material *)m;
 +                      ((LineStyleColorModifier_Material *)new_m)->color_ramp = MEM_dupallocN(p->color_ramp);
 +                      ((LineStyleColorModifier_Material *)new_m)->mat_attr = p->mat_attr;
 +              }
 +              break;
 +      default:
 +              return NULL; /* unknown modifier type */
 +      }
 +      add_to_modifier_list(&linestyle->color_modifiers, new_m);
 +
 +      return new_m;
 +}
 +
 +void FRS_remove_linestyle_color_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *m)
 +{
 +      switch (m->type) {
 +      case LS_MODIFIER_ALONG_STROKE:
 +              MEM_freeN(((LineStyleColorModifier_AlongStroke *)m)->color_ramp);
 +              break;
 +      case LS_MODIFIER_DISTANCE_FROM_CAMERA:
 +              MEM_freeN(((LineStyleColorModifier_DistanceFromCamera *)m)->color_ramp);
 +              break;
 +      case LS_MODIFIER_DISTANCE_FROM_OBJECT:
 +              MEM_freeN(((LineStyleColorModifier_DistanceFromObject *)m)->color_ramp);
 +              break;
 +      case LS_MODIFIER_MATERIAL:
 +              MEM_freeN(((LineStyleColorModifier_Material *)m)->color_ramp);
 +              break;
 +      }
 +      BLI_freelinkN(&linestyle->color_modifiers, m);
 +}
 +
 +static LineStyleModifier *alloc_alpha_modifier(int type)
 +{
 +      size_t size;
 +
 +      switch (type) {
 +      case LS_MODIFIER_ALONG_STROKE:
 +              size = sizeof(LineStyleAlphaModifier_AlongStroke);
 +              break;
 +      case LS_MODIFIER_DISTANCE_FROM_CAMERA:
 +              size = sizeof(LineStyleAlphaModifier_DistanceFromCamera);
 +              break;
 +      case LS_MODIFIER_DISTANCE_FROM_OBJECT:
 +              size = sizeof(LineStyleAlphaModifier_DistanceFromObject);
 +              break;
 +      case LS_MODIFIER_MATERIAL:
 +              size = sizeof(LineStyleAlphaModifier_Material);
 +              break;
 +      default:
 +              return NULL; /* unknown modifier type */
 +      }
 +      return new_modifier(type, size);
 +}
 +
 +LineStyleModifier *FRS_add_linestyle_alpha_modifier(FreestyleLineStyle *linestyle, int type)
 +{
 +      LineStyleModifier *m;
 +      
 +      m = alloc_alpha_modifier(type);
 +      if (!m)
 +              return NULL;
 +      m->blend = LS_VALUE_BLEND;
 +      switch (type) {
 +      case LS_MODIFIER_ALONG_STROKE:
 +              ((LineStyleAlphaModifier_AlongStroke *)m)->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
 +              break;
 +      case LS_MODIFIER_DISTANCE_FROM_CAMERA:
 +              ((LineStyleAlphaModifier_DistanceFromCamera *)m)->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
 +              ((LineStyleAlphaModifier_DistanceFromCamera *)m)->range_min = 0.0f;
 +              ((LineStyleAlphaModifier_DistanceFromCamera *)m)->range_max = 10000.0f;
 +              break;
 +      case LS_MODIFIER_DISTANCE_FROM_OBJECT:
 +              ((LineStyleAlphaModifier_DistanceFromObject *)m)->target = NULL;
 +              ((LineStyleAlphaModifier_DistanceFromObject *)m)->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
 +              ((LineStyleAlphaModifier_DistanceFromObject *)m)->range_min = 0.0f;
 +              ((LineStyleAlphaModifier_DistanceFromObject *)m)->range_max = 10000.0f;
 +              break;
 +      case LS_MODIFIER_MATERIAL:
 +              ((LineStyleAlphaModifier_Material *)m)->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
 +              ((LineStyleAlphaModifier_Material *)m)->mat_attr = LS_MODIFIER_MATERIAL_DIFF;
 +              break;
 +      default:
 +              return NULL; /* unknown modifier type */
 +      }
 +      add_to_modifier_list(&linestyle->alpha_modifiers, m);
 +
 +      return m;
 +}
 +
 +LineStyleModifier *FRS_copy_linestyle_alpha_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *m)
 +{
 +      LineStyleModifier *new_m;
 +      
 +      new_m = alloc_alpha_modifier(m->type);
 +      if (!new_m)
 +              return NULL;
 +      new_m->influence = m->influence;
 +      new_m->flags = m->flags;
 +      new_m->blend = m->blend;
 +      switch (m->type) {
 +      case LS_MODIFIER_ALONG_STROKE:
 +              {
 +                      LineStyleAlphaModifier_AlongStroke *p = (LineStyleAlphaModifier_AlongStroke *)m;
 +                      ((LineStyleAlphaModifier_AlongStroke *)new_m)->curve = curvemapping_copy(p->curve);
 +              }
 +              break;
 +      case LS_MODIFIER_DISTANCE_FROM_CAMERA:
 +              {
 +                      LineStyleAlphaModifier_DistanceFromCamera *p = (LineStyleAlphaModifier_DistanceFromCamera *)m;
 +                      ((LineStyleAlphaModifier_DistanceFromCamera *)new_m)->curve = curvemapping_copy(p->curve);
 +                      ((LineStyleAlphaModifier_DistanceFromCamera *)new_m)->range_min = p->range_min;
 +                      ((LineStyleAlphaModifier_DistanceFromCamera *)new_m)->range_max = p->range_max;
 +              }
 +              break;
 +      case LS_MODIFIER_DISTANCE_FROM_OBJECT:
 +              {
 +                      LineStyleAlphaModifier_DistanceFromObject *p = (LineStyleAlphaModifier_DistanceFromObject *)m;
 +                      if (p->target)
 +                              p->target->id.us++;
 +                      ((LineStyleAlphaModifier_DistanceFromObject *)new_m)->target = p->target;
 +                      ((LineStyleAlphaModifier_DistanceFromObject *)new_m)->curve = curvemapping_copy(p->curve);
 +                      ((LineStyleAlphaModifier_DistanceFromObject *)new_m)->range_min = p->range_min;
 +                      ((LineStyleAlphaModifier_DistanceFromObject *)new_m)->range_max = p->range_max;
 +              }
 +              break;
 +      case LS_MODIFIER_MATERIAL:
 +              {
 +                      LineStyleAlphaModifier_Material *p = (LineStyleAlphaModifier_Material *)m;
 +                      ((LineStyleAlphaModifier_Material *)new_m)->curve = curvemapping_copy(p->curve);
 +                      ((LineStyleAlphaModifier_Material *)new_m)->mat_attr = p->mat_attr;
 +              }
 +              break;
 +      default:
 +              return NULL; /* unknown modifier type */
 +      }
 +      add_to_modifier_list(&linestyle->alpha_modifiers, new_m);
 +
 +      return new_m;
 +}
 +
 +void FRS_remove_linestyle_alpha_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *m)
 +{
 +      switch (m->type) {
 +      case LS_MODIFIER_ALONG_STROKE:
 +              curvemapping_free(((LineStyleAlphaModifier_AlongStroke *)m)->curve);
 +              break;
 +      case LS_MODIFIER_DISTANCE_FROM_CAMERA:
 +              curvemapping_free(((LineStyleAlphaModifier_DistanceFromCamera *)m)->curve);
 +              break;
 +      case LS_MODIFIER_DISTANCE_FROM_OBJECT:
 +              curvemapping_free(((LineStyleAlphaModifier_DistanceFromObject *)m)->curve);
 +              break;
 +      case LS_MODIFIER_MATERIAL:
 +              curvemapping_free(((LineStyleAlphaModifier_Material *)m)->curve);
 +              break;
 +      }
 +      BLI_freelinkN(&linestyle->alpha_modifiers, m);
 +}
 +
 +static LineStyleModifier *alloc_thickness_modifier(int type)
 +{
 +      size_t size;
 +
 +      switch (type) {
 +      case LS_MODIFIER_ALONG_STROKE:
 +              size = sizeof(LineStyleThicknessModifier_AlongStroke);
 +              break;
 +      case LS_MODIFIER_DISTANCE_FROM_CAMERA:
 +              size = sizeof(LineStyleThicknessModifier_DistanceFromCamera);
 +              break;
 +      case LS_MODIFIER_DISTANCE_FROM_OBJECT:
 +              size = sizeof(LineStyleThicknessModifier_DistanceFromObject);
 +              break;
 +      case LS_MODIFIER_MATERIAL:
 +              size = sizeof(LineStyleThicknessModifier_Material);
 +              break;
 +      case LS_MODIFIER_CALLIGRAPHY:
 +              size = sizeof(LineStyleThicknessModifier_Calligraphy);
 +              break;
 +      default:
 +              return NULL; /* unknown modifier type */
 +      }
 +      return new_modifier(type, size);
 +}
 +
 +LineStyleModifier *FRS_add_linestyle_thickness_modifier(FreestyleLineStyle *linestyle, int type)
 +{
 +      LineStyleModifier *m;
 +
 +      m = alloc_thickness_modifier(type);
 +      if (!m)
 +              return NULL;
 +      m->blend = LS_VALUE_BLEND;
 +      switch (type) {
 +      case LS_MODIFIER_ALONG_STROKE:
 +              ((LineStyleThicknessModifier_AlongStroke *)m)->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
 +              ((LineStyleThicknessModifier_AlongStroke *)m)->value_min = 0.0f;
 +              ((LineStyleThicknessModifier_AlongStroke *)m)->value_max = 1.0f;
 +              break;
 +      case LS_MODIFIER_DISTANCE_FROM_CAMERA:
 +              ((LineStyleThicknessModifier_DistanceFromCamera *)m)->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
 +              ((LineStyleThicknessModifier_DistanceFromCamera *)m)->range_min = 0.0f;
 +              ((LineStyleThicknessModifier_DistanceFromCamera *)m)->range_max = 1000.0f;
 +              ((LineStyleThicknessModifier_DistanceFromCamera *)m)->value_min = 0.0f;
 +              ((LineStyleThicknessModifier_DistanceFromCamera *)m)->value_max = 1.0f;
 +              break;
 +      case LS_MODIFIER_DISTANCE_FROM_OBJECT:
 +              ((LineStyleThicknessModifier_DistanceFromObject *)m)->target = NULL;
 +              ((LineStyleThicknessModifier_DistanceFromObject *)m)->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
 +              ((LineStyleThicknessModifier_DistanceFromObject *)m)->range_min = 0.0f;
 +              ((LineStyleThicknessModifier_DistanceFromObject *)m)->range_max = 1000.0f;
 +              ((LineStyleThicknessModifier_DistanceFromObject *)m)->value_min = 0.0f;
 +              ((LineStyleThicknessModifier_DistanceFromObject *)m)->value_max = 1.0f;
 +              break;
 +      case LS_MODIFIER_MATERIAL:
 +              ((LineStyleThicknessModifier_Material *)m)->curve = curvemapping_add(1, 0.0f, 0.0f, 1.0f, 1.0f);
 +              ((LineStyleThicknessModifier_Material *)m)->mat_attr = LS_MODIFIER_MATERIAL_DIFF;
 +              ((LineStyleThicknessModifier_Material *)m)->value_min = 0.0f;
 +              ((LineStyleThicknessModifier_Material *)m)->value_max = 1.0f;
 +              break;
 +      case LS_MODIFIER_CALLIGRAPHY:
 +              ((LineStyleThicknessModifier_Calligraphy *)m)->min_thickness = 1.0f;
 +              ((LineStyleThicknessModifier_Calligraphy *)m)->max_thickness = 10.0f;
 +              ((LineStyleThicknessModifier_Calligraphy *)m)->orientation = 60.0f;
 +              break;
 +      default:
 +              return NULL; /* unknown modifier type */
 +      }
 +      add_to_modifier_list(&linestyle->thickness_modifiers, m);
 +
 +      return m;
 +}
 +
 +LineStyleModifier *FRS_copy_linestyle_thickness_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *m)
 +{
 +      LineStyleModifier *new_m;
 +
 +      new_m = alloc_thickness_modifier(m->type);
 +      if (!new_m)
 +              return NULL;
 +      new_m->influence = m->influence;
 +      new_m->flags = m->flags;
 +      new_m->blend = m->blend;
 +      switch (m->type) {
 +      case LS_MODIFIER_ALONG_STROKE:
 +              {
 +                      LineStyleThicknessModifier_AlongStroke *p = (LineStyleThicknessModifier_AlongStroke *)m;
 +                      ((LineStyleThicknessModifier_AlongStroke *)new_m)->curve = curvemapping_copy(p->curve);
 +                      ((LineStyleThicknessModifier_AlongStroke *)new_m)->value_min = p->value_min;
 +                      ((LineStyleThicknessModifier_AlongStroke *)new_m)->value_max = p->value_max;
 +              }
 +              break;
 +      case LS_MODIFIER_DISTANCE_FROM_CAMERA:
 +              {
 +                      LineStyleThicknessModifier_DistanceFromCamera *p = (LineStyleThicknessModifier_DistanceFromCamera *)m;
 +                      ((LineStyleThicknessModifier_DistanceFromCamera *)new_m)->curve = curvemapping_copy(p->curve);
 +                      ((LineStyleThicknessModifier_DistanceFromCamera *)new_m)->range_min = p->range_min;
 +                      ((LineStyleThicknessModifier_DistanceFromCamera *)new_m)->range_max = p->range_max;
 +                      ((LineStyleThicknessModifier_DistanceFromCamera *)new_m)->value_min = p->value_min;
 +                      ((LineStyleThicknessModifier_DistanceFromCamera *)new_m)->value_max = p->value_max;
 +              }
 +              break;
 +      case LS_MODIFIER_DISTANCE_FROM_OBJECT:
 +              {
 +                      LineStyleThicknessModifier_DistanceFromObject *p = (LineStyleThicknessModifier_DistanceFromObject *)m;
 +                      if (p->target)
 +                              p->target->id.us++;
 +                      ((LineStyleThicknessModifier_DistanceFromObject *)new_m)->target = p->target;
 +                      ((LineStyleThicknessModifier_DistanceFromObject *)new_m)->curve = curvemapping_copy(p->curve);
 +                      ((LineStyleThicknessModifier_DistanceFromObject *)new_m)->range_min = p->range_min;
 +                      ((LineStyleThicknessModifier_DistanceFromObject *)new_m)->range_max = p->range_max;
 +                      ((LineStyleThicknessModifier_DistanceFromObject *)new_m)->value_min = p->value_min;
 +                      ((LineStyleThicknessModifier_DistanceFromObject *)new_m)->value_max = p->value_max;
 +              }
 +              break;
 +      case LS_MODIFIER_MATERIAL:
 +              {
 +                      LineStyleThicknessModifier_Material *p = (LineStyleThicknessModifier_Material *)m;
 +                      ((LineStyleThicknessModifier_Material *)new_m)->curve = curvemapping_copy(p->curve);
 +                      ((LineStyleThicknessModifier_Material *)new_m)->mat_attr = p->mat_attr;
 +                      ((LineStyleThicknessModifier_Material *)new_m)->value_min = p->value_min;
 +                      ((LineStyleThicknessModifier_Material *)new_m)->value_max = p->value_max;
 +              }
 +              break;
 +      case LS_MODIFIER_CALLIGRAPHY:
 +              {
 +                      LineStyleThicknessModifier_Calligraphy *p = (LineStyleThicknessModifier_Calligraphy *)m;
 +                      ((LineStyleThicknessModifier_Calligraphy *)new_m)->min_thickness = p->min_thickness;
 +                      ((LineStyleThicknessModifier_Calligraphy *)new_m)->max_thickness = p->max_thickness;
 +                      ((LineStyleThicknessModifier_Calligraphy *)new_m)->orientation = p->orientation;
 +              }
 +              break;
 +      default:
 +              return NULL; /* unknown modifier type */
 +      }
 +      add_to_modifier_list(&linestyle->thickness_modifiers, new_m);
 +
 +      return new_m;
 +}
 +
 +void FRS_remove_linestyle_thickness_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *m)
 +{
 +      switch (m->type) {
 +      case LS_MODIFIER_ALONG_STROKE:
 +              curvemapping_free(((LineStyleThicknessModifier_AlongStroke *)m)->curve);
 +              break;
 +      case LS_MODIFIER_DISTANCE_FROM_CAMERA:
 +              curvemapping_free(((LineStyleThicknessModifier_DistanceFromCamera *)m)->curve);
 +              break;
 +      case LS_MODIFIER_DISTANCE_FROM_OBJECT:
 +              curvemapping_free(((LineStyleThicknessModifier_DistanceFromObject *)m)->curve);
 +              break;
 +      case LS_MODIFIER_MATERIAL:
 +              curvemapping_free(((LineStyleThicknessModifier_Material *)m)->curve);
 +              break;
 +      case LS_MODIFIER_CALLIGRAPHY:
 +              break;
 +      }
 +      BLI_freelinkN(&linestyle->thickness_modifiers, m);
 +}
 +
 +static LineStyleModifier *alloc_geometry_modifier(int type)
 +{
 +      size_t size;
 +
 +      switch (type) {
 +      case LS_MODIFIER_SAMPLING:
 +              size = sizeof(LineStyleGeometryModifier_Sampling);
 +              break;
 +      case LS_MODIFIER_BEZIER_CURVE:
 +              size = sizeof(LineStyleGeometryModifier_BezierCurve);
 +              break;
 +      case LS_MODIFIER_SINUS_DISPLACEMENT:
 +              size = sizeof(LineStyleGeometryModifier_SinusDisplacement);
 +              break;
 +      case LS_MODIFIER_SPATIAL_NOISE:
 +              size = sizeof(LineStyleGeometryModifier_SpatialNoise);
 +              break;
 +      case LS_MODIFIER_PERLIN_NOISE_1D:
 +              size = sizeof(LineStyleGeometryModifier_PerlinNoise1D);
 +              break;
 +      case LS_MODIFIER_PERLIN_NOISE_2D:
 +              size = sizeof(LineStyleGeometryModifier_PerlinNoise2D);
 +              break;
 +      case LS_MODIFIER_BACKBONE_STRETCHER:
 +              size = sizeof(LineStyleGeometryModifier_BackboneStretcher);
 +              break;
 +      case LS_MODIFIER_TIP_REMOVER:
 +              size = sizeof(LineStyleGeometryModifier_TipRemover);
 +              break;
 +      case LS_MODIFIER_POLYGONIZATION:
 +              size = sizeof(LineStyleGeometryModifier_Polygonalization);
 +              break;
 +      case LS_MODIFIER_GUIDING_LINES:
 +              size = sizeof(LineStyleGeometryModifier_GuidingLines);
 +              break;
 +      case LS_MODIFIER_BLUEPRINT:
 +              size = sizeof(LineStyleGeometryModifier_Blueprint);
 +              break;
 +      case LS_MODIFIER_2D_OFFSET:
 +              size = sizeof(LineStyleGeometryModifier_2DOffset);
 +              break;
 +      case LS_MODIFIER_2D_TRANSFORM:
 +              size = sizeof(LineStyleGeometryModifier_2DTransform);
 +              break;
 +      default:
 +              return NULL; /* unknown modifier type */
 +      }
 +      return new_modifier(type, size);
 +}
 +
 +LineStyleModifier *FRS_add_linestyle_geometry_modifier(FreestyleLineStyle *linestyle, int type)
 +{
 +      LineStyleModifier *m;
 +
 +      m = alloc_geometry_modifier(type);
 +      if (!m)
 +              return NULL;
 +      switch (type) {
 +      case LS_MODIFIER_SAMPLING:
 +              ((LineStyleGeometryModifier_Sampling *)m)->sampling = 10.0;
 +              break;
 +      case LS_MODIFIER_BEZIER_CURVE:
 +              ((LineStyleGeometryModifier_BezierCurve *)m)->error = 10.0;
 +              break;
 +      case LS_MODIFIER_SINUS_DISPLACEMENT:
 +              ((LineStyleGeometryModifier_SinusDisplacement *)m)->wavelength = 20.0;
 +              ((LineStyleGeometryModifier_SinusDisplacement *)m)->amplitude = 5.0;
 +              ((LineStyleGeometryModifier_SinusDisplacement *)m)->phase = 0.0;
 +              break;
 +      case LS_MODIFIER_SPATIAL_NOISE:
 +              ((LineStyleGeometryModifier_SpatialNoise *)m)->amplitude = 5.0;
 +              ((LineStyleGeometryModifier_SpatialNoise *)m)->scale = 20.0;
 +              ((LineStyleGeometryModifier_SpatialNoise *)m)->octaves = 4;
 +              ((LineStyleGeometryModifier_SpatialNoise *)m)->flags = LS_MODIFIER_SPATIAL_NOISE_SMOOTH | LS_MODIFIER_SPATIAL_NOISE_PURERANDOM;
 +              break;
 +      case LS_MODIFIER_PERLIN_NOISE_1D:
 +              ((LineStyleGeometryModifier_PerlinNoise1D *)m)->frequency = 10.0;
 +              ((LineStyleGeometryModifier_PerlinNoise1D *)m)->amplitude = 10.0;
 +              ((LineStyleGeometryModifier_PerlinNoise1D *)m)->octaves = 4;
 +              ((LineStyleGeometryModifier_PerlinNoise1D *)m)->angle = 45.0;
 +              break;
 +      case LS_MODIFIER_PERLIN_NOISE_2D:
 +              ((LineStyleGeometryModifier_PerlinNoise2D *)m)->frequency = 10.0;
 +              ((LineStyleGeometryModifier_PerlinNoise2D *)m)->amplitude = 10.0;
 +              ((LineStyleGeometryModifier_PerlinNoise2D *)m)->octaves = 4;
 +              ((LineStyleGeometryModifier_PerlinNoise2D *)m)->angle = 45.0;
 +              break;
 +      case LS_MODIFIER_BACKBONE_STRETCHER:
 +              ((LineStyleGeometryModifier_BackboneStretcher *)m)->backbone_length = 10.0;
 +              break;
 +      case LS_MODIFIER_TIP_REMOVER:
 +              ((LineStyleGeometryModifier_TipRemover *)m)->tip_length = 10.0;
 +              break;
 +      case LS_MODIFIER_POLYGONIZATION:
 +              ((LineStyleGeometryModifier_Polygonalization *)m)->error = 10.0;
 +              break;
 +      case LS_MODIFIER_GUIDING_LINES:
 +              ((LineStyleGeometryModifier_GuidingLines *)m)->offset = 0.0;
 +              break;
 +      case LS_MODIFIER_BLUEPRINT:
 +              ((LineStyleGeometryModifier_Blueprint *)m)->flags = LS_MODIFIER_BLUEPRINT_CIRCLES;
 +              ((LineStyleGeometryModifier_Blueprint *)m)->rounds = 1;
 +              ((LineStyleGeometryModifier_Blueprint *)m)->backbone_length = 10.f;
 +              ((LineStyleGeometryModifier_Blueprint *)m)->random_radius = 3;
 +              ((LineStyleGeometryModifier_Blueprint *)m)->random_center = 5;
 +              ((LineStyleGeometryModifier_Blueprint *)m)->random_backbone = 5;
 +              break;
 +      case LS_MODIFIER_2D_OFFSET:
 +              ((LineStyleGeometryModifier_2DOffset *)m)->start = 0.f;
 +              ((LineStyleGeometryModifier_2DOffset *)m)->end = 0.f;
 +              ((LineStyleGeometryModifier_2DOffset *)m)->x = 0.f;
 +              ((LineStyleGeometryModifier_2DOffset *)m)->y = 0.f;
 +              break;
 +      case LS_MODIFIER_2D_TRANSFORM:
 +              ((LineStyleGeometryModifier_2DTransform *)m)->pivot = LS_MODIFIER_2D_TRANSFORM_PIVOT_CENTER;
 +              ((LineStyleGeometryModifier_2DTransform *)m)->scale_x = 1.f;
 +              ((LineStyleGeometryModifier_2DTransform *)m)->scale_y = 1.f;
 +              ((LineStyleGeometryModifier_2DTransform *)m)->angle = 0.f;
 +              ((LineStyleGeometryModifier_2DTransform *)m)->pivot_u = 0.5f;
 +              ((LineStyleGeometryModifier_2DTransform *)m)->pivot_x = 0.f;
 +              ((LineStyleGeometryModifier_2DTransform *)m)->pivot_y = 0.f;
 +              break;
 +      default:
 +              return NULL; /* unknown modifier type */
 +      }
 +      add_to_modifier_list(&linestyle->geometry_modifiers, m);
 +
 +      return m;
 +}
 +
 +LineStyleModifier *FRS_copy_linestyle_geometry_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *m)
 +{
 +      LineStyleModifier *new_m;
 +
 +      new_m = alloc_geometry_modifier(m->type);
 +      if (!new_m)
 +              return NULL;
 +      new_m->flags = m->flags;
 +      switch (m->type) {
 +      case LS_MODIFIER_SAMPLING:
 +              {
 +                      LineStyleGeometryModifier_Sampling *p = (LineStyleGeometryModifier_Sampling *)m;
 +                      ((LineStyleGeometryModifier_Sampling *)new_m)->sampling = p->sampling;
 +              }
 +              break;
 +      case LS_MODIFIER_BEZIER_CURVE:
 +              {
 +                      LineStyleGeometryModifier_BezierCurve *p = (LineStyleGeometryModifier_BezierCurve *)m;
 +                      ((LineStyleGeometryModifier_BezierCurve *)new_m)->error = p->error;
 +              }
 +              break;
 +      case LS_MODIFIER_SINUS_DISPLACEMENT:
 +              {
 +                      LineStyleGeometryModifier_SinusDisplacement *p = (LineStyleGeometryModifier_SinusDisplacement *)m;
 +                      ((LineStyleGeometryModifier_SinusDisplacement *)new_m)->wavelength = p->wavelength;
 +                      ((LineStyleGeometryModifier_SinusDisplacement *)new_m)->amplitude = p->amplitude;
 +                      ((LineStyleGeometryModifier_SinusDisplacement *)new_m)->phase = p->phase;
 +              }
 +              break;
 +      case LS_MODIFIER_SPATIAL_NOISE:
 +              {
 +                      LineStyleGeometryModifier_SpatialNoise *p = (LineStyleGeometryModifier_SpatialNoise *)m;
 +                      ((LineStyleGeometryModifier_SpatialNoise *)new_m)->amplitude = p->amplitude;
 +                      ((LineStyleGeometryModifier_SpatialNoise *)new_m)->scale = p->scale;
 +                      ((LineStyleGeometryModifier_SpatialNoise *)new_m)->octaves = p->octaves;
 +                      ((LineStyleGeometryModifier_SpatialNoise *)new_m)->flags = p->flags;
 +              }
 +              break;
 +      case LS_MODIFIER_PERLIN_NOISE_1D:
 +              {
 +                      LineStyleGeometryModifier_PerlinNoise1D *p = (LineStyleGeometryModifier_PerlinNoise1D *)m;
 +                      ((LineStyleGeometryModifier_PerlinNoise1D *)new_m)->frequency = p->frequency;
 +                      ((LineStyleGeometryModifier_PerlinNoise1D *)new_m)->amplitude = p->amplitude;
 +                      ((LineStyleGeometryModifier_PerlinNoise1D *)new_m)->octaves = p->octaves;
 +                      ((LineStyleGeometryModifier_PerlinNoise1D *)new_m)->angle = p->angle;
 +              }
 +              break;
 +      case LS_MODIFIER_PERLIN_NOISE_2D:
 +              {
 +                      LineStyleGeometryModifier_PerlinNoise2D *p = (LineStyleGeometryModifier_PerlinNoise2D *)m;
 +                      ((LineStyleGeometryModifier_PerlinNoise2D *)new_m)->frequency = p->frequency;
 +                      ((LineStyleGeometryModifier_PerlinNoise2D *)new_m)->amplitude = p->amplitude;
 +                      ((LineStyleGeometryModifier_PerlinNoise2D *)new_m)->octaves = p->octaves;
 +                      ((LineStyleGeometryModifier_PerlinNoise2D *)new_m)->angle = p->angle;
 +              }
 +              break;
 +      case LS_MODIFIER_BACKBONE_STRETCHER:
 +              {
 +                      LineStyleGeometryModifier_BackboneStretcher *p = (LineStyleGeometryModifier_BackboneStretcher *)m;
 +                      ((LineStyleGeometryModifier_BackboneStretcher *)new_m)->backbone_length = p->backbone_length;
 +              }
 +              break;
 +      case LS_MODIFIER_TIP_REMOVER:
 +              {
 +                      LineStyleGeometryModifier_TipRemover *p = (LineStyleGeometryModifier_TipRemover *)m;
 +                      ((LineStyleGeometryModifier_TipRemover *)new_m)->tip_length = p->tip_length;
 +              }
 +              break;
 +      case LS_MODIFIER_POLYGONIZATION:
 +              {
 +                      LineStyleGeometryModifier_Polygonalization *p = (LineStyleGeometryModifier_Polygonalization *)m;
 +                      ((LineStyleGeometryModifier_Polygonalization *)new_m)->error = p->error;
 +              }
 +              break;
 +      case LS_MODIFIER_GUIDING_LINES:
 +              {
 +                      LineStyleGeometryModifier_GuidingLines *p = (LineStyleGeometryModifier_GuidingLines *)m;
 +                      ((LineStyleGeometryModifier_GuidingLines *)new_m)->offset = p->offset;
 +              }
 +              break;
 +      case LS_MODIFIER_BLUEPRINT:
 +              {
 +                      LineStyleGeometryModifier_Blueprint *p = (LineStyleGeometryModifier_Blueprint *)m;
 +                      ((LineStyleGeometryModifier_Blueprint *)new_m)->flags = p->flags;
 +                      ((LineStyleGeometryModifier_Blueprint *)new_m)->rounds = p->rounds;
 +                      ((LineStyleGeometryModifier_Blueprint *)new_m)->backbone_length = p->backbone_length;
 +                      ((LineStyleGeometryModifier_Blueprint *)new_m)->random_radius = p->random_radius;
 +                      ((LineStyleGeometryModifier_Blueprint *)new_m)->random_center = p->random_center;
 +                      ((LineStyleGeometryModifier_Blueprint *)new_m)->random_backbone = p->random_backbone;
 +              }
 +              break;
 +      case LS_MODIFIER_2D_OFFSET:
 +              {
 +                      LineStyleGeometryModifier_2DOffset *p = (LineStyleGeometryModifier_2DOffset *)m;
 +                      ((LineStyleGeometryModifier_2DOffset *)new_m)->start = p->start;
 +                      ((LineStyleGeometryModifier_2DOffset *)new_m)->end = p->end;
 +                      ((LineStyleGeometryModifier_2DOffset *)new_m)->x = p->x;
 +                      ((LineStyleGeometryModifier_2DOffset *)new_m)->y = p->y;
 +              }
 +              break;
 +      case LS_MODIFIER_2D_TRANSFORM:
 +              {
 +                      LineStyleGeometryModifier_2DTransform *p = (LineStyleGeometryModifier_2DTransform *)m;
 +                      ((LineStyleGeometryModifier_2DTransform *)new_m)->pivot = p->pivot;
 +                      ((LineStyleGeometryModifier_2DTransform *)new_m)->scale_x = p->scale_x;
 +                      ((LineStyleGeometryModifier_2DTransform *)new_m)->scale_y = p->scale_y;
 +                      ((LineStyleGeometryModifier_2DTransform *)new_m)->angle = p->angle;
 +                      ((LineStyleGeometryModifier_2DTransform *)new_m)->pivot_u = p->pivot_u;
 +                      ((LineStyleGeometryModifier_2DTransform *)new_m)->pivot_x = p->pivot_x;
 +                      ((LineStyleGeometryModifier_2DTransform *)new_m)->pivot_y = p->pivot_y;
 +              }
 +              break;
 +      default:
 +              return NULL; /* unknown modifier type */
 +      }
 +      add_to_modifier_list(&linestyle->geometry_modifiers, new_m);
 +
 +      return new_m;
 +}
 +
 +void FRS_remove_linestyle_geometry_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *m)
 +{
 +      switch (m->type) {
 +      case LS_MODIFIER_SAMPLING:
 +              break;
 +      case LS_MODIFIER_BEZIER_CURVE:
 +              break;
 +      case LS_MODIFIER_SINUS_DISPLACEMENT:
 +              break;
 +      case LS_MODIFIER_SPATIAL_NOISE:
 +              break;
 +      case LS_MODIFIER_PERLIN_NOISE_1D:
 +              break;
 +      case LS_MODIFIER_PERLIN_NOISE_2D:
 +              break;
 +      case LS_MODIFIER_BACKBONE_STRETCHER:
 +              break;
 +      case LS_MODIFIER_TIP_REMOVER:
 +              break;
 +      case LS_MODIFIER_POLYGONIZATION:
 +              break;
 +      case LS_MODIFIER_GUIDING_LINES:
 +              break;
 +      case LS_MODIFIER_BLUEPRINT:
 +              break;
 +      case LS_MODIFIER_2D_OFFSET:
 +              break;
 +      case LS_MODIFIER_2D_TRANSFORM:
 +              break;
 +      }
 +      BLI_freelinkN(&linestyle->geometry_modifiers, m);
 +}
 +
 +static void move_modifier(ListBase *lb, LineStyleModifier *modifier, int direction)
 +{
 +      BLI_remlink(lb, modifier);
 +      if (direction > 0)
 +              BLI_insertlinkbefore(lb, modifier->prev, modifier);
 +      else
 +              BLI_insertlinkafter(lb, modifier->next, modifier);
 +}
 +
 +void FRS_move_linestyle_color_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *modifier, int direction)
 +{
 +      move_modifier(&linestyle->color_modifiers, modifier, direction);
 +}
 +
 +void FRS_move_linestyle_alpha_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *modifier, int direction)
 +{
 +      move_modifier(&linestyle->alpha_modifiers, modifier, direction);
 +}
 +
 +void FRS_move_linestyle_thickness_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *modifier, int direction)
 +{
 +      move_modifier(&linestyle->thickness_modifiers, modifier, direction);
 +}
 +
 +void FRS_move_linestyle_geometry_modifier(FreestyleLineStyle *linestyle, LineStyleModifier *modifier, int direction)
 +{
 +      move_modifier(&linestyle->geometry_modifiers, modifier, direction);
 +}
 +
 +void FRS_list_modifier_color_ramps(FreestyleLineStyle *linestyle, ListBase *listbase)
 +{
 +      LineStyleModifier *m;
 +      ColorBand *color_ramp;
 +      LinkData *link;
 +
 +      listbase->first = listbase->last = NULL;
 +      for (m = (LineStyleModifier *)linestyle->color_modifiers.first; m; m = m->next) {
 +              switch (m->type) {
 +              case LS_MODIFIER_ALONG_STROKE:
 +                      color_ramp = ((LineStyleColorModifier_AlongStroke *)m)->color_ramp;
 +                      break;
 +              case LS_MODIFIER_DISTANCE_FROM_CAMERA:
 +                      color_ramp = ((LineStyleColorModifier_DistanceFromCamera *)m)->color_ramp;
 +                      break;
 +              case LS_MODIFIER_DISTANCE_FROM_OBJECT:
 +                      color_ramp = ((LineStyleColorModifier_DistanceFromObject *)m)->color_ramp;
 +                      break;
 +              case LS_MODIFIER_MATERIAL:
 +                      color_ramp = ((LineStyleColorModifier_Material *)m)->color_ramp;
 +                      break;
 +              default:
 +                      continue;
 +              }
 +              link = (LinkData *) MEM_callocN( sizeof(LinkData), "link to color ramp");
 +              link->data = color_ramp;
 +              BLI_addtail(listbase, link);
 +      }
 +}
 +
 +char *FRS_path_from_ID_to_color_ramp(FreestyleLineStyle *linestyle, ColorBand *color_ramp)
 +{
 +      LineStyleModifier *m;
 +
 +      for (m = (LineStyleModifier *)linestyle->color_modifiers.first; m; m = m->next) {
 +              switch (m->type) {
 +              case LS_MODIFIER_ALONG_STROKE:
 +                      if (color_ramp == ((LineStyleColorModifier_AlongStroke *)m)->color_ramp)
 +                              goto found;
 +                      break;
 +              case LS_MODIFIER_DISTANCE_FROM_CAMERA:
 +                      if (color_ramp == ((LineStyleColorModifier_DistanceFromCamera *)m)->color_ramp)
 +                              goto found;
 +                      break;
 +              case LS_MODIFIER_DISTANCE_FROM_OBJECT:
 +                      if (color_ramp == ((LineStyleColorModifier_DistanceFromObject *)m)->color_ramp)
 +                              goto found;
 +                      break;
 +              case LS_MODIFIER_MATERIAL:
 +                      if (color_ramp == ((LineStyleColorModifier_Material *)m)->color_ramp)
 +                              goto found;
 +                      break;
 +              }
 +      }
 +      printf("FRS_path_from_ID_to_color_ramp: No color ramps correspond to the given pointer.\n");
 +      return NULL;
 +
 +found:
 +      return BLI_sprintfN("color_modifiers[\"%s\"].color_ramp", m->name);
 +}
 +
 +void FRS_unlink_linestyle_target_object(FreestyleLineStyle *linestyle, struct Object *ob)
 +{
 +      LineStyleModifier *m;
 +
 +      for (m = (LineStyleModifier *)linestyle->color_modifiers.first; m; m = m->next) {
 +              if (m->type == LS_MODIFIER_DISTANCE_FROM_OBJECT) {
 +                      if (((LineStyleColorModifier_DistanceFromObject *)m)->target == ob) {
 +                              ((LineStyleColorModifier_DistanceFromObject *)m)->target = NULL;
 +                      }
 +              }
 +      }
 +      for (m = (LineStyleModifier *)linestyle->alpha_modifiers.first; m; m = m->next) {
 +              if (m->type == LS_MODIFIER_DISTANCE_FROM_OBJECT) {
 +                      if (((LineStyleAlphaModifier_DistanceFromObject *)m)->target == ob) {
 +                              ((LineStyleAlphaModifier_DistanceFromObject *)m)->target = NULL;
 +                      }
 +              }
 +      }
 +      for (m = (LineStyleModifier *)linestyle->thickness_modifiers.first; m; m = m->next) {
 +              if (m->type == LS_MODIFIER_DISTANCE_FROM_OBJECT) {
 +                      if (((LineStyleThicknessModifier_DistanceFromObject *)m)->target == ob) {
 +                              ((LineStyleThicknessModifier_DistanceFromObject *)m)->target = NULL;
 +                      }
 +              }
 +      }
 +}
@@@ -110,46 -110,45 +110,46 @@@ void BKE_material_free(Material *ma
  
  void init_material(Material *ma)
  {
-       ma->r= ma->g= ma->b= ma->ref= 0.8;
-       ma->specr= ma->specg= ma->specb= 1.0;
-       ma->mirr= ma->mirg= ma->mirb= 1.0;
-       ma->spectra= 1.0;
-       ma->amb= 1.0;
-       ma->alpha= 1.0;
-       ma->spec= ma->hasize= 0.5;
-       ma->har= 50;
-       ma->starc= ma->ringc= 4;
-       ma->linec= 12;
-       ma->flarec= 1;
-       ma->flaresize= ma->subsize= 1.0;
-       ma->flareboost= 1;
-       ma->seed2= 6;
-       ma->friction= 0.5;
-       ma->refrac= 4.0;
-       ma->roughness= 0.5;
-       ma->param[0]= 0.5;
-       ma->param[1]= 0.1;
-       ma->param[2]= 0.5;
-       ma->param[3]= 0.1;
-       ma->rms= 0.1;
-       ma->darkness= 1.0;      
-       
-       ma->strand_sta= ma->strand_end= 1.0f;
-       
-       ma->ang= 1.0;
-       ma->ray_depth= 2;
-       ma->ray_depth_tra= 2;
-       ma->fresnel_mir= 0.0;
-       ma->fresnel_tra= 0.0;
-       ma->fresnel_tra_i= 1.25;
-       ma->fresnel_mir_i= 1.25;
-       ma->tx_limit= 0.0;
-       ma->tx_falloff= 1.0;
-       ma->shad_alpha= 1.0f;
-       ma->vcol_alpha= 0;
-       
-       ma->gloss_mir = ma->gloss_tra= 1.0;
-       ma->samp_gloss_mir = ma->samp_gloss_tra= 18;
+       ma->r = ma->g = ma->b = ma->ref = 0.8;
+       ma->specr = ma->specg = ma->specb = 1.0;
+       ma->mirr = ma->mirg = ma->mirb = 1.0;
+       ma->spectra = 1.0;
+       ma->amb = 1.0;
+       ma->alpha = 1.0;
+       ma->spec = ma->hasize = 0.5;
+       ma->har = 50;
+       ma->starc = ma->ringc = 4;
+       ma->linec = 12;
+       ma->flarec = 1;
+       ma->flaresize = ma->subsize = 1.0;
+       ma->flareboost = 1;
+       ma->seed2 = 6;
+       ma->friction = 0.5;
+       ma->refrac = 4.0;
+       ma->roughness = 0.5;
+       ma->param[0] = 0.5;
+       ma->param[1] = 0.1;
+       ma->param[2] = 0.5;
+       ma->param[3] = 0.1;
+       ma->rms = 0.1;
+       ma->darkness = 1.0;
+       ma->strand_sta = ma->strand_end = 1.0f;
+       ma->ang = 1.0;
+       ma->ray_depth = 2;
+       ma->ray_depth_tra = 2;
+       ma->fresnel_mir = 0.0;
+       ma->fresnel_tra = 0.0;
+       ma->fresnel_tra_i = 1.25;
+       ma->fresnel_mir_i = 1.25;
+       ma->tx_limit = 0.0;
+       ma->tx_falloff = 1.0;
+       ma->shad_alpha = 1.0f;
++      ma->vcol_alpha = 0;
+       
+       ma->gloss_mir = ma->gloss_tra = 1.0;
+       ma->samp_gloss_mir = ma->samp_gloss_tra = 18;
        ma->adapt_thresh_mir = ma->adapt_thresh_tra = 0.005;
        ma->dist_mir = 0.0;
        ma->fadeto_mir = MA_RAYMIR_FADETOSKY;
  
  #include "GPU_material.h"
  
 +#include "FRS_freestyle.h"
 +
  /* Local function protos */
- static void solve_parenting (Scene *scene, Object *ob, Object *par, float obmat[][4], float slowmat[][4], int simul);
+ static void solve_parenting(Scene *scene, Object *ob, Object *par, float obmat[][4], float slowmat[][4], int simul);
  
- float originmat[3][3];        /* after where_is_object(), can be used in other functions (bad!) */
+ float originmat[3][3];  /* after BKE_object_where_is_calc(), can be used in other functions (bad!) */
  
- void clear_workob(Object *workob)
+ void BKE_object_workob_clear(Object *workob)
  {
        memset(workob, 0, sizeof(Object));
        
@@@ -636,17 -614,9 +616,17 @@@ void BKE_object_unlink(Object *ob
                                }
                                SEQ_END
                        }
 +
 +                      {
 +                              SceneRenderLayer *srl;
 +
 +                              for (srl= sce->r.layers.first; srl; srl= srl->next) {
 +                                      FRS_unlink_target_object(&srl->freestyleConfig, ob);
 +                              }
 +                      }
                }
  
-               sce= sce->id.next;
+               sce = sce->id.next;
        }
        
        /* screens */
@@@ -246,15 -244,14 +246,15 @@@ Scene *BKE_scene_copy(Scene *sce, int t
  }
  
  /* do not free scene itself */
- void free_scene(Scene *sce)
+ void BKE_scene_free(Scene *sce)
  {
        Base *base;
 -
 +      SceneRenderLayer *srl;
 +      
-       base= sce->base.first;
+       base = sce->base.first;
        while (base) {
                base->object->id.us--;
-               base= base->next;
+               base = base->next;
        }
        /* do not free objects! */
        
@@@ -1114,10 -1107,9 +1114,10 @@@ SceneRenderLayer *BKE_scene_add_render_
        BLI_addtail(&sce->r.layers, srl);
  
        /* note, this is also in render, pipeline.c, to make layer when scenedata doesnt have it */
-       srl->lay= (1<<20) -1;
-       srl->layflag= 0x7FFF;   /* solid ztra halo edge strand */
-       srl->passflag= SCE_PASS_COMBINED|SCE_PASS_Z;
+       srl->lay = (1 << 20) - 1;
+       srl->layflag = 0x7FFF;   /* solid ztra halo edge strand */
+       srl->passflag = SCE_PASS_COMBINED | SCE_PASS_Z;
 +      FRS_add_freestyle_config( srl );
  
        return srl;
  }
@@@ -1144,8 -1221,7 +1221,7 @@@ static void ccgDM_copyFinalEdgeArray(De
  
                if (edgeFlags) {
                        if (edgeIdx != -1) {
-                               flags |= (edgeFlags[index] & (ME_SEAM | ME_SHARP | ME_FREESTYLE_EDGE))
-                                        | ME_EDGEDRAW | ME_EDGERENDER;
 -                              flags |= ((edgeFlags[index] & (ME_SEAM | ME_SHARP)) | ME_EDGEDRAW | ME_EDGERENDER);
++                              flags |= ((edgeFlags[index] & (ME_SEAM | ME_SHARP | ME_FREESTYLE_EDGE)) | ME_EDGEDRAW | ME_EDGERENDER);
                        }
                }
                else {
@@@ -7605,36 -7502,26 +7691,56 @@@ static void do_versions(FileData *fd, L
                }
        }
  
+       if (main->versionfile < 263 || (main->versionfile == 263 && main->subversionfile < 5))
+       {
+               {
+                       /* file output node paths are now stored in the file info struct instead socket name */
+                       Scene *sce;
+                       bNodeTree *ntree;
+                       
+                       for (sce = main->scene.first; sce; sce=sce->id.next) {
+                               if (sce->nodetree) {
+                                       do_versions_nodetree_file_output_layers_2_64_5(sce->nodetree);
+                                       do_versions_nodetree_image_layer_2_64_5(sce->nodetree);
+                               }
+                       }
+                       for (ntree = main->nodetree.first; ntree; ntree=ntree->id.next) {
+                               do_versions_nodetree_file_output_layers_2_64_5(ntree);
+                               do_versions_nodetree_image_layer_2_64_5(ntree);
+                       }
+               }
+       }
 +      /* 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 == 0)
 +                                      srl->freestyleConfig.raycasting_algorithm= FREESTYLE_ALGO_CULLED_ADAPTIVE_CUMULATIVE;
 +                      }
 +              }
 +              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! */
  
@@@ -1964,101 -1963,24 +1964,101 @@@ static void *acf_dsntree_setting_ptr(bA
  }
  
  /* node tree expander type define */
- static bAnimChannelType ACF_DSNTREE
+ static bAnimChannelType ACF_DSNTREE =
  {
-       "Node Tree Expander",                   /* type name */
-       
-       acf_generic_dataexpand_color,   /* backdrop color */
-       acf_generic_dataexpand_backdrop,/* backdrop */
-       acf_generic_indention_1,                /* indent level */
-       acf_dsntree_offset,                             /* offset */
+       "Node Tree Expander",           /* type name */
        
-       acf_generic_idblock_name,               /* name */
-       acf_generic_idblock_nameprop,   /* name prop */
-       acf_dsntree_icon,                               /* icon */
-       
-       acf_generic_dataexpand_setting_valid,   /* has setting */
-       acf_dsntree_setting_flag,                               /* flag for setting */
-       acf_dsntree_setting_ptr                                 /* pointer for setting */
+       acf_generic_dataexpand_color,   /* backdrop color */
+       acf_generic_dataexpand_backdrop, /* backdrop */
+       acf_generic_indention_1,        /* indent level */
+       acf_dsntree_offset,             /* offset */
+       acf_generic_idblock_name,       /* name */
+       acf_generic_idblock_nameprop,   /* name prop */
+       acf_dsntree_icon,               /* icon */
+       acf_generic_dataexpand_setting_valid,   /* has setting */
+       acf_dsntree_setting_flag,               /* flag for setting */
+       acf_dsntree_setting_ptr                 /* pointer for setting */
  };
  
 +/* LineStyle Expander  ------------------------------------------- */
 +
 +// TODO: just get this from RNA?
 +static int acf_dslinestyle_icon(bAnimListElem *ale)
 +{
 +      return ICON_BRUSH_DATA; /* FIXME */
 +}
 +
 +/* get the appropriate flag(s) for the setting when it is valid  */
 +static int acf_dslinestyle_setting_flag(bAnimContext *ac, int setting, short *neg)
 +{
 +      /* clear extra return data first */
 +      *neg= 0;
 +      
 +      switch (setting) {
 +              case ACHANNEL_SETTING_EXPAND: /* expanded */
 +                      return LS_DS_EXPAND;
 +                      
 +              case ACHANNEL_SETTING_MUTE: /* mute (only in NLA) */
 +                      return ADT_NLA_EVAL_OFF;
 +                      
 +              case ACHANNEL_SETTING_VISIBLE: /* visible (only in Graph Editor) */
 +                      *neg= 1;
 +                      return ADT_CURVES_NOT_VISIBLE;
 +                      
 +              case ACHANNEL_SETTING_SELECT: /* selected */
 +                      return ADT_UI_SELECTED;
 +                      
 +              default: /* unsupported */
 +                      return 0;
 +      }
 +}
 +
 +/* get pointer to the setting */
 +static void *acf_dslinestyle_setting_ptr(bAnimListElem *ale, int setting, short *type)
 +{
 +      FreestyleLineStyle *linestyle= (FreestyleLineStyle *)ale->data;
 +      
 +      /* clear extra return data first */
 +      *type= 0;
 +      
 +      switch (setting) {
 +              case ACHANNEL_SETTING_EXPAND: /* expanded */
 +                      return GET_ACF_FLAG_PTR(linestyle->flag, type);
 +                      
 +              case ACHANNEL_SETTING_SELECT: /* selected */
 +              case ACHANNEL_SETTING_MUTE: /* muted (for NLA only) */
 +              case ACHANNEL_SETTING_VISIBLE: /* visible (for Graph Editor only) */
 +                      if (linestyle->adt)
 +                              return GET_ACF_FLAG_PTR(linestyle->adt->flag, type);
 +                      else
 +                              return NULL;
 +                      
 +              default: /* unsupported */
 +                      return NULL;
 +      }
 +}
 +
 +/* node tree expander type define */
 +static bAnimChannelType ACF_DSLINESTYLE= 
 +{
 +      "Line Style Expander",                  /* type name */
 +      
 +      acf_generic_dataexpand_color,   /* backdrop color */
 +      acf_generic_dataexpand_backdrop,/* backdrop */
 +      acf_generic_indention_1,                /* indent level */
 +      acf_generic_basic_offset,               /* offset */
 +      
 +      acf_generic_idblock_name,               /* name */
 +      acf_generic_idblock_nameprop,   /* name prop */
 +      acf_dslinestyle_icon,                   /* icon */
 +      
 +      acf_generic_dataexpand_setting_valid,   /* has setting */
 +      acf_dslinestyle_setting_flag,                   /* flag for setting */
 +      acf_dslinestyle_setting_ptr                             /* pointer for setting */
 +};
 +
  /* Mesh Expander  ------------------------------------------- */
  
  // TODO: just get this from RNA?
@@@ -2575,47 -2497,46 +2575,47 @@@ static void ANIM_init_channel_typeinfo_
        
        /* start initializing if necessary... */
        if (ACF_INIT) {
-               ACF_INIT= 0;
-               
-               animchannelTypeInfo[type++]= NULL;                              /* None */
-               animchannelTypeInfo[type++]= NULL;                              /* AnimData */
-               animchannelTypeInfo[type++]= NULL;                              /* Special */
-               
-               animchannelTypeInfo[type++]= &ACF_SUMMARY;              /* Motion Summary */
-               
-               animchannelTypeInfo[type++]= &ACF_SCENE;                /* Scene */
-               animchannelTypeInfo[type++]= &ACF_OBJECT;               /* Object */
-               animchannelTypeInfo[type++]= &ACF_GROUP;                /* Group */
-               animchannelTypeInfo[type++]= &ACF_FCURVE;               /* F-Curve */
-               
-               animchannelTypeInfo[type++]= &ACF_FILLACTD;     /* Object Action Expander */
-               animchannelTypeInfo[type++]= &ACF_FILLDRIVERS;  /* Drivers Expander */
-               
-               animchannelTypeInfo[type++]= &ACF_DSMAT;                /* Material Channel */
-               animchannelTypeInfo[type++]= &ACF_DSLAM;                /* Lamp Channel */
-               animchannelTypeInfo[type++]= &ACF_DSCAM;                /* Camera Channel */
-               animchannelTypeInfo[type++]= &ACF_DSCUR;                /* Curve Channel */
-               animchannelTypeInfo[type++]= &ACF_DSSKEY;               /* ShapeKey Channel */
-               animchannelTypeInfo[type++]= &ACF_DSWOR;                /* World Channel */
-               animchannelTypeInfo[type++]= &ACF_DSNTREE;              /* NodeTree Channel */
-               animchannelTypeInfo[type++]= &ACF_DSPART;               /* Particle Channel */
-               animchannelTypeInfo[type++]= &ACF_DSMBALL;              /* MetaBall Channel */
-               animchannelTypeInfo[type++]= &ACF_DSARM;                /* Armature Channel */
-               animchannelTypeInfo[type++]= &ACF_DSMESH;               /* Mesh Channel */
-               animchannelTypeInfo[type++]= &ACF_DSTEX;                /* Texture Channel */
-               animchannelTypeInfo[type++]= &ACF_DSLAT;                /* Lattice Channel */
-               animchannelTypeInfo[type++]= &ACF_DSLINESTYLE;  /* LineStyle Channel */
-               animchannelTypeInfo[type++]= &ACF_DSSPK;                /* Speaker Channel */
-               
-               animchannelTypeInfo[type++]= &ACF_SHAPEKEY;             /* ShapeKey */
-               
-               animchannelTypeInfo[type++]= &ACF_GPD;                  /* Grease Pencil Datablock */ 
-               animchannelTypeInfo[type++]= &ACF_GPL;                  /* Grease Pencil Layer */ 
-               
-                       // TODO: these types still need to be implemented!!!
-                       // probably need a few extra flags for these special cases...
-               animchannelTypeInfo[type++]= NULL;                              /* NLA Track */ 
-               animchannelTypeInfo[type++]= NULL;                              /* NLA Action */ 
+               ACF_INIT = 0;
+               
+               animchannelTypeInfo[type++] = NULL;              /* None */
+               animchannelTypeInfo[type++] = NULL;              /* AnimData */
+               animchannelTypeInfo[type++] = NULL;              /* Special */
+               animchannelTypeInfo[type++] = &ACF_SUMMARY;      /* Motion Summary */
+               animchannelTypeInfo[type++] = &ACF_SCENE;        /* Scene */
+               animchannelTypeInfo[type++] = &ACF_OBJECT;       /* Object */
+               animchannelTypeInfo[type++] = &ACF_GROUP;        /* Group */
+               animchannelTypeInfo[type++] = &ACF_FCURVE;       /* F-Curve */
+               animchannelTypeInfo[type++] = &ACF_FILLACTD;     /* Object Action Expander */
+               animchannelTypeInfo[type++] = &ACF_FILLDRIVERS;  /* Drivers Expander */
+               animchannelTypeInfo[type++] = &ACF_DSMAT;        /* Material Channel */
+               animchannelTypeInfo[type++] = &ACF_DSLAM;        /* Lamp Channel */
+               animchannelTypeInfo[type++] = &ACF_DSCAM;        /* Camera Channel */
+               animchannelTypeInfo[type++] = &ACF_DSCUR;        /* Curve Channel */
+               animchannelTypeInfo[type++] = &ACF_DSSKEY;       /* ShapeKey Channel */
+               animchannelTypeInfo[type++] = &ACF_DSWOR;        /* World Channel */
+               animchannelTypeInfo[type++] = &ACF_DSNTREE;      /* NodeTree Channel */
+               animchannelTypeInfo[type++] = &ACF_DSPART;       /* Particle Channel */
+               animchannelTypeInfo[type++] = &ACF_DSMBALL;      /* MetaBall Channel */
+               animchannelTypeInfo[type++] = &ACF_DSARM;        /* Armature Channel */
+               animchannelTypeInfo[type++] = &ACF_DSMESH;       /* Mesh Channel */
+               animchannelTypeInfo[type++] = &ACF_DSTEX;        /* Texture Channel */
+               animchannelTypeInfo[type++] = &ACF_DSLAT;        /* Lattice Channel */
+               animchannelTypeInfo[type++] = &ACF_DSSPK;        /* Speaker Channel */
++              animchannelTypeInfo[type++] = &ACF_DSLINESTYLE;  /* LineStyle Channel */
+               animchannelTypeInfo[type++] = &ACF_SHAPEKEY;     /* ShapeKey */
+               animchannelTypeInfo[type++] = &ACF_GPD;          /* Grease Pencil Datablock */
+               animchannelTypeInfo[type++] = &ACF_GPL;          /* Grease Pencil Layer */
+               // TODO: these types still need to be implemented!!!
+               // probably need a few extra flags for these special cases...
+               animchannelTypeInfo[type++] = NULL;              /* NLA Track */
+               animchannelTypeInfo[type++] = NULL;              /* NLA Action */
        }
  } 
  
@@@ -569,186 -578,173 +579,186 @@@ static bAnimListElem *make_new_animlist
                        
                        case ANIMTYPE_DSMAT:
                        {
-                               Material *ma= (Material *)data;
-                               AnimData *adt= ma->adt;
+                               Material *ma = (Material *)data;
+                               AnimData *adt = ma->adt;
                                
-                               ale->flag= FILTER_MAT_OBJD(ma);
+                               ale->flag = FILTER_MAT_OBJD(ma);
                                
-                               ale->key_data= (adt) ? adt->action : NULL;
-                               ale->datatype= ALE_ACT;
+                               ale->key_data = (adt) ? adt->action : NULL;
+                               ale->datatype = ALE_ACT;
                                
-                               ale->adt= BKE_animdata_from_id(data);
+                               ale->adt = BKE_animdata_from_id(data);
                        }
-                               break;
+                       break;
                        case ANIMTYPE_DSLAM:
                        {
-                               Lamp *la= (Lamp *)data;
-                               AnimData *adt= la->adt;
+                               Lamp *la = (Lamp *)data;
+                               AnimData *adt = la->adt;
                                
-                               ale->flag= FILTER_LAM_OBJD(la);
+                               ale->flag = FILTER_LAM_OBJD(la);
                                
-                               ale->key_data= (adt) ? adt->action : NULL;
-                               ale->datatype= ALE_ACT;
+                               ale->key_data = (adt) ? adt->action : NULL;
+                               ale->datatype = ALE_ACT;
                                
-                               ale->adt= BKE_animdata_from_id(data);
+                               ale->adt = BKE_animdata_from_id(data);
                        }
-                               break;
+                       break;
                        case ANIMTYPE_DSCAM:
                        {
-                               Camera *ca= (Camera *)data;
-                               AnimData *adt= ca->adt;
+                               Camera *ca = (Camera *)data;
+                               AnimData *adt = ca->adt;
                                
-                               ale->flag= FILTER_CAM_OBJD(ca);
+                               ale->flag = FILTER_CAM_OBJD(ca);
                                
-                               ale->key_data= (adt) ? adt->action : NULL;
-                               ale->datatype= ALE_ACT;
+                               ale->key_data = (adt) ? adt->action : NULL;
+                               ale->datatype = ALE_ACT;
                                
-                               ale->adt= BKE_animdata_from_id(data);
+                               ale->adt = BKE_animdata_from_id(data);
                        }
-                               break;
+                       break;
                        case ANIMTYPE_DSCUR:
                        {
-                               Curve *cu= (Curve *)data;
-                               AnimData *adt= cu->adt;
+                               Curve *cu = (Curve *)data;
+                               AnimData *adt = cu->adt;
                                
-                               ale->flag= FILTER_CUR_OBJD(cu);
+                               ale->flag = FILTER_CUR_OBJD(cu);
                                
-                               ale->key_data= (adt) ? adt->action : NULL;
-                               ale->datatype= ALE_ACT;
+                               ale->key_data = (adt) ? adt->action : NULL;
+                               ale->datatype = ALE_ACT;
                                
-                               ale->adt= BKE_animdata_from_id(data);
+                               ale->adt = BKE_animdata_from_id(data);
                        }
-                               break;
+                       break;
                        case ANIMTYPE_DSARM:
                        {
-                               bArmature *arm= (bArmature *)data;
-                               AnimData *adt= arm->adt;
+                               bArmature *arm = (bArmature *)data;
+                               AnimData *adt = arm->adt;
                                
-                               ale->flag= FILTER_ARM_OBJD(arm);
+                               ale->flag = FILTER_ARM_OBJD(arm);
                                
-                               ale->key_data= (adt) ? adt->action : NULL;
-                               ale->datatype= ALE_ACT;
+                               ale->key_data = (adt) ? adt->action : NULL;
+                               ale->datatype = ALE_ACT;
                                
-                               ale->adt= BKE_animdata_from_id(data);
+                               ale->adt = BKE_animdata_from_id(data);
                        }
-                               break;
+                       break;
                        case ANIMTYPE_DSMESH:
                        {
-                               Mesh *me= (Mesh *)data;
-                               AnimData *adt= me->adt;
+                               Mesh *me = (Mesh *)data;
+                               AnimData *adt = me->adt;
                                
-                               ale->flag= FILTER_MESH_OBJD(me);
+                               ale->flag = FILTER_MESH_OBJD(me);
                                
-                               ale->key_data= (adt) ? adt->action : NULL;
-                               ale->datatype= ALE_ACT;
+                               ale->key_data = (adt) ? adt->action : NULL;
+                               ale->datatype = ALE_ACT;
                                
-                               ale->adt= BKE_animdata_from_id(data);
+                               ale->adt = BKE_animdata_from_id(data);
                        }
-                               break;
+                       break;
                        case ANIMTYPE_DSLAT:
                        {
-                               Lattice *lt= (Lattice *)data;
-                               AnimData *adt= lt->adt;
+                               Lattice *lt = (Lattice *)data;
+                               AnimData *adt = lt->adt;
                                
-                               ale->flag= FILTER_LATTICE_OBJD(lt);
+                               ale->flag = FILTER_LATTICE_OBJD(lt);
                                
-                               ale->key_data= (adt) ? adt->action : NULL;
-                               ale->datatype= ALE_ACT;
+                               ale->key_data = (adt) ? adt->action : NULL;
+                               ale->datatype = ALE_ACT;
                                
-                               ale->adt= BKE_animdata_from_id(data);
+                               ale->adt = BKE_animdata_from_id(data);
                        }       
-                               break;
+                       break;
                        case ANIMTYPE_DSSPK:
                        {
-                               Speaker *spk= (Speaker *)data;
-                               AnimData *adt= spk->adt;
+                               Speaker *spk = (Speaker *)data;
+                               AnimData *adt = spk->adt;
  
-                               ale->flag= FILTER_SPK_OBJD(spk);
+                               ale->flag = FILTER_SPK_OBJD(spk);
  
-                               ale->key_data= (adt) ? adt->action : NULL;
-                               ale->datatype= ALE_ACT;
+                               ale->key_data = (adt) ? adt->action : NULL;
+                               ale->datatype = ALE_ACT;
  
-                               ale->adt= BKE_animdata_from_id(data);
+                               ale->adt = BKE_animdata_from_id(data);
                        }
-                               break;
+                       break;
                        case ANIMTYPE_DSSKEY:
                        {
-                               Key *key= (Key *)data;
-                               AnimData *adt= key->adt;
+                               Key *key = (Key *)data;
+                               AnimData *adt = key->adt;
                                
-                               ale->flag= FILTER_SKE_OBJD(key); 
+                               ale->flag = FILTER_SKE_OBJD(key);
                                
-                               ale->key_data= (adt) ? adt->action : NULL;
-                               ale->datatype= ALE_ACT;
+                               ale->key_data = (adt) ? adt->action : NULL;
+                               ale->datatype = ALE_ACT;
                                
-                               ale->adt= BKE_animdata_from_id(data);
+                               ale->adt = BKE_animdata_from_id(data);
                        }
-                               break;
+                       break;
                        case ANIMTYPE_DSWOR:
                        {
-                               World *wo= (World *)data;
-                               AnimData *adt= wo->adt;
+                               World *wo = (World *)data;
+                               AnimData *adt = wo->adt;
                                
-                               ale->flag= FILTER_WOR_SCED(wo); 
+                               ale->flag = FILTER_WOR_SCED(wo);
                                
-                               ale->key_data= (adt) ? adt->action : NULL;
-                               ale->datatype= ALE_ACT;
+                               ale->key_data = (adt) ? adt->action : NULL;
+                               ale->datatype = ALE_ACT;
                                
-                               ale->adt= BKE_animdata_from_id(data);
+                               ale->adt = BKE_animdata_from_id(data);
                        }
-                               break;
+                       break;
                        case ANIMTYPE_DSNTREE:
                        {
-                               bNodeTree *ntree= (bNodeTree *)data;
-                               AnimData *adt= ntree->adt;
+                               bNodeTree *ntree = (bNodeTree *)data;
+                               AnimData *adt = ntree->adt;
                                
-                               ale->flag= FILTER_NTREE_DATA(ntree); 
+                               ale->flag = FILTER_NTREE_DATA(ntree);
                                
-                               ale->key_data= (adt) ? adt->action : NULL;
-                               ale->datatype= ALE_ACT;
+                               ale->key_data = (adt) ? adt->action : NULL;
+                               ale->datatype = ALE_ACT;
                                
-                               ale->adt= BKE_animdata_from_id(data);
+                               ale->adt = BKE_animdata_from_id(data);
                        }
-                               break;
+                       break;
 +                      case ANIMTYPE_DSLINESTYLE:
 +                      {
 +                              FreestyleLineStyle *linestyle= (FreestyleLineStyle *)data;
 +                              AnimData *adt= linestyle->adt;
 +                              
 +                              ale->flag= FILTER_LS_SCED(linestyle); 
 +                              
 +                              ale->key_data= (adt) ? adt->action : NULL;
 +                              ale->datatype= ALE_ACT;
 +                              
 +                              ale->adt= BKE_animdata_from_id(data);
 +                      }
 +                              break;
                        case ANIMTYPE_DSPART:
                        {
-                               ParticleSettings *part= (ParticleSettings*)ale->data;
-                               AnimData *adt= part->adt;
+                               ParticleSettings *part = (ParticleSettings *)ale->data;
+                               AnimData *adt = part->adt;
                                
-                               ale->flag= FILTER_PART_OBJD(part); 
+                               ale->flag = FILTER_PART_OBJD(part);
                                
-                               ale->key_data= (adt) ? adt->action : NULL;
-                               ale->datatype= ALE_ACT;
+                               ale->key_data = (adt) ? adt->action : NULL;
+                               ale->datatype = ALE_ACT;
                                
-                               ale->adt= BKE_animdata_from_id(data);
+                               ale->adt = BKE_animdata_from_id(data);
                        }
-                               break;
+                       break;
                        case ANIMTYPE_DSTEX:
                        {
-                               Tex *tex= (Tex *)data;
-                               AnimData *adt= tex->adt;
+                               Tex *tex = (Tex *)data;
+                               AnimData *adt = tex->adt;
                                
-                               ale->flag= FILTER_TEX_DATA(tex); 
+                               ale->flag = FILTER_TEX_DATA(tex);
                                
-                               ale->key_data= (adt) ? adt->action : NULL;
-                               ale->datatype= ALE_ACT;
+                               ale->key_data = (adt) ? adt->action : NULL;
+                               ale->datatype = ALE_ACT;
                                
-                               ale->adt= BKE_animdata_from_id(data);
+                               ale->adt = BKE_animdata_from_id(data);
                        }
-                               break;
+                       break;
                                
                        case ANIMTYPE_GROUP:
                        {
@@@ -1393,60 -1389,12 +1403,60 @@@ static size_t animdata_filter_ds_nodetr
        return items;
  }
  
 +static size_t animdata_filter_ds_linestyle (bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Scene *sce, int filter_mode)
 +{
 +      SceneRenderLayer *srl;
 +      size_t items = 0;
 +
 +      for (srl= sce->r.layers.first; srl; srl= srl->next) {
 +              FreestyleLineSet *lineset;
 +
 +              /* skip render layers without Freestyle enabled */
 +              if (!(srl->layflag & SCE_LAY_FRS))
 +                      continue;
 +
 +              /* loop over linesets defined in the render layer */
 +              for (lineset= srl->freestyleConfig.linesets.first; lineset; lineset= lineset->next) {
 +                      FreestyleLineStyle *linestyle = lineset->linestyle;
 +                      ListBase tmp_data = {NULL, NULL};
 +                      size_t tmp_items = 0;
 +
 +                      /* add scene-level animation channels */
 +                      BEGIN_ANIMFILTER_SUBCHANNELS(FILTER_LS_SCED(linestyle))
 +                      {
 +                              /* animation data filtering */
 +                              tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)linestyle, filter_mode);
 +                      }
 +                      END_ANIMFILTER_SUBCHANNELS;
 +
 +                      /* did we find anything? */
 +                      if (tmp_items) {
 +                              /* include anim-expand widget first */
 +                              if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
 +                                      /* check if filtering by active status */
 +                                      if ANIMCHANNEL_ACTIVEOK(linestyle) {
 +                                              ANIMCHANNEL_NEW_CHANNEL(linestyle, ANIMTYPE_DSLINESTYLE, sce);
 +                                      }
 +                              }
 +                              
 +                              /* now add the list of collected channels */
 +                              BLI_movelisttolist(anim_data, &tmp_data);
 +                              BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
 +                              items += tmp_items;
 +                      }
 +              }
 +      }
 +      
 +      /* return the number of items added to the list */
 +      return items;
 +}
 +
  /* NOTE: owner_id is either material, lamp, or world block, which is the direct owner of the texture stack in question */
- static size_t animdata_filter_ds_textures (bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, ID *owner_id, int filter_mode)
+ static size_t animdata_filter_ds_textures(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, ID *owner_id, int filter_mode)
  {
        MTex **mtex = NULL;
-       size_t items=0;
-       int a=0;
+       size_t items = 0;
+       int a = 0;
        
        /* get datatype specific data first */
        if (owner_id == NULL)
@@@ -184,8 -181,7 +182,8 @@@ void MESH_OT_rip(struct wmOperatorType 
  
  void MESH_OT_shape_propagate_to_all(struct wmOperatorType *ot);
  void MESH_OT_blend_from_shape(struct wmOperatorType *ot);
- void MESH_OT_sort_faces(struct wmOperatorType *ot);
+ void MESH_OT_sort_elements(struct wmOperatorType *ot);
 +void MESH_OT_mark_freestyle_face(struct wmOperatorType *ot);
  
  /* ******************* mesh_data.c */
  
@@@ -118,8 -116,7 +116,8 @@@ void ED_operatortypes_mesh(void
        WM_operatortype_append(MESH_OT_dissolve_limited);
        WM_operatortype_append(MESH_OT_faces_shade_smooth);
        WM_operatortype_append(MESH_OT_faces_shade_flat);
-       WM_operatortype_append(MESH_OT_sort_faces);
+       WM_operatortype_append(MESH_OT_sort_elements);
 +      WM_operatortype_append(MESH_OT_mark_freestyle_face);
  
        WM_operatortype_append(MESH_OT_delete);
        WM_operatortype_append(MESH_OT_edge_collapse);
@@@ -368,12 -368,8 +368,12 @@@ static void buttons_area_listener(ScrAr
                /* Listener for preview render, when doing an global undo. */
                case NC_WINDOW:
                        ED_area_tag_redraw(sa);
-                       sbuts->preview= 1;
+                       sbuts->preview = 1;
                        break;
 +              case NC_LINESTYLE:
 +                      ED_area_tag_redraw(sa);
 +                      sbuts->preview= 1;
 +                      break;
        }
  
        if (wmn->data == ND_KEYS)
index adb46d8,0000000..f8c87fd
mode 100644,000000..100644
--- /dev/null
@@@ -1,356 -1,0 +1,356 @@@
-       freestyle_scene = add_scene(name);
 +# include "BlenderStrokeRenderer.h"
 +# include "../stroke/Canvas.h"
 +# include "../application/AppConfig.h"
 +
 +# include "BlenderTextureManager.h"
 +
 +#ifdef __cplusplus
 +extern "C" {
 +#endif
 +
 +#include "MEM_guardedalloc.h"
 +
 +#include "DNA_camera_types.h"
 +#include "DNA_customdata_types.h"
 +#include "DNA_listBase.h"
 +#include "DNA_mesh_types.h"
 +#include "DNA_meshdata_types.h"
 +#include "DNA_object_types.h"
 +#include "DNA_screen_types.h"
 +
 +#include "BKE_customdata.h"
 +#include "BKE_global.h"
 +#include "BKE_library.h" /* free_libblock */
 +#include "BKE_material.h"
 +#include "BKE_main.h" /* struct Main */
 +#include "BKE_object.h"
 +#include "BKE_scene.h"
 +
 +#include "RE_pipeline.h"
 +
 +#ifdef __cplusplus
 +}
 +#endif
 +
 +
 +BlenderStrokeRenderer::BlenderStrokeRenderer(Render* re, int render_count)
 +:StrokeRenderer(){
 +      
 +      // TEMPORARY - need a  texture manager
 +      _textureManager = new BlenderTextureManager;
 +      _textureManager->load();
 +
 +      _width = re->winx; _height = re->winy; // for stroke mesh generation
 +
 +      // Scene.New("FreestyleStrokes")
 +      old_scene = re->scene;
 +
 +      char name[22];
 +      snprintf(name, sizeof(name), "FRS%d_%s", render_count, re->scene->id.name+2);
-       set_scene_bg( G.main, freestyle_scene );
++      freestyle_scene = BKE_scene_add(name);
 +      freestyle_scene->r.cfra = old_scene->r.cfra;
 +      freestyle_scene->r.mode= old_scene->r.mode &
 +              ~( R_EDGE_FRS | R_SHADOW | R_SSS | R_PANORAMA | R_ENVMAP | R_MBLUR | R_BORDER );
 +      freestyle_scene->r.xsch= re->rectx; // old_scene->r.xsch
 +      freestyle_scene->r.ysch= re->recty; // old_scene->r.ysch
 +      freestyle_scene->r.xasp= 1.f; // old_scene->r.xasp;
 +      freestyle_scene->r.yasp= 1.f; // old_scene->r.yasp;
 +      freestyle_scene->r.xparts= old_scene->r.xparts;
 +      freestyle_scene->r.yparts= old_scene->r.yparts;
 +      freestyle_scene->r.size= 100; // old_scene->r.size
 +      freestyle_scene->r.maximsize= old_scene->r.maximsize;
 +      freestyle_scene->r.ocres = old_scene->r.ocres;
 +      freestyle_scene->r.color_mgt_flag = 0; // old_scene->r.color_mgt_flag;
 +      freestyle_scene->r.scemode= old_scene->r.scemode & ~( R_SINGLE_LAYER );
 +      freestyle_scene->r.flag= old_scene->r.flag;
 +      freestyle_scene->r.threads= old_scene->r.threads;
 +      freestyle_scene->r.border.xmin= old_scene->r.border.xmin;
 +      freestyle_scene->r.border.ymin= old_scene->r.border.ymin;
 +      freestyle_scene->r.border.xmax= old_scene->r.border.xmax;
 +      freestyle_scene->r.border.ymax= old_scene->r.border.ymax;
 +      strcpy(freestyle_scene->r.pic, old_scene->r.pic);
 +      freestyle_scene->r.safety.xmin= old_scene->r.safety.xmin;
 +      freestyle_scene->r.safety.ymin= old_scene->r.safety.ymin;
 +      freestyle_scene->r.safety.xmax= old_scene->r.safety.xmax;
 +      freestyle_scene->r.safety.ymax= old_scene->r.safety.ymax;
 +      freestyle_scene->r.osa= old_scene->r.osa;
 +      freestyle_scene->r.filtertype= old_scene->r.filtertype;
 +      freestyle_scene->r.gauss= old_scene->r.gauss;
 +      freestyle_scene->r.dither_intensity= old_scene->r.dither_intensity;
 +      BLI_strncpy(freestyle_scene->r.engine, old_scene->r.engine, sizeof(freestyle_scene->r.engine));
 +      freestyle_scene->r.im_format.planes = R_IMF_PLANES_RGBA; 
 +      freestyle_scene->r.im_format.imtype = R_IMF_IMTYPE_PNG;
-       Object* object_camera = add_object(freestyle_scene, OB_CAMERA);
++      BKE_scene_set_background( G.main, freestyle_scene );
 +
 +      // Camera
-       material = add_material("stroke_material");
++      Object* object_camera = BKE_object_add(freestyle_scene, OB_CAMERA);
 +      
 +      Camera* camera = (Camera *) object_camera->data;
 +      camera->type = CAM_ORTHO;
 +      camera->ortho_scale = max(re->rectx, re->recty);
 +    camera->clipsta = 0.1f;
 +    camera->clipend = 100.0f;
 +
 +    _z_delta = 0.00001f;
 +    _z = camera->clipsta + _z_delta;
 +
 +    // test
 +    //_z = 999.90f; _z_delta = 0.01f;
 +
 +      object_camera->loc[0] = re->disprect.xmin + 0.5f * re->rectx;
 +      object_camera->loc[1] = re->disprect.ymin + 0.5f * re->recty;
 +      object_camera->loc[2] = 1.f;
 +      
 +      freestyle_scene->camera = object_camera;
 +      
 +      // Material
-                       free_libblock( &G.main->object, ob );
-                       free_libblock( &G.main->mesh, data );
++      material = BKE_material_add("stroke_material");
 +      material->mode |= MA_VERTEXCOLP;
 +      material->mode |= MA_TRANSP;
 +      material->mode |= MA_SHLESS;
 +      material->vcol_alpha = 1;
 +}
 +
 +BlenderStrokeRenderer::~BlenderStrokeRenderer(){
 +      
 +      if(0 != _textureManager)
 +      {
 +              delete _textureManager;
 +              _textureManager = 0;
 +      }
 +
 +      // The freestyle_scene object is not released here.  Instead,
 +      // the scene is released in free_all_freestyle_renders() in
 +      // source/blender/render/intern/source/pipeline.c, after the
 +      // compositor has finished.
 +
 +      // release objects and data blocks
 +      Base *b = (Base *)freestyle_scene->base.first;
 +      while(b) {
 +              Object *ob = b->object;
 +              void *data = ob->data;
 +              char name[24];
 +              strcpy(name, ob->id.name);
 +              //cout << "removing " << name[0] << name[1] << ":" << (name+2) << endl;
 +              switch (ob->type) {
 +              case OB_MESH:
-                       free_libblock( &G.main->object, ob );
-                       free_libblock( &G.main->camera, data );
++                      BKE_libblock_free( &G.main->object, ob );
++                      BKE_libblock_free( &G.main->mesh, data );
 +                      break;
 +              case OB_CAMERA:
-       free_libblock( &G.main->mat, material );
++                      BKE_libblock_free( &G.main->object, ob );
++                      BKE_libblock_free( &G.main->camera, data );
 +                      freestyle_scene->camera = NULL;
 +                      break;
 +              default:
 +                      cerr << "Warning: unexpected object in the scene: " << name[0] << name[1] << ":" << (name+2) << endl;
 +              }
 +              b = b->next;
 +      }
 +      BLI_freelistN( &freestyle_scene->base );
 +
 +      // release material
-       set_scene_bg( G.main, old_scene );
++      BKE_libblock_free( &G.main->mat, material );
 +      
-               Object* object_mesh = add_object(freestyle_scene, OB_MESH);
++      BKE_scene_set_background( G.main, old_scene );
 +}
 +
 +float BlenderStrokeRenderer::get_stroke_vertex_z(void) const {
 +    float z = _z;
 +    BlenderStrokeRenderer *self = const_cast<BlenderStrokeRenderer *>(this);
 +    if (!(_z < _z_delta * 100000.0f))
 +        self->_z_delta *= 10.0f;
 +    self->_z += _z_delta;
 +    return -z;
 +}
 +
 +void BlenderStrokeRenderer::RenderStrokeRep(StrokeRep *iStrokeRep) const{
 +  RenderStrokeRepBasic(iStrokeRep);
 +}
 +
 +void BlenderStrokeRenderer::RenderStrokeRepBasic(StrokeRep *iStrokeRep) const{
 +      
 +      ////////////////////
 +      //  Build up scene
 +      ////////////////////
 +      
 +        vector<Strip*>& strips = iStrokeRep->getStrips();
 +        Strip::vertex_container::iterator v[3];
 +        StrokeVertexRep *svRep[3];
 +        Vec3r color[3];
 +        unsigned int vertex_index;
 +        Vec2r p;
 +      
 +        for(vector<Strip*>::iterator s=strips.begin(), send=strips.end();
 +        s!=send;
 +        ++s){         
 +              
 +          Strip::vertex_container& strip_vertices = (*s)->vertices();
 +              int strip_vertex_count = (*s)->sizeStrip();
 +              int m, n, visible_faces, visible_segments;
 +              bool visible;
 +
 +              // iterate over all vertices and count visible faces and strip segments
 +              // (note: a strip segment is a series of visible faces, while two strip
 +              // segments are separated by one or more invisible faces)
 +              v[0] = strip_vertices.begin();
 +          v[1] = v[0]; ++(v[1]);
 +          v[2] = v[1]; ++(v[2]);
 +              visible_faces = visible_segments = 0;
 +              visible = false;
 +          for (n = 2; n < strip_vertex_count; n++)
 +              {
 +                      svRep[0] = *(v[0]);
 +                      svRep[1] = *(v[1]);
 +                      svRep[2] = *(v[2]);
 +                      m = 0;
 +                      for (int j = 0; j < 3; j++) {
 +                              p = svRep[j]->point2d();
 +                              if (p[0] < 0.0 || p[0] > _width || p[1] < 0.0 || p[1] > _height)
 +                                      m++;
 +                      }
 +                      if (m == 3) {
 +                              visible = false;
 +                      } else {
 +                              visible_faces++;
 +                              if (!visible)
 +                                      visible_segments++;
 +                              visible = true;
 +                      }
 +                      ++v[0]; ++v[1]; ++v[2];
 +              }
 +              if (visible_faces == 0)
 +                      continue;
 +
 +              // me = Mesh.New()
-               MEM_freeN(mesh->bb);
-               mesh->bb= NULL;
-               mesh->id.us = 0;
++              Object* object_mesh = BKE_object_add(freestyle_scene, OB_MESH);
 +              Mesh* mesh = (Mesh *) object_mesh->data;
++              // MEM_freeN(mesh->bb);
++              // mesh->bb= NULL;
++              // mesh->id.us = 0;
 +              
 +#if 1
 +              // me.materials = [mat]
 +              mesh->mat = ( Material ** ) MEM_mallocN( 1 * sizeof( Material * ), "MaterialList" );
 +              mesh->mat[0] = material;
 +              mesh->totcol = 1;
 +              test_object_materials( (ID*) mesh );
 +#else
 +              assign_material(object_mesh, material, object_mesh->totcol+1);
 +              object_mesh->actcol= object_mesh->totcol;
 +#endif
 +              
 +              // vertices allocation
 +              mesh->totvert = visible_faces + visible_segments * 2;
 +              mesh->mvert = (MVert*) CustomData_add_layer( &mesh->vdata, CD_MVERT, CD_CALLOC, NULL, mesh->totvert);
 +                      
 +              // faces allocation
 +              mesh->totface = visible_faces;
 +              mesh->mface = (MFace*) CustomData_add_layer( &mesh->fdata, CD_MFACE, CD_CALLOC, NULL, mesh->totface);
 +              
 +              // colors allocation  - me.vertexColors = True
 +              mesh->mcol = (MCol *) CustomData_add_layer( &mesh->fdata, CD_MCOL, CD_CALLOC, NULL, mesh->totface );
 +
 +              ////////////////////
 +              //  Data copy
 +              ////////////////////
 +              
 +              MVert* vertices = mesh->mvert;
 +              MFace* faces = mesh->mface;
 +              MCol* colors = mesh->mcol;
 +              
 +          v[0] = strip_vertices.begin();
 +          v[1] = v[0]; ++(v[1]);
 +          v[2] = v[1]; ++(v[2]);
 +
 +              vertex_index = 0;
 +              visible = false;
 +              
 +          for (n = 2; n < strip_vertex_count; n++)
 +              {
 +                      svRep[0] = *(v[0]);
 +                      svRep[1] = *(v[1]);
 +                      svRep[2] = *(v[2]);
 +                      m = 0;
 +                      for (int j = 0; j < 3; j++) {
 +                              p = svRep[j]->point2d();
 +                              if (p[0] < 0.0 || p[0] > _width || p[1] < 0.0 || p[1] > _height)
 +                                      m++;
 +                      }
 +                      if (m == 3) {
 +                              visible = false;
 +                      } else {
 +                              if (!visible) {
 +                                      vertex_index += 2;
 +
 +                                      // first vertex
 +                                      vertices->co[0] = svRep[0]->point2d()[0];
 +                                      vertices->co[1] = svRep[0]->point2d()[1];
 +                                      vertices->co[2] = get_stroke_vertex_z();
 +                                      ++vertices;
 +                                      
 +                                      // second vertex
 +                                      vertices->co[0] = svRep[1]->point2d()[0];
 +                                      vertices->co[1] = svRep[1]->point2d()[1];
 +                                      vertices->co[2] = get_stroke_vertex_z();
 +                                      ++vertices;
 +                              }
 +                              visible = true;
 +
 +                              // vertex
 +                              vertices->co[0] = svRep[2]->point2d()[0];
 +                              vertices->co[1] = svRep[2]->point2d()[1];
 +                              vertices->co[2] = get_stroke_vertex_z();
 +                              
 +                              // faces
 +                              faces->v1 = vertex_index - 2;
 +                              faces->v2 = vertex_index - 1;
 +                              faces->v3 = vertex_index;
 +                              faces->v4 = 0;
 +                              
 +                              // colors
 +                              // red and blue are swapped - cf DNA_meshdata_types.h : MCol    
 +                              color[0] = svRep[0]->color();
 +                              color[1] = svRep[1]->color();
 +                              color[2] = svRep[2]->color();
 +
 +                              colors->r = (short)(255.0f*(color[0])[2]);
 +                              colors->g = (short)(255.0f*(color[0])[1]);
 +                              colors->b = (short)(255.0f*(color[0])[0]);
 +                              colors->a = (short)(255.0f*svRep[0]->alpha());
 +                              ++colors;
 +                              
 +                              colors->r = (short)(255.0f*(color[1])[2]);
 +                              colors->g = (short)(255.0f*(color[1])[1]);
 +                              colors->b = (short)(255.0f*(color[1])[0]);
 +                              colors->a = (short)(255.0f*svRep[1]->alpha());
 +                              ++colors;
 +                              
 +                              colors->r = (short)(255.0f*(color[2])[2]);
 +                              colors->g = (short)(255.0f*(color[2])[1]);
 +                              colors->b = (short)(255.0f*(color[2])[0]);
 +                              colors->a = (short)(255.0f*svRep[2]->alpha());
 +                              ++colors;
 +
 +                              ++faces; ++vertices; ++colors;
 +                              ++vertex_index;
 +                      }
 +                      ++v[0]; ++v[1]; ++v[2];
 +
 +              } // loop over strip vertices 
 +      
 +      } // loop over strips   
 +
 +}
 +
 +Render* BlenderStrokeRenderer::RenderScene( Render *re ) {
 +    Camera *camera = (Camera *)freestyle_scene->camera->data;
 +    if (camera->clipend < _z)
 +        camera->clipend = _z + _z_delta * 100.0f;
 +    //cout << "clipsta " << camera->clipsta << ", clipend " << camera->clipend << endl;
 +
 +      Render *freestyle_render = RE_NewRender(freestyle_scene->id.name);
 +
 +      RE_RenderFreestyleStrokes(freestyle_render, G.main, freestyle_scene);
 +      return freestyle_render;
 +}
index 2788072,0000000..93b5f69
mode 100644,000000..100644
--- /dev/null
@@@ -1,39 -1,0 +1,39 @@@
-               unlink_text(G.main, _text);
-               free_libblock(&G.main->text, _text);
 +#ifndef BLENDERSTYLEMODULE_H
 +#define BLENDERSTYLEMODULE_H
 +
 +#include "../stroke/StyleModule.h"
 +#include "../system/PythonInterpreter.h"
 +
 +extern "C" {
 +#include "BKE_global.h"
 +#include "BKE_library.h"
 +#include "BKE_text.h"
 +}
 +
 +class BlenderStyleModule : public StyleModule
 +{
 +public:       
 +
 +      BlenderStyleModule(struct Text *text, const string &name,
 +              Interpreter *inter) : StyleModule(name, inter) {
 +              _text = text;
 +      }
 +
 +      virtual ~BlenderStyleModule() {
++              BKE_text_unlink(G.main, _text);
++              BKE_libblock_free(&G.main->text, _text);
 +      }
 +
 +protected:
 +
 +      virtual int interpret() {
 +              PythonInterpreter* py_inter = dynamic_cast<PythonInterpreter*>(_inter);
 +              assert(py_inter != 0);
 +              return py_inter->interpretText(_text, getFileName());
 +      }
 +
 +private:
 +      struct Text *_text;
 +};
 +
 +#endif // BLENDERSTYLEMODULE_H
index fbcea9c,0000000..c9a0501
mode 100644,000000..100644
--- /dev/null
@@@ -1,736 -1,0 +1,736 @@@
-               Text *text= add_empty_text(lineset_name);
-               write_text(text, "import parameter_editor; parameter_editor.process('");
-               write_text(text, s1);
-               write_text(text, "', '");
-               write_text(text, s2);
-               write_text(text, "')\n");
 +#include "../application/Controller.h"
 +#include "../application/AppView.h"
 +#include "../application/AppConfig.h"
 +#include "../application/AppCanvas.h"
 +
 +#include <iostream>
 +#include <map>
 +#include <set>
 +using namespace std;
 +
 +#ifdef __cplusplus
 +extern "C" {
 +#endif
 +
 +#include "MEM_guardedalloc.h"
 +
 +#include "DNA_camera_types.h"
 +#include "DNA_text_types.h"
 +#include "DNA_group_types.h"
 +#include "DNA_freestyle_types.h"
 +
 +#include "BKE_global.h"
 +#include "BKE_library.h"
 +#include "BKE_linestyle.h"
 +#include "BKE_main.h"
 +#include "BKE_text.h"
 +#include "BLI_blenlib.h"
 +#include "BLI_math.h"
 +#include "BPY_extern.h"
 +
 +#include "renderpipeline.h"
 +#include "pixelblending.h"
 +
 +#include "../../FRS_freestyle.h"
 +#include "../../FRS_freestyle_config.h"
 +
 +#define DEFAULT_SPHERE_RADIUS  1.0f
 +#define DEFAULT_DKR_EPSILON    0.0f
 +
 +      // Freestyle configuration
 +      static short freestyle_is_initialized = 0;
 +      static Config::Path *pathconfig = NULL;
 +      static Controller *controller = NULL;
 +      static AppView *view = NULL;
 +
 +      // line set buffer for copy & paste
 +      static FreestyleLineSet lineset_buffer;
 +      static bool lineset_copied = false;
 +
 +      // camera information
 +      float freestyle_viewpoint[3];
 +      float freestyle_mv[4][4];
 +      float freestyle_proj[4][4];
 +      int freestyle_viewport[4];
 +      
 +      // current scene
 +      Scene *freestyle_scene;
 +
 +      string default_module_path;
 +
 +      //=======================================================
 +      //   Initialization 
 +      //=======================================================
 +
 +      void FRS_initialize() {
 +              
 +              if( freestyle_is_initialized )
 +                      return;
 +
 +              pathconfig = new Config::Path;
 +              controller = new Controller();
 +              view = new AppView;
 +              controller->setView(view);
 +              controller->Clear();
 +              freestyle_scene = NULL;
 +              lineset_copied = false;
 +                      
 +              default_module_path = pathconfig->getProjectDir() + Config::DIR_SEP + "style_modules" + Config::DIR_SEP + "contour.py";
 +                      
 +              freestyle_is_initialized = 1;
 +      }
 +      
 +      void FRS_set_context(bContext* C) {
 +              cout << "FRS_set_context: context 0x" << C << " scene 0x" << CTX_data_scene(C) << endl;
 +              controller->setContext(C);
 +      }
 +
 +      void FRS_read_file(bContext* C) {
 +              lineset_copied = false;
 +      }
 +
 +      void FRS_exit() {
 +              delete pathconfig;
 +              delete controller;
 +              delete view;
 +      }
 +
 +      //=======================================================
 +      //   Rendering 
 +      //=======================================================
 +
 +      static void init_view(Render* re){
 +              int width = re->winx;
 +              int height = re->winy;
 +              int xmin = re->disprect.xmin;
 +              int ymin = re->disprect.ymin;
 +              int xmax = re->disprect.xmax;
 +              int ymax = re->disprect.ymax;
 +              
 +              float thickness = 1.f;
 +              switch (re->r.line_thickness_mode) {
 +              case R_LINE_THICKNESS_ABSOLUTE:
 +                      thickness = re->r.unit_line_thickness * (re->r.size / 100.f);
 +                      break;
 +              case R_LINE_THICKNESS_RELATIVE:
 +                      thickness = height / 480.f;
 +                      break;
 +              }
 +
 +              freestyle_viewport[0] = freestyle_viewport[1] = 0;
 +              freestyle_viewport[2] = width;
 +              freestyle_viewport[3] = height;
 +              
 +              view->setWidth( width );
 +              view->setHeight( height );
 +              view->setBorder( xmin, ymin, xmax, ymax );
 +              view->setThickness( thickness );
 +
 +              cout << "\n===  Dimensions of the 2D image coordinate system  ===" << endl;
 +              cout << "Width  : " << width << endl;
 +              cout << "Height : " << height << endl;
 +              if (re->r.mode & R_BORDER)
 +                      cout << "Border : (" << xmin << ", " << ymin << ") - (" << xmax << ", " << ymax << ")" << endl;
 +              cout << "Unit line thickness : " << thickness << " pixel(s)" << endl;
 +      }
 +
 +      static void init_camera(Render* re){
 +              // It is assumed that imported meshes are in the camera coordinate system.
 +              // Therefore, the view point (i.e., camera position) is at the origin, and
 +              // the the model-view matrix is simply the identity matrix.
 +
 +              freestyle_viewpoint[0] = 0.0;
 +              freestyle_viewpoint[1] = 0.0;
 +              freestyle_viewpoint[2] = 0.0;
 +              
 +              for( int i = 0; i < 4; i++ )
 +                 for( int j = 0; j < 4; j++ )
 +                      freestyle_mv[i][j] = (i == j) ? 1.0 : 0.0;
 +              
 +              for( int i = 0; i < 4; i++ )
 +                 for( int j = 0; j < 4; j++ )
 +                      freestyle_proj[i][j] = re->winmat[i][j];
 +
 +              //print_m4("mv", freestyle_mv);
 +              //print_m4("proj", freestyle_proj);
 +      }
 +
 +      static char *escape_quotes(char *name)
 +      {
 +              char *s= (char *)MEM_mallocN(strlen(name) * 2 + 1, "escape_quotes");
 +              char *p= s;
 +              while (*name) {
 +                      if (*name == '\'')
 +                              *p++= '\\';
 +                      *p++ = *name++;
 +              }
 +              *p = '\0';
 +              return s;
 +      }
 +
 +      static Text *create_lineset_handler(char *layer_name, char *lineset_name)
 +      {
 +              char *s1= escape_quotes(layer_name);
 +              char *s2= escape_quotes(lineset_name);
++              Text *text= BKE_text_add(lineset_name);
++              BKE_text_write(text, "import parameter_editor; parameter_editor.process('");
++              BKE_text_write(text, s1);
++              BKE_text_write(text, "', '");
++              BKE_text_write(text, s2);
++              BKE_text_write(text, "')\n");
 +              MEM_freeN(s1);
 +              MEM_freeN(s2);
 +              return text;
 +      }
 +
 +      struct edge_type_condition {
 +              int edge_type, value;
 +      };
 +
 +      // examines the conditions and returns true if the target edge type needs to be computed
 +      static bool test_edge_type_conditions(struct edge_type_condition *conditions,
 +              int num_edge_types, bool logical_and, int target, bool distinct)
 +      {
 +              int target_condition = 0;
 +              int num_non_target_positive_conditions = 0;
 +              int num_non_target_negative_conditions = 0;
 +
 +              for (int i = 0; i < num_edge_types; i++) {
 +                      if (conditions[i].edge_type == target)
 +                              target_condition = conditions[i].value;
 +                      else if (conditions[i].value > 0)
 +                              ++num_non_target_positive_conditions;
 +                      else if (conditions[i].value < 0)
 +                              ++num_non_target_negative_conditions;
 +              }
 +              if (distinct) {
 +                      // In this case, the 'target' edge type is assumed to appear on distinct edge
 +                      // of its own and never together with other edge types.
 +                      if (logical_and) {
 +                              if (num_non_target_positive_conditions > 0)
 +                                      return false;
 +                              if (target_condition > 0)
 +                                      return true;
 +                              if (target_condition < 0)
 +                                      return false;
 +                              if (num_non_target_negative_conditions > 0)
 +                                      return true;
 +                      } else {
 +                              if (target_condition > 0)
 +                                      return true;
 +                              if (num_non_target_negative_conditions > 0)
 +                                      return true;
 +                              if (target_condition < 0)
 +                                      return false;
 +                              if (num_non_target_positive_conditions > 0)
 +                                      return false;
 +                      }
 +              } else {
 +                      // In this case, the 'target' edge type may appear together with other edge types.
 +                      if (target_condition > 0)
 +                              return true;
 +                      if (target_condition < 0)
 +                              return true;
 +                      if (logical_and) {
 +                              if (num_non_target_positive_conditions > 0)
 +                                      return false;
 +                              if (num_non_target_negative_conditions > 0)
 +                                      return true;
 +                      } else {
 +                              if (num_non_target_negative_conditions > 0)
 +                                      return true;
 +                              if (num_non_target_positive_conditions > 0)
 +                                      return false;
 +                      }
 +              }
 +              return true;
 +      }
 +
 +      static void prepare(Render* re, SceneRenderLayer* srl ) {
 +                              
 +              // load mesh
 +        re->i.infostr= "Freestyle: Mesh loading";
 +              re->stats_draw(re->sdh, &re->i);
 +        re->i.infostr= NULL;
 +              if( controller->LoadMesh(re, srl) ) // returns if scene cannot be loaded or if empty
 +                      return;
 +        if( re->test_break(re->tbh) )
 +            return;
 +              
 +              // add style modules
 +              FreestyleConfig* config = &srl->freestyleConfig;
 +              
 +              cout << "\n===  Rendering options  ===" << endl;
 +              int layer_count = 0;
 +              
 +              switch (config->mode) {
 +              case FREESTYLE_CONTROL_SCRIPT_MODE:
 +                      cout << "Modules :"<< endl;
 +                      for (FreestyleModuleConfig* module_conf = (FreestyleModuleConfig *)config->modules.first; module_conf; module_conf = module_conf->next) {
 +                              if( module_conf->is_displayed ) {
 +                                      cout << "  " << layer_count+1 << ": " << module_conf->module_path << endl;
 +                                      controller->InsertStyleModule( layer_count, module_conf->module_path );
 +                                      controller->toggleLayer(layer_count, true);
 +                                      layer_count++;
 +                              }
 +                      }
 +                      cout << endl;
 +                      controller->setComputeRidgesAndValleysFlag( (config->flags & FREESTYLE_RIDGES_AND_VALLEYS_FLAG) ? true : false);
 +                      controller->setComputeSuggestiveContoursFlag( (config->flags & FREESTYLE_SUGGESTIVE_CONTOURS_FLAG) ? true : false);
 +                      controller->setComputeMaterialBoundariesFlag( (config->flags & FREESTYLE_MATERIAL_BOUNDARIES_FLAG) ? true : false);
 +                      break;
 +              case FREESTYLE_CONTROL_EDITOR_MODE:
 +                      int use_ridges_and_valleys = 0;
 +                      int use_suggestive_contours = 0;
 +                      int use_material_boundaries = 0;
 +                      struct edge_type_condition conditions[] = {
 +                              {FREESTYLE_FE_SILHOUETTE, 0},
 +                              {FREESTYLE_FE_BORDER, 0},
 +                              {FREESTYLE_FE_CREASE, 0},
 +                              {FREESTYLE_FE_RIDGE_VALLEY, 0},
 +                              {FREESTYLE_FE_SUGGESTIVE_CONTOUR, 0},
 +                              {FREESTYLE_FE_MATERIAL_BOUNDARY, 0},
 +                              {FREESTYLE_FE_CONTOUR, 0},
 +                              {FREESTYLE_FE_EXTERNAL_CONTOUR, 0},
 +                              {FREESTYLE_FE_EDGE_MARK, 0}};
 +                      int num_edge_types = sizeof(conditions) / sizeof(struct edge_type_condition);
 +                      cout << "Linesets:"<< endl;
 +                      for (FreestyleLineSet *lineset = (FreestyleLineSet *)config->linesets.first; lineset; lineset = lineset->next) {
 +                              if (lineset->flags & FREESTYLE_LINESET_ENABLED) {
 +                                      cout << "  " << layer_count+1 << ": " << lineset->name << " - " << lineset->linestyle->id.name+2 << endl;
 +                                      Text *text = create_lineset_handler(srl->name, lineset->name);
 +                                      controller->InsertStyleModule( layer_count, lineset->name, text );
 +                                      controller->toggleLayer(layer_count, true);
 +                                      if (!(lineset->selection & FREESTYLE_SEL_EDGE_TYPES) || !lineset->edge_types) {
 +                                              ++use_ridges_and_valleys;
 +                                              ++use_suggestive_contours;
 +                                              ++use_material_boundaries;
 +                                      } else {
 +                                              // conditions for feature edge selection by edge types
 +                                              for (int i = 0; i < num_edge_types; i++) {
 +                                                      if (!(lineset->edge_types & conditions[i].edge_type))
 +                                                              conditions[i].value = 0; // no condition specified
 +                                                      else if (!(lineset->exclude_edge_types & conditions[i].edge_type))
 +                                                              conditions[i].value = 1; // condition: X
 +                                                      else
 +                                                              conditions[i].value = -1; // condition: NOT X
 +                                              }
 +                                              // logical operator for the selection conditions
 +                                              bool logical_and = ((lineset->flags & FREESTYLE_LINESET_FE_AND) != 0);
 +                                              // negation operator
 +                                              if (lineset->flags & FREESTYLE_LINESET_FE_NOT) {
 +                                                      // convert an Exclusive condition into an Inclusive equivalent using De Morgan's laws:
 +                                                      //   NOT (X OR Y) --> (NOT X) AND (NOT Y)
 +                                                      //   NOT (X AND Y) --> (NOT X) OR (NOT Y)
 +                                                      for (int i = 0; i < num_edge_types; i++)
 +                                                              conditions[i].value *= -1;
 +                                                      logical_and = !logical_and;
 +                                              }
 +                                              if (test_edge_type_conditions(conditions, num_edge_types, logical_and, FREESTYLE_FE_RIDGE_VALLEY, true))
 +                                                      ++use_ridges_and_valleys;
 +                                              if (test_edge_type_conditions(conditions, num_edge_types, logical_and, FREESTYLE_FE_SUGGESTIVE_CONTOUR, true))
 +                                                      ++use_suggestive_contours;
 +                                              if (test_edge_type_conditions(conditions, num_edge_types, logical_and, FREESTYLE_FE_MATERIAL_BOUNDARY, true))
 +                                                      ++use_material_boundaries;
 +                                      }
 +                                      layer_count++;
 +                              }
 +                      }
 +                      controller->setComputeRidgesAndValleysFlag( use_ridges_and_valleys > 0 );
 +                      controller->setComputeSuggestiveContoursFlag( use_suggestive_contours > 0 );
 +                      controller->setComputeMaterialBoundariesFlag( use_material_boundaries > 0 );
 +                      break;
 +              }
 +              
 +              // set parameters
 +              if (config->mode == FREESTYLE_CONTROL_SCRIPT_MODE && (config->flags & FREESTYLE_ADVANCED_OPTIONS_FLAG)) {
 +                      controller->setSphereRadius( config->sphere_radius );
 +                      controller->setSuggestiveContourKrDerivativeEpsilon( config->dkr_epsilon );
 +              } else {
 +                      controller->setSphereRadius( DEFAULT_SPHERE_RADIUS );
 +                      controller->setSuggestiveContourKrDerivativeEpsilon( DEFAULT_DKR_EPSILON );
 +              }
 +              controller->setFaceSmoothness( (config->flags & FREESTYLE_FACE_SMOOTHNESS_FLAG) ? true : false);
 +              controller->setCreaseAngle( config->crease_angle );
 +              controller->setVisibilityAlgo( config->raycasting_algorithm );
 +
 +              cout << "Crease angle : " << controller->getCreaseAngle() << endl;
 +              cout << "Sphere radius : " << controller->getSphereRadius() << endl;
 +              cout << "Face smoothness : " << (controller->getFaceSmoothness() ? "enabled" : "disabled") << endl;
 +              cout << "Redges and valleys : " << (controller->getComputeRidgesAndValleysFlag() ? "enabled" : "disabled") << endl;
 +              cout << "Suggestive contours : " << (controller->getComputeSuggestiveContoursFlag() ? "enabled" : "disabled") << endl;
 +              cout << "Suggestive contour Kr derivative epsilon : " << controller->getSuggestiveContourKrDerivativeEpsilon() << endl;
 +              cout << "Material boundaries : " << (controller->getComputeMaterialBoundariesFlag() ? "enabled" : "disabled") << endl;
 +              cout << endl;
 +
 +        // set diffuse and z depth passes
 +        RenderLayer *rl = RE_GetRenderLayer(re->result, srl->name);
 +              bool diffuse = false, z = false;
 +              for (RenderPass *rpass = (RenderPass *)rl->passes.first; rpass; rpass = rpass->next) {
 +                      switch (rpass->passtype) {
 +                      case SCE_PASS_DIFFUSE:
 +                              controller->setPassDiffuse(rpass->rect, rpass->rectx, rpass->recty);
 +                              diffuse = true;
 +                              break;
 +                      case SCE_PASS_Z:
 +                              controller->setPassZ(rpass->rect, rpass->rectx, rpass->recty);
 +                              z = true;
 +                              break;
 +                      }
 +              }
 +        cout << "Passes :" << endl;
 +        cout << "  Diffuse = " << (diffuse ? "enabled" : "disabled") << endl;
 +        cout << "  Z = " << (z ? "enabled" : "disabled") << endl;
 +
 +              // compute view map
 +        re->i.infostr= "Freestyle: View map creation";
 +              re->stats_draw(re->sdh, &re->i);
 +        re->i.infostr= NULL;
 +              controller->ComputeViewMap();
 +      }
 +      
 +      void FRS_composite_result(Render* re, SceneRenderLayer* srl, Render* freestyle_render)
 +      {
 +              RenderLayer *rl;
 +          float *src, *dest, *pixSrc, *pixDest;
 +              int x, y, rectx, recty;
 +              
 +              if( freestyle_render == NULL || freestyle_render->result == NULL )
 +                      return;
 +
 +              rl = render_get_active_layer( freestyle_render, freestyle_render->result );
 +          if( !rl || rl->rectf == NULL) { cout << "Cannot find Freestyle result image" << endl; return; }
 +              src  = rl->rectf;
 +              //cout << "src: " << rl->rectx << " x " << rl->recty << endl;
 +              
 +              rl = RE_GetRenderLayer(re->result, srl->name);
 +          if( !rl || rl->rectf == NULL) { cout << "No layer to composite to" << endl; return; }
 +              dest  = rl->rectf;
 +              //cout << "dest: " << rl->rectx << " x " << rl->recty << endl;
 +
 +              rectx = re->rectx;
 +              recty = re->recty;
 +              for( y = 0; y < recty; y++) {
 +              for( x = 0; x < rectx; x++) {
 +                  pixSrc = src + 4 * (rectx * y + x);
 +                  if( pixSrc[3] > 0.0) {
 +                                      pixDest = dest + 4 * (rectx * y + x);
 +                                      addAlphaOverFloat(pixDest, pixSrc);
 +                              }
 +                      }
 +          }
 +      }
 +      
 +      static int displayed_layer_count( SceneRenderLayer* srl ) {
 +              int count = 0;
 +
 +              switch (srl->freestyleConfig.mode) {
 +              case FREESTYLE_CONTROL_SCRIPT_MODE:
 +                      for (FreestyleModuleConfig* module = (FreestyleModuleConfig *)srl->freestyleConfig.modules.first; module; module = module->next) {
 +                              if( module->is_displayed )
 +                                      count++;
 +                      }
 +                      break;
 +              case FREESTYLE_CONTROL_EDITOR_MODE:
 +                      for (FreestyleLineSet *lineset = (FreestyleLineSet *)srl->freestyleConfig.linesets.first; lineset; lineset = lineset->next) {
 +                              if (lineset->flags & FREESTYLE_LINESET_ENABLED)
 +                                      count++;
 +                      }
 +                      break;
 +              }
 +              return count;
 +      }
 +
 +      int FRS_is_freestyle_enabled(SceneRenderLayer* srl) {
 +              return (!(srl->layflag & SCE_LAY_DISABLE) &&
 +                              srl->layflag & SCE_LAY_FRS &&
 +                              displayed_layer_count(srl) > 0);
 +      }
 +      
 +      void FRS_init_stroke_rendering(Render* re) {
 +
 +              cout << "\n#===============================================================" << endl;
 +              cout << "#  Freestyle" << endl;
 +              cout << "#===============================================================" << endl;
 +              
 +              init_view(re);
 +              init_camera(re);
 +
 +              controller->ResetRenderCount();
 +      }
 +      
 +      Render* FRS_do_stroke_rendering(Render* re, SceneRenderLayer *srl) {
 +              
 +              Render* freestyle_render = NULL;
 +              
 +              cout << "\n----------------------------------------------------------" << endl;
 +              cout << "|  " << (re->scene->id.name+2) << "|" << srl->name << endl;
 +              cout << "----------------------------------------------------------" << endl;
 +              
 +              // prepare Freestyle:
 +              //   - load mesh
 +              //   - add style modules
 +              //   - set parameters
 +              //   - compute view map
 +              prepare(re, srl);
 +
 +        if( re->test_break(re->tbh) ) {
 +                      controller->CloseFile();
 +            return NULL;
 +        }
 +
 +              // render and composite Freestyle result
 +              if( controller->_ViewMap ) {
 +                      
 +                      // render strokes                                       
 +            re->i.infostr= "Freestyle: Stroke rendering";
 +            re->stats_draw(re->sdh, &re->i);
 +              re->i.infostr= NULL;
 +                      freestyle_scene = re->scene;
 +                      controller->DrawStrokes();
 +                      freestyle_render = controller->RenderStrokes(re);
 +                      controller->CloseFile();
 +                      freestyle_scene = NULL;
 +                      
 +                      // composite result
 +                      FRS_composite_result(re, srl, freestyle_render);
 +                      RE_FreeRenderResult(freestyle_render->result);
 +                      freestyle_render->result = NULL;
 +              }
 +
 +              return freestyle_render;
 +      }
 +
 +      void FRS_finish_stroke_rendering(Render* re) {
 +              // clear canvas
 +              controller->Clear();
 +      }
 +
 +      //=======================================================
 +      //   Freestyle Panel Configuration
 +      //=======================================================
 +
 +      void FRS_add_freestyle_config( SceneRenderLayer* srl )
 +      {               
 +              FreestyleConfig* config = &srl->freestyleConfig;
 +              
 +              config->mode = FREESTYLE_CONTROL_SCRIPT_MODE;
 +
 +              config->modules.first = config->modules.last = NULL;
 +              config->flags = 0;
 +              config->sphere_radius = DEFAULT_SPHERE_RADIUS;
 +              config->dkr_epsilon = DEFAULT_DKR_EPSILON;
 +              config->crease_angle = 134.43f;
 +
 +              config->linesets.first = config->linesets.last = NULL;
 +
 +              config->raycasting_algorithm = FREESTYLE_ALGO_REGULAR;
 +      }
 +      
 +      void FRS_free_freestyle_config( SceneRenderLayer* srl )
 +      {               
 +              FreestyleLineSet *lineset;
 +
 +              for(lineset=(FreestyleLineSet *)srl->freestyleConfig.linesets.first; lineset; lineset=lineset->next) {
 +                      if (lineset->group) {
 +                              lineset->group->id.us--;
 +                              lineset->group = NULL;
 +                      }
 +                      lineset->linestyle->id.us--;
 +                      lineset->linestyle = NULL;
 +              }
 +              BLI_freelistN( &srl->freestyleConfig.linesets );
 +              BLI_freelistN( &srl->freestyleConfig.modules );
 +      }
 +
 +      void FRS_add_module(FreestyleConfig *config)
 +      {
 +              FreestyleModuleConfig* module_conf = (FreestyleModuleConfig*) MEM_callocN( sizeof(FreestyleModuleConfig), "style module configuration");
 +              BLI_addtail(&config->modules, (void*) module_conf);
 +              
 +              strcpy( module_conf->module_path, default_module_path.c_str() );
 +              module_conf->is_displayed = 1;  
 +      }
 +      
 +      void FRS_delete_module(FreestyleConfig *config, FreestyleModuleConfig *module_conf)
 +      {
 +              BLI_freelinkN(&config->modules, module_conf);
 +      }
 +      
 +      void FRS_move_module_up(FreestyleConfig *config, FreestyleModuleConfig *module_conf)
 +      {
 +              BLI_remlink(&config->modules, module_conf);
 +              BLI_insertlinkbefore(&config->modules, module_conf->prev, module_conf);
 +      }
 +      
 +      void FRS_move_module_down(FreestyleConfig *config, FreestyleModuleConfig *module_conf)
 +      {                       
 +              BLI_remlink(&config->modules, module_conf);
 +              BLI_insertlinkafter(&config->modules, module_conf->next, module_conf);
 +      }
 +
 +      static void unique_lineset_name(FreestyleConfig *config, FreestyleLineSet *lineset)
 +      {
 +              BLI_uniquename(&config->linesets, lineset, "FreestyleLineSet", '.', offsetof(FreestyleLineSet, name), sizeof(lineset->name));
 +      }
 +
 +      FreestyleLineSet *FRS_add_lineset(FreestyleConfig *config)
 +      {
 +              int lineset_index = BLI_countlist(&config->linesets);
 +
 +              FreestyleLineSet *lineset = (FreestyleLineSet *) MEM_callocN( sizeof(FreestyleLineSet), "Freestyle line set");
 +              BLI_addtail(&config->linesets, (void *) lineset);
 +              FRS_set_active_lineset_index(config, lineset_index);
 +
 +              lineset->linestyle = FRS_new_linestyle("LineStyle", NULL);
 +              lineset->flags |= FREESTYLE_LINESET_ENABLED;
 +              lineset->selection = FREESTYLE_SEL_IMAGE_BORDER;
 +              lineset->qi = FREESTYLE_QI_VISIBLE;
 +              lineset->qi_start = 0;
 +              lineset->qi_end = 100;
 +              lineset->edge_types = FREESTYLE_FE_SILHOUETTE | FREESTYLE_FE_BORDER | FREESTYLE_FE_CREASE;
 +              lineset->exclude_edge_types = 0;
 +              lineset->group = NULL;
 +              if (lineset_index > 0)
 +                      sprintf(lineset->name, "LineSet %i", lineset_index+1);
 +              else
 +                      strcpy(lineset->name, "LineSet");
 +              unique_lineset_name(config, lineset);
 +
 +              return lineset;
 +      }
 +
 +      void FRS_copy_active_lineset(FreestyleConfig *config)
 +      {
 +              FreestyleLineSet *lineset = FRS_get_active_lineset(config);
 +
 +              if (lineset) {
 +                      lineset_buffer.linestyle = lineset->linestyle;
 +                      lineset_buffer.flags = lineset->flags;
 +                      lineset_buffer.selection = lineset->selection;
 +                      lineset_buffer.qi = lineset->qi;
 +                      lineset_buffer.qi_start = lineset->qi_start;
 +                      lineset_buffer.qi_end = lineset->qi_end;
 +                      lineset_buffer.edge_types = lineset->edge_types;
 +                      lineset_buffer.exclude_edge_types = lineset->exclude_edge_types;
 +                      lineset_buffer.group = lineset->group;
 +                      strcpy(lineset_buffer.name, lineset->name);
 +                      lineset_copied = true;
 +              }
 +      }
 +
 +      void FRS_paste_active_lineset(FreestyleConfig *config)
 +      {
 +              if (!lineset_copied)
 +                      return;
 +
 +              FreestyleLineSet *lineset = FRS_get_active_lineset(config);
 +
 +              if (lineset) {
 +                      lineset->linestyle->id.us--;
 +                      lineset->linestyle = lineset_buffer.linestyle;
 +                      lineset->linestyle->id.us++;
 +                      lineset->flags = lineset_buffer.flags;
 +                      lineset->selection = lineset_buffer.selection;
 +                      lineset->qi = lineset_buffer.qi;
 +                      lineset->qi_start = lineset_buffer.qi_start;
 +                      lineset->qi_end = lineset_buffer.qi_end;
 +                      lineset->edge_types = lineset_buffer.edge_types;
 +                      lineset->exclude_edge_types = lineset_buffer.exclude_edge_types;
 +                      if (lineset->group) {
 +                              lineset->group->id.us--;
 +                              lineset->group = NULL;
 +                      }
 +                      if (lineset_buffer.group) {
 +                              lineset->group = lineset_buffer.group;
 +                              lineset->group->id.us++;
 +                      }
 +                      strcpy(lineset->name, lineset_buffer.name);
 +                      unique_lineset_name(config, lineset);
 +                      lineset->flags |= FREESTYLE_LINESET_CURRENT;
 +              }
 +      }
 +
 +      void FRS_delete_active_lineset(FreestyleConfig *config)
 +      {
 +              FreestyleLineSet *lineset = FRS_get_active_lineset(config);
 +
 +              if (lineset) {
 +                      if (lineset->group) {
 +                              lineset->group->id.us--;
 +                              lineset->group = NULL;
 +                      }
 +                      lineset->linestyle->id.us--;
 +                      lineset->linestyle = NULL;
 +                      BLI_remlink(&config->linesets, lineset);
 +                      MEM_freeN(lineset);
 +                      FRS_set_active_lineset_index(config, 0);
 +              }
 +      }
 +
 +      void FRS_move_active_lineset_up(FreestyleConfig *config)
 +      {
 +              FreestyleLineSet *lineset = FRS_get_active_lineset(config);
 +
 +              if (lineset) {
 +                      BLI_remlink(&config->linesets, lineset);
 +                      BLI_insertlinkbefore(&config->linesets, lineset->prev, lineset);
 +              }
 +      }
 +
 +      void FRS_move_active_lineset_down(FreestyleConfig *config)
 +      {
 +              FreestyleLineSet *lineset = FRS_get_active_lineset(config);
 +
 +              if (lineset) {
 +                      BLI_remlink(&config->linesets, lineset);
 +                      BLI_insertlinkafter(&config->linesets, lineset->next, lineset);
 +              }
 +      }
 +
 +      FreestyleLineSet *FRS_get_active_lineset(FreestyleConfig *config)
 +      {
 +              FreestyleLineSet *lineset;
 +
 +              for(lineset=(FreestyleLineSet *)config->linesets.first; lineset; lineset=lineset->next)
 +                      if(lineset->flags & FREESTYLE_LINESET_CURRENT)
 +                              return lineset;
 +              return NULL;
 +      }
 +
 +      short FRS_get_active_lineset_index(FreestyleConfig *config)
 +      {
 +              FreestyleLineSet *lineset;
 +              short i;
 +
 +              for(lineset=(FreestyleLineSet *)config->linesets.first, i=0; lineset; lineset=lineset->next, i++)
 +                      if(lineset->flags & FREESTYLE_LINESET_CURRENT)
 +                              return i;
 +              return 0;
 +      }
 +
 +      void FRS_set_active_lineset_index(FreestyleConfig *config, short index)
 +      {
 +              FreestyleLineSet *lineset;
 +              short i;
 +
 +              for(lineset=(FreestyleLineSet *)config->linesets.first, i=0; lineset; lineset=lineset->next, i++) {
 +                      if(i == index)
 +                              lineset->flags |= FREESTYLE_LINESET_CURRENT;
 +                      else
 +                              lineset->flags &= ~FREESTYLE_LINESET_CURRENT;
 +              }
 +      }
 +
 +      void FRS_unlink_target_object(FreestyleConfig *config, Object *ob)
 +      {
 +              FreestyleLineSet *lineset;
 +
 +              for(lineset=(FreestyleLineSet *)config->linesets.first; lineset; lineset=lineset->next) {
 +                      FRS_unlink_linestyle_target_object(lineset->linestyle, ob);
 +              }
 +      }
 +
 +#ifdef __cplusplus
 +}
 +#endif
index 24b03bd,0000000..81e2f70
mode 100755,000000..100755
--- /dev/null
@@@ -1,180 -1,0 +1,180 @@@
-       Text *text = add_text(fn, G.main->name);
 +//
 +//  Filename         : PythonInterpreter.h
 +//  Author(s)        : Emmanuel Turquin
 +//  Purpose          : Python Interpreter
 +//  Date of creation : 17/04/2003
 +//
 +///////////////////////////////////////////////////////////////////////////////
 +
 +
 +//
 +//  Copyright (C) : Please refer to the COPYRIGHT file distributed 
 +//   with this source distribution. 
 +//
 +//  This program is free software; you can redistribute it and/or
 +//  modify it under the terms of the GNU General Public License
 +//  as published by the Free Software Foundation; either version 2
 +//  of the License, or (at your option) any later version.
 +//
 +//  This program is distributed in the hope that it will be useful,
 +//  but WITHOUT ANY WARRANTY; without even the implied warranty of
 +//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 +//  GNU General Public License for more details.
 +//
 +//  You should have received a copy of the GNU General Public License
 +//  along with this program; if not, write to the Free Software
 +//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 +//
 +///////////////////////////////////////////////////////////////////////////////
 +
 +#ifndef  PYTHON_INTERPRETER_H
 +# define PYTHON_INTERPRETER_H
 +
 +# include <Python.h>
 +# include <iostream>
 +# include "StringUtils.h"
 +# include "Interpreter.h"
 +
 +//soc
 +extern "C" {
 +#include "MEM_guardedalloc.h"
 +#include "DNA_text_types.h"
 +#include "BKE_main.h"
 +#include "BKE_context.h"
 +#include "BKE_global.h"
 +#include "BKE_report.h"
 +#include "BKE_text.h"
 +#include "BKE_library.h"
 +#include "BPY_extern.h"
 +}
 +
 +class LIB_SYSTEM_EXPORT PythonInterpreter : public Interpreter
 +{
 + public:
 +
 +  PythonInterpreter() {
 +    _language = "Python";
 +      _context = 0;
 +    //Py_Initialize();
 +  }
 +
 +  virtual ~PythonInterpreter() {
 +    //Py_Finalize();
 +  }
 +
 +  void setContext(bContext *C) {
 +      _context = C;
 +  }
 +
 +  int interpretFile(const string& filename) {
 +
 +      initPath();
 +      
 +      ReportList* reports = CTX_wm_reports(_context);
 +      BKE_reports_clear(reports);
 +      char *fn = const_cast<char*>(filename.c_str());
 +#if 0
 +      int status = BPY_filepath_exec(_context, fn, reports);
 +#else
 +      int status;
-               unlink_text(G.main, text);
-               free_libblock(&G.main->text, text);
++      Text *text = BKE_text_load(fn, G.main->name);
 +      if (text) {
 +              status = BPY_text_exec(_context, text, reports, false);
-       struct Text *text = add_empty_text("tmp_freestyle_initpath.txt");
++              BKE_text_unlink(G.main, text);
++              BKE_libblock_free(&G.main->text, text);
 +      } else {
 +              BKE_reportf(reports, RPT_ERROR, "Cannot open file: %s", fn);
 +              status = 0;
 +      }
 +#endif
 +
 +      if (status != 1) {
 +              cout << "\nError executing Python script from PythonInterpreter::interpretFile" << endl;
 +              cout << "File: " << fn << endl;
 +              cout << "Errors: " << endl;
 +              BKE_reports_print(reports, RPT_ERROR);
 +              return 1;
 +      }
 +
 +      // cleaning up
 +      BKE_reports_clear(reports);
 +      
 +      return 0;
 +  }
 +
 +  int interpretText(struct Text *text, const string& name) {
 +
 +      initPath();
 +
 +      ReportList* reports = CTX_wm_reports(_context);
 +
 +      BKE_reports_clear(reports);
 +
 +      if (!BPY_text_exec(_context, text, reports, false)) {
 +              cout << "\nError executing Python script from PythonInterpreter::interpretText" << endl;
 +              cout << "Name: " << name << endl;
 +              cout << "Errors: " << endl;
 +              BKE_reports_print(reports, RPT_ERROR);
 +              return 1;
 +      }
 +
 +      BKE_reports_clear(reports);
 +
 +      return 0;
 +  }
 +
 +  struct Options
 +  {
 +    static void setPythonPath(const string& path) {
 +      _path = path;
 +    }
 +
 +    static string getPythonPath() {
 +      return _path;
 +    }
 +  };
 +
 +  void reset() {
 +    Py_Finalize();
 +    Py_Initialize();
 +    _initialized = false;
 +  }
 +
 +private:
 +
 +  bContext* _context;
 +
 +  void initPath() {
 +      if (_initialized)
 +              return;
 +
 +      vector<string> pathnames;
 +      StringUtils::getPathName(_path, "", pathnames);
 +      
-       unlink_text(G.main, text);
-       free_libblock(&G.main->text, text);
++      struct Text *text = BKE_text_add("tmp_freestyle_initpath.txt");
 +      string cmd = "import sys\n";
 +      txt_insert_buf(text, const_cast<char*>(cmd.c_str()));
 +      
 +      for (vector<string>::const_iterator it = pathnames.begin(); it != pathnames.end();++it) {
 +              if ( !it->empty() ) {
 +                      cout << "Adding Python path: " << *it << endl;
 +                      cmd = "sys.path.append(r\"" + *it + "\")\n";
 +                      txt_insert_buf(text, const_cast<char*>(cmd.c_str()));
 +              }
 +      }
 +      
 +      BPY_text_exec(_context, text, NULL, false);
 +      
 +      // cleaning up
++      BKE_text_unlink(G.main, text);
++      BKE_libblock_free(&G.main->text, text);
 +      
 +      //PyRun_SimpleString("from Freestyle import *");
 +    _initialized = true;
 +  }
 +
 +  static bool _initialized;
 +  static string _path;
 +};
 +
 +#endif // PYTHON_INTERPRETER_H
Simple merge
@@@ -540,23 -539,6 +540,23 @@@ void rna_Main_movieclips_remove(Main *b
        /* XXX python now has invalid pointer? */
  }
  
-               free_libblock(&bmain->linestyle, linestyle);
 +FreestyleLineStyle *rna_Main_linestyles_new(Main *bmain, const char* name)
 +{
 +      FreestyleLineStyle *linestyle = FRS_new_linestyle(name, bmain);
 +      id_us_min(&linestyle->id);
 +      return linestyle;
 +}
 +
 +void rna_Main_linestyles_remove(Main *bmain, ReportList *reports, FreestyleLineStyle *linestyle)
 +{
 +      if(ID_REAL_USERS(linestyle) <= 0)
++              BKE_libblock_free(&bmain->linestyle, linestyle);
 +      else
 +              BKE_reportf(reports, RPT_ERROR, "Line style \"%s\" must have zero users to be removed, found %d.", linestyle->id.name+2, ID_REAL_USERS(linestyle));
 +
 +      /* XXX python now has invalid pointer? */
 +}
 +
  /* tag functions, all the same */
  void rna_Main_cameras_tag(Main *bmain, int value) { tag_main_lb(&bmain->camera, value); }
  void rna_Main_scenes_tag(Main *bmain, int value) { tag_main_lb(&bmain->scene, value); }
@@@ -1516,8 -1483,8 +1529,9 @@@ static void rna_def_tool_settings(Blend
                {EDGE_MODE_TAG_SHARP, "SHARP", 0, "Tag Sharp", ""},
                {EDGE_MODE_TAG_CREASE, "CREASE", 0, "Tag Crease", ""},
                {EDGE_MODE_TAG_BEVEL, "BEVEL", 0, "Tag Bevel", ""},
-               {0, NULL, 0, NULL, NULL}};
 +              {EDGE_MODE_TAG_FREESTYLE, "FREESTYLE", 0, "Tag Freestyle Edge Mark", ""},
+               {0, NULL, 0, NULL, NULL}
+       };
  
        srna = RNA_def_struct(brna, "ToolSettings", NULL);
        RNA_def_struct_ui_text(srna, "Tool Settings", "");
        RNA_def_property_ui_text(prop, "Project Individual Elements",
                                 "Project individual elements on the surface of other objects");
        RNA_def_property_ui_icon(prop, ICON_RETOPO, 0);
-       RNA_def_property_update(prop, NC_SCENE|ND_TOOLSETTINGS, NULL); /* header redraw */
+       RNA_def_property_update(prop, NC_SCENE | ND_TOOLSETTINGS, NULL); /* header redraw */
 -
 +      
        prop = RNA_def_property(srna, "use_snap_self", PROP_BOOLEAN, PROP_NONE);
        RNA_def_property_boolean_negative_sdna(prop, NULL, "snap_flag", SCE_SNAP_NO_SELF);
        RNA_def_property_ui_text(prop, "Project to Self", "Snap onto itself (editmode)");
@@@ -2039,15 -2008,9 +2055,15 @@@ void rna_def_render_layer_common(Struct
        prop = RNA_def_property(srna, "use_strand", PROP_BOOLEAN, PROP_NONE);
        RNA_def_property_boolean_sdna(prop, NULL, "layflag", SCE_LAY_STRAND);
        RNA_def_property_ui_text(prop, "Strand", "Render Strands in this Layer");
-       if (scene) RNA_def_property_update(prop, NC_SCENE|ND_RENDER_OPTIONS, NULL);
+       if (scene) RNA_def_property_update(prop, NC_SCENE | ND_RENDER_OPTIONS, NULL);
        else RNA_def_property_clear_flag(prop, PROP_EDITABLE);
  
 +      prop= RNA_def_property(srna, "use_freestyle", PROP_BOOLEAN, PROP_NONE);
 +      RNA_def_property_boolean_sdna(prop, NULL, "layflag", SCE_LAY_FRS);
 +      RNA_def_property_ui_text(prop, "Freestyle", "Render stylized strokes in this Layer");
 +      if(scene) RNA_def_property_update(prop, NC_SCENE|ND_RENDER_OPTIONS, NULL);
 +      else RNA_def_property_clear_flag(prop, PROP_EDITABLE);
 +
        /* passes */
        prop = RNA_def_property(srna, "use_pass_combined", PROP_BOOLEAN, PROP_NONE);
        RNA_def_property_boolean_sdna(prop, NULL, "passflag", SCE_PASS_COMBINED);
@@@ -3683,13 -3294,9 +3726,14 @@@ static void rna_def_scene_render_data(B
  
        static EnumPropertyItem engine_items[] = {
                {0, "BLENDER_RENDER", 0, "Blender Render", "Use the Blender internal rendering engine for rendering"},
-               {0, NULL, 0, NULL, NULL}};
+               {0, NULL, 0, NULL, NULL}
+       };
  
 +      static EnumPropertyItem freestyle_thickness_items[] = {
 +              {R_LINE_THICKNESS_ABSOLUTE, "ABSOLUTE", 0, "Absolute", "Specify unit line thickness in pixels"},
 +              {R_LINE_THICKNESS_RELATIVE, "RELATIVE", 0, "Relative", "Unit line thickness is scaled by the proportion of the present vertical image resolution to 480 pixels"},
 +              {0, NULL, 0, NULL, NULL}};
 +
        rna_def_scene_ffmpeg_settings(brna);
  #ifdef WITH_QUICKTIME
        rna_def_scene_quicktime_settings(brna);
        RNA_def_property_float_sdna(prop, NULL, "edgeR");
        RNA_def_property_array(prop, 3);
        RNA_def_property_ui_text(prop, "Edge Color", "Edge color");
-       RNA_def_property_update(prop, NC_SCENE|ND_RENDER_OPTIONS, NULL);
+       RNA_def_property_update(prop, NC_SCENE | ND_RENDER_OPTIONS, NULL);
        
 +      prop= RNA_def_property(srna, "use_freestyle", PROP_BOOLEAN, PROP_NONE);
 +      RNA_def_property_boolean_sdna(prop, NULL, "mode", R_EDGE_FRS);
 +      RNA_def_property_ui_text(prop, "Edge", "Draw stylized strokes using Freestyle");
 +      RNA_def_property_update(prop, NC_SCENE|ND_RENDER_OPTIONS, NULL);
 +
        /* threads */
        prop = RNA_def_property(srna, "threads", PROP_INT, PROP_NONE);
        RNA_def_property_int_sdna(prop, NULL, "threads");
Simple merge
@@@ -1389,73 -1379,6 +1389,73 @@@ static void render_composit_stats(void 
        R.i.infostr= NULL;
  }
  
-                               unlink_scene(G.main, freestyle_render->scene, scene);
 +/* invokes Freestyle stroke rendering */
 +static void add_freestyle(Render *re)
 +{
 +      SceneRenderLayer *srl, *actsrl;
 +      LinkData *link;
 +
 +      actsrl = BLI_findlink(&re->r.layers, re->r.actlay);
 +
 +      FRS_init_stroke_rendering(re);
 +
 +      for (srl= (SceneRenderLayer *)re->r.layers.first; srl; srl= srl->next) {
 +
 +              link = (LinkData *)MEM_callocN(sizeof(LinkData), "LinkData to Freestyle render");
 +              BLI_addtail(&re->freestyle_renders, link);
 +
 +              if ((re->r.scemode & R_SINGLE_LAYER) && srl != actsrl)
 +                      continue;
 +              if (FRS_is_freestyle_enabled(srl)) {
 +                      link->data = (void *)FRS_do_stroke_rendering(re, srl);
 +              }
 +      }
 +
 +      FRS_finish_stroke_rendering(re);
 +}
 +
 +/* merges the results of Freestyle stroke rendering into a given render result */
 +static void composite_freestyle_renders(Render *re, int sample)
 +{
 +      Render *freestyle_render;
 +      SceneRenderLayer *srl, *actsrl;
 +      LinkData *link;
 +
 +      actsrl = BLI_findlink(&re->r.layers, re->r.actlay);
 +
 +      link = (LinkData *)re->freestyle_renders.first;
 +      for (srl= (SceneRenderLayer *)re->r.layers.first; srl; srl= srl->next) {
 +              if ((re->r.scemode & R_SINGLE_LAYER) && srl != actsrl)
 +                      continue;
 +              if (FRS_is_freestyle_enabled(srl)) {
 +                      freestyle_render = (Render *)link->data;
 +                      render_result_exr_file_read(freestyle_render, sample);
 +                      FRS_composite_result(re, srl, freestyle_render);
 +                      RE_FreeRenderResult(freestyle_render->result);
 +                      freestyle_render->result = NULL;
 +              }
 +              link = link->next;
 +      }
 +}
 +
 +/* releases temporary scenes and renders for Freestyle stroke rendering */
 +static void free_all_freestyle_renders(Scene *scene)
 +{
 +      Render *re1, *freestyle_render;
 +      LinkData *link;
 +
 +      for (re1= RenderGlobal.renderlist.first; re1; re1= re1->next) {
 +              for (link = (LinkData *)re1->freestyle_renders.first; link; link = link->next) {
 +                      if (link->data) {
 +                              freestyle_render = (Render *)link->data;
++                              BKE_scene_unlink(G.main, freestyle_render->scene, scene);
 +                              RE_FreeRender(freestyle_render);
 +                      }
 +              }
 +              BLI_freelistN( &re1->freestyle_renders );
 +      }
 +}
 +
  
  /* reads all buffers, calls optional composite, merges in first result->rectf */
  static void do_merge_fullsample(Render *re, bNodeTree *ntree)
@@@ -504,14 -501,8 +504,15 @@@ void BPY_pyconstraint_exec(struct bPyth
  void macro_wrapper(struct wmOperatorType *ot, void *userdata) {}
  int pyrna_id_FromPyObject(struct PyObject *obj, struct ID **id){ return 0; }
  struct PyObject *pyrna_id_CreatePyObject(struct ID *id) {return NULL; }
+ void BPY_context_update(struct bContext *C){};
  
 +/* Freestyle */
 +void FRS_add_freestyle_config(struct SceneRenderLayer* srl) {}
 +void FRS_free_freestyle_config(struct SceneRenderLayer* srl) {}
 +struct FreestyleLineSet *FRS_get_active_lineset(struct FreestyleConfig *config) { return NULL; }
 +short FRS_get_active_lineset_index(struct FreestyleConfig *config) { return 0; }
 +void FRS_set_active_lineset_index(struct FreestyleConfig *config, short index) {}
 +void FRS_unlink_target_object(struct FreestyleConfig *config, struct Object *ob) {}
  /* intern/dualcon */
  struct DualConMesh;
  struct DualConMesh *dualcon(const struct DualConMesh *input_mesh,
Simple merge
@@@ -1270,9 -1285,9 +1287,9 @@@ int main(int argc, const char **argv
        /* for all platforms, even windos has it! */
        if (G.background) signal(SIGINT, blender_esc);  /* ctrl c out bg render */
  #endif
 -
 +      
        /* background render uses this font too */
-       BKE_font_register_builtin(datatoc_Bfont, datatoc_Bfont_size);
+       BKE_vfont_builtin_register(datatoc_Bfont, datatoc_Bfont_size);
  
        /* Initialize ffmpeg if built in, also needed for bg mode if videos are
         * rendered via ffmpeg */