00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
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
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
00068 return dict;
00069 }
00070
00071 static PyObject *set_default(PyObject *self, PyObject *args) {
00072 int ind;
00073 if (!PyArg_ParseTuple(args, (char *)"i:material.default", &ind))
00074 return NULL;
00075 if (!get_vmdapp()->material_restore_default(ind)) {
00076 PyErr_SetString(PyExc_ValueError, (char *)"Material has no default settings");
00077 return NULL;
00078 }
00079 Py_INCREF(Py_None);
00080 return Py_None;
00081 }
00082
00083
00084
00085
00086 static PyObject *add(PyObject *self, PyObject *args) {
00087 char *name = NULL;
00088 char *copy = NULL;
00089 if (!PyArg_ParseTuple(args, (char *)"|ss:material.add", &name, ©))
00090 return NULL;
00091
00092 VMDApp *app = get_vmdapp();
00093 MaterialList *mlist = app->materialList;
00094 if (name) {
00095 int ind = mlist->material_index(name);
00096 if (ind >= 0) {
00097 PyErr_SetString(PyExc_ValueError, (char *)"Material already exists");
00098 return NULL;
00099 }
00100 }
00101 if (copy) {
00102 int ind = mlist->material_index(copy);
00103 if (ind < 0) {
00104 PyErr_SetString(PyExc_ValueError, (char *)"Material to copy doesn't exist");
00105 return NULL;
00106 }
00107 }
00108 const char *result = app->material_add(name, copy);
00109 if (!result) {
00110 PyErr_SetString(PyExc_ValueError, (char *)"Unable to add material.");
00111 return NULL;
00112 }
00113 return PyString_FromString((char *)result);
00114 }
00115
00116
00117 static PyObject *matdelete(PyObject *self, PyObject *args) {
00118 char *name = NULL;
00119 if (!PyArg_ParseTuple(args, (char *)"s:material.delete", &name))
00120 return NULL;
00121 VMDApp *app = get_vmdapp();
00122 if (!app->material_delete(name)) {
00123 PyErr_SetString(PyExc_ValueError, (char *)"Unable to delete material.");
00124 return NULL;
00125 }
00126 Py_INCREF(Py_None);
00127 return Py_None;
00128 }
00129
00130
00131
00132 static PyObject *rename(PyObject *self, PyObject *args) {
00133 char *oldname, *newname;
00134 if (!PyArg_ParseTuple(args, (char *)"ss:material.rename",&oldname, &newname))
00135 return NULL;
00136
00137 VMDApp *app = get_vmdapp();
00138 MaterialList *mlist = app->materialList;
00139 int ind = mlist->material_index(oldname);
00140 if (ind < 0) {
00141 PyErr_SetString(PyExc_ValueError, "Material to change does not exist");
00142 return NULL;
00143 }
00144 if (mlist->material_index(newname) >= 0) {
00145 PyErr_SetString(PyExc_ValueError, "New name already exists");
00146 return NULL;
00147 }
00148 if (!app->material_rename(oldname, newname)) {
00149 PyErr_SetString(PyExc_ValueError, "Could not change material name");
00150 return NULL;
00151 }
00152 Py_INCREF(Py_None);
00153 return Py_None;
00154 }
00155
00156
00157 static PyObject *change(PyObject *self, PyObject *args, PyObject *keywds) {
00158 char *name;
00159 float ambient, specular, diffuse, shininess, opacity;
00160
00161 static char *kwlist[] = {
00162 (char *)"name", (char *)"ambient", (char *)"specular", (char *)"diffuse",
00163 (char *)"shininess", (char *)"opacity", NULL
00164 };
00165
00166 if (!PyArg_ParseTupleAndKeywords(args, keywds, (char *)"s|fffff", kwlist,
00167 &name, &ambient, &specular, &diffuse, &shininess, &opacity))
00168 return NULL;
00169
00170 VMDApp *app = get_vmdapp();
00171 MaterialList *mlist = app->materialList;
00172 int ind = mlist->material_index(name);
00173 if (ind < 0) {
00174 PyErr_SetString(PyExc_ValueError, "Material to change does not exist");
00175 return NULL;
00176 }
00177
00178 if (PyDict_GetItemString(keywds, (char *)"ambient"))
00179 app->material_change(name, MAT_AMBIENT, ambient);
00180 if (PyDict_GetItemString(keywds, (char *)"specular"))
00181 app->material_change(name, MAT_SPECULAR, specular);
00182 if (PyDict_GetItemString(keywds, (char *)"diffuse"))
00183 app->material_change(name, MAT_DIFFUSE, diffuse);
00184 if (PyDict_GetItemString(keywds, (char *)"shininess"))
00185 app->material_change(name, MAT_SHININESS, shininess);
00186 if (PyDict_GetItemString(keywds, (char *)"opacity"))
00187 app->material_change(name, MAT_OPACITY, opacity);
00188
00189 Py_INCREF(Py_None);
00190 return Py_None;
00191 }
00192
00193
00194 static PyMethodDef methods[] = {
00195 {(char *)"listall", (vmdPyMethod)listall, METH_VARARGS },
00196 {(char *)"settings", (vmdPyMethod)settings, METH_VARARGS },
00197 {(char *)"add", (vmdPyMethod)add, METH_VARARGS },
00198 {(char *)"delete", (vmdPyMethod)matdelete, METH_VARARGS },
00199 {(char *)"rename", (vmdPyMethod)rename, METH_VARARGS },
00200 {(char *)"change", (PyCFunction)change, METH_VARARGS | METH_KEYWORDS },
00201 {(char *)"default", (vmdPyMethod)set_default, METH_VARARGS },
00202 {NULL, NULL}
00203 };
00204
00205 void initmaterial() {
00206 (void) Py_InitModule((char *)"material", methods);
00207 }
00208
00209
00210
00211