Skip to content

Commit

Permalink
LOOKDEVX-407 | New metadata commands for setting/removing metadata. (#…
Browse files Browse the repository at this point in the history
…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
feldstj and feldstj authored Feb 18, 2021
1 parent 6494791 commit 1018ba4
Show file tree
Hide file tree
Showing 12 changed files with 575 additions and 3 deletions.
4 changes: 2 additions & 2 deletions source/MaterialXRuntime/Commands/AttributeCommands.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// All rights reserved. See LICENSE.txt for license.
//

#ifndef MATERIALX_RTCONNECTIONCOMMANDS_H
#define MATERIALX_RTCONNECTIONCOMMANDS_H
#ifndef MATERIALX_RTATTRIBUTECOMMANDS_H
#define MATERIALX_RTATTRIBUTECOMMANDS_H

/// @file
/// Commands for connection handling.
Expand Down
50 changes: 50 additions & 0 deletions source/MaterialXRuntime/Commands/MetadataCommands.cpp
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());
}
}

}

}
29 changes: 29 additions & 0 deletions source/MaterialXRuntime/Commands/MetadataCommands.h
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 source/MaterialXRuntime/Private/Commands/PvtRemoveMetadataCmd.cpp
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 source/MaterialXRuntime/Private/Commands/PvtRemoveMetadataCmd.h
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 source/MaterialXRuntime/Private/Commands/PvtSetMetadataCmd.cpp
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"));
}
}

}
Loading

0 comments on commit 1018ba4

Please sign in to comment.