Skip to content

Commit 0155195

Browse files
committed
Merge pull request #193 from davidsminor/linkedSceneFromIndexedIO
LinkedScene can now wrap writable SceneCaches
2 parents 1c39dff + a6895c9 commit 0155195

File tree

5 files changed

+86
-2
lines changed

5 files changed

+86
-2
lines changed

include/IECore/SceneCache.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ class SceneCache : public SampledSceneInterface
131131
virtual SceneInterfacePtr scene( const Path &path, MissingBehaviour missingBehaviour = ThrowIfMissing );
132132
virtual ConstSceneInterfacePtr scene( const Path &path, SceneInterface::MissingBehaviour missingBehaviour = ThrowIfMissing ) const;
133133

134+
/// tells you if this scene cache is read only or writable:
135+
bool readOnly() const;
136+
134137
// The attribute names used to mark animated topology and primitive variables
135138
// when SceneCache objects are Primitives.
136139
static const Name &animatedObjectTopologyAttribute;

src/IECore/LinkedScene.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ LinkedScene::LinkedScene( const std::string &fileName, IndexedIO::OpenMode mode
7676

7777
LinkedScene::LinkedScene( ConstSceneInterfacePtr mainScene ) : m_mainScene(const_cast<SceneInterface*>(mainScene.get())), m_linkedScene(0), m_rootLinkDepth(0), m_readOnly(true), m_atLink(false), m_timeRemapped(false)
7878
{
79+
if( SceneCachePtr scc = runTimeCast<SceneCache>( m_mainScene ) )
80+
{
81+
m_readOnly = scc->readOnly();
82+
}
83+
7984
m_sampled = (runTimeCast<const SampledSceneInterface>(mainScene.get()) != NULL);
8085
}
8186

src/IECore/SceneCache.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2027,6 +2027,7 @@ SceneCache::SceneCache( IECore::IndexedIOPtr indexedIO )
20272027
{
20282028
ObjectPtr header = HeaderGenerator::header();
20292029
header->save( indexedIO, headerEntry );
2030+
indexedIO->subdirectory( sampleTimesEntry, IndexedIO::CreateIfMissing );
20302031
indexedIO = indexedIO->subdirectory( rootEntry, IndexedIO::CreateIfMissing );
20312032
indexedIO->removeAll();
20322033
m_implementation = new WriterImplementation( indexedIO );
@@ -2371,3 +2372,8 @@ SceneCachePtr SceneCache::duplicate( ImplementationPtr& impl ) const
23712372
{
23722373
return new SceneCache( impl );
23732374
}
2375+
2376+
bool SceneCache::readOnly() const
2377+
{
2378+
return dynamic_cast< const ReaderImplementation* >( m_implementation.get() ) != NULL;
2379+
}

test/IECore/LinkedSceneTest.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,43 @@ def testLinkBoundTransformMismatch( self ) :
699699
self.assertEqual( parent.readTransformAtSample( 0 ), transform )
700700
self.failUnless( LinkedSceneTest.compareBBox( linked.readBoundAtSample( 0 ), IECore.Box3d( IECore.V3d( 1, 0, 0 ), IECore.V3d( 3, 1, 1 ) ) ) )
701701
self.failUnless( LinkedSceneTest.compareBBox( linked.readBoundAtSample( 0 ), linked.readBoundAtSample( 1 ) ) )
702-
702+
703+
def testMemoryIndexedIOReadWrite( self ) :
704+
705+
# create inital file structure in memory:
706+
mio = IECore.MemoryIndexedIO( IECore.CharVectorData(), IECore.IndexedIO.OpenMode.Write )
707+
708+
# write to the actual linkedscene:
709+
scc = IECore.SceneCache( mio )
710+
l = IECore.LinkedScene( scc )
711+
712+
c0 = l.createChild("child0")
713+
c1 = l.createChild("child1")
714+
715+
c0.writeAttribute( "testAttr", IECore.StringData("test0"), 0 )
716+
c1.writeAttribute( "testAttr", IECore.StringData("test1"), 0 )
717+
718+
# write the "file" to memory
719+
del l, scc, c0, c1
720+
721+
# can we read it back again?
722+
mioData = mio.buffer()
723+
mio = IECore.MemoryIndexedIO( mioData, IECore.IndexedIO.OpenMode.Read )
724+
725+
scc = IECore.SceneCache( mio )
726+
l = IECore.LinkedScene( scc )
727+
728+
self.assertEqual( set( l.childNames() ), set( ["child0", "child1"] ) )
729+
730+
# no write access!
731+
self.assertRaises( RuntimeError, l.createChild, "child2" )
732+
733+
c0 = l.child("child0")
734+
c1 = l.child("child1")
735+
736+
self.assertEqual( c0.readAttribute( "testAttr", 0 ), IECore.StringData( "test0" ) )
737+
self.assertEqual( c1.readAttribute( "testAttr", 0 ), IECore.StringData( "test1" ) )
738+
703739
if __name__ == "__main__":
704740
unittest.main()
705741

test/IECore/SceneCacheTest.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,41 @@ def testSampleTimeOrder( self ):
661661

662662
m = IECore.SceneInterface.create( "/tmp/test.scc", IECore.IndexedIO.OpenMode.Read )
663663
self.assertTrue( m.boundSampleTime(0) < m.boundSampleTime(1) )
664-
664+
665+
def testMemoryIndexedIOReadWrite( self ) :
666+
667+
# create inital file structure in memory:
668+
mio = IECore.MemoryIndexedIO( IECore.CharVectorData(), IECore.IndexedIO.OpenMode.Write )
669+
670+
# write to the actual linkedscene:
671+
scc = IECore.SceneCache( mio )
672+
673+
c0 = scc.createChild("child0")
674+
c1 = scc.createChild("child1")
675+
676+
c0.writeAttribute( "testAttr", IECore.StringData("test0"), 0 )
677+
c1.writeAttribute( "testAttr", IECore.StringData("test1"), 0 )
678+
679+
# write the "file" to memory
680+
del scc, c0, c1
681+
682+
# can we read it back again?
683+
mioData = mio.buffer()
684+
mio = IECore.MemoryIndexedIO( mioData, IECore.IndexedIO.OpenMode.Read )
685+
686+
scc = IECore.SceneCache( mio )
687+
688+
self.assertEqual( set( scc.childNames() ), set( ["child0", "child1"] ) )
689+
690+
# no write access!
691+
self.assertRaises( RuntimeError, scc.createChild, "child2" )
692+
693+
c0 = scc.child("child0")
694+
c1 = scc.child("child1")
695+
696+
self.assertEqual( c0.readAttribute( "testAttr", 0 ), IECore.StringData( "test0" ) )
697+
self.assertEqual( c1.readAttribute( "testAttr", 0 ), IECore.StringData( "test1" ) )
698+
665699

666700
if __name__ == "__main__":
667701
unittest.main()

0 commit comments

Comments
 (0)