Skip to content

Commit f4adc70

Browse files
committed
AlembicScene : Support visibility attributes
Fixes #944
1 parent 8195ca3 commit f4adc70

File tree

2 files changed

+82
-8
lines changed

2 files changed

+82
-8
lines changed

contrib/IECoreAlembic/src/IECoreAlembic/AlembicScene.cpp

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#include "Alembic/AbcGeom/IXform.h"
5555
#include "Alembic/AbcGeom/OXform.h"
5656
#include "Alembic/AbcGeom/OGeomParam.h"
57+
#include "Alembic/AbcGeom/Visibility.h"
5758

5859
#include "Alembic/AbcCollection/ICollections.h"
5960
#include "Alembic/AbcCollection/OCollections.h"
@@ -329,8 +330,13 @@ class AlembicScene::AlembicReader : public AlembicIO
329330
return false;
330331
}
331332

332-
ICompoundProperty userProperties = m_xform.getSchema().getUserProperties();
333+
if( name == visibilityName )
334+
{
335+
IObject xformObject( m_xform );
336+
return GetVisibilityProperty( xformObject );
337+
}
333338

339+
ICompoundProperty userProperties = m_xform.getSchema().getUserProperties();
334340
if( !userProperties.valid() )
335341
{
336342
return false;
@@ -349,8 +355,13 @@ class AlembicScene::AlembicReader : public AlembicIO
349355
return;
350356
}
351357

352-
ICompoundProperty userProperties = m_xform.getSchema().getUserProperties();
358+
IObject xformObject( m_xform );
359+
if( GetVisibilityProperty( xformObject ) )
360+
{
361+
attrs.push_back( visibilityName );
362+
}
353363

364+
ICompoundProperty userProperties = m_xform.getSchema().getUserProperties();
354365
if( !userProperties.valid() )
355366
{
356367
return;
@@ -374,6 +385,12 @@ class AlembicScene::AlembicReader : public AlembicIO
374385
return nullptr;
375386
}
376387

388+
if( name == visibilityName )
389+
{
390+
IObject xformObject( m_xform );
391+
return GetVisibilityProperty( xformObject ).getPtr();
392+
}
393+
377394
ICompoundProperty userProperties = m_xform.getSchema().getUserProperties();
378395
if( !userProperties.valid() )
379396
{
@@ -429,6 +446,15 @@ class AlembicScene::AlembicReader : public AlembicIO
429446
return nullptr;
430447
}
431448

449+
if( name == visibilityName )
450+
{
451+
int8_t v;
452+
reader->getSample( sampleIndex, &v );
453+
return new IECore::BoolData(
454+
v == kVisibilityHidden ? false : true
455+
);
456+
}
457+
432458
const DataType dataType = reader->getDataType();
433459
const PlainOldDataType pod = dataType.getPod();
434460
const uint8_t extent = dataType.getExtent();
@@ -1462,25 +1488,34 @@ class AlembicScene::AlembicWriter : public AlembicIO
14621488
// Attribute
14631489
// =--------
14641490

1465-
template<typename T, typename D>
1466-
void setProperty( const Name &name, double time, const D *data )
1491+
template<typename T>
1492+
void setProperty( const Name &name, double time, const typename T::value_type &value )
14671493
{
14681494
m_attributeSampleTimes[name].push_back( time );
14691495

14701496
auto it = m_scalarProperties.find( name );
14711497

14721498
if( it != m_scalarProperties.end() )
14731499
{
1474-
it->second.set( (void *) &data->readable() );
1500+
it->second.set( (void *)&value );
14751501
return;
14761502
}
14771503

14781504
OXformSchema &schema = m_xform.getSchema();
1505+
T prop(
1506+
name == kVisibilityPropertyName ? m_xform.getProperties() : schema.getUserProperties(),
1507+
name
1508+
);
14791509

1480-
T prop( schema.getUserProperties(), name );
14811510
m_scalarProperties.insert( std::make_pair( name, prop ) );
14821511

1483-
prop.set( data->readable() );
1512+
prop.set( value );
1513+
}
1514+
1515+
template<typename T, typename D>
1516+
void setProperty( const Name &name, double time, const D *data )
1517+
{
1518+
setProperty<T>( name, time, data->readable() );
14841519
}
14851520

14861521
void writeAttribute( const Name &name, const IECore::Object *attribute, double time )
@@ -1498,7 +1533,23 @@ class AlembicScene::AlembicWriter : public AlembicIO
14981533
return;
14991534
}
15001535

1501-
if( const IECore::BoolData *bData = runTimeCast<const IECore::BoolData>( attribute ) )
1536+
if( name == visibilityName )
1537+
{
1538+
if( const IECore::BoolData *data = runTimeCast<const IECore::BoolData>( attribute ) )
1539+
{
1540+
setProperty<OCharProperty>( kVisibilityPropertyName, time, data->readable() ? kVisibilityDeferred : kVisibilityHidden );
1541+
}
1542+
else
1543+
{
1544+
IECore::msg(
1545+
IECore::MessageHandler::Level::Warning, "AlembicScene::writeAttribute",
1546+
boost::format(
1547+
"Expected BoolData for attribute \"%s\" but got \"%s\"."
1548+
) % name % attribute->typeName()
1549+
);
1550+
}
1551+
}
1552+
else if( const IECore::BoolData *bData = runTimeCast<const IECore::BoolData>( attribute ) )
15021553
{
15031554
setProperty<OBoolProperty>( name, time, bData );
15041555
}

contrib/IECoreAlembic/test/IECoreAlembic/AlembicSceneTest.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1960,5 +1960,28 @@ def testIndexedCurveUVs( self ) :
19601960
curves
19611961
)
19621962

1963+
def testVisibilityAttribute( self ) :
1964+
1965+
fileName = os.path.join( self.temporaryDirectory(), "visibilityAttribute.abc" )
1966+
root = IECoreScene.SceneInterface.create( fileName, IECore.IndexedIO.OpenMode.Write )
1967+
root.createChild( "withoutAttribute" )
1968+
child = root.createChild( "withAttribute" )
1969+
child.writeAttribute( "scene:visible", IECore.BoolData( True ), 0 )
1970+
child.writeAttribute( "scene:visible", IECore.BoolData( False ), 1 )
1971+
1972+
del child, root
1973+
1974+
root = IECoreScene.SceneInterface.create( fileName, IECore.IndexedIO.OpenMode.Read )
1975+
1976+
child = root.child( "withAttribute" )
1977+
self.assertIn( "scene:visible", child.attributeNames() )
1978+
self.assertTrue( child.hasAttribute( "scene:visible" ) )
1979+
self.assertEqual( child.readAttribute( "scene:visible", 0 ), IECore.BoolData( True ) )
1980+
self.assertEqual( child.readAttribute( "scene:visible", 1 ), IECore.BoolData( False ) )
1981+
1982+
child = root.child( "withoutAttribute" )
1983+
self.assertNotIn( "scene:visible", child.attributeNames() )
1984+
self.assertFalse( child.hasAttribute( "scene:visible" ) )
1985+
19631986
if __name__ == "__main__":
19641987
unittest.main()

0 commit comments

Comments
 (0)