def generate_composable_listops(num=3): lists = generate_lists(num) for explicit in lists: yield _ExplicitItems(explicit) for (a,b,c) in itertools.combinations_with_replacement(lists, 3): op = Sdf.IntListOp() (op.appendedItems, op.prependedItems, op.deletedItems) = (a,b,c) yield op
def test_BasicSemantics(self): # Default empty listop does nothing. self.assertEqual(Sdf.IntListOp().ApplyOperations([]), []) # Explicit items replace whatever was there previously. self.assertEqual( _ExplicitItems([1, 2, 3]).ApplyOperations([]), [1, 2, 3]) self.assertEqual( _ExplicitItems([1, 2, 3]).ApplyOperations([0, 3]), [1, 2, 3]) # "Add" leaves existing values in place and appends any new values. self.assertEqual(_AddedItems([1, 2, 3]).ApplyOperations([]), [1, 2, 3]) self.assertEqual( _AddedItems([1, 2, 3]).ApplyOperations([3, 2, 1]), [3, 2, 1]) self.assertEqual( _AddedItems([1, 2, 3]).ApplyOperations([0, 3]), [0, 3, 1, 2]) # "Delete" removes values and leaves the rest in place, in order. self.assertEqual(_DeletedItems([1, 2, 3]).ApplyOperations([]), []) self.assertEqual( _DeletedItems([1, 2, 3]).ApplyOperations([0, 3, 5]), [0, 5]) self.assertEqual( _DeletedItems([1, 2, 3]).ApplyOperations([1024, 1]), [1024]) # "Append" adds the given items to the end of the list, moving # them if they existed previously. self.assertEqual( _AppendedItems([1, 2, 3]).ApplyOperations([]), [1, 2, 3]) self.assertEqual( _AppendedItems([1, 2, 3]).ApplyOperations([2]), [1, 2, 3]) self.assertEqual( _AppendedItems([1, 2, 3]).ApplyOperations([3, 4]), [4, 1, 2, 3]) # "Prepend" is similar, but for the front of the list. self.assertEqual( _PrependedItems([1, 2, 3]).ApplyOperations([]), [1, 2, 3]) self.assertEqual( _PrependedItems([1, 2, 3]).ApplyOperations([2]), [1, 2, 3]) self.assertEqual( _PrependedItems([1, 2, 3]).ApplyOperations([0, 1]), [1, 2, 3, 0]) # "Order" is the most subtle. self.assertEqual(_OrderedItems([1, 2, 3]).ApplyOperations([]), []) self.assertEqual(_OrderedItems([1, 2, 3]).ApplyOperations([2]), [2]) self.assertEqual( _OrderedItems([1, 2, 3]).ApplyOperations([2, 1]), [1, 2]) self.assertEqual( _OrderedItems([1, 2, 3]).ApplyOperations([2, 4, 1]), [1, 2, 4]) # Values that were not mentioned in the "ordered items" list # will end up in a position that depends on their position relative # to the nearest surrounding items that are in the ordered items list. self.assertEqual( _OrderedItems([1, 2, 3]).ApplyOperations([0, 2, 4, 1, 5, 3]), [0, 1, 5, 2, 4, 3])
def test_ComposedListOpMetadata(self): '''Tests composition of list op-valued metadata fields''' def _TestComposition(fieldName, weakListOp, strongListOp, expectedListOp): for fmt in allFormats: s = Usd.Stage.CreateNew("TestComposedListOpMetadata."+fmt) ref = s.OverridePrim('/Ref') ref.SetMetadata(fieldName, weakListOp) root = s.OverridePrim('/Root') root.SetMetadata(fieldName, strongListOp) root.GetReferences().AddInternalReference('/Ref') self.assertEqual(root.GetMetadata(fieldName), expectedListOp) # Sdf.IntListOp weakListOp = Sdf.IntListOp() weakListOp.explicitItems = [10, 20, 30] strongListOp = Sdf.IntListOp() strongListOp.addedItems = [-2147483648, 1, 2, 3, 2147483647] strongListOp.deletedItems = [-2147483648, 10, 20, 30, 2147483647] strongListOp.orderedItems = [2147483647, 3, 2, 1, -2147483648] expectedListOp = Sdf.IntListOp() expectedListOp.explicitItems = [2147483647, 3, 2, 1, -2147483648] _TestComposition('intListOpTest', weakListOp, strongListOp, expectedListOp) # Sdf.Int64ListOp weakListOp = Sdf.Int64ListOp() weakListOp.explicitItems = [10, 20, 30] strongListOp = Sdf.Int64ListOp() strongListOp.addedItems = [-9223372036854775808, 1, 2, 3, 9223372036854775807] strongListOp.deletedItems = [-9223372036854775808, 10, 20, 30, 9223372036854775807] strongListOp.orderedItems = [9223372036854775807, 3, 2, 1, -9223372036854775808] expectedListOp = Sdf.Int64ListOp() expectedListOp.explicitItems = [9223372036854775807, 3, 2, 1, -9223372036854775808] _TestComposition('int64ListOpTest', weakListOp, strongListOp, expectedListOp) # Sdf.UIntListOp weakListOp = Sdf.UIntListOp() weakListOp.explicitItems = [10, 20, 30] strongListOp = Sdf.UIntListOp() strongListOp.addedItems = [1, 2, 3, 4294967295] strongListOp.deletedItems = [10, 20, 30, 4294967295] strongListOp.orderedItems = [4294967295, 3, 2, 1] expectedListOp = Sdf.UIntListOp() expectedListOp.explicitItems = [4294967295, 3, 2, 1] _TestComposition('uintListOpTest', weakListOp, strongListOp, expectedListOp) # Sdf.UInt64ListOp weakListOp = Sdf.UInt64ListOp() weakListOp.explicitItems = [10, 20, 30] strongListOp = Sdf.UInt64ListOp() strongListOp.addedItems = [1, 2, 3, 18446744073709551615] strongListOp.deletedItems = [10, 20, 30, 18446744073709551615] strongListOp.orderedItems = [18446744073709551615, 3, 2, 1] expectedListOp = Sdf.UInt64ListOp() expectedListOp.explicitItems = [18446744073709551615, 3, 2, 1] _TestComposition('uint64ListOpTest', weakListOp, strongListOp, expectedListOp) # Sdf.StringListOp weakListOp = Sdf.StringListOp() weakListOp.explicitItems = ["baz"] strongListOp = Sdf.StringListOp() strongListOp.addedItems = ["foo", "bar"] strongListOp.deletedItems = ["baz"] strongListOp.orderedItems = ["bar", "foo"] expectedListOp = Sdf.StringListOp() expectedListOp.explicitItems = ["bar", "foo"] _TestComposition('stringListOpTest', weakListOp, strongListOp, expectedListOp) # Sdf.TokenListOp weakListOp = Sdf.TokenListOp() weakListOp.explicitItems = ["baz"] strongListOp = Sdf.TokenListOp() strongListOp.addedItems = ["foo", "bar"] strongListOp.deletedItems = ["baz"] strongListOp.orderedItems = ["bar", "foo"] expectedListOp = Sdf.TokenListOp() expectedListOp.explicitItems = ["bar", "foo"] _TestComposition('tokenListOpTest', weakListOp, strongListOp, expectedListOp)
def test_BasicListOpMetadata(self): '''Tests basic metadata API with supported list op types''' def _TestBasic(fieldName, listOp, expectedListOp): for fmt in allFormats: s = Usd.Stage.CreateNew("TestBasicListOpMetadata."+fmt) prim = s.OverridePrim('/Root') assert not prim.HasMetadata(fieldName) assert not prim.HasAuthoredMetadata(fieldName) self.assertEqual(prim.GetMetadata(fieldName), None) assert prim.SetMetadata(fieldName, listOp) assert prim.HasMetadata(fieldName) assert prim.HasAuthoredMetadata(fieldName) self.assertEqual(prim.GetMetadata(fieldName), expectedListOp) prim.ClearMetadata(fieldName) assert not prim.HasMetadata(fieldName) assert not prim.HasAuthoredMetadata(fieldName) self.assertEqual(prim.GetMetadata(fieldName), None) # List ops are applied into a single explicit list op during # value resolution, so the expected list op isn't the same # as the given list op. # Sdf.IntListOp listOp = Sdf.IntListOp() listOp.addedItems = [-2147483648, 1, 2, 3, 2147483647] listOp.deletedItems = [-2147483648, 10, 20, 30, 2147483647] listOp.orderedItems = [2147483647, 3, 2, 1, -2147483648] expectedListOp = Sdf.IntListOp() expectedListOp.explicitItems = [2147483647, 3, 2, 1, -2147483648] _TestBasic('intListOpTest', listOp, expectedListOp) # Sdf.Int64ListOp listOp = Sdf.Int64ListOp() listOp.addedItems = [-9223372036854775808, 1, 2, 3, 9223372036854775807] listOp.deletedItems = [-9223372036854775808, 10, 20, 30, 9223372036854775807] listOp.orderedItems = [9223372036854775807, 3, 2, 1, -9223372036854775808] expectedListOp = Sdf.Int64ListOp() expectedListOp.explicitItems = [9223372036854775807, 3, 2, 1, -9223372036854775808] _TestBasic('int64ListOpTest', listOp, expectedListOp) # Sdf.UIntListOp listOp = Sdf.UIntListOp() listOp.addedItems = [1, 2, 3, 4294967295] listOp.deletedItems = [10, 20, 30, 4294967295] listOp.orderedItems = [4294967295, 3, 2, 1] expectedListOp = Sdf.UIntListOp() expectedListOp.explicitItems = [4294967295, 3, 2, 1] _TestBasic('uintListOpTest', listOp, expectedListOp) # Sdf.UInt64ListOp listOp = Sdf.UInt64ListOp() listOp.addedItems = [1, 2, 3, 18446744073709551615] listOp.deletedItems = [10, 20, 30, 18446744073709551615] listOp.orderedItems = [18446744073709551615, 3, 2, 1] expectedListOp = Sdf.UInt64ListOp() expectedListOp.explicitItems = [18446744073709551615, 3, 2, 1] _TestBasic('uint64ListOpTest', listOp, expectedListOp) # Sdf.StringListOp listOp = Sdf.StringListOp() listOp.addedItems = ["foo", "bar"] listOp.deletedItems = ["baz"] listOp.orderedItems = ["bar", "foo"] expectedListOp = Sdf.StringListOp() expectedListOp.explicitItems = ["bar", "foo"] _TestBasic('stringListOpTest', listOp, expectedListOp) # Sdf.TokenListOp listOp = Sdf.TokenListOp() listOp.addedItems = ["foo", "bar"] listOp.deletedItems = ["baz"] listOp.orderedItems = ["bar", "foo"] expectedListOp = Sdf.TokenListOp() expectedListOp.explicitItems = ["bar", "foo"] _TestBasic('tokenListOpTest', listOp, expectedListOp)
def _DeletedItems(l): r = Sdf.IntListOp() r.deletedItems = l return r
def _OrderedItems(l): r = Sdf.IntListOp() r.orderedItems = l return r
def _AppendedItems(l): r = Sdf.IntListOp() r.appendedItems = l return r
def _PrependedItems(l): r = Sdf.IntListOp() r.prependedItems = l return r
def _AddedItems(l): r = Sdf.IntListOp() r.addedItems = l return r
def _ExplicitItems(l): r = Sdf.IntListOp() r.explicitItems = l return r