Skip to content

Commit

Permalink
Command for setting attribute from string value (#979)
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasharrysson authored Sep 29, 2020
1 parent c77049f commit 6e91ddf
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
18 changes: 14 additions & 4 deletions source/MaterialXRuntime/Commands/AttributeCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,20 @@ namespace MaterialX
namespace RtCommand
{

void setAttribute(const RtAttribute& attr, const RtValue& value, RtCommandResult& result)
{
PvtCommandPtr cmd = PvtSetAttributeCmd::create(attr, value);
PvtApi::cast(RtApi::get())->getCommandEngine().execute(cmd, result);
void setAttributeFromString(const RtAttribute& attr, const string& valueString, RtCommandResult& result)
{
// Use try/catch since the conversion from string might fail and throw.
try
{
RtValue v = RtValue::createNew(attr.getType(), attr.getParent());
RtValue::fromString(attr.getType(), valueString, v);
PvtCommandPtr cmd = PvtSetAttributeCmd::create(attr, v);
PvtApi::cast(RtApi::get())->getCommandEngine().execute(cmd, result);
}
catch (Exception& e)
{
result = RtCommandResult(false, e.what());
}
}

void setAttribute(const RtAttribute& attr, bool value, RtCommandResult& result)
Expand Down
4 changes: 2 additions & 2 deletions source/MaterialXRuntime/Commands/AttributeCommands.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ namespace MaterialX

namespace RtCommand
{
/// Set value on an attribute.
void setAttribute(const RtAttribute& attr, const RtValue& value, RtCommandResult& result);
/// Set an attribute value from data given as a value string.
void setAttributeFromString(const RtAttribute& attr, const string& valueString, RtCommandResult& result);

/// Set value on a bool attribute.
void setAttribute(const RtAttribute& attr, bool value, RtCommandResult& result);
Expand Down
11 changes: 9 additions & 2 deletions source/MaterialXTest/MaterialXRuntime/Runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1115,7 +1115,7 @@ TEST_CASE("Runtime: Conflict resolution", "[runtime]")
++numAfter;
++stageTraverser;
}
std::cout << std::endl;

// Everything duplicated except the nodedef ND_default_shader and its
// nodegraph implementation NG_default_shader
REQUIRE(numBefore * 2 - 2 == numAfter);
Expand Down Expand Up @@ -2184,7 +2184,14 @@ TEST_CASE("Runtime: Commands", "[runtime]")
REQUIRE(in1.getValue().asFloat() == 3.0f);
REQUIRE(in2.getValue().asFloat() == 7.0f);

REQUIRE(setAttrCount == 6);
mx::RtCommand::setAttributeFromString(in2, "42.0", result);
REQUIRE(result);
REQUIRE(in2.getValue().asFloat() == 42.0f);

mx::RtCommand::setAttributeFromString(in2, "nonsense", result);
REQUIRE(!result);

REQUIRE(setAttrCount == 7);

//
// Test making and breaking connections
Expand Down

0 comments on commit 6e91ddf

Please sign in to comment.