First of all greetings to everyone on this list. First post, zero python
experience, so please be patient. :)
I'm writing a command to query and modify fluid voxel data and having
some difficulties understanding a python data type.
MFnFluid.density is supposed to return a pointer to a float array but
the Python API returns a 'PySwigObject'
that I don't know how to handle. How do I iterate through its values? Or
am I doing something completely wrong?
Cheers,
Szabolcs
--
fluidNode = OpenMayaFX.MFnFluid(node)
numberOfVoxels = fluidNode.gridSize()
densityValues = fluidNode.density()
print densityValues
print type(densityValues)
--
Python does not have pointer types. Elementary types are passed by
value and objects are passed by reference.
To get around this, the Maya API provides the MScriptUtil class which
can be used to generate a pointer or reference to an elementary type,
or to extract a value from a pointer to an elementary type.
In your case you can use
maya.OpenMaya.MScriptUtil.getFloatArrayItem(densityValues, i) to
retrieve the i'th value from the array.
--
-deane
Thanks for the code example, getting density values is working fine now!
I saw in your code that you had problems with getting the grid resolution.
This is working for me, not really elegant though:
--
fluidNode = OpenMayaFX.MFnFluid(node)
xRes_p = OpenMaya.MScriptUtil().asUintPtr()
yRes_p = OpenMaya.MScriptUtil().asUintPtr()
zRes_p = OpenMaya.MScriptUtil().asUintPtr()
fluidNode.getResolution(xRes_p, yRes_p, zRes_p)
xRes = OpenMaya.MScriptUtil().getUint(xRes_p)
yRes = OpenMaya.MScriptUtil().getUint(yRes_p)
zRes = OpenMaya.MScriptUtil().getUint(zRes_p)
print ("%d %d %d" % (xRes, yRes, zRes))
--
Cheers,
Szabolcs
> To get around this, the Maya API provides the MScriptUtil class which
> can be used to generate a pointer or reference to an elementary type,
> or to extract a value from a pointer to an elementary type.
>
I see. So this PySwigObject is the object that holds the data internally
and getFloatArrayItem is simply a "convenient" wrapper function to query
the items?
I can't find any documentation about this class in the API docs. Where
can I find more information about MScriptUtil?
> In your case you can use
> maya.OpenMaya.MScriptUtil.getFloatArrayItem(densityValues, i) to
> retrieve the i'th value from the array
Its working fine now, thanks. And how can I modify the content of this
array if I'd like to modify the density values?
And by the way, after getting density working I advanced to level 2 with
MFnFluid.getVelocity.
getVelocity expects three float array pointers. I thought that something
like this might work:
xVel = OpenMaya.MScriptUtil().asFloatPtr()
yVel = OpenMaya.MScriptUtil().asFloatPtr()
zVel = OpenMaya.MScriptUtil().asFloatPtr()
fluidNode.getVelocity(xVel, yVel, zVel)
But of course I was totally wrong. I got this error message:
# TypeError: argument number 2: a 'floatArrayPtr' is expected,
'PySwigObject(_p_float)' is received #
Cheers,
Szabolcs
That's right.
> I can't find any documentation about this class in the API docs. Where
> can I find more information about MScriptUtil?
Check your Maya 2008 API docs. It should be in there. If not, you
might need to download Maya 2008 Service Pack 1.
> Its working fine now, thanks. And how can I modify the content of this
> array if I'd like to modify the density values?
maya.OpenMaya.MScriptUtil.setFloatArray(densityValues, i, newValue)
where 'newValue' is a double for some reason.
> And by the way, after getting density working I advanced to level 2 with
> MFnFluid.getVelocity.
At the moment there is no way that I know of to call
MFnFluid.getVelocity from Python. The problem is that it expects
references to pointers which not even MScriptUtil currently supports.
--
-deane
> At the moment there is no way that I know of to call
> MFnFluid.getVelocity from Python. The problem is that it expects
> references to pointers which not even MScriptUtil currently supports.
The answer I was afraid of... So much for doing rapid-development using
the python api.
Thanks Dean, at least I know not to try.
Cheers,
Szabolcs