Skip to content

Commit

Permalink
addresses alembic#23, auto-creates missing nested objects
Browse files Browse the repository at this point in the history
  • Loading branch information
rsgalloway committed Mar 4, 2015
1 parent 30dc0cf commit 36428af
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 8 deletions.
25 changes: 20 additions & 5 deletions python/examples/cask/cask.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#-******************************************************************************
#
# Copyright (c) 2012-2014,
# Copyright (c) 2012-2015,
# Sony Pictures Imageworks Inc. and
# Industrial Light & Magic, a division of Lucasfilm Entertainment Company Ltd.
#
Expand Down Expand Up @@ -42,7 +42,7 @@
More information can be found at http://docs.alembic.io/python/cask.html
"""
__version__ = "0.9.4"
__version__ = "0.9.5a"

import os
import re
Expand Down Expand Up @@ -477,7 +477,12 @@ def __setitem__(self, name, item):
obj = obj.get_item(name)
except KeyError:
if name != names[-1]:
raise
if type(item) == Property:
child = obj.properties[name] = Property()
else:
child = obj.children[name] = Xform()
child.parent = obj
obj = child
new = True
if new is False:
obj = obj.parent
Expand Down Expand Up @@ -1108,7 +1113,7 @@ def close(self):
"""
Closes this property by removing references to internal OProperty.
"""
if self.parent:
if self.parent and self.name in self.parent.properties:
del self.parent.properties[self.name]
self._iobject = None
self._oobject = None
Expand All @@ -1133,10 +1138,20 @@ def save(self):
del value
else:
for prop in self.properties.values():
prop.parent = self
up = False
if not prop.iobject and prop.parent.name == ".userProperties":
up = Property()
up._oobject = prop.object().oobject.getSchema().getUserProperties()
up.properties[prop.name] = prop
prop.parent = up
else:
prop.parent = self
prop.save()
prop.close()
del prop
if up:
up.close()
del up
self.close()

class Object(object):
Expand Down
59 changes: 56 additions & 3 deletions python/examples/cask/test/testCask.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#-******************************************************************************
#
# Copyright (c) 2012-2014,
# Copyright (c) 2012-2015,
# Sony Pictures Imageworks Inc. and
# Industrial Light & Magic, a division of Lucasfilm Entertainment Company Ltd.
#
Expand Down Expand Up @@ -524,8 +524,12 @@ def test_deep_dict(self):
self.assertEqual(t.children["A/B/C/D/meshy"].type(), "Xform")
self.assertEqual(p2.type(), "Xform")

# test deep set item when middle node does not exist
self.assertRaises(KeyError, t.children.__setitem__, "A/foo/C/D/bar", cask.Xform())
# test deep set item when middle nodes do not exist
try:
x = t.children["A/foo/C/D/bar"] = cask.Xform()
p = x.properties[".xform/.userProperties/foo"] = cask.Property()
except KeyError:
raise

# write the archive
a.write_to_file(filename)
Expand Down Expand Up @@ -785,6 +789,8 @@ def test_verify_deep_dict(self):

class Test3_Issues(unittest.TestCase):
def test_issue_318(self):
"""google code issue #318"""

filename = "cask_test_issue_318.abc"

# create a test file
Expand All @@ -804,6 +810,8 @@ def test_issue_318(self):
self.assertEqual(test_file_1, test_file_2)

def test_issue_345(self):
"""google code issue #345"""

test_file_mesh = os.path.join(TEMPDIR, "cask_write_mesh.abc")
test_file_geom = os.path.join(TEMPDIR, "cask_write_geom.abc")
test_file_lights = os.path.join(TEMPDIR, "cask_test_lights.abc")
Expand Down Expand Up @@ -874,6 +882,8 @@ def compare_props(props1, props2):
compare_props(ageom.properties, bgeom.properties)

def test_issue_346(self):
"""google code issue #346"""

filename_1 = "cask_test_issue_346_1.abc"
filename_2 = "cask_test_issue_346_2.abc"

Expand Down Expand Up @@ -911,6 +921,8 @@ def test_issue_346(self):
self.assertEqual(str(tst_3), str(tst_4))

def test_issue_349(self):
"""google code issue #349"""

test_file = os.path.join(TEMPDIR, "cask_test_issue_349.abc")

# create a new archive and some objects
Expand All @@ -937,5 +949,46 @@ def test_issue_349(self):
self.assertEqual(a.start_frame(), 0)
self.assertEqual(a.end_frame(), 23)

def test_issue_23(self):
"""github issue #23"""

test_file = os.path.join(TEMPDIR, "cask_test_issue_user_props.abc")

a = cask.Archive()
x = a.top.children["x"] = cask.Xform()
x.properties[".xform"] = cask.Property()

# create the .userProperties compound prop
up = x.properties[".xform"].properties[".userProperties"] = cask.Property()

# create some user properties
p1 = up.properties["foo"] = cask.Property()
p1.set_value("bar")
p2 = up.properties["bar"] = cask.Property()
p2.set_value(1.0)

# export it
a.write_to_file(test_file)
a.close()

# read it back in and check for the user properties
a = cask.Archive(test_file)
x = a.top.children["x"]
self.assertEqual(x.properties.keys(), [".xform"])
self.assertEqual(x.properties[".xform"].properties.keys(),
[".userProperties"])
up = x.properties[".xform/.userProperties"]

# assert the values are the same
self.assertEqual(len(up.properties), 2)
self.assertEqual(up.properties["foo"].values[0], "bar")
self.assertEqual(up.properties["bar"].values[0], 1.0)

# use the alembic python api directly
ph = a.top.children["x"].schema.getUserProperties().propertyheaders
self.assertEqual(len(ph), 2)
self.assertEqual(ph[0].getName(), "foo")
self.assertEqual(ph[1].getName(), "bar")

if __name__ == '__main__':
unittest.main()

0 comments on commit 36428af

Please # to comment.