WIP: MaterialX: Implement Script node #113341
|
@ -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
|
||||
STRNCPY(filepath, script->filepath);
|
||||
BLI_path_abs(filepath, BKE_main_blendfile_path(bmain));
|
||||
DagerD marked this conversation as resolved
Outdated
Bogdan Nagirniak
commented
move this part with 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
Bogdan Nagirniak
commented
I think no need to use 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
Bogdan Nagirniak
commented
Seem 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
Bogdan Nagirniak
commented
`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 {
|
||||
|
|
Loading…
Reference in New Issue
Use explicit
STREQ(script->filepath, "")