00001 /*************************************************************************** 00002 *cr 00003 *cr (C) Copyright 1995-2011 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.14 $ $Date: 2010/12/16 04:08:56 $ 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 if (!module) return NULL; 00113 PyObject *module_dict = PyModule_GetDict(module); 00114 if (!module_dict) return NULL; 00115 PyObject *c_obj = PyDict_GetItemString(module_dict, (char *)"-vmdapp-"); 00116 if (!c_obj) return NULL; 00117 if (PyCObject_Check(c_obj)) 00118 return (VMDApp *)PyCObject_AsVoidPtr(c_obj); 00119 return NULL; 00120 } 00121 00122 void set_vmdapp(VMDApp *app) { 00123 PyObject *mod = PyImport_ImportModule((char *)"__builtin__"); 00124 PyObject_SetAttrString(mod, (char *)"-vmdapp-", 00125 PyCObject_FromVoidPtr(app, NULL)); 00126 Py_DECREF(mod); 00127 } 00128 00129 int py_array_from_obj(PyObject *obj, float *arr) { 00130 if (PyTuple_Check(obj)) { 00131 if (PyTuple_Size(obj) != 3) { 00132 PyErr_SetString(PyExc_ValueError, (char *)"Tuple must have length 3"); 00133 return 0; 00134 } 00135 for (int i=0; i<3; i++) { 00136 PyObject *elem = PyTuple_GET_ITEM(obj, i); 00137 arr[i] = PyFloat_AsDouble(elem); 00138 if (PyErr_Occurred()) 00139 return 0; 00140 } 00141 return 1; // successful return 00142 } 00143 PyErr_SetString(PyExc_ValueError, (char *)"Invalid tuple"); 00144 return 0; 00145 } 00146 00147 Timestep *parse_timestep(VMDApp *app, int molid, int frame) { 00148 if (molid < 0) molid = app->molecule_top(); 00149 Molecule *mol = app->moleculeList->mol_from_id(molid); 00150 if (!mol) { 00151 PyErr_SetString(PyExc_ValueError, (char *)"Invalid molecule id"); 00152 return NULL; 00153 } 00154 Timestep *ts = NULL; 00155 if (frame == -1) { 00156 ts = mol->current(); 00157 } else if (frame == -2) { 00158 ts = mol->get_last_frame(); 00159 } else { 00160 ts = mol->get_frame(frame); 00161 } 00162 if (!ts) { 00163 PyErr_SetString(PyExc_ValueError, (char *)"Invalid frame"); 00164 return NULL; 00165 } 00166 return ts; 00167 } 00168
1.2.14 written by Dimitri van Heesch,
© 1997-2002