File: PyIObject.cpp

package info (click to toggle)
alembic-graphics 1.8.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,708 kB
  • sloc: cpp: 89,261; python: 8,053; makefile: 19; csh: 4
file content (300 lines) | stat: -rw-r--r-- 10,603 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//-*****************************************************************************
//
// Copyright (c) 2009-2012,
//  Sony Pictures Imageworks Inc. and
//  Industrial Light & Magic, a division of Lucasfilm Entertainment Company Ltd.
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
// *       Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// *       Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// *       Neither the name of Sony Pictures Imageworks, nor
// Industrial Light & Magic, nor the names of their contributors may be used
// to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//-*****************************************************************************

#include <Foundation.h>

using namespace boost::python;

//-*****************************************************************************
class ChildIterator
{
public:
    ChildIterator( Abc::IObject &p )
        : _p( p ) , _iter( 0 ), _end( p.getNumChildren() ) {}

    Abc::IObject next()
    {
        if ( _iter >= _end )
            boost::python::objects::stop_iteration_error();

        return _p.getChild( _iter++ );
    }
private:
    Abc::IObject _p;
    size_t _iter;
    size_t _end;
};

//-*****************************************************************************
class ChildList
{
public:
    ChildList( Abc::IObject &p )
        : _p( p ) {}

    Py_ssize_t len()
    {
        return (Py_ssize_t)_p.getNumChildren();
    }

    Abc::IObject getItem( Py_ssize_t index )
    {
        return _p.getChild( ( AbcA::index_t )index );
    }

    ChildIterator* getIterator()
    {
        return new ChildIterator( _p );
    }
private:
    Abc::IObject _p;
};

//-*****************************************************************************
static ChildList getChildList( Abc::IObject& p)
{
    return ChildList(p);
}

//-*****************************************************************************
static Abc::IObject getChildByIndex( Abc::IObject &o, size_t i )
{
    if ( i >= o.getNumChildren() )
    {
        std::stringstream stream;
        stream << i;
        throwPythonIndexException( stream.str().c_str() );
    }
    const Abc::IObject& c = o.getChild( i );
    if ( !c.valid() )
    {
        return Abc::IObject();
    }
    return c;
}

//-*****************************************************************************
static Abc::IObject getChildByName( Abc::IObject &o, const std::string& name )
{
    const Abc::IObject& c = o.getChild( name );
    if ( c.valid() )
    {
        return c;
    }

    std::stringstream stream;
    stream << name;
    throwPythonKeyException( stream.str().c_str() );
    return Abc::IObject();
}

//-*****************************************************************************
static bool isChildInstanceByIndex( Abc::IObject &o, size_t i )
{
    if ( i >= o.getNumChildren() )
    {
        std::stringstream stream;
        stream << i;
        throwPythonIndexException( stream.str().c_str() );
    }
    const Abc::IObject& c = o.getChild( i );
    if ( !c.valid() )
    {
        return false;
    }

    return c.isInstanceDescendant();
}

//-*****************************************************************************
static bool isChildInstanceByName( Abc::IObject &o, const std::string& name )
{
    const Abc::IObject& c = o.getChild( name );
    if ( c.valid() )
    {
        return c.isInstanceDescendant();
    }

    std::stringstream stream;
    stream << name;
    throwPythonKeyException( stream.str().c_str() );
    return false;
}

//-*****************************************************************************
static std::string getPropertiesHash( Abc::IObject &o )
{
    Alembic::Util::Digest pHash;
    if ( !o.getPropertiesHash( pHash ) )
        return "";

    return pHash.str();
}

//-*****************************************************************************
static std::string getChildrenHash( Abc::IObject &o )
{
    Alembic::Util::Digest cHash;
    if ( !o.getChildrenHash( cHash ) )
        return "";

    return cHash.str();
}

