dymokomi/luce-usd
Source of release 0.9.2
0.9.2 / src / flatten / properties.lucb
## Properties flattened (_CopyProperty): an attribute keeps its composed
## type, custom, variability and metadata, the time samples or spline of
## its strongest value opinion when that opinion is one (moved into stage
## time), its strongest default when any opinion authors one (an array
## edit composed over what it edits), and its connections in stage
## namespace as an explicit list; a relationship keeps its metadata and its
## targets likewise.
extend Flattener:
## Flatten property `name` of the prim at `path` (output spec `prim`).
func property(index: const PrimIndex*, path: u32, prim: usize, name: u32) -> !:
var opinions = Growing[Opinion]()
defer opinions.close()
try self.gather(index, name, &opinions)
if opinions.count == 0: return
let composer = self.composer
let keys = &self.out.keys
let first = opinions.items[0]
let kind = composer.layers.layer(first.layer).spec(first.spec).kind
# Specs of the other property kind are not part of this property.
var kept = Growing[Opinion]()
defer kept.close()
for opinion in opinions.view():
if composer.layers.layer(opinion.layer).spec(opinion.spec).kind == kind: try kept.push(opinion)
let attribute = kind == SpecType.attribute
# A built-in property (UsdPrimDefinition) takes its type and
# variability from its schema, and is not custom.
var builtin = self.builtin(name)
if let found = builtin:
if found.relationship == attribute: builtin = none
if attribute and builtin == none and self.type_name(kept.view()) == 0: return
let property_path = try self.names.property(path, name)
let spec = try self.out.create_spec(property_path, kind)
try self.grow(prim)
try self.properties.items[prim].push(name)
var fields = Growing[u32]()
defer fields.close()
for opinion in kept.view():
for field in composer.layers.layer(opinion.layer).spec(opinion.spec).fields.view():
if not self.private_field(field.name, kind): try add_unique(&fields, field.name)
try add_unique(&fields, keys.custom)
try add_unique(&fields, keys.variability)
if attribute and builtin != none: try add_unique(&fields, keys.type_name)
for key in fields.view():
if key == keys.custom:
# Custom and variability come from the weakest spec of any
# kind (_GetPropCustomImpl, _GetAttrVariabilityImpl): both are
# required fields, so a spec without one gives the fallback
# (SdfLayer::HasField).
var custom: u64 = 0
if let found = self.weakest_spec_field(opinions.view(), key): custom = found.bits
if builtin != none: custom = 0
try self.out.set(spec, key, Value.scalar(ValueKind.boolean, custom))
continue
if key == keys.variability and attribute:
var variability = variability_varying
if let found = self.weakest_spec_field(opinions.view(), key): variability = found.bits
if let found = builtin: variability = variability_uniform if found.uniform else variability_varying
try self.out.set(spec, key, Value.scalar(ValueKind.variability, variability))
continue
if key == keys.type_name and attribute:
if let found = builtin:
try self.out.set(spec, key, Value.scalar(ValueKind.token, (u64)found.type_name))
continue
if let value = try self.composed_value(index, kept.view(), key): try self.out.set(spec, key, value)
var targets = Growing[u32]()
defer targets.close()
var deleted = Growing[u32]()
defer deleted.close()
try composer.target_paths(index, name, attribute, false, &targets, &deleted)
if targets.count > 0:
let list = try ListOp.create()
try self.out.set(spec, keys.connection_paths if attribute else keys.target_paths, Value.of_object(ValueKind.path_list_op, (void*)list))
list.explicit = true
for target in targets.view(): try list.push(ListPart.explicit, ListItem(a = (u64)(try self.in_prototype(target)), b = 0, offset = 0.0, scale = 1.0, custom = none))
if attribute: try self.attribute_values(index, kept.view(), spec)
## An attribute's samples (when its strongest value opinion is samples)
## and default (when any opinion has one).
func attribute_values(index: const PrimIndex*, opinions: const Opinion[], spec: usize) -> !:
let keys = &self.out.keys
let composer = self.composer
for opinion in opinions:
let data = composer.layers.layer(opinion.layer)
if let found = data.get(opinion.spec, keys.time_samples):
let samples = found.time_samples() else break
if samples.count() == 0: continue
try self.out.set(spec, keys.time_samples, try self.samples_to_stage(index, opinion, samples))
break
if let found = data.get(opinion.spec, keys.spline):
let offset = self.layer_offset(index, opinion)
try self.out.set(spec, keys.spline, try offset_spline(found, offset.offset, offset.scale))
break
if data.get(opinion.spec, keys.default) != none: break
# An animation block is no default: a weaker one shows through, and
# with none the default is blocked (attr.Get fails, SdfValueBlock).
var blocked = false
for at in 0..<opinions.length:
let opinion = opinions[at]
let found = composer.layers.layer(opinion.layer).get(opinion.spec, keys.default) else continue
if found.kind == ValueKind.animation_block:
blocked = true
continue
if composes(found): try self.out.set(spec, keys.default, try self.composed_edits(index, opinions[at..]))
else: try self.out.set(spec, keys.default, try self.value_to_stage(index, opinion, found))
blocked = false
break
if blocked: try self.out.set(spec, keys.default, Value.marker(ValueKind.value_block))
## Time samples in stage time, their values moved too.
func samples_to_stage(index: const PrimIndex*, opinion: Opinion, samples: const TimeSamples*) -> Value!:
let offset = self.layer_offset(index, opinion)
let made = try TimeSamples.create()
var owner = Value.of_object(ValueKind.time_samples, (void*)made)
errdefer owner.release()
for at in 0..<samples.count():
let sample = &samples.values.items[at]
# An edited sample composes over the empty background.
if composes(sample):
let background = Value.marker(ValueKind.empty)
try made.put(offset.apply(samples.times.items[at]), try edited(sample, &background))
else: try made.put(offset.apply(samples.times.items[at]), try self.value_to_stage(index, opinion, sample))
return owner
## The strongest default, an array edit, composed over the weaker
## defaults (VtValue's ComposeOver): edits stack down to an array, or to
## the empty background.
func composed_edits(index: const PrimIndex*, opinions: const Opinion[]) -> Value!:
let keys = &self.out.keys
var stack = Growing[const Value*]()
defer stack.close()
var base = Value.marker(ValueKind.empty)
defer base.release()
for opinion in opinions:
let found = self.composer.layers.layer(opinion.layer).get(opinion.spec, keys.default) else continue
if composes(found):
try stack.push(found)
continue
if found.array: base = try self.value_to_stage(index, opinion, found)
break
var result = try edited(stack.items[stack.count - 1], &base)
var at = stack.count - 1
while at > 0:
at -= 1
let stronger = try edited(stack.items[at], &result)
result.release()
result = stronger
return result
## An attribute value in stage terms (_MakeResolvedAssetPathsValue and
## Usd_ApplyLayerOffsetToValue): asset paths anchored, time codes moved.
func value_to_stage(index: const PrimIndex*, opinion: Opinion, value: const Value*) -> Value!:
return try self.to_stage(index, opinion, value)
## Whether a value is an array edit flattening composes (asset edits stay
## edits, as UsdStage's flatten leaves them).
func composes(value: const Value*) -> bool:
let edit = value.array_edit() else return false
return edit.kind != ValueKind.asset
## The edit in `value` applied to `weaker` (an array of its kind; the empty
## array otherwise).
func edited(value: const Value*, weaker: const Value*) -> Value!:
let edit = value.array_edit() else return value.share()
var bytes = Growing[u8]()
defer bytes.close()
var none_found: u8[]
var source: const u8[] = none_found
if weaker.kind == edit.kind and weaker.array: source = weaker.bytes()
try apply_edit(edit, source, &bytes)
let size = element_bytes(edit.kind)
return Value.of_array(edit.kind, bytes.count // size, try Column[u8].copy_of(bytes.view()))