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
|
// K-3D
// Copyright (c) 1995-2006, Timothy M. Shead
//
// Contact: tshead@k-3d.com
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
/** \file
\author Timothy M. Shead (tshead@k-3d.com)
*/
#include <boost/python.hpp>
#include <k3dsdk/python/any_python.h>
#include <k3dsdk/python/offscreen_context_factory_gl_python.h>
#include <k3dsdk/python/idocument_exporter_python.h>
#include <k3dsdk/python/idocument_importer_python.h>
#include <k3dsdk/python/ifile_change_notifier_python.h>
#include <k3dsdk/python/ikeyframer_python.h>
#include <k3dsdk/python/imesh_storage_python.h>
#include <k3dsdk/python/imeta_object_python.h>
#include <k3dsdk/python/imetadata_python.h>
#include <k3dsdk/python/inode_python.h>
#include <k3dsdk/python/inode_selection_python.h>
#include <k3dsdk/python/iplugin_factory_python.h>
#include <k3dsdk/python/iproperty_collection_python.h>
#include <k3dsdk/python/iproperty_python.h>
#include <k3dsdk/python/irender_camera_frame_python.h>
#include <k3dsdk/python/irender_camera_preview_python.h>
#include <k3dsdk/python/isnappable_python.h>
#include <k3dsdk/python/iunknown_python.h>
#include <k3dsdk/python/iuser_interface_python.h>
#include <k3dsdk/iproperty_collection.h>
#include <k3dsdk/iproperty.h>
#include <k3dsdk/iwritable_property.h>
#include <k3dsdk/log.h>
#include <k3dsdk/property.h>
#include <k3dsdk/types.h>
using namespace boost::python;
namespace k3d
{
namespace python
{
object wrap_unknown(iunknown* Unknown)
{
return Unknown ? wrap_unknown(*Unknown) : object();
}
object wrap_unknown(iunknown& Unknown)
{
object result = object(iunknown_wrapper(Unknown));
define_methods_imeta_object(Unknown, result);
define_methods_offscreen_context_factory_gl(Unknown, result);
define_methods_idocument_exporter(Unknown, result);
define_methods_idocument_importer(Unknown, result);
define_methods_ifile_change_notifier(Unknown, result);
define_methods_ikeyframer(Unknown, result);
define_methods_imesh_storage(Unknown, result);
define_methods_imetadata(Unknown, result);
define_methods_inode(Unknown, result);
define_methods_inode_selection(Unknown, result);
define_methods_iplugin_factory(Unknown, result);
define_methods_iproperty(Unknown, result);
define_methods_iproperty_collection(Unknown, result);
define_methods_irender_camera_frame(Unknown, result);
define_methods_irender_camera_preview(Unknown, result);
define_methods_isnappable(Unknown, result);
define_methods_iuser_interface(Unknown, result);
return result;
}
// This rightfully belongs with the rest of the iproperty_collection code, but for some reason __getattr__
// doesn't seem to work with our dynamically-added instance methods.
static object getattr(iunknown_wrapper& Self, const string_t& Name)
{
// Return K-3D properties as attributes whenever they're available ...
if(k3d::iproperty_collection* const property_collection = Self.wrapped_ptr<k3d::iproperty_collection>())
{
if(k3d::iproperty* property = k3d::property::get(*property_collection, Name))
return any_to_python(k3d::property::pipeline_value(*property));
throw std::invalid_argument("unknown property: " + Name);
}
return object();
}
// This rightfully belongs with the rest of the iproperty_collection code, but for some reason __getattr__
// doesn't seem to work with our dynamically-added instance methods.
static void setattr(object& Self, const string_t& Name, const object& Value)
{
// If this is a K-3D property, set its internal value ...
extract<iunknown_wrapper> unknown(Self);
if(unknown.check())
{
if(k3d::iproperty_collection* const property_collection = unknown().wrapped_ptr<k3d::iproperty_collection>())
{
if(k3d::iproperty* const property = k3d::property::get(*property_collection, Name))
{
if(k3d::iwritable_property* const writable = dynamic_cast<k3d::iwritable_property*>(property))
{
writable->property_set_value(python_to_any(Value, property->property_type()));
return;
}
throw std::invalid_argument("read-only property: " + Name);
}
}
}
// Otherwise, fallback on default behavior ...
Self.attr("__dict__")[Name] = Value;
}
static bool eq(const object& Self, const object& Other)
{
if(Other == boost::python::object())
return false;
extract<iunknown_wrapper> self(Self);
extract<iunknown_wrapper> other(Other);
return self == other;
}
static bool ne(const object& Self, const object& Other)
{
if(Other == boost::python::object())
return false;
extract<iunknown_wrapper> self(Self);
extract<iunknown_wrapper> other(Other);
return !(self == other);
}
void define_class_iunknown()
{
class_<iunknown_wrapper>("iunknown",
"Abstract interface that represents an object with unknown capabilities.\n\n"
"Methods for other implemented interfaces are added dynamically at runtime.",
no_init)
.def("__getattr__", getattr)
.def("__setattr__", setattr)
.def("__eq__", eq)
.def("__ne__", ne)
;
}
} // namespace python
} // namespace k3d
|