00001 /*************************************************************************** 00002 *cr 00003 *cr (C) Copyright 1995-2008 The Board of Trustees of the 00004 *cr University of Illinois 00005 *cr All Rights Reserved 00006 *cr 00007 ***************************************************************************/ 00008 00009 /*************************************************************************** 00010 * RCS INFORMATION: 00011 * 00012 * $RCSfile: py_commands.C,v $ 00013 * $Author: johns $ $Locker: $ $State: Exp $ 00014 * $Revision: 1.11 $ $Date: 2008/03/27 19:36:52 $ 00015 * 00016 *************************************************************************** 00017 * DESCRIPTION: 00018 * Core VMD Python interface 00019 ***************************************************************************/ 00020 00021 #include "py_commands.h" 00022 #include "VMDApp.h" 00023 #include "Molecule.h" 00024 #include "MoleculeList.h" 00025 00026 /* 00027 00028 Some distributed versions of VMD are linked against the Python 2.0 00029 library. The following BeOpen license agreement permits us to use the 00030 Python 2.0 libraries in this fashion. The BeOpen license agreement is in 00031 no way applicable to the license under which VMD itself is distributed; 00032 persuant to item 2 below, we merely include a copy of the BeOpen license 00033 to indicate our use of the BeOpen software. 00034 00035 HISTORY OF THE SOFTWARE 00036 ======================= 00037 00038 Python was created in the early 1990s by Guido van Rossum at Stichting 00039 Mathematisch Centrum (CWI) in the Netherlands as a successor of a 00040 language called ABC. Guido is Python's principal author, although it 00041 includes many contributions from others. The last version released 00042 from CWI was Python 1.2. In 1995, Guido continued his work on Python 00043 at the Corporation for National Research Initiatives (CNRI) in Reston, 00044 Virginia where he released several versions of the software. Python 00045 1.6 was the last of the versions released by CNRI. In 2000, Guido and 00046 the Python core developement team moved to BeOpen.com to form the 00047 BeOpen PythonLabs team (www.pythonlabs.com). Python 2.0 is the first 00048 release from PythonLabs. Thanks to the many outside volunteers who 00049 have worked under Guido's direction to make this release possible. 00050 00051 00052 00053 BEOPEN.COM TERMS AND CONDITIONS FOR PYTHON 2.0 00054 ============================================== 00055 00056 BEOPEN PYTHON OPEN SOURCE LICENSE AGREEMENT VERSION 1 00057 ----------------------------------------------------- 00058 00059 1. This LICENSE AGREEMENT is between BeOpen.com ("BeOpen"), having an 00060 office at 160 Saratoga Avenue, Santa Clara, CA 95051, and the 00061 Individual or Organization ("Licensee") accessing and otherwise using 00062 this software in source or binary form and its associated 00063 documentation ("the Software"). 00064 00065 2. Subject to the terms and conditions of this BeOpen Python License 00066 Agreement, BeOpen hereby grants Licensee a non-exclusive, 00067 royalty-free, world-wide license to reproduce, analyze, test, perform 00068 and/or display publicly, prepare derivative works, distribute, and 00069 otherwise use the Software alone or in any derivative version, 00070 provided, however, that the BeOpen Python License is retained in the 00071 Software, alone or in any derivative version prepared by Licensee. 00072 00073 3. BeOpen is making the Software available to Licensee on an "AS IS" 00074 basis. BEOPEN MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR 00075 IMPLIED. BY WAY OF EXAMPLE, BUT NOT LIMITATION, BEOPEN MAKES NO AND 00076 DISCLAIMS ANY REPRESENTATION OR WARRANTY OF MERCHANTABILITY OR FITNESS 00077 FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF THE SOFTWARE WILL NOT 00078 INFRINGE ANY THIRD PARTY RIGHTS. 00079 00080 4. BEOPEN SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF THE 00081 SOFTWARE FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS 00082 AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THE SOFTWARE, OR ANY 00083 DERIVATIVE THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF. 00084 00085 5. This License Agreement will automatically terminate upon a material 00086 breach of its terms and conditions. 00087 00088 6. This License Agreement shall be governed by and interpreted in all 00089 respects by the law of the State of California, excluding conflict of 00090 law provisions. Nothing in this License Agreement shall be deemed to 00091 create any relationship of agency, partnership, or joint venture 00092 between BeOpen and Licensee. This License Agreement does not grant 00093 permission to use BeOpen trademarks or trade names in a trademark 00094 sense to endorse or promote products or services of Licensee, or any 00095 third party. As an exception, the "BeOpen Python" logos available at 00096 http://www.pythonlabs.com/logos.html may be used according to the 00097 permissions granted on that web page. 00098 00099 7. By copying, installing or otherwise using the software, Licensee 00100 agrees to be bound by the terms and conditions of this License 00101 Agreement. 00102 00103 00104 */ 00105 00106 00107 // The VMDApp instance will be found in the VMDApp module, under the 00108 // VMDApp dictionary entry. Got it? 00109 00110 VMDApp *get_vmdapp() { 00111 PyObject *module = PyImport_ImportModule((char *)"__builtin__"); 00112 PyObject *module_dict = PyModule_GetDict(module); 00113 PyObject *c_obj = PyDict_GetItemString(module_dict, (char *)"-vmdapp-"); 00114 if (PyCObject_Check(c_obj)) 00115 return (VMDApp *)PyCObject_AsVoidPtr(c_obj); 00116 return NULL; 00117 } 00118 00119 void set_vmdapp(VMDApp *app) { 00120 PyObject *mod = PyImport_ImportModule((char *)"__builtin__"); 00121 PyObject_SetAttrString(mod, (char *)"-vmdapp-", 00122 PyCObject_FromVoidPtr(app, NULL)); 00123 Py_DECREF(mod); 00124 } 00125 00126 int py_array_from_obj(PyObject *obj, float *arr) { 00127 if (PyTuple_Check(obj)) { 00128 if (PyTuple_Size(obj) != 3) { 00129 PyErr_SetString(PyExc_ValueError, (char *)"Tuple must have length 3"); 00130 return 0; 00131 } 00132 for (int i=0; i<3; i++) { 00133 PyObject *elem = PyTuple_GET_ITEM(obj, i); 00134 arr[i] = PyFloat_AsDouble(elem); 00135 if (PyErr_Occurred()) 00136 return 0; 00137 } 00138 return 1; // successful return 00139 } 00140 PyErr_SetString(PyExc_ValueError, (char *)"Invalid tuple"); 00141 return 0; 00142 } 00143 00144 Timestep *parse_timestep(VMDApp *app, int molid, int frame) { 00145 if (molid < 0) molid = app->molecule_top(); 00146 Molecule *mol = app->moleculeList->mol_from_id(molid); 00147 if (!mol) { 00148 PyErr_SetString(PyExc_ValueError, (char *)"Invalid molecule id"); 00149 return NULL; 00150 } 00151 Timestep *ts = NULL; 00152 if (frame == -1) { 00153 ts = mol->current(); 00154 } else if (frame == -2) { 00155 ts = mol->get_last_frame(); 00156 } else { 00157 ts = mol->get_frame(frame); 00158 } 00159 if (!ts) { 00160 PyErr_SetString(PyExc_ValueError, (char *)"Invalid frame"); 00161 return NULL; 00162 } 00163 return ts; 00164 } 00165
1.2.14 written by Dimitri van Heesch,
© 1997-2002