Skip to content

ExamplesDataCompoundObjects

Ben Toogood edited this page Jun 13, 2013 · 2 revisions

Overview

The CompoundObject class provides string-keyed storage of Cortex objects with IO functionality. Think of it as a struct that you can assemble dynamically.

Examples

Storing data on CompoundObjects

Here we store a single int and an array of floats in a CompoundObject.

fromIECoreimport*single_number=IntData( 123 )
multiple_numbers=FloatVectorData( [ 1.0, 2.0, 3.142 ] )
object=CompoundObject()
object['a_single_number'] =single_numberobject['some_more_numbers'] =multiple_numbersWriter.create( object, "test.cob" ).write()
object2=Reader.create( "test.cob" ).read()

Hierarchies of CompoundObjects

We can also store CompoundObjects within CompoundObjects, allowing us to create hierarchical data.

fromIECoreimport*child=CompoundObject()
child['numbers'] =FloatVectorData( [1.0, 2.0, 3.142] )
child['strings'] =StringVectorData( ["hello", "world"] )
parent=CompoundObject()
parent['child'] =childWriter.create( parent, "test.cob" ).write()
copied_object=Reader.create("test.cob").read()

Storing an array of CompoundObjects

We are using the ObjectVector class to work with arrays of CompoundObjects. Note that each entry in the array could have a different structure.

importIECoreobjvec=IECore.ObjectVector()
foriinrange(0,5):
#create an object with some dataelement=IECore.CompoundObject()
element['id'] =IECore.IntData( i )
element['name'] =IECore.StringData( "bob_"+str(i) )
#append it to the object vectorobjvec.append(element)
IECore.Writer.create( objvec, "test.cob" ).write()
copied_objvec=IECore.Reader.create("test.cob").read()
forxinrange(0,len(copied_objvec)):
printcopied_objvec[x]

which yields

IECore.CompoundObject({'id':IECore.IntData( 0 ),'name':IECore.StringData( "bob_0" )})
IECore.CompoundObject({'id':IECore.IntData( 1 ),'name':IECore.StringData( "bob_1" )})
IECore.CompoundObject({'id':IECore.IntData( 2 ),'name':IECore.StringData( "bob_2" )})
IECore.CompoundObject({'id':IECore.IntData( 3 ),'name':IECore.StringData( "bob_3" )})
IECore.CompoundObject({'id':IECore.IntData( 4 ),'name':IECore.StringData( "bob_4" )})

Clone this wiki locally