forked from AcademySoftwareFoundation/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LOOKDEVX-407 | New metadata commands for setting/removing metadata. (#…
…1117) * LOOKDEVX-407 | New metadata commands for setting/removing metadata. * LOOKDEVX-407 | Applied code review feedback. * LOOKDEVX-407 | Applied additional code review feedback. * LOOKDEVX-407 | Applied additional code review feedback. Co-authored-by: Jonathan Feldstein <[email protected]>
- Loading branch information
Showing
12 changed files
with
575 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// | ||
// TM & (c) 2020 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd. | ||
// All rights reserved. See LICENSE.txt for license. | ||
// | ||
#include <MaterialXRuntime/Commands/MetadataCommands.h> | ||
|
||
#include <MaterialXRuntime/Private/Commands/PvtSetMetadataCmd.h> | ||
#include <MaterialXRuntime/Private/Commands/PvtRemoveMetadataCmd.h> | ||
|
||
#include <MaterialXRuntime/Private/PvtApi.h> | ||
|
||
#include <MaterialXRuntime/RtTypeDef.h> | ||
|
||
namespace MaterialX | ||
{ | ||
|
||
namespace RtCommand | ||
{ | ||
|
||
void setMetadata(const RtObject& src, const RtToken& metadataName, const string& metadataValue, RtCommandResult& result) | ||
{ | ||
try | ||
{ | ||
RtValue v = RtValue::createNew(RtType::STRING, src); | ||
RtValue::fromString(RtType::STRING, metadataValue, v); | ||
PvtCommandPtr cmd = PvtSetMetadataCmd::create(src, metadataName, v); | ||
PvtApi::cast(RtApi::get())->getCommandEngine().execute(cmd, result); | ||
} | ||
catch (Exception& e) | ||
{ | ||
result = RtCommandResult(false, e.what()); | ||
} | ||
} | ||
|
||
void removeMetadata(const RtObject& src, const RtToken& metadataName, RtCommandResult& result) | ||
{ | ||
try | ||
{ | ||
PvtCommandPtr cmd = PvtRemoveMetadataCmd::create(src, metadataName); | ||
PvtApi::cast(RtApi::get())->getCommandEngine().execute(cmd, result); | ||
} | ||
catch (Exception& e) | ||
{ | ||
result = RtCommandResult(false, e.what()); | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// TM & (c) 2020 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd. | ||
// All rights reserved. See LICENSE.txt for license. | ||
// | ||
|
||
#ifndef MATERIALX_RTMETADATACOMMANDS_H | ||
#define MATERIALX_RTMETADATACOMMANDS_H | ||
|
||
/// @file | ||
/// Commands for connection handling. | ||
|
||
#include <MaterialXRuntime/RtCommand.h> | ||
#include <MaterialXRuntime/RtToken.h> | ||
|
||
namespace MaterialX | ||
{ | ||
|
||
namespace RtCommand | ||
{ | ||
/// Set a metadata value from data given as a value string. Creates the metadata if it doesn't exist. | ||
void setMetadata(const RtObject& src, const RtToken& metadataName, const string& metadataValue, RtCommandResult& result); | ||
|
||
/// Removes the named metadata | ||
void removeMetadata(const RtObject& src, const RtToken& metadataName, RtCommandResult& result); | ||
} | ||
|
||
} | ||
|
||
#endif |
112 changes: 112 additions & 0 deletions
112
source/MaterialXRuntime/Private/Commands/PvtRemoveMetadataCmd.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// | ||
// TM & (c) 2020 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd. | ||
// All rights reserved. See LICENSE.txt for license. | ||
// | ||
|
||
#include <MaterialXRuntime/Private/Commands/PvtRemoveMetadataCmd.h> | ||
#include <MaterialXRuntime/Private/PvtCommand.h> | ||
|
||
#include <MaterialXRuntime/RtPrim.h> | ||
#include <MaterialXRuntime/RtTypeDef.h> | ||
#include <MaterialXRuntime/RtValue.h> | ||
|
||
namespace MaterialX | ||
{ | ||
|
||
PvtRemoveMetadataCmd::PvtRemoveMetadataCmd(const RtObject& obj, const RtToken& name) | ||
: PvtCommand() | ||
, _obj(obj) | ||
, _name(name) | ||
, _oldValue() | ||
{ | ||
} | ||
|
||
PvtCommandPtr PvtRemoveMetadataCmd::create(const RtObject& obj, const RtToken& name) | ||
{ | ||
return std::make_shared<PvtRemoveMetadataCmd>(obj, name); | ||
} | ||
|
||
void PvtRemoveMetadataCmd::execute(RtCommandResult& result) | ||
{ | ||
if (_obj.isValid()) | ||
{ | ||
try | ||
{ | ||
RtTypedValue* md = _obj.getMetadata(_name, RtType::STRING); | ||
if (md) | ||
{ | ||
// Send message that the metadata is being removed | ||
msg().sendRemoveMetadataMessage(_obj, _name); | ||
|
||
// Save old value for undo/redo | ||
_oldValue = RtValue::clone(RtType::STRING, md->getValue(), _obj.getParent()); | ||
|
||
// Remove the metadata value | ||
_obj.removeMetadata(_name); | ||
result = RtCommandResult(true); | ||
} | ||
else | ||
{ | ||
result = RtCommandResult(false, string("Object does not have specified metadata")); | ||
} | ||
} | ||
catch (const ExceptionRuntimeError& e) | ||
{ | ||
result = RtCommandResult(false, string(e.what())); | ||
} | ||
} | ||
else | ||
{ | ||
result = RtCommandResult(false, string("Object to remove metadata from is no longer valid")); | ||
} | ||
} | ||
|
||
void PvtRemoveMetadataCmd::undo(RtCommandResult& result) | ||
{ | ||
if (_obj.isValid()) | ||
{ | ||
try | ||
{ | ||
// Send message that the metadata is being set | ||
msg().sendSetMetadataMessage(_obj, _name, _oldValue); | ||
|
||
RtTypedValue* md = _obj.addMetadata(_name, RtType::STRING); | ||
md->setValue(_oldValue); | ||
result = RtCommandResult(true); | ||
} | ||
catch (const ExceptionRuntimeError& e) | ||
{ | ||
result = RtCommandResult(false, string(e.what())); | ||
} | ||
} | ||
else | ||
{ | ||
result = RtCommandResult(false, string("Node to add metadata to is no longer valid")); | ||
} | ||
} | ||
|
||
void PvtRemoveMetadataCmd::redo(RtCommandResult& result) | ||
{ | ||
if (_obj.isValid()) | ||
{ | ||
try | ||
{ | ||
// Send message that the metadata is being removed | ||
msg().sendRemoveMetadataMessage(_obj, _name); | ||
|
||
// Remove the metadata value | ||
_obj.removeMetadata(_name); | ||
result = RtCommandResult(true); | ||
} | ||
catch (const ExceptionRuntimeError& e) | ||
{ | ||
result = RtCommandResult(false, string(e.what())); | ||
} | ||
} | ||
else | ||
{ | ||
result = RtCommandResult(false, string("Node to remove metadata from is no longer valid")); | ||
} | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
source/MaterialXRuntime/Private/Commands/PvtRemoveMetadataCmd.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// | ||
// TM & (c) 2020 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd. | ||
// All rights reserved. See LICENSE.txt for license. | ||
// | ||
|
||
#ifndef MATERIALX_PVTREMOVEMETADATACMD_H | ||
#define MATERIALX_PVTREMOVEMETADATACMD_H | ||
|
||
#include <MaterialXRuntime/Private/PvtCommand.h> | ||
|
||
#include <MaterialXRuntime/RtValue.h> | ||
|
||
namespace MaterialX | ||
{ | ||
|
||
class PvtRemoveMetadataCmd : public PvtCommand | ||
{ | ||
public: | ||
PvtRemoveMetadataCmd(const RtObject& obj, const RtToken& name); | ||
|
||
static PvtCommandPtr create(const RtObject& obj, const RtToken& name); | ||
|
||
void execute(RtCommandResult& result) override; | ||
void undo(RtCommandResult& result) override; | ||
void redo(RtCommandResult& result) override; | ||
|
||
private: | ||
RtObject _obj; | ||
RtToken _name; | ||
RtValue _oldValue; | ||
}; | ||
|
||
} | ||
|
||
#endif |
143 changes: 143 additions & 0 deletions
143
source/MaterialXRuntime/Private/Commands/PvtSetMetadataCmd.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
// | ||
// TM & (c) 2020 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd. | ||
// All rights reserved. See LICENSE.txt for license. | ||
// | ||
|
||
#include <MaterialXRuntime/Private/Commands/PvtSetMetadataCmd.h> | ||
#include <MaterialXRuntime/Private/PvtCommand.h> | ||
|
||
#include <MaterialXRuntime/RtPrim.h> | ||
#include <MaterialXRuntime/RtTypeDef.h> | ||
#include <MaterialXRuntime/RtValue.h> | ||
|
||
namespace MaterialX | ||
{ | ||
|
||
PvtSetMetadataCmd::PvtSetMetadataCmd(const RtObject& obj, const RtToken& name, const RtValue& value) | ||
: PvtCommand() | ||
, _obj(obj) | ||
, _name(name) | ||
, _value(value) | ||
, _oldValue() | ||
, _metadataCreated(false) | ||
{ | ||
} | ||
|
||
PvtCommandPtr PvtSetMetadataCmd::create(const RtObject& obj, const RtToken& name, const RtValue& value) | ||
{ | ||
return std::make_shared<PvtSetMetadataCmd>(obj, name, value); | ||
} | ||
|
||
void PvtSetMetadataCmd::execute(RtCommandResult& result) | ||
{ | ||
if (_obj.isValid()) | ||
{ | ||
try | ||
{ | ||
// Send message that the metadata is changing | ||
msg().sendSetMetadataMessage(_obj, _name, _value); | ||
|
||
RtTypedValue* md = _obj.getMetadata(_name, RtType::STRING); | ||
|
||
// Do we need to create the metadata or does it already exist? | ||
if (!md) | ||
{ | ||
md = _obj.addMetadata(_name, RtType::STRING); | ||
_metadataCreated = true; | ||
} | ||
|
||
// Save old value for undo/redo | ||
_oldValue = RtValue::clone(RtType::STRING, md->getValue(), _obj.getParent()); | ||
|
||
md->setValue(_value); | ||
result = RtCommandResult(true); | ||
} | ||
catch (const ExceptionRuntimeError& e) | ||
{ | ||
result = RtCommandResult(false, string(e.what())); | ||
} | ||
} | ||
else | ||
{ | ||
result = RtCommandResult(false, string("Object to set metadata on is no longer valid")); | ||
} | ||
} | ||
|
||
void PvtSetMetadataCmd::undo(RtCommandResult& result) | ||
{ | ||
if (_obj.isValid()) | ||
{ | ||
try | ||
{ | ||
|
||
if (_metadataCreated) | ||
{ | ||
// Send message that the metadata is being removed | ||
msg().sendRemoveMetadataMessage(_obj, _name); | ||
|
||
_obj.removeMetadata(_name); | ||
_metadataCreated = false; | ||
result = RtCommandResult(true); | ||
} | ||
else | ||
{ | ||
// Send message that the metadata is changing | ||
msg().sendSetMetadataMessage(_obj, _name, _oldValue); | ||
|
||
// Reset the value | ||
RtTypedValue* md = _obj.getMetadata(_name, RtType::STRING); | ||
if (md) | ||
{ | ||
md->setValue(_oldValue); | ||
result = RtCommandResult(true); | ||
} | ||
else | ||
{ | ||
result = RtCommandResult(false, "Metadata is no longer valid"); | ||
} | ||
} | ||
} | ||
catch (const ExceptionRuntimeError& e) | ||
{ | ||
result = RtCommandResult(false, string(e.what())); | ||
} | ||
} | ||
else | ||
{ | ||
result = RtCommandResult(false, string("Object to set metadata on is no longer valid")); | ||
} | ||
} | ||
|
||
void PvtSetMetadataCmd::redo(RtCommandResult& result) | ||
{ | ||
if (_obj.isValid()) | ||
{ | ||
try | ||
{ | ||
// Send message that the metadata is changing | ||
msg().sendSetMetadataMessage(_obj, _name, _value); | ||
|
||
RtTypedValue* md = _obj.getMetadata(_name, RtType::STRING); | ||
|
||
// Do we need to create the metadata or does it already exist? | ||
if (!md) | ||
{ | ||
md = _obj.addMetadata(_name, RtType::STRING); | ||
_metadataCreated = true; | ||
} | ||
|
||
// Reset the value | ||
md->setValue(_value); | ||
} | ||
catch (const ExceptionRuntimeError& e) | ||
{ | ||
result = RtCommandResult(false, string(e.what())); | ||
} | ||
} | ||
else | ||
{ | ||
result = RtCommandResult(false, string("Object to set metadata on is no longer valid")); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.