Freestyle: an improved workflow of line style shading nodes.

Removed the previous changes for passing a line style through the Controller, and
revised the BlenderTextureShader to assign the shader node tree of a line style
(if specified) to strokes.  This way the assignment of shading nodes can be done
through both the Freestyle GUI and Python scripting.
This commit is contained in:
Tamito Kajiyama 2014-07-19 18:52:32 +09:00
parent c38e80d632
commit 34c133a488
17 changed files with 92 additions and 116 deletions

@ -1078,8 +1078,7 @@ def process(layer_name, lineset_name):
has_tex = False
if scene.render.use_shading_nodes:
if linestyle.use_nodes and linestyle.node_tree:
### TODO ###
#shaders_list.append(BlenderTextureShader(linestyle.nodetree))
shaders_list.append(BlenderTextureShader(linestyle.node_tree))
has_tex = True
else:
if linestyle.use_texture:

@ -892,10 +892,9 @@ void Controller::InsertStyleModule(unsigned index, const char *iFileName)
_Canvas->InsertStyleModule(index, sm);
}
void Controller::InsertStyleModule(unsigned index, const char *iName, struct Text *iText,
struct FreestyleLineStyle *iLineStyle, bool iUseShadingNodes)
void Controller::InsertStyleModule(unsigned index, const char *iName, struct Text *iText)
{
StyleModule *sm = new BlenderStyleModule(iName, _inter, iText, iLineStyle, iUseShadingNodes);
StyleModule *sm = new BlenderStyleModule(iText, iName, _inter);
_Canvas->InsertStyleModule(index, sm);
}

@ -89,8 +89,7 @@ public:
Render *RenderStrokes(Render *re, bool render);
void SwapStyleModules(unsigned i1, unsigned i2);
void InsertStyleModule(unsigned index, const char *iFileName);
void InsertStyleModule(unsigned index, const char *iName, struct Text *iText,
struct FreestyleLineStyle *iLineStyle, bool iUseShadingNodes);
void InsertStyleModule(unsigned index, const char *iName, struct Text *iText);
void AddStyleModule(const char *iFileName);
void RemoveStyleModule(unsigned index);
void ReloadStyleModule(unsigned index, const char * iFileName);

