Class: OpenUSD::Layer

Inherits:
Object
  • Object
show all
Defined in:
lib/openusd/layer.rb

Overview

A single authored USD layer.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#identifierObject

Returns the value of attribute identifier.



6
7
8
# File 'lib/openusd/layer.rb', line 6

def identifier
  @identifier
end

#metadataObject (readonly)

Returns the value of attribute metadata.



6
7
8
# File 'lib/openusd/layer.rb', line 6

def 
  @metadata
end

#root_primsObject (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.

Returns:



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.

Returns:



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

  expanded = File.expand_path(path)
  Format::Registry.reader_for(expanded).read(expanded)
rescue Errno::ENOENT
  raise CompositionError, "layer not found: #{expanded}"
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

Raises:



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_primPrimSpec?

Returns authored default prim.

Returns:

  • (PrimSpec, nil)

    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.

Returns:



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.

Returns:



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

Raises:



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.

Returns:



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.

Returns:

  • (PrimSpec, nil)

    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

#saveObject

Raises:



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_pathsArray<String>

Returns authored sublayer asset paths.

Returns:

  • (Array<String>)

    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_usdaString

Returns deterministic USDA representation.

Returns:

  • (String)

    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