File: engine.cpp

package info (click to toggle)
k3d 0.4.3.0-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 51,716 kB
  • ctags: 81,610
  • sloc: cpp: 283,698; ansic: 64,095; xml: 61,533; sh: 9,026; makefile: 5,282; python: 431; perl: 308; awk: 130
file content (266 lines) | stat: -rw-r--r-- 7,252 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
// K-3D
// Copyright (c) 1995-2004, 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
		\brief Implements Python engine, an implementation of k3d::iscript_engine that supports the Python language
		\author Anders Dahnielson (anders@dahnielson.com)
		\author Romain Behar (romainbehar@yahoo.com)
		\author Adam Hupp (hupp@cs.wisc.edu)
*/

#include <k3dsdk/application.h>
#include <k3dsdk/classes.h>
#include <k3dsdk/command_node.h>
#include <k3dsdk/file_helpers.h>
#include <k3dsdk/ideletable.h>
#include <k3dsdk/idocument.h>
#include <k3dsdk/iobject.h>
#include <k3dsdk/iscript_engine.h>
#include <k3dsdk/module.h>
#include <k3dsdk/string_modifiers.h>

#include "object_model.h"

#include <boost/filesystem/fstream.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>

/// Namespace reserved for the Python scripting engine module, to protect public symbols from name clashes with other modules
namespace libk3dpython
{

const std::string magic_token("#python");

class engine :
	public k3d::iscript_engine,
	public k3d::ideletable
{
public:
	engine()
	{
		// Fire up the interpreter
#if defined K3D_PLATFORM_DARWIN
		PyMac_Initialize();
#else
		Py_Initialize();
#endif
	}

	virtual ~engine()
	{
		// Shut down the interpreter
		if(Py_IsInitialized())
			Py_Finalize();
	}

	// k3d::iscript_engine implementation ...
	const std::string language()
	{
		return "Python";
	}

	bool can_execute(const std::string& Script)
	{
		// Look for magic token
		return Script.substr(0, magic_token.size()) == magic_token;
	}

	bool execute(const std::string& ScriptName, const std::string& Script, const context_t& Context)
	{
		// Sanity checks ...
		return_val_if_fail(Script.size(), false);
		return_val_if_fail(ScriptName.size(), false);

		// ReInitialize the Interpreter ...
		if(Py_IsInitialized())
			Py_Finalize();
		Py_Initialize();

		// Init modules
		init_modules();

		Py_XDECREF(m_document_context);
		m_document_context = 0;

		Py_XDECREF(m_object_context);
		m_object_context = 0;

		for(context_t::const_iterator i = Context.begin(); i != Context.end(); ++i)
			{
				if(k3d::idocument* j = dynamic_cast<k3d::idocument*>(*i))
					{
						m_document_context = CPythonDocument::Create(j);
						return_val_if_fail(m_document_context, false);
					}
				else if(k3d::iobject* j = dynamic_cast<k3d::iobject*>(*i))
					{
						m_object_context = CPythonObject::Create(j);
						return_val_if_fail(m_object_context, false);
					}
			}

		Py_InitModule("_My", Methods);

		PyRun_SimpleString("import _My");

		if(m_document_context)
			PyRun_SimpleString("MyDocument = DocumentClass(_My.MyDocument())");
		else
			PyRun_SimpleString("MyDocument = None");

		if(m_object_context)
			PyRun_SimpleString("MyObject = ObjectClass(_My.MyObject())");
		else
			PyRun_SimpleString("MyObject = None");

		// Evaluate the script ...
		int returncode;
		returncode = PyRun_SimpleString((char*) Script.c_str());

		return (returncode == 0) ? true : false;
	}

	bool halt()
	{
		return false;
	}

	void bless_script(std::string& Script)
	{
		// Don't keep adding magic tokens to code that already has one ...
		if(can_execute(Script))
			return;

		Script.insert(Script.begin(), magic_token.begin(), magic_token.end());
	}

	bool convert_command(k3d::icommand_node& CommandNode, const std::string& Name, const std::string& Arguments, std::string& Result)
	{
		Result = "Application.CommandNode(\"";
		Result += k3d::command_node_path(CommandNode);
		Result += "\").Command(\"";
		Result += Name;
		Result += "\", \"";
		Result += k3d::replace_all("\"", "\\\"", Arguments); // Make sure arguments are properly escaped
		Result += "\");";

		return true;
	}

private:
	static PyObject *m_document_context;
	static PyObject *m_object_context;
	static PyMethodDef Methods[];

	bool init_modules()
	{
		// Initialize the Modules ...
		Py_InitModule("_ApplicationModule", CPythonApplication::Methods);
		Py_InitModule("_PluginFactoryModule", CPythonPluginFactory::Methods);
		Py_InitModule("_CommandNodeModule", CPythonCommandNode::Methods);
		Py_InitModule("_PropertyModule", CPythonProperty::Methods);
		Py_InitModule("_DocumentModule", CPythonDocument::Methods);
		Py_InitModule("_BezierChannelModule", CPythonBezierChannel::Methods);
		Py_InitModule("_ObjectModule", CPythonObject::Methods);

		Py_InitModule("_UIModule", CPythonUserInterface::Methods);
		Py_InitModule("_ScriptEnginesModule", CPythonScriptEngines::Methods);

		Py_InitModule("_mesh_module", python_mesh::methods);
		Py_InitModule("_point_module", python_point::methods);
		Py_InitModule("_polyhedron_module", python_polyhedron::methods);
		Py_InitModule("_face_module", python_face::methods);
		Py_InitModule("_blobby_module", python_blobby_opcode::methods);

		// Initialize the classes ...
		boost::filesystem::path python_interface(k3d::application().share_path() / "python_engine/k3d_interface.py");
		if(!boost::filesystem::exists(python_interface))
			{
				std::cerr << error << "Python Engine file 'k3d_interface.py' not found" << std::endl;
				return false;
			}

		boost::filesystem::ifstream py_file(python_interface);
		return_val_if_fail(py_file.good(), false);

		// Load Python interface ...
		std::string k3d_modules;
		while(!py_file.eof())
			{
				std::string line;
				k3d::getline(py_file, line);
				k3d_modules.append(line + "\n");
			}

		PyRun_SimpleString(const_cast<char*>(k3d_modules.c_str()));

		return true;
	}

	static PyObject* MyDocument(PyObject* self, PyObject* args)
	{
		if(m_document_context)
			{
				Py_INCREF(m_document_context);
				return m_document_context;
			}

		return Py_None;
	}

	static PyObject* MyObject(PyObject* self, PyObject* args)
	{
		if(m_object_context)
			{
				Py_INCREF(m_object_context);
				return m_object_context;
			}

		return Py_None;
	}
};

PyObject* engine::m_document_context = 0;
PyObject* engine::m_object_context = 0;

PyMethodDef engine::Methods[] =
{
	{"MyDocument", MyDocument, METH_VARARGS, "Return MyDocument"},
	{"MyObject", MyObject, METH_VARARGS, "Return MyObject"},
	{NULL, NULL, 0, NULL}
};

k3d::iplugin_factory& engine_factory()
{
	static k3d::plugin_factory<k3d::application_plugin<engine>, k3d::interface_list<k3d::iscript_engine> > factory(
		k3d::classes::PythonEngine(),
		"Python",
		"Python scripting engine",
		"ScriptEngines");

	return factory;
}

} // namespace libk3dpython

K3D_MODULE_START(k3d::uuid(0x9d1e8e42, 0x3dc7422c, 0xbe1c3215, 0x99bd67c3), Registry)
	Registry.register_factory(libk3dpython::engine_factory());
K3D_MODULE_END