Skip to content

Commit

Permalink
Add AggregateValue to centralize struct string parsing. Add support f…
Browse files Browse the repository at this point in the history
…or recursive structs in places - may not be complete yet - centralized syntax parsing - and unit tests pass currently.
  • Loading branch information
ld-kerley committed May 29, 2024
1 parent 108008b commit 4403f1e
Show file tree
Hide file tree
Showing 36 changed files with 554 additions and 399 deletions.
2 changes: 1 addition & 1 deletion libraries/experimental/experimental_defs.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</typedef>

<nodedef name="ND_extract_s_texcoord" node="extracts" nodegroup="shader" >
<input name="in" type="texCoord" value="0.1;0.1" />
<input name="in" type="texCoord" value="{0.1;0.1}" />
<!-- <input name="taco" type="float" value="0.123"/>-->
<output name="out" type="float" value="0.0" />
</nodedef>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<nodegraph name="test_struct_texcoord">

<extracts name="extracts" type="float">
<input name="in" type="texCoord" value="{0.1;0.5}"/>
<input name="in" type="texCoord" value="{0.01;0.5}"/>
</extracts>

<convert name="float_to_color3" type="color3">
Expand Down
8 changes: 8 additions & 0 deletions source/MaterialXCore/Definition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,12 @@ vector<UnitDefPtr> UnitTypeDef::getUnitDefs() const
return unitDefs;
}

ValuePtr AttributeDef::getValue() const
{
if (!hasValue())
return ValuePtr();

return Value::createValueFromStrings(getValueString(), getType(), getDocument()->getTypeDef(getType()));
}

MATERIALX_NAMESPACE_END
117 changes: 56 additions & 61 deletions source/MaterialXCore/Definition.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,61 +17,61 @@ MATERIALX_NAMESPACE_BEGIN

extern MX_CORE_API const string COLOR_SEMANTIC;
extern MX_CORE_API const string SHADER_SEMANTIC;

class NodeDef;
class Implementation;
class TypeDef;
class TargetDef;
class Member;
class Unit;
class UnitDef;
class UnitTypeDef;
class AttributeDef;

/// A shared pointer to a NodeDef
using NodeDefPtr = shared_ptr<NodeDef>;
/// A shared pointer to a const NodeDef
using ConstNodeDefPtr = shared_ptr<const NodeDef>;

/// A shared pointer to an Implementation
using ImplementationPtr = shared_ptr<Implementation>;
/// A shared pointer to a const Implementation
using ConstImplementationPtr = shared_ptr<const Implementation>;

/// A shared pointer to a TypeDef
using TypeDefPtr = shared_ptr<TypeDef>;
/// A shared pointer to a const TypeDef
using ConstTypeDefPtr = shared_ptr<const TypeDef>;

/// A shared pointer to a TargetDef
using TargetDefPtr = shared_ptr<TargetDef>;
/// A shared pointer to a const TargetDef
using ConstTargetDefPtr = shared_ptr<const TargetDef>;

/// A shared pointer to a Member
using MemberPtr = shared_ptr<Member>;
/// A shared pointer to a const Member
using ConstMemberPtr = shared_ptr<const Member>;

/// A shared pointer to a Unit
using UnitPtr = shared_ptr<Unit>;
/// A shared pointer to a const Unit
using ConstUnitPtr = shared_ptr<const Unit>;

/// A shared pointer to a UnitDef
using UnitDefPtr = shared_ptr<UnitDef>;
/// A shared pointer to a const UnitDef
using ConstUnitDefPtr = shared_ptr<const UnitDef>;

/// A shared pointer to a UnitTypeDef
using UnitTypeDefPtr = shared_ptr<UnitTypeDef>;
/// A shared pointer to a const UnitTypeDef
using ConstUnitTypeDefPtr = shared_ptr<const UnitTypeDef>;

