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.

from IECore import *

single_number = IntData( 123 )
multiple_numbers = FloatVectorData( [ 1.0, 2.0, 3.142 ] )

object = CompoundObject()

object['a_single_number'] = single_number
object['some_more_numbers'] = multiple_numbers

Writer.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.

from IECore import *

child = CompoundObject()
child['numbers'] = FloatVectorData( [1.0, 2.0, 3.142] )
child['strings'] = StringVectorData( ["hello", "world"] )

parent = CompoundObject()
parent['child'] = child

Writer.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.

import IECore
 
objvec = IECore.ObjectVector()
 
for i in range(0,5):
 
     #create an object with some data
     element = IECore.CompoundObject()
     element['id'] = IECore.IntData( i )
     element['name'] = IECore.StringData( "bob_" + str(i) )
      
     #append it to the object vector
     objvec.append(element)
 
IECore.Writer.create( objvec, "test.cob" ).write()
copied_objvec = IECore.Reader.create("test.cob").read()
 
for x in range(0,len(copied_objvec)):
     print copied_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