Metaball wasnt checking the length of any of the input arg lists which resulted in errors in unrelated parts of the script.

Updated the doc too and added a longer example (converting an envalope armature into a meta object)
This commit is contained in:
2006-08-26 07:26:04 +00:00
parent f479235f8e
commit ee302f8693
2 changed files with 105 additions and 12 deletions

View File

@@ -413,21 +413,24 @@ static PyObject *Metaball_addMetaelem( BPy_Metaball * self, PyObject * args )
float x, y, z, rad, s, expx, expy, expz;
if( !PyArg_ParseTuple( args, "O", &listargs ) )
return ( EXPP_ReturnPyObjError
( PyExc_TypeError, "expected a list" ) );
( PyExc_TypeError, "expected a list of 9 args" ) );
if( !PyList_Check( listargs ) )
return ( EXPP_ReturnPyObjError
( PyExc_TypeError, "expected a list" ) );
( PyExc_TypeError, "expected a list of 9 args" ) );
if (PyList_Size(listargs) != 9)
return ( EXPP_ReturnPyObjError
( PyExc_TypeError, "expected a list of 9 args" ) );
type = PyInt_AsLong( PyList_GetItem( listargs, 0 ) );
x = (float)PyFloat_AsDouble( PyList_GetItem( listargs, 1 ) );
y = (float)PyFloat_AsDouble( PyList_GetItem( listargs, 2 ) );
z = (float)PyFloat_AsDouble( PyList_GetItem( listargs, 3 ) );
rad = (float)PyFloat_AsDouble( PyList_GetItem( listargs, 4 ) );
s = (float)PyFloat_AsDouble( PyList_GetItem( listargs, 6 ) );
expx = (float)PyFloat_AsDouble( PyList_GetItem( listargs, 7 ) );
expy = (float)PyFloat_AsDouble( PyList_GetItem( listargs, 8 ) );
expz = (float)PyFloat_AsDouble( PyList_GetItem( listargs, 9 ) );
s = (float)PyFloat_AsDouble( PyList_GetItem( listargs, 5 ) );
expx = (float)PyFloat_AsDouble( PyList_GetItem( listargs, 6 ) );
expy = (float)PyFloat_AsDouble( PyList_GetItem( listargs, 7 ) );
expz = (float)PyFloat_AsDouble( PyList_GetItem( listargs, 8 ) );
ml = MEM_callocN( sizeof( MetaElem ), "metaelem" );
BLI_addhead( &( self->metaball->elems ), ml );
@@ -533,10 +536,14 @@ static PyObject *Metaball_setloc( BPy_Metaball * self, PyObject * args )
int i;
if( !PyArg_ParseTuple( args, "O", &listargs ) )
return ( EXPP_ReturnPyObjError
( PyExc_TypeError, "expected a list" ) );
( PyExc_TypeError, "expected a list of 3 floats" ) );
if( !PyList_Check( listargs ) )
return ( EXPP_ReturnPyObjError
( PyExc_TypeError, "expected a list" ) );
( PyExc_TypeError, "expected a list of 3 floats" ) );
if (PyList_Size(listargs) != 3)
return ( EXPP_ReturnPyObjError
( PyExc_TypeError, "expected a list of 3 floats" ) );
for( i = 0; i < 3; i++ ) {
PyObject *xx = PyList_GetItem( listargs, i );
self->metaball->loc[i] = (float)PyFloat_AsDouble( xx );
@@ -564,6 +571,10 @@ static PyObject *Metaball_setrot( BPy_Metaball * self, PyObject * args )
if( !PyList_Check( listargs ) )
return ( EXPP_ReturnPyObjError
( PyExc_TypeError, "expected a list" ) );
if (PyList_Size(listargs) != 3)
return ( EXPP_ReturnPyObjError
( PyExc_TypeError, "expected a list of 3 floats" ) );
for( i = 0; i < 3; i++ ) {
PyObject *xx = PyList_GetItem( listargs, i );
self->metaball->rot[i] = (float)PyFloat_AsDouble( xx );
@@ -614,6 +625,10 @@ static PyObject *Metaball_setsize( BPy_Metaball * self, PyObject * args )
if( !PyList_Check( listargs ) )
return ( EXPP_ReturnPyObjError
( PyExc_TypeError, "expected a list" ) );
if (PyList_Size(listargs) != 3)
return ( EXPP_ReturnPyObjError
( PyExc_TypeError, "expected a list of 3 floats" ) );
for( i = 0; i < 3; i++ ) {
PyObject *xx = PyList_GetItem( listargs, i );
self->metaball->size[i] = (float)PyFloat_AsDouble( xx );
@@ -1192,6 +1207,10 @@ static PyObject *Metaelem_setdims( BPy_Metaelem * self, PyObject * args )
if( !PyList_Check( listargs ) )
return ( EXPP_ReturnPyObjError
( PyExc_TypeError, "expected a list" ) );
if (PyList_Size(listargs) != 3)
return ( EXPP_ReturnPyObjError
( PyExc_TypeError, "expected a list of 3 floats" ) );
self->metaelem->expx =
(float)PyFloat_AsDouble( PyList_GetItem( listargs, 0 ) );
self->metaelem->expy =
@@ -1224,6 +1243,10 @@ static PyObject *Metaelem_setcoords( BPy_Metaelem * self, PyObject * args )
if( !PyList_Check( listargs ) )
return ( EXPP_ReturnPyObjError
( PyExc_TypeError, "expected a list" ) );
if (PyList_Size(listargs) != 3)
return ( EXPP_ReturnPyObjError
( PyExc_TypeError, "expected a list of 3 floats" ) );
self->metaelem->x = (float)PyFloat_AsDouble( PyList_GetItem( listargs, 0 ) );
self->metaelem->y = (float)PyFloat_AsDouble( PyList_GetItem( listargs, 1 ) );
self->metaelem->z = (float)PyFloat_AsDouble( PyList_GetItem( listargs, 2 ) );

View File

@@ -15,6 +15,77 @@ Example::
ob.link(mb)
sc = Blender.Scene.getCurrent()
sc.link(ob)
from Blender import *
Example::
# Converts the active armature into metaballs
def main():
scn= Scene.GetCurrent()
ob_arm= scn.getActiveObject()
if not ob_arm or ob_arm.type!='Armature':
Draw.PupMenu('No Armature Selected')
return
arm= ob_arm.data
res= Draw.PupFloatInput('res:', 0.2, 0.05, 2.0)
if not res:
return
# Make a metaball
ob_mb= Object.New('Mball')
mb= Metaball.New()
mb.setWiresize(res)
# Link to the Scene
ob_mb.link(mb)
scn.link(ob_mb)
ob_mb.sel=1
ob_arm.sel= 0
ob_mb.setMatrix(ob_arm.matrixWorld)
meta_type= 0 # all elemts are ball type
meta_shiftness= 2.0 # Volume
for bone in arm.bones.values():
print bone
# Find out how many metaballs to add based on bone length, 4 min
length= bone.length
if length < res:
mballs= 4
else:
mballs= int(length/res)
if mballs < 4:
mballs = 4
print 'metaball count', mballs
# get the bone properties
head_rad= bone.headRadius
tail_rad= bone.tailRadius
head_loc= bone.head['ARMATURESPACE']
tail_loc= bone.tail['ARMATURESPACE']
for i in range(mballs):
f= float(i)
w1= f/mballs # weighting of this position on the bone for rad and loc
w2= 1-w1
loc= head_loc*w1 + tail_loc*w2
rad= (head_rad*w1 + tail_rad*w2) * 1.3
# Add the metaball
mb.addMetaelem([meta_type, loc.x, loc.y, loc.z, rad, meta_shiftness, 0, 0, 0])
Window.RedrawAll()
main()
"""
@@ -64,9 +135,8 @@ class Metaball:
- 7 for a cube
- params 2,3,4: floats - the x, y and z coordinates of the metaelem.
- param 5: float - the rad value of the metaelem.
- param 6: int - the lay value.
- param 7: float - the s value of the metaelem.
- params 8,9,10: floats - the expx, expy and expz values of the metaelem.
- param 6: float - the shiftness value (default is 2.0).
- params 7,8,9: floats - the expx, expy and expz values of the metaelem.
@type paramslist: list
@param paramslist: the list of the parameters for creating a new metaelem.
@rtype: None