Trying to scoop in wrappers down to neck before I jump into pymel

well, I was going to start reading the documentation of Pymel rather than watching some video tutorial but before doing so I wanted to clear out my doubts.

On this page http://autodesk.com/us/maya/2011help/PyMel/why_pymel.html

the paragraph under maya.Open maya says

In the case of the C++ API, Autodesk went with an open source wrapper called swig which generates python functions and classes from C++ counterparts.

So do they mean these python functions generated by SWIG are wrappers that can be used by a python programmer as if importing a module

for example http://www.swig.org/tutorial.html.

Building a Python module
Turning C code into a Python module is also easy. Simply do the following (shown for Irix, see the SWIG Wiki Shared Libraries page for help with other operating systems):

     unix % swig -python example.i
     unix % gcc -c example.c example_wrap.c \
            -I/usr/local/include/python2.1
     unix % ld -shared example.o example_wrap.o -o _example.so 
     

We can now use the Python module as follows :

     >>> import example
     >>> example.fact(5)
     120
     >>> example.my_mod(7,3)
     1
     >>> example.get_time()
     'Sun Feb 11 23:01:07 1996'
     >>>
     


So the example being imported above is from example.so that is generated by SWIG as python wrapper ?

Your question is a little ADD but here goes:

A simplified explanation of SWIG is that it creates a 1:1 python binding for the C++ function.
ie, you use the python modules as you would any other python module. It then uses swig behind the scenes to interface with C++ and return the value as you would expect in python.

As a user, you ideally don’t need to care about any of the backend stuff.Especially in the context of something like pymel where all of it is abstracted, you don’t need to care if something is using SWIG.

For the Python API 1, the only part that gets annoying is that most of the documentation requires you to understand C++ or atleast be able to sort of decipher. But the implementation itself requires none of this.
Python API 2 is much better at this.

I didnt understood why you said “you use the python modules as you would import any other python module” ? in the next line when you mentioned “swig behind the scene” do you mean the wrapper function ? I do not care about SWIG !! ignoring the links in my post above the subject mention its about wrapper function .

So my understanding is SWIG is used to write wrapper function for interfacing to python from C++…

I think you may need to rephrase your question in a better way, but yes SWIG is used to help autogenerate a Python binding for C++ functions

Once a bit of C++ is wrapped with swig, it looks like a regular python module. You import it using the standard python import statement, and it exposes classes and functions just like a hand-written bit of python. If you didn’t know it was ‘really’ c++, you might never realize…

…except…

the programming models for the two languages are quite different. There are lots of concepts in c++ that don’t have natural analogs in python (the most obvious being manual memory management, pointers, and strong typing). SWIG doesn’t do anything about that – you have to do it yourself, and it involves a bunch of grunt work. That’s why so much OpenMaya code reads like gibberish – you’re constantly doing stuff like:

  • creating empty variables and passing them in to functions, expecting them to come back filled
  • jumping through hoops to turn simple python types (numbers, strings) into the exact right C++ type (longs, shorts, floats, doubles, chars, *chars… etc)
  • crashing Maya completely, which is otherwise pretty hard to do in python :slight_smile:

A more sophisticated wrapper – like PyMel – hides a lot of this nonsense from you, but it’s still happening under the hood.

way to go !!!

:nod:

[QUOTE=Theodox;18469] If you didn’t know it was ‘really’ c++, you might never realize…[/QUOTE]

And then Maya crashes with no explanation!

What do you mean ‘no explanation’. It’s a “Fatal Error.” Surely that’s explanation enough!

Note habitual switching between single quotes and doubles… too much python…

Hey at least it attempted to save!