Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help
Special pages
SBGrid Resources
SBGrid Consortium
SBGrid Data Bank
Software Webinars
PyMOL Webinar
PyMOL Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Advanced Scripting
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Sending the Results back to Python/PyMOL === Once you're done with your calculations and want to send your data back to PyMOL, you need to package it up into a Python object, using the Python API, and then return it. You should be aware of the expected return value and how you're packaging the results. If you user calls, <source lang="python"> (results1,results2) = someCFunction(parameters1,parameters2) </source> then you need to package a list with two values. To build values for returning to PyMOL, use '''[http://www.python.org/doc/1.5.2p2/ext/buildValue.html Py_BuildValue]'''. Py_BuildValue takes a string indicating the type, and then a list of values. [http://docs.python.org/ext/buildValue.html Building values] for return has been documented very well. Consider an example: if I want to package an array of integers, the type specifier for two ints for Py_BuildValue is, "[i,i]", so my call could be: <source lang="c"> # Package the two ints into a Python pair of ints. PyObject* thePair = Py_BuildValue( "[i,i]", int1, in2 ); # Don't forget to tell Python about the object. Py_INCREF(thePair); </source> If you need to make a list of things to return, you iterate through a list and make a bunch of '''thePairs''' and add them to a Python list as follows: <source lang="c"> # Make the python list PyObject* theList = PyList_New(0); # Tell Python about it Py_INCREF(theList); for ( int i = 0; i < someLim; i++ ) { PyObject* thePair = Py_BuildValue( "[i,i]", int1, in2 ); Py_INCREF(thePair); PyList_Append(theList,thePair); </source> To add a list of lists, just make an outer list, <source lang="c">PyObject* outerList = PyList_New(0);</source> and iteratively add to it your inner lists: <source lang="c"> PyObject* outerList = PyList_New(0); Py_INCREF(outerList); for ( int i = 0; i < someLim; i++ ) { // make the inner list, called curList; curList = PyObject* curList = PyList_New(0); Py_INCREF(curList); // fill the inner list, using PyList_Append with some data, shown above ... PyList_Append(outerList,curList); </source> Great, now we can extract data from Python, use it in C/C++, and package it back up for returning to Python. Now, we need to learn about the minimal baggage needed for C to operate with Python. Keep reading; almost done.
Summary:
Please note that all contributions to PyMOL Wiki are considered to be released under the GNU Free Documentation License 1.2 (see
PyMOL Wiki:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Advanced Scripting
(section)
Add topic