//-*****************************************************************************
void register_iobject()
{
    // overloads
    //
    const AbcA::ObjectHeader&
        ( Abc::IObject::*getChildHeaderByIndex )( size_t ) const = \
        &Abc::IObject::getChildHeader;

    const AbcA::ObjectHeader*
        ( Abc::IObject::*getChildHeaderByName )( const std::string & ) const = \
        &Abc::IObject::getChildHeader;

    // IObject
    //
    class_<Abc::IObject>(
         "IObject",
         "The IObject class as an object reader",
         init<Abc::IObject, const std::string&>(
             ( arg( "parent" ), arg( "name" ) ),
             "Create an IObject with the given parent IObject and name" ) )
        .def( init<>( "Create an empty IObject" ) )
        .def( "getHeader",
              &Abc::IObject::getHeader,
              "Return the header which contains all the MetaData that was "
              "specified upon their creation",
              return_internal_reference<1>() )
        .def( "getName",
              &Abc::IObject::getName,
              "Return the name of this object. The name is unique amongst "
              "their siblings",
              return_value_policy<copy_const_reference>() )
        .def( "getFullName",
              &Abc::IObject::getFullName,
              "Return the full name of this object. The full name is unique "
              "within the entire archive",
              return_value_policy<copy_const_reference>() )
        .def( "getNumChildren",
              &Abc::IObject::getNumChildren,
              "Return the number of child IObjects that this object has" )
        .def( "getChildHeader",
              getChildHeaderByIndex,
              ( arg( "index" ) ),
              "Return the header of a child IObject with the given index",
              return_internal_reference<1>() )
        .def( "getChildHeader",
              getChildHeaderByName,
              ( arg( "name" ) ),
              "Return the header of a child IObject with the given name",
              return_value_policy<reference_existing_object>() )
        .def( "getProperties",
              &Abc::IObject::getProperties,
              "Return the single top-level ICompoundProperty",
              with_custodian_and_ward_postcall<0,1>() )
        .def( "getChild",
              &getChildByIndex,
              ( arg( "index" ) ),
              "Return a child IObject with the given index",
              with_custodian_and_ward_postcall<0,1>() )
        .def( "getChild",
              &getChildByName,
              ( arg( "name" ) ),
              "Return a child IObject with the given name",
              with_custodian_and_ward_postcall<0,1>() )
        .def( "getArchive",
              &Abc::IObject::getArchive,
              "Return this object's archive",
              with_custodian_and_ward_postcall<0,1>() )
        .def( "getParent",
              &Abc::IObject::getParent,
              "Return this object's parent\n"
              "If the object is the top level object, the IObject returned "
              "will be an empty Object",
              with_custodian_and_ward_postcall<0,1>() )
        .def( "getMetaData",
              &Abc::IObject::getMetaData,
              "Return the MetaData of this object",
              return_internal_reference<1>() )
        .def( "isInstanceRoot",
              &Abc::IObject::isInstanceRoot,
              "Return true if this IObject is an instance root" )
        .def( "isInstanceDescendant",
              &Abc::IObject::isInstanceDescendant,
              "Return true if this IObject is an instanceRoot, or an ancestor of one" )
        .def( "instanceSourcePath",
              &Abc::IObject::instanceSourcePath,
              "Return the instance source path that the instance points at. Empty string if not found." )
        .def( "isChildInstance",
              &isChildInstanceByIndex,
              ( arg( "index" ) ),
              "Return true if the child by index is an instance root object" )
        .def( "isChildInstance",
              &isChildInstanceByName,
              ( arg( "name" ) ),
              "Return true if the named child is an instance root object" )
        .def( "getPropertiesHash",
              &getPropertiesHash,
              "Return a string representation of the properties hash for this object. Empty string if not found." )
        .def( "getChildrenHash",
              &getChildrenHash,
              "Return a string representation of the children hash for this object. Empty string if not found." )
        .def( "valid", &Abc::IObject::valid )
        .def( "reset", &Abc::IObject::reset )
        .def( "__str__", &Abc::IObject::getFullName,
              return_value_policy<copy_const_reference>() )
        .def( ALEMBIC_PYTHON_BOOL_NAME, &Abc::IObject::valid )
        .add_property( "children", &getChildList )
        ;

    // List and Iterator for child IObjects
    //
    class_<ChildList>
        ( "ChildList", no_init )
        .def( "__len__", &ChildList::len )
        .def( "__getitem__", &ChildList::getItem )
        .def( "__iter__", &ChildList::getIterator,
              return_value_policy<manage_new_object>() )
        ;

    class_<ChildIterator>
        ( "ChildIterator", no_init )
        .def( ALEMBIC_PYTHON_NEXT_NAME, &ChildIterator::next )
        ;
}