@ -146,7 +146,9 @@ BlenderStrokeRenderer::BlenderStrokeRenderer(bContext *C, Render *re, int render
BlenderStrokeRenderer::~BlenderStrokeRenderer()
{
return; //XXX
#if 0
return; // XXX
#endif
// The freestyle_scene object is not released here. Instead,
// the scene is released in free_all_freestyle_renders() in
@ -217,7 +219,7 @@ unsigned int BlenderStrokeRenderer::get_stroke_mesh_id(void) const
return mesh_id;
}
Material* BlenderStrokeRenderer::GetStrokeShader(bContext *C, Main *bmain, FreestyleLineStyle *linestyle)
Material* BlenderStrokeRenderer::GetStrokeShader(bContext *C, Main *bmain, bNodeTree *iNodeTree)
{
Material *ma = BKE_material_add(bmain, "stroke_shader");
bNodeTree *ntree;
@ -226,9 +228,9 @@ Material* BlenderStrokeRenderer::GetStrokeShader(bContext *C, Main *bmain, Frees
PointerRNA fromptr, toptr;
cout << "linestyle " << linestyle << " nodetree " << linestyle->nodetree << " use_nodes " << linestyle->use_nodes << endl;
if (linestyle && linestyle->use_nodes && linestyle->nodetree) {
if (iNodeTree) {
// make a copy of linestyle->nodetree
ntree = ntreeCopyTree_ex(linestyle->nodetree, bmain, true);
ntree = ntreeCopyTree_ex(iNodeTree, bmain, true);
// find the active Output Line Style node
for (bNode *node = (bNode *)ntree->nodes.first; node; node = node->next) {
@ -357,8 +359,8 @@ Material* BlenderStrokeRenderer::GetStrokeShader(bContext *C, Main *bmain, Frees
void BlenderStrokeRenderer::RenderStrokeRep(StrokeRep *iStrokeRep) const
{
if (iStrokeRep->useShadingNodes()) {
Material *ma = BlenderStrokeRenderer::GetStrokeShader(_context, freestyle_bmain, iStrokeRep->getLineStyle());
if (BKE_scene_use_new_shading_nodes(freestyle_scene)) {
Material *ma = BlenderStrokeRenderer::GetStrokeShader(_context, freestyle_bmain, iStrokeRep->getNodeTree());
if (strcmp(freestyle_scene->r.engine, "CYCLES") == 0) {
PointerRNA scene_ptr;

@ -30,11 +30,12 @@
extern "C" {
struct Main;
struct Material;
struct Object;
struct Render;
struct Scene;
struct bContext;
struct FreestyleLineStyle;
struct bNodeTree;
}
namespace Freestyle {
@ -53,7 +54,7 @@ public:
Render *RenderScene(Render *re, bool render);
static Material* GetStrokeShader(bContext *C, Main *bmain, FreestyleLineStyle *linestyle);
static Material* GetStrokeShader(bContext *C, Main *bmain, bNodeTree *iNodeTree);
protected:
Main *freestyle_bmain;

@ -31,7 +31,6 @@
extern "C" {
#include "BLI_utildefines.h" // BLI_assert()
struct FreestyleLineStyle;
struct Scene;
struct Text;
}
@ -41,26 +40,15 @@ namespace Freestyle {
class BlenderStyleModule : public StyleModule
{
public:
BlenderStyleModule(const string &name, Interpreter *inter, struct Text *text,
struct FreestyleLineStyle *lineStyle, bool useShadingNodes)
: StyleModule(name, inter)
BlenderStyleModule(struct Text *text, const string &name, Interpreter *inter) : StyleModule(name, inter)
{
_text = text;
_lineStyle = lineStyle;
_useShadingNodes = useShadingNodes;
}
virtual ~BlenderStyleModule()
{
}
virtual StrokeLayer *execute()
{
StrokeLayer *sl = StyleModule::execute();
sl->setLineStyle(_lineStyle, _useShadingNodes);
return sl;
}
protected:
virtual int interpret()
{
@ -71,8 +59,6 @@ protected:
private:
struct Text *_text;
struct FreestyleLineStyle *_lineStyle;
bool _useShadingNodes;
#ifdef WITH_CXX_GUARDEDALLOC
MEM_CXX_CLASS_ALLOC_FUNCS("Freestyle:BlenderStyleModule")

@ -50,7 +50,6 @@ extern "C" {
#include "BKE_library.h"
#include "BKE_linestyle.h"
#include "BKE_main.h"
#include "BKE_scene.h"
#include "BKE_text.h"
#include "BKE_context.h"
@ -295,8 +294,6 @@ static bool test_edge_type_conditions(struct edge_type_condition *conditions,
static void prepare(Main *bmain, Render *re, SceneRenderLayer *srl)
{
const bool use_shading_nodes = BKE_scene_use_new_shading_nodes(re->scene);
// load mesh
re->i.infostr = "Freestyle: Mesh loading";
re->stats_draw(re->sdh, &re->i);
@ -331,7 +328,7 @@ static void prepare(Main *bmain, Render *re, SceneRenderLayer *srl)
cout << " (" << module_conf->script->name << ")";
cout << endl;
}
controller->InsertStyleModule(layer_count, id_name, module_conf->script, NULL, use_shading_nodes);
controller->InsertStyleModule(layer_count, id_name, module_conf->script);
controller->toggleLayer(layer_count, true);
layer_count++;
}
@ -372,7 +369,7 @@ static void prepare(Main *bmain, Render *re, SceneRenderLayer *srl)
(lineset->linestyle ? (lineset->linestyle->id.name + 2) : "<NULL>") << endl;
}
Text *text = create_lineset_handler(bmain, srl->name, lineset->name);
controller->InsertStyleModule(layer_count, lineset->name, text, lineset->linestyle, use_shading_nodes);
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;
@ -743,8 +740,9 @@ Material *FRS_create_stroke_material(bContext *C, Main *bmain, Scene *scene)
if (!linestyle) {
cout << "FRS_create_stroke_material: No active line style in the current scene" << endl;
return NULL;
}
return BlenderStrokeRenderer::GetStrokeShader(C, bmain, linestyle);
return BlenderStrokeRenderer::GetStrokeShader(C, bmain, linestyle->nodetree);
}
} // extern "C"

@ -41,38 +41,46 @@ static char BlenderTextureShader___doc__[] =
"\n"
"[Texture shader]\n"
"\n"
".. method:: __init__(LineStyleTextureSlot)\n"
".. method:: __init__(texture)\n"
"\n"
" Builds a BlenderTextureShader object.\n"
"\n"
" :arg mtex: texture slot to add to stroke shading.\n"
" :type mtex: LineStyleTextureSlot\n"
" :arg texture: A line style texture slot or a shader node tree to define\n"
" a set of textures.\n"
" :type texture: :class:`LineStyleTextureSlot` or :class:`ShaderNodeTree`\n"
"\n"
".. method:: shade(stroke)\n"
"\n"
" Assigns a blender texture slot to the stroke shading\n"
" in order to simulate marks.\n"
" Assigns a blender texture slot to the stroke shading in order to\n"
" simulate marks.\n"
"\n"
" :arg stroke: A Stroke object.\n"
" :type stroke: :class:`Stroke`\n";
static int BlenderTextureShader___init__(BPy_BlenderTextureShader *self, PyObject *args, PyObject *kwds)
{
static const char *kwlist[] = {"LineStyleTextureSlot", NULL};
PyObject *py_mtex;
static const char *kwlist[] = {"texture", NULL};
PyObject *obj;
MTex *_mtex;
bNodeTree *_nodetree;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", (char **)kwlist, &py_mtex))
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", (char **)kwlist, &obj))
return -1;
_mtex = (MTex*)PyC_RNA_AsPointer(py_mtex, "LineStyleTextureSlot");
_mtex = (MTex *)PyC_RNA_AsPointer(obj, "LineStyleTextureSlot");
if (_mtex) {
self->py_ss.ss = new StrokeShaders::BlenderTextureShader(_mtex);
}
return 0;
}
PyErr_Clear();
_nodetree = (bNodeTree *)PyC_RNA_AsPointer(obj, "ShaderNodeTree");
if (_nodetree) {
self->py_ss.ss = new StrokeShaders::BlenderTextureShader(_nodetree);
return 0;
}
PyErr_Format(PyExc_TypeError,
"expected either 'LineStyleTextureSlot' or 'ShaderNodeTree', "
"found '%.200s' instead", Py_TYPE(obj)->tp_name);
return -1;
}
/*-----------------------BPy_BlenderTextureShader type definition ------------------------------*/

@ -451,7 +451,13 @@ int ColorNoiseShader::shade(Stroke& stroke) const
int BlenderTextureShader::shade(Stroke& stroke) const
{
if (_mtex)
return stroke.setMTex(_mtex);
if (_nodeTree) {
stroke.setNodeTree(_nodeTree);
return 0;
}
return -1;
}
int StrokeTextureStepShader::shade(Stroke& stroke) const

@ -36,7 +36,10 @@
#include "../geometry/Bezier.h"
#include "../geometry/Geom.h"
extern "C" {
struct MTex;
struct bNodeTree;
}
using namespace std;
@ -904,6 +907,7 @@ class BlenderTextureShader : public StrokeShader
{
private:
MTex *_mtex;
bNodeTree *_nodeTree;
public:
/*! Builds the shader.
@ -913,6 +917,17 @@ public:
BlenderTextureShader(MTex *mtex) : StrokeShader()
{
_mtex = mtex;
_nodeTree = NULL;
}
/*! Builds the shader.
* \param nodetree
* A node tree (of new shading nodes) to define textures.
*/
BlenderTextureShader(bNodeTree *nodetree) : StrokeShader()
{
_nodeTree = nodetree;
_mtex = NULL;
}
virtual string getName() const

@ -30,10 +30,7 @@
#include "StrokeAdvancedIterators.h"
#include "StrokeRenderer.h"
#include "DNA_linestyle_types.h"
#include "BKE_global.h"
#include "BKE_linestyle.h"
#include "BKE_node.h"
namespace Freestyle {
@ -398,11 +395,10 @@ Stroke::Stroke()
_mediumType = OPAQUE_MEDIUM;
_textureId = 0;
_textureStep = 1.0;
_lineStyle = NULL;
_useShadingNodes = false;
for (int a = 0; a < MAX_MTEX; a++) {
_mtex[a] = NULL;
}
_nodeTree = NULL;
_tips = false;
}
@ -421,8 +417,6 @@ Stroke::Stroke(const Stroke& iBrother)
_mediumType = iBrother._mediumType;
_textureId = iBrother._textureId;
_textureStep = iBrother._textureStep;
_lineStyle = iBrother._lineStyle;
_useShadingNodes = iBrother._useShadingNodes;
for (int a = 0; a < MAX_MTEX; a++) {
if (iBrother._mtex) {
_mtex[a] = iBrother._mtex[a];
@ -431,6 +425,7 @@ Stroke::Stroke(const Stroke& iBrother)
_mtex[a] = NULL;
}
}
_nodeTree = iBrother._nodeTree;
_tips = iBrother._tips;
}
@ -760,11 +755,6 @@ void Stroke::ScaleThickness(float iFactor)
}
}
bool Stroke::hasTex() const
{
return BKE_linestyle_use_textures(_lineStyle, _useShadingNodes);
}
void Stroke::Render(const StrokeRenderer *iRenderer)
{
StrokeRep rep(this);

@ -46,7 +46,7 @@
extern "C" {
#include "DNA_material_types.h"
struct FreestyleLineStyle;
struct bNodeTree;
}
#ifndef MAX_MTEX
@ -543,10 +543,9 @@ private:
MediumType _mediumType;
unsigned int _textureId;
MTex *_mtex[MAX_MTEX];
bNodeTree *_nodeTree;
bool _tips;
Vec2r _extremityOrientations[2]; // the orientations of the first and last extermity
FreestyleLineStyle *_lineStyle;
bool _useShadingNodes;
public:
/*! default constructor */
@ -645,18 +644,6 @@ public:
return _mediumType;
}
/*! Return the line style associated to this Stroke. */
inline FreestyleLineStyle *getLineStyle()
{
return _lineStyle;
}
/*! Return true if the new shading nodes are used. */
inline bool useShadingNodes()
{
return _useShadingNodes;
}
/*! Returns the id of the texture used to simulate th marks system for this Stroke */
inline unsigned int getTextureId() {return _textureId;}
@ -668,8 +655,17 @@ public:
return _mtex[idx];
}
/*! Return the shader node tree to define textures. */
inline bNodeTree *getNodeTree()
{
return _nodeTree;
}
/*! Returns true if this Stroke has textures assigned, false otherwise. */
bool hasTex() const;
inline bool hasTex() const
{
return (_mtex && _mtex[0] != NULL) || _nodeTree;
}
/*! Returns true if this Stroke uses a texture with tips, false otherwise. */
inline bool hasTips() const
@ -749,13 +745,6 @@ public:
/*! sets the 2D length of the Stroke. */
void setLength(float iLength);
/*! sets the line style of the Stroke. */
void setLineStyle(struct FreestyleLineStyle *iLineStyle, bool iUseShadingNodes)
{
_lineStyle = iLineStyle;
_useShadingNodes = iUseShadingNodes;
}
/*! sets the medium type that must be used for this Stroke. */
inline void setMediumType(MediumType iType)
{
@ -786,6 +775,12 @@ public:
return -1; /* no free slots */
}
/*! assigns a node tree (of new shading nodes) to define textures. */
inline void setNodeTree(bNodeTree *iNodeTree)
{
_nodeTree = iNodeTree;
}
/*! sets the flag telling whether this stroke is using a texture with tips or not. */
inline void setTips(bool iTips)
{

@ -36,13 +36,6 @@ StrokeLayer::~StrokeLayer()
clear();
}
void StrokeLayer::setLineStyle(struct FreestyleLineStyle *iLineStyle, bool iUseShadingNodes)
{
for (StrokeLayer::stroke_container::iterator s = _strokes.begin(), send = _strokes.end(); s != send; ++s) {
(*s)->setLineStyle(iLineStyle, iUseShadingNodes);
}
}
void StrokeLayer::ScaleThickness(float iFactor)
{
for (StrokeLayer::stroke_container::iterator s = _strokes.begin(), send = _strokes.end(); s != send; ++s) {

@ -34,10 +34,6 @@
#include "MEM_guardedalloc.h"
#endif
extern "C" {
struct FreestyleLineStyle;
}
namespace Freestyle {
class Stroke;
@ -105,8 +101,6 @@ public:
_strokes.push_back(iStroke);
}
void setLineStyle(struct FreestyleLineStyle *iLineStyle, bool iUseShadingNodes);
#ifdef WITH_CXX_GUARDEDALLOC
MEM_CXX_CLASS_ALLOC_FUNCS("Freestyle:StrokeLayer")
#endif

@ -705,8 +705,7 @@ StrokeRep::StrokeRep()
{
_stroke = 0;
_strokeType = Stroke::OPAQUE_MEDIUM;
_lineStyle = NULL;
_useShadingNodes = false;
_nodeTree = NULL;
_hasTex = false;
_textureStep = 1.0;
for (int a = 0; a < MAX_MTEX; a++) {
@ -728,8 +727,7 @@ StrokeRep::StrokeRep(Stroke *iStroke)
{
_stroke = iStroke;
_strokeType = iStroke->getMediumType();
_lineStyle = iStroke->getLineStyle();
_useShadingNodes = iStroke->useShadingNodes();
_nodeTree = iStroke->getNodeTree();
_hasTex = iStroke->hasTex();
_textureId = iStroke->getTextureId();
_textureStep = iStroke->getTextureStep();
@ -764,8 +762,7 @@ StrokeRep::StrokeRep(const StrokeRep& iBrother)
_strokeType = iBrother._strokeType;
_textureId = iBrother._textureId;
_textureStep = iBrother._textureStep;
_lineStyle = iBrother._lineStyle;
_useShadingNodes = iBrother._useShadingNodes;
_nodeTree = iBrother._nodeTree;
_hasTex = iBrother._hasTex;
for (int a = 0; a < MAX_MTEX; a++) {
if (iBrother._mtex[a]) {

@ -38,7 +38,7 @@
extern "C" {
#include "DNA_material_types.h" // for MAX_MTEX
struct FreestyleLineStyle;
struct bNodeTree;
}
namespace Freestyle {
@ -186,9 +186,8 @@ protected:
unsigned int _textureId;
float _textureStep;
MTex *_mtex[MAX_MTEX];
bNodeTree *_nodeTree;
Material *_material;
FreestyleLineStyle *_lineStyle;
bool _useShadingNodes;
bool _hasTex;
// float _averageTextureAlpha;
@ -226,14 +225,9 @@ public:
return _material;
}
inline FreestyleLineStyle *getLineStyle() const
inline bNodeTree *getNodeTree() const
{
return _lineStyle;
}
inline bool useShadingNodes() const
{
return _useShadingNodes;
return _nodeTree;
}
inline bool hasTex() const

@ -62,7 +62,7 @@ public:
virtual ~StyleModule() {}
virtual StrokeLayer *execute()
StrokeLayer *execute()
{
if (!_inter) {
cerr << "Error: no interpreter was found to execute the script" << endl;