Query fluid voxel information

112 views
Skip to first unread message

Horvátth Szabolcs

unread,
Sep 26, 2008, 10:59:14 AM9/26/08
to python_inside_maya
Hi,

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)
--

Dean Edmonds

unread,
Sep 27, 2008, 5:25:26 PM9/27/08
to python_in...@googlegroups.com
On Fri, Sep 26, 2008 at 7:59 AM, Horvátth Szabolcs
<szab...@impresszio.hu> wrote:
>
> 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?

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

doug hammond

unread,
Sep 28, 2008, 6:50:55 AM9/28/08
to python_in...@googlegroups.com
I've written a basic Fluid parser as part of a render engine export script. The relevant file is viewable here:
http://cvs.savannah.gnu.org/viewvc/ecume/exporters/luxmaya/luxPlugin/Lux/LuxExportModules/Volume.py?revision=1.1&view=markup

Horvátth Szabolcs

unread,
Sep 29, 2008, 4:56:04 AM9/29/08
to python_in...@googlegroups.com
Hi Doug,

> I've written a basic Fluid parser as part of a render engine export
> script. The relevant file is viewable here:
> http://cvs.savannah.gnu.org/viewvc/ecume/exporters/luxmaya/luxPlugin/Lux/LuxExportModules/Volume.py?revision=1.1&view=markup
> <http://cvs.savannah.gnu.org/viewvc/ecume/exporters/luxmaya/luxPlugin/Lux/LuxExportModules/Volume.py?revision=1.1&view=markup>

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

Horvátth Szabolcs

unread,
Sep 29, 2008, 5:25:48 AM9/29/08
to python_in...@googlegroups.com
Thanks Dean, your still the API gooroo.

> 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

Dean Edmonds

unread,
Oct 6, 2008, 12:03:26 AM10/6/08
to python_in...@googlegroups.com
On Mon, Sep 29, 2008 at 5:25 AM, Horvátth Szabolcs
<szab...@impresszio.hu> wrote:
>
> 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?

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

Horvátth Szabolcs

unread,
Oct 6, 2008, 7:46:57 AM10/6/08
to python_in...@googlegroups.com
Dean Edmonds wrote:
> Check your Maya 2008 API docs. It should be in there. If not, you
> might need to download Maya 2008 Service Pack 1.
>
Thank, in the meantime I found the documentation. I was looking in the
8.5 docs and back then it was not included at all.

> 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

Reply all
Reply to author
Forward
0 new messages