开发者

Exposing a new type to python at run time

开发者 https://www.devze.com 2023-03-16 05:27 出处:网络
I have gone through and defined my new types, stored them in a pytypeobject and called the following functions (after initializing the interpreter):

I have gone through and defined my new types, stored them in a pytypeobject and called the following functions (after initializing the interpreter):

PyType_Ready(); //this takes my defined typed

PyModule_AddObject(); //this adds the defined type to a module I created using PyModule_Create();

Now when I try to use the type I have defined the interprete开发者_如何学Pythonr says it doesn't exist so I am assuming there is another step that must be taken in order to add a type at run time or there is some other set of steps I must take to achieve this.

Any help will be greatly appreciated.

Python Syntax Error:

Traceback (most recent call last):
  File "testscript.py", line 1, in <module>
    import Bound
ImportError: No module named Bound

Test script contents:

import Bound
l = Bound.Foo()

Bound is the name I defined the module, and Foo is the type I am trying to define, for testing purposes.


So I figured out what I needed to do, I had to call a function before Py_Initialize,

PyImport_AppendInittab( ModuleName, ModuleInitFunction );

This adds the module name to the python module dictionary so when you call import ModuleName, if it is for the first time it will call the ModuleInitFunction ( which I had, but wasn't using in the proper place ), which creates the module. Now after a module is created you can then proceed to add types to it at run-time which is what I do.

Right now I can successfully bind user defined C++ classes to python at run-time, then use them in a script in our Game Engine. Right now I am working out run-time kinks, but it should be up and running within the next few days. Anyways if anyone else ever decides to embed python into a game engine for scripting I'm sure I can help.

0

精彩评论

暂无评论...
验证码 换一张
取 消