Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

py_material.C

Go to the documentation of this file.
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_material.C,v $
00013  *      $Author: johns $        $Locker:  $             $State: Exp $
00014  *      $Revision: 1.22 $       $Date: 2011/06/29 16:03:13 $
00015  *
00016  ***************************************************************************
00017  * DESCRIPTION:
00018  *  Python interface for managing materials.
00019  ***************************************************************************/
00020 
00021 #include "py_commands.h"
00022 #include "CmdMaterial.h"
00023 #include "utilities.h"
00024 #include "MaterialList.h"
00025 #include "CommandQueue.h"
00026 #include "VMDApp.h"
00027 #include <stdlib.h>
00028 #include <ctype.h>
00029 
00030 // listall(): list all materials
00031 static PyObject *listall(PyObject *self, PyObject *args) {
00032   if (!PyArg_ParseTuple(args, (char *)""))
00033     return NULL;
00034 
00035   MaterialList *mlist = get_vmdapp()->materialList;
00036   PyObject *newlist = PyList_New(0);
00037   for (int i=0; i<mlist->num(); i++) 
00038     PyList_Append(newlist, PyString_FromString(mlist->material_name(i)));
00039 
00040   return newlist;
00041 }
00042 
00043 // settings(name): return a dictionary object with the material settings 
00044 static PyObject *settings(PyObject *self, PyObject *args) {
00045   char *name;
00046   if (!PyArg_ParseTuple(args, (char *)"s", &name))
00047     return NULL;
00048 
00049   MaterialList *mlist = get_vmdapp()->materialList;
00050   int ind = mlist->material_index(name);
00051   if (ind < 0) {
00052     PyErr_SetString(PyExc_ValueError, "No such material");
00053     return NULL;
00054   }
00055   
00056   PyObject *dict = PyDict_New();
00057   PyDict_SetItemString(dict, (char *)"ambient", 
00058     PyFloat_FromDouble(mlist->get_ambient(ind)));
00059   PyDict_SetItemString(dict, (char *)"specular", 
00060     PyFloat_FromDouble(mlist->get_specular(ind)));
00061   PyDict_SetItemString(dict, (char *)"diffuse", 
00062     PyFloat_FromDouble(mlist->get_diffuse(ind)));
00063   PyDict_SetItemString(dict, (char *)"shininess", 
00064     PyFloat_FromDouble(mlist->get_shininess(ind)));
00065   PyDict_SetItemString(dict, (char *)"opacity", 
00066     PyFloat_FromDouble(mlist->get_opacity(ind)));
00067   PyDict_SetItemString(dict, (char *)"outline", 
00068     PyFloat_FromDouble(mlist->get_outline(ind)));
00069   PyDict_SetItemString(dict, (char *)"outlinewidth", 
00070     PyFloat_FromDouble(mlist->get_outlinewidth(ind)));
00071   PyDict_SetItemString(dict, (char *)"transmode", 
00072     PyFloat_FromDouble(mlist->get_transmode(ind)));
00073  
00074   return dict;
00075 }
00076 
00077 static PyObject *set_default(PyObject *self, PyObject *args) {
00078   int ind;
00079   if (!PyArg_ParseTuple(args, (char *)"i:material.default", &ind))
00080     return NULL;
00081   if (!get_vmdapp()->material_restore_default(ind)) {
00082     PyErr_SetString(PyExc_ValueError, (char *)"Material has no default settings");
00083     return NULL;
00084   }
00085   Py_INCREF(Py_None);
00086   return Py_None;
00087 }
00088 
00089 // add('name=None', copy=None): create a new material with the given name.
00090 // optionally copy the given name.  If no name is given, make up a new name.
00091 
00092 static PyObject *add(PyObject *self, PyObject *args) {
00093   char *name = NULL;
00094   char *copy = NULL;
00095   if (!PyArg_ParseTuple(args, (char *)"|ss:material.add", &name, &copy))
00096     return NULL;
00097 
00098   VMDApp *app = get_vmdapp();
00099   MaterialList *mlist = app->materialList;
00100   if (name) {
00101     int ind = mlist->material_index(name);
00102     if (ind >= 0) { // material already exists
00103       PyErr_SetString(PyExc_ValueError, (char *)"Material already exists");
00104       return NULL;
00105     }
00106   }
00107   if (copy) {
00108     int ind = mlist->material_index(copy);
00109     if (ind < 0) { // material doesn't exist
00110       PyErr_SetString(PyExc_ValueError, (char *)"Material to copy doesn't exist");
00111       return NULL;
00112     }
00113   }
00114   const char *result = app->material_add(name, copy);
00115   if (!result) {
00116     PyErr_SetString(PyExc_ValueError, (char *)"Unable to add material.");
00117     return NULL;
00118   }
00119   return PyString_FromString((char *)result);
00120 }
00121 
00122 // delete('name').
00123 static PyObject *matdelete(PyObject *self, PyObject *args) {
00124   char *name = NULL;
00125   if (!PyArg_ParseTuple(args, (char *)"s:material.delete", &name)) 
00126     return NULL;
00127   VMDApp *app = get_vmdapp();
00128   if (!app->material_delete(name)) {
00129     PyErr_SetString(PyExc_ValueError, (char *)"Unable to delete material.");
00130     return NULL;
00131   }
00132   Py_INCREF(Py_None);
00133   return Py_None;
00134 }
00135 
00136 // rename(oldname, newname): change the name of an existing material
00137 
00138 static PyObject *rename(PyObject *self, PyObject *args) {
00139   char *oldname, *newname;
00140   if (!PyArg_ParseTuple(args, (char *)"ss:material.rename",&oldname, &newname))
00141     return NULL;
00142 
00143   VMDApp *app = get_vmdapp();
00144   MaterialList *mlist = app->materialList;
00145   int ind = mlist->material_index(oldname);
00146   if (ind < 0) {
00147     PyErr_SetString(PyExc_ValueError, "Material to change does not exist");
00148     return NULL;
00149   }
00150   if (mlist->material_index(newname) >= 0) {
00151     PyErr_SetString(PyExc_ValueError, "New name already exists");
00152     return NULL;
00153   }
00154   if (!app->material_rename(oldname, newname)) {
00155     PyErr_SetString(PyExc_ValueError, "Could not change material name");
00156     return NULL;
00157   }
00158   Py_INCREF(Py_None);
00159   return Py_None;
00160 }
00161 
00162 // change(name, ambient, specular, diffuse, shininess, opacity, 
00163 //        outline, outlinewidth, transmode)
00164 static PyObject *change(PyObject *self, PyObject *args, PyObject *keywds) {
00165   char *name;
00166   float ambient, specular, diffuse, shininess, opacity;
00167   float outline, outlinewidth, transmode;
00168 
00169   static char *kwlist[] = {
00170     (char *)"name",
00171     (char *)"ambient",
00172     (char *)"specular",
00173     (char *)"diffuse",
00174     (char *)"shininess",
00175     (char *)"opacity",
00176     (char *)"outline",
00177     (char *)"outlinewidth",
00178     (char *)"transmode", 
00179     NULL
00180   };
00181 
00182   if (!PyArg_ParseTupleAndKeywords(args, keywds, (char *)"s|ffffffff", kwlist,
00183       &name, &ambient, &specular, &diffuse, &shininess, &opacity, 
00184       &outline, &outlinewidth, &transmode))
00185     return NULL;
00186 
00187   VMDApp *app = get_vmdapp();
00188   MaterialList *mlist = app->materialList;
00189   int ind = mlist->material_index(name);
00190   if (ind < 0) {
00191     PyErr_SetString(PyExc_ValueError, "Material to change does not exist");
00192     return NULL;
00193   }
00194 
00195   if (PyDict_GetItemString(keywds, (char *)"ambient"))
00196     app->material_change(name, MAT_AMBIENT, ambient);
00197   if (PyDict_GetItemString(keywds, (char *)"specular"))
00198     app->material_change(name, MAT_SPECULAR, specular);
00199   if (PyDict_GetItemString(keywds, (char *)"diffuse"))
00200     app->material_change(name, MAT_DIFFUSE, diffuse);
00201   if (PyDict_GetItemString(keywds, (char *)"shininess"))
00202     app->material_change(name, MAT_SHININESS, shininess);
00203   if (PyDict_GetItemString(keywds, (char *)"opacity"))
00204     app->material_change(name, MAT_OPACITY, opacity);
00205   if (PyDict_GetItemString(keywds, (char *)"outline"))
00206     app->material_change(name, MAT_OUTLINE, outline);
00207   if (PyDict_GetItemString(keywds, (char *)"outlinewidth"))
00208     app->material_change(name, MAT_OUTLINEWIDTH, outlinewidth);
00209   if (PyDict_GetItemString(keywds, (char *)"transmode"))
00210     app->material_change(name, MAT_TRANSMODE, transmode);
00211   
00212   Py_INCREF(Py_None);
00213   return Py_None;
00214 }
00215    
00216 // default(index) - restore default of material with given index
00217 static PyMethodDef methods[] = {
00218   {(char *)"listall", (vmdPyMethod)listall, METH_VARARGS },
00219   {(char *)"settings", (vmdPyMethod)settings, METH_VARARGS },
00220   {(char *)"add", (vmdPyMethod)add, METH_VARARGS },
00221   {(char *)"delete", (vmdPyMethod)matdelete, METH_VARARGS },
00222   {(char *)"rename", (vmdPyMethod)rename, METH_VARARGS },
00223   {(char *)"change", (PyCFunction)change, METH_VARARGS | METH_KEYWORDS },
00224   {(char *)"default", (vmdPyMethod)set_default, METH_VARARGS },
00225   {NULL, NULL}
00226 };
00227 
00228 void initmaterial() {
00229   (void) Py_InitModule((char *)"material", methods);
00230 }
00231 
00232  
00233 
00234 

Generated on Thu May 24 01:51:26 2012 for VMD (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002