How to access an Array and a struct within a struct wrapped by Swig ?

Discussion in 'Python' started by emmanuel.rivoire@gmail.com, Aug 16, 2008.

  1. Guest

    Hello,

    I spent almost a week to be able to embed Python within my C++ game
    engine.
    I wrote a mini-tutorial of what I was able to do so far here :
    http://forums.indiegamer.com/showpost.php?p=169352&postcount=9

    At the end of my tutorial, I wrote : "You can do the same with C++
    classes, and with structures within structures. Swig can parse & wrap
    most C/C++ declarations."

    Gloups, I was so wrong..! :-S

    It seems that I can read a struct within my struct, but I cannot write
    values to it.
    Also, I cannot access the array from within my struct.

    Any help to achieve this would be very welcome.

    Here the problem in detail :
    I'm using Python 2.5.2 & Swig 1.3.36.

    The structures :
    struct SIn
    {
    int in1, in2;
    };

    struct SPythoned
    {
    SIn Inner;
    float Skill[16];
    };

    The python code :
    =======================
    def TestObject(o):
    print(type(o))
    print(type(o.Skill))
    print(o.Skill)
    print(type(o.Inner))
    print(type(o.Inner.in1))
    print(o.Inner.in1)
    print(o.Inner.in2)

    o.Inner.in1 = 12
    o.Inner.in2 = 17

    print(o.Inner.in1)
    print(o.Inner.in2)
    =======================


    It gives me this output :
    =======================
    <class 'TE2008.SPythoned'>
    <type 'PySwigObject'>
    _20fd1300_p_float
    <class 'TE2008.SIn'>
    <type 'int'>
    2
    22
    2
    22
    =======================

    So althought I can read the value, the "o.Inner.inX = Y" are without
    effect..! (and checking the value in the object in the C part confirms
    this).

    Moreover, if I add these lines :
    for i in range(16) :
    print(o.Skill)

    I get this error :
    Traceback (most recent call last):
    File "d:\TE 2008\PyTest.py", line 32, in TestObject
    print(o.Skill)
    TypeError: 'PySwigObject' object is unsubscriptable

    Any idea how to make this work ..?
     
    , Aug 16, 2008
    #1
    1. Advertising

  2. Guest

    Re: How to access an Array and a struct within a struct wrapped bySwig ?

    Ok, Swig needs the structure to be declared like this :
    typedef struct SIn
    {
    int in1, in2;
    } SIn;

    Else, it doesn't understand it's a structure... :-S
    Now, I can write to it.

    I also found out that Swig didn't allow to write to the array :
    http://www.swig.org/Doc1.3/SWIG.html#SWIG_nn34 ... :-S
    So I just have to find out how to read the array, and I'll be all
    good..!
     
    , Aug 16, 2008
    #2
    1. Advertising

  3. Re: How to access an Array and a struct within a struct wrapped bySwig ?

    Hi,
    If you have struct defined in .h file then it's ok or can dea easyily
    with struct written with .i file,
    in .h file,
    struct def
    {
    int a;
    char name[10];
    };
    can be referenced as
    >>strvar=new_def() //now strvar is your

    object for struct def
    and can access a and name as, get and
    set for individual member see,
    >>def_a_set(strvar,10)
    >>def_a_get(strvar) will return 10.

    or you can define struct as,

    typedef struct
    {
    int sage;
    }coll;
    within .h file and access similarly.

    regard's
    Anish Chapagain
    www.mysticnepal.com

    On Aug 16, 11:38 am, wrote:
    > Hello,
    >
    > I spent almost a week to be able to embed Python within my C++ game
    > engine.
    > I wrote a mini-tutorial of what I was able to do so far here :http://forums.indiegamer.com/showpost.php?p=169352&postcount=9
    >
    > At the end of my tutorial, I wrote : "You can do the same with C++
    > classes, and with structures within structures. Swig can parse & wrap
    > most C/C++ declarations."
    >
    > Gloups, I was so wrong..! :-S
    >
    > It seems that I can read a struct within my struct, but I cannot write
    > values to it.
    > Also, I cannot access the array from within my struct.
    >
    > Any help to achieve this would be very welcome.
    >
    > Here the problem in detail :
    > I'm using Python 2.5.2 & Swig 1.3.36.
    >
    > The structures :
    > struct SIn
    > {
    >         int in1, in2;
    >
    > };
    >
    > struct SPythoned
    > {
    >         SIn Inner;
    >         float Skill[16];
    >
    > };
    >
    > The python code :
    > =======================
    > def TestObject(o):
    >         print(type(o))
    >         print(type(o.Skill))
    >         print(o.Skill)
    >         print(type(o.Inner))
    >         print(type(o.Inner.in1))
    >         print(o.Inner.in1)
    >         print(o.Inner.in2)
    >
    >         o.Inner.in1 = 12
    >         o.Inner.in2 = 17
    >
    >         print(o.Inner.in1)
    >         print(o.Inner.in2)
    > =======================
    >
    > It gives me this output :
    > =======================
    > <class 'TE2008.SPythoned'>
    > <type 'PySwigObject'>
    > _20fd1300_p_float
    > <class 'TE2008.SIn'>
    > <type 'int'>
    > 2
    > 22
    > 2
    > 22
    > =======================
    >
    > So althought I can read the value, the "o.Inner.inX = Y" are without
    > effect..! (and checking the value in the object in the C part confirms
    > this).
    >
    > Moreover, if I add these lines :
    >         for i in range(16) :
    >                 print(o.Skill)
    >
    > I get this error :
    > Traceback (most recent call last):
    >   File "d:\TE 2008\PyTest.py", line 32, in TestObject
    >     print(o.Skill)
    > TypeError: 'PySwigObject' object is unsubscriptable
    >
    > Any idea how to make this work ..?


    ---------------
     
    Anish Chapagain, Aug 16, 2008
    #3
  4. Guest

    Re: How to access an Array and a struct within a struct wrapped bySwig ?

    Anish Chapagain, I already know how to use structure, as my example
    shown it.
    I had trouble only with the nested structures, and it was coz of
    missing typedef (ie: Swig need C struct declaration, not C++).

    And I still cannot find how to get my arrays... :-S
     
    , Aug 18, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daniel Rudy
    Replies:
    7
    Views:
    455
    Daniel Rudy
    Mar 31, 2006
  2. Replies:
    8
    Views:
    322
  3. Gennady Bystritksy

    Cloning SWIG wrapped C++ classes

    Gennady Bystritksy, Apr 29, 2005, in forum: Ruby
    Replies:
    0
    Views:
    128
    Gennady Bystritksy
    Apr 29, 2005
  4. Lyes Amazouz
    Replies:
    2
    Views:
    170
    Lyes Amazouz
    Aug 20, 2008
  5. Tuan  Bui
    Replies:
    14
    Views:
    476
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page