WIP: MaterialX: Implement Script node #113341

Draft
Georgiy Markelov wants to merge 5 commits from DagerD/blender:matx-add-script-node into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
1 changed files with 92 additions and 21 deletions

View File

@ -2,14 +2,22 @@
*
* SPDX-License-Identifier: GPL-2.0-or-later */
#include <sstream>
#include <MaterialXFormat/XmlIo.h>
#include "material.h"
#include "node_parser.h"
#include "BKE_main.h"
#include "BLI_path_util.h"
#include "BLI_string.h"
#include "DEG_depsgraph.hh"
#include "DEG_depsgraph_query.hh"
#include "DNA_material_types.h"
#include "DNA_text_types.h"
#include "NOD_shader.h"
@ -54,6 +62,64 @@ class DefaultMaterialNodeParser : public NodeParser {
}
};
static bool export_script_node(MaterialX::DocumentPtr doc,
Depsgraph *depsgraph,
Material *material)
{
Main *bmain = DEG_get_bmain(depsgraph);
LISTBASE_FOREACH (bNode *, node, &material->nodetree->nodes) {
if (node->typeinfo->type == SH_NODE_SCRIPT) {
NodeShaderScript *script = static_cast<NodeShaderScript *>(node->storage);
if (script->mode == NODE_SCRIPT_EXTERNAL) {
if (!STREQ(script->filepath, "")) {
char filepath[FILE_MAX];
DagerD marked this conversation as resolved Outdated

Use explicit STREQ(script->filepath, "")

Use explicit `STREQ(script->filepath, "")`
STRNCPY(filepath, script->filepath);
BLI_path_abs(filepath, BKE_main_blendfile_path(bmain));
DagerD marked this conversation as resolved Outdated

move this part with if to separate function

move this part with `if` to separate function
try {
MaterialX::readFromXmlFile(doc, filepath);
}
catch (MaterialX::ExceptionParseError) {
CLOG_WARN(LOG_MATERIALX_SHADER,
"Material: %s, Node: %s: parsing error",
material->id.name,
node->name);
}
catch (MaterialX::ExceptionFileMissing) {
CLOG_WARN(LOG_MATERIALX_SHADER,
"Material: %s, Node: %s: file not found",
material->id.name,
node->name);
}
return true;
}
}
DagerD marked this conversation as resolved Outdated

I think no need to use script_found just return true here

I think no need to use `script_found` just `return true` here
else if (script->mode == NODE_SCRIPT_INTERNAL) {
Text *text = reinterpret_cast<Text *>(node->id);
if (text) {
std::stringstream text_content;
LISTBASE_FOREACH (TextLine *, line, &text->lines) {
text_content << line->line;
}
try {
MaterialX::readFromXmlStream(doc, text_content);
}
DagerD marked this conversation as resolved Outdated

Seem std::stringstream suits better here.

Seem `std::stringstream` suits better here.
catch (MaterialX::ExceptionParseError) {
CLOG_WARN(LOG_MATERIALX_SHADER,
"Material: %s, Node: %s: parsing error",
material->id.name,
node->name);
}
return true;
}
}
else {
BLI_assert_unreachable();
}
}
}
return false;
}
MaterialX::DocumentPtr export_to_materialx(Depsgraph *depsgraph,
Material *material,
ExportImageFunction export_image_fn)
@ -63,27 +129,32 @@ MaterialX::DocumentPtr export_to_materialx(Depsgraph *depsgraph,
MaterialX::DocumentPtr doc = MaterialX::createDocument();
if (material->use_nodes) {
material->nodetree->ensure_topology_cache();
bNode *output_node = ntreeShaderOutputNode(material->nodetree, SHD_OUTPUT_ALL);
if (output_node) {
NodeParserData data = {doc.get(),
depsgraph,
material,
NodeItem::Type::Material,
nullptr,
NodeItem(doc.get()),
export_image_fn};
output_node->typeinfo->materialx_fn(&data, output_node, nullptr);
}
else {
DefaultMaterialNodeParser(doc.get(),
depsgraph,
material,
nullptr,
nullptr,
NodeItem::Type::Material,
nullptr,
export_image_fn)
.compute_error();
/* Look for first script node with assigned file.
* If it's found, we load content and ignore everything other. */
if (!export_script_node(doc, depsgraph, material)) {
bNode *output_node = ntreeShaderOutputNode(material->nodetree, SHD_OUTPUT_ALL);
if (output_node) {
NodeParserData data = {doc.get(),
depsgraph,
material,
NodeItem::Type::Material,
DagerD marked this conversation as resolved
Review

if (!export_script_node(doc, depsgraph, material)) {

`if (!export_script_node(doc, depsgraph, material)) {`
nullptr,
NodeItem(doc.get()),
export_image_fn};
output_node->typeinfo->materialx_fn(&data, output_node, nullptr);
}
else {
DefaultMaterialNodeParser(doc.get(),
depsgraph,
material,
nullptr,
nullptr,
NodeItem::Type::Material,
nullptr,
export_image_fn)
.compute_error();
}
}
}
else {