/// A shared pointer to an AttributeDef
using AttributeDefPtr = shared_ptr<AttributeDef>;
/// A shared pointer to a const AttributeDef
using AttributeDefDefPtr = shared_ptr<const AttributeDef>;
//
//class NodeDef;
//class Implementation;
//class TypeDef;
//class TargetDef;
//class Member;
//class Unit;
//class UnitDef;
//class UnitTypeDef;
//class AttributeDef;
//
///// A shared pointer to a NodeDef
//using NodeDefPtr = shared_ptr<NodeDef>;
///// A shared pointer to a const NodeDef
//using ConstNodeDefPtr = shared_ptr<const NodeDef>;
//
///// A shared pointer to an Implementation
//using ImplementationPtr = shared_ptr<Implementation>;
///// A shared pointer to a const Implementation
//using ConstImplementationPtr = shared_ptr<const Implementation>;
//
///// A shared pointer to a TypeDef
//using TypeDefPtr = shared_ptr<TypeDef>;
///// A shared pointer to a const TypeDef
//using ConstTypeDefPtr = shared_ptr<const TypeDef>;
//
///// A shared pointer to a TargetDef
//using TargetDefPtr = shared_ptr<TargetDef>;
///// A shared pointer to a const TargetDef
//using ConstTargetDefPtr = shared_ptr<const TargetDef>;
//
///// A shared pointer to a Member
//using MemberPtr = shared_ptr<Member>;
///// A shared pointer to a const Member
//using ConstMemberPtr = shared_ptr<const Member>;
//
///// A shared pointer to a Unit
//using UnitPtr = shared_ptr<Unit>;
///// A shared pointer to a const Unit
//using ConstUnitPtr = shared_ptr<const Unit>;
//
///// A shared pointer to a UnitDef
//using UnitDefPtr = shared_ptr<UnitDef>;
///// A shared pointer to a const UnitDef
//using ConstUnitDefPtr = shared_ptr<const UnitDef>;
//
///// A shared pointer to a UnitTypeDef
//using UnitTypeDefPtr = shared_ptr<UnitTypeDef>;
///// A shared pointer to a const UnitTypeDef
//using ConstUnitTypeDefPtr = shared_ptr<const UnitTypeDef>;
//
///// A shared pointer to an AttributeDef
//using AttributeDefPtr = shared_ptr<AttributeDef>;
///// A shared pointer to a const AttributeDef
//using AttributeDefDefPtr = shared_ptr<const AttributeDef>;

/// @class NodeDef
/// A node definition element within a Document.
Expand Down Expand Up @@ -618,12 +618,7 @@ class MX_CORE_API AttributeDef : public TypedElement
///
/// @return A shared pointer to the typed value of this element, or an
/// empty shared pointer if no value is present.
ValuePtr getValue() const
{
if (!hasValue())
return ValuePtr();
return Value::createValueFromStrings(getValueString(), getType());
}
ValuePtr getValue() const;

/// @}
/// @name Elements
Expand Down
16 changes: 16 additions & 0 deletions source/MaterialXCore/Element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,22 @@ string ValueElement::getResolvedValueString(StringResolverPtr resolver) const
return resolver->resolve(getValueString(), getType());
}

ValuePtr ValueElement::getValue() const
{
if (!hasValue())
return ValuePtr();

return Value::createValueFromStrings(getValueString(), getType(), getDocument()->getTypeDef(getType()));
}

ValuePtr ValueElement::getResolvedValue(StringResolverPtr resolver) const
{
if (!hasValue())
return ValuePtr();

return Value::createValueFromStrings(getResolvedValueString(resolver), getType(), getDocument()->getTypeDef(getType()));
}

ValuePtr ValueElement::getDefaultValue() const
{
ConstElementPtr parent = getParent();
Expand Down
14 changes: 2 additions & 12 deletions source/MaterialXCore/Element.h
Original file line number Diff line number Diff line change
Expand Up @@ -1023,12 +1023,7 @@ class MX_CORE_API ValueElement : public TypedElement
///
/// @return A shared pointer to the typed value of this element, or an
/// empty shared pointer if no value is present.
ValuePtr getValue() const
{
if (!hasValue())
return ValuePtr();
return Value::createValueFromStrings(getValueString(), getType());
}
ValuePtr getValue() const;

/// Return the resolved value of an element as a generic value object, which
/// may be queried to access its data.
Expand All @@ -1038,12 +1033,7 @@ class MX_CORE_API ValueElement : public TypedElement
/// will be created at this scope and applied to the return value.
/// @return A shared pointer to the typed value of this element, or an
/// empty shared pointer if no value is present.
ValuePtr getResolvedValue(StringResolverPtr resolver = nullptr) const
{
if (!hasValue())
return ValuePtr();
return Value::createValueFromStrings(getResolvedValueString(resolver), getType());
}
ValuePtr getResolvedValue(StringResolverPtr resolver = nullptr) const;

/// Return the default value for this element as a generic value object, which
/// may be queried to access its data.
Expand Down
8 changes: 8 additions & 0 deletions source/MaterialXCore/Exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ class MX_CORE_API Exception : public std::exception
string _msg;
};

/// @class ExceptionTypeError
/// An exception that is thrown when a type mismatch is encountered.
class MX_CORE_API ExceptionTypeError : public Exception
{
public:
using Exception::Exception;
};

MATERIALX_NAMESPACE_END

#endif
73 changes: 73 additions & 0 deletions source/MaterialXCore/MaterialXCore_fwd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//
// Created by Lee Kerley on 5/23/24.
//

#ifndef MATERIALX_MATERIALXCORE_FWD_H
#define MATERIALX_MATERIALXCORE_FWD_H

