Class: OpenUSD::Layer
- Inherits:
-
Object
- Object
- OpenUSD::Layer
- Defined in:
- lib/openusd/layer.rb
Overview
A single authored USD layer.
Instance Attribute Summary collapse
-
#identifier ⇒ Object
Returns the value of attribute identifier.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#root_prims ⇒ Object
readonly
Returns the value of attribute root_prims.
Class Method Summary collapse
-
.create(identifier) ⇒ Layer
Create an empty layer.
-
.open(path) ⇒ Layer
Open a USDA, USD, or USDZ layer.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Compare authored semantic content, ignoring identifiers.
- #add_root_prim(prim) ⇒ Object
-
#default_prim ⇒ PrimSpec?
Authored default prim.
-
#each_prim(&block) ⇒ Enumerator, Layer
Traverse authored specs depth-first.
-
#export(path) ⇒ Layer
Export using the format selected by the destination extension.
-
#initialize(identifier = nil, metadata: {}, root_prims: []) ⇒ Layer
constructor
A new instance of Layer.
- #prim_at(path) ⇒ Object
-
#remove_root_prim(name) ⇒ PrimSpec?
Remove and return a root prim by name.
-
#root_prim_named(name) ⇒ PrimSpec?
Root prim with the requested name.
- #save ⇒ Object
-
#sub_layer_paths ⇒ Array<String>
Authored sublayer asset paths.
-
#to_usda ⇒ String
Deterministic USDA representation.
Constructor Details
#initialize(identifier = nil, metadata: {}, root_prims: []) ⇒ Layer
Returns a new instance of Layer.
30 31 32 33 34 35 36 |
# File 'lib/openusd/layer.rb', line 30 def initialize(identifier = nil, metadata: {}, root_prims: []) @identifier = identifier&.to_s @metadata = .dup @root_prims = [] @root_prim_index = {} root_prims.each { |prim| add_root_prim(prim) } end |
Instance Attribute Details
#identifier ⇒ Object
Returns the value of attribute identifier.
6 7 8 |
# File 'lib/openusd/layer.rb', line 6 def identifier @identifier end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
6 7 8 |
# File 'lib/openusd/layer.rb', line 6 def @metadata end |
#root_prims ⇒ Object (readonly)
Returns the value of attribute root_prims.
6 7 8 |
# File 'lib/openusd/layer.rb', line 6 def root_prims @root_prims end |
Class Method Details
.create(identifier) ⇒ Layer
Create an empty layer.
11 12 13 |
# File 'lib/openusd/layer.rb', line 11 def create(identifier) new(identifier) end |
.open(path) ⇒ Layer
Open a USDA, USD, or USDZ layer.
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/openusd/layer.rb', line 17 def open(path) if path.to_s.match?(/\.usdz\[[^\]]+\]\z/i) require_relative "format/usdz/reader" return Format::Usdz::Reader.read_uri(path.to_s) end = File.(path) Format::Registry.reader_for().read() rescue Errno::ENOENT raise CompositionError, "layer not found: #{}" end |
Instance Method Details
#==(other) ⇒ Object
Compare authored semantic content, ignoring identifiers.
121 122 123 124 |
# File 'lib/openusd/layer.rb', line 121 def ==(other) other.is_a?(self.class) && == other. && root_prims.map(&:to_h) == other.root_prims.map(&:to_h) end |
#add_root_prim(prim) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/openusd/layer.rb', line 42 def add_root_prim(prim) raise OpenUSD::TypeError, "root prim must be a PrimSpec" unless prim.is_a?(PrimSpec) raise PathError, "duplicate root prim: #{prim.name}" if @root_prim_index.key?(prim.name) prim.parent = nil root_prims << prim @root_prim_index[prim.name] = prim prim end |
#default_prim ⇒ PrimSpec?
Returns authored default prim.
95 96 97 98 99 |
# File 'lib/openusd/layer.rb', line 95 def default_prim name = ["defaultPrim"] name = name.to_s if name root_prim_named(name) end |
#each_prim(&block) ⇒ Enumerator, Layer
Traverse authored specs depth-first.
78 79 80 81 82 83 84 85 |
# File 'lib/openusd/layer.rb', line 78 def each_prim(&block) return enum_for(__method__) unless block root_prims.each do |prim| yield prim prim.each_descendant(&block) end end |
#export(path) ⇒ Layer
Export using the format selected by the destination extension.
109 110 111 112 |
# File 'lib/openusd/layer.rb', line 109 def export(path) Format::Registry.writer_for(path).write(self, path) self end |
#prim_at(path) ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/openusd/layer.rb', line 66 def prim_at(path) parsed = Path.parse(path) raise PathError, "prim path required" if parsed.property? return nil unless parsed.absolute? return nil if parsed.to_s == "/" names = parsed.to_s.delete_prefix("/").split("/") names.drop(1).reduce(root_prim_named(names.first)) { |prim, name| prim&.child_named(name) } end |
#remove_root_prim(name) ⇒ PrimSpec?
Remove and return a root prim by name.
54 55 56 57 58 59 |
# File 'lib/openusd/layer.rb', line 54 def remove_root_prim(name) prim = root_prim_named(name) root_prims.delete(prim) @root_prim_index.delete(name.to_s) prim end |
#root_prim_named(name) ⇒ PrimSpec?
Returns root prim with the requested name.
62 63 64 |
# File 'lib/openusd/layer.rb', line 62 def root_prim_named(name) @root_prim_index[name.to_s] end |
#save ⇒ Object
101 102 103 104 105 |
# File 'lib/openusd/layer.rb', line 101 def save raise Error, "in-memory layer has no identifier" unless identifier export(identifier) end |
#sub_layer_paths ⇒ Array<String>
Returns authored sublayer asset paths.
88 89 90 91 92 |
# File 'lib/openusd/layer.rb', line 88 def sub_layer_paths value = ["subLayers"] value = value.value if value.is_a?(ListOp) Array(value).map { |path| path.is_a?(AssetPath) ? path.path : path.to_s } end |
#to_usda ⇒ String
Returns deterministic USDA representation.
115 116 117 118 |
# File 'lib/openusd/layer.rb', line 115 def to_usda require_relative "format/usda/writer" Format::Usda::Writer.new.write_to_string(self) end |