#include <MaterialXCore/Export.h>


MATERIALX_NAMESPACE_BEGIN

extern MX_CORE_API const string COLOR_SEMANTIC;
extern MX_CORE_API const string SHADER_SEMANTIC;

class NodeDef;
class Implementation;
class TypeDef;
class TargetDef;
class Member;
class Unit;
class UnitDef;
class UnitTypeDef;
class AttributeDef;

/// A shared pointer to a NodeDef
using NodeDefPtr = shared_ptr<NodeDef>;
/// A shared pointer to a const NodeDef
using ConstNodeDefPtr = shared_ptr<const NodeDef>;

/// A shared pointer to an Implementation
using ImplementationPtr = shared_ptr<Implementation>;
/// A shared pointer to a const Implementation
using ConstImplementationPtr = shared_ptr<const Implementation>;

/// A shared pointer to a TypeDef
using TypeDefPtr = shared_ptr<TypeDef>;
/// A shared pointer to a const TypeDef
using ConstTypeDefPtr = shared_ptr<const TypeDef>;

/// A shared pointer to a TargetDef
using TargetDefPtr = shared_ptr<TargetDef>;
/// A shared pointer to a const TargetDef
using ConstTargetDefPtr = shared_ptr<const TargetDef>;

/// A shared pointer to a Member
using MemberPtr = shared_ptr<Member>;
/// A shared pointer to a const Member
using ConstMemberPtr = shared_ptr<const Member>;

/// A shared pointer to a Unit
using UnitPtr = shared_ptr<Unit>;
/// A shared pointer to a const Unit
using ConstUnitPtr = shared_ptr<const Unit>;

/// A shared pointer to a UnitDef
using UnitDefPtr = shared_ptr<UnitDef>;
/// A shared pointer to a const UnitDef
using ConstUnitDefPtr = shared_ptr<const UnitDef>;

/// A shared pointer to a UnitTypeDef
using UnitTypeDefPtr = shared_ptr<UnitTypeDef>;
/// A shared pointer to a const UnitTypeDef
using ConstUnitTypeDefPtr = shared_ptr<const UnitTypeDef>;

/// A shared pointer to an AttributeDef
using AttributeDefPtr = shared_ptr<AttributeDef>;
/// A shared pointer to a const AttributeDef
using AttributeDefDefPtr = shared_ptr<const AttributeDef>;

MATERIALX_NAMESPACE_END

#endif // MATERIALX_MATERIALXCORE_FWD_H
52 changes: 52 additions & 0 deletions source/MaterialXCore/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,58 @@ string joinStrings(const StringVec& stringVec, const string& sep)
return res;
}

StringVec splitListInitializer(const string& str)
{
const char SEPARATOR = ';';
const char OPEN_BRACE = '{';
const char CLOSE_BRACE = '}';

if (str.empty())
return StringVec();

// validate the string is correctly formatted - must be atleast 2 characters long and start and end with braces
if (str.size() < 2 || (str[0] != OPEN_BRACE || str[str.size()-1] != CLOSE_BRACE))
{
// TODO - whats the best way to report this error?
printf("ERROR2");
return StringVec();
}

StringVec split;

// strip off the surrounding braces
string substring = str.substr(1, str.size()-2);

// sequentially examine each character to parse the list initializer.
string part = "";
int braceDepth = 0;
for (const char c : substring) {
if (c == OPEN_BRACE) {
// we've already trimmed the starting brace, so any additional braces indicate members that are themselves list initializers.
// we will just return this as a string of the list initializer.
braceDepth += 1;
}
if (braceDepth > 0 && c == CLOSE_BRACE) {
braceDepth -= 1;
}

if (braceDepth == 0 && c == SEPARATOR) {
// when we hit a separator we store the currently accumlated part, and clear to start collecting the next.
split.emplace_back(part);
part = "";
}
else
{
part += c;
}
}

if (!part.empty())
split.emplace_back(part);

return split;
}

string replaceSubstrings(string str, const StringMap& stringMap)
{
for (const auto& pair : stringMap)
Expand Down
5 changes: 5 additions & 0 deletions source/MaterialXCore/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ MX_CORE_API StringVec splitString(const string& str, const string& sep);
/// separator between each substring.
MX_CORE_API string joinStrings(const StringVec& strVec, const string& sep);

/// Split a string in the form of a list initializer, i.e, "{1,2,3}" into a
/// vector of substrings.
/// Note: "{1,2,{3,4,5}}" will be split in to ["1", "2", "{3,4,5}"]
MX_CORE_API StringVec splitListInitializer(const string& str);

/// Apply the given substring substitutions to the input string.
MX_CORE_API string replaceSubstrings(string str, const StringMap& stringMap);

Expand Down
Loading

0 comments on commit 4403f1e

Please sign in to comment.