How can I correctly read a matrix attribute?

238 vistas
Ir al primer mensaje no leído

Giulio Martella

no leída,
23 nov 2017, 7:08:20 p. m.23/11/17
para Python Programming for Autodesk Maya
Hi all,
I'm trying without success to read an input matrix attribute of my custom node. Here an example of the method I'm using to read a matrix attribute (this also prints the matrix contents and outputs the sum of its elements).

import maya.api.OpenMaya as om

def maya_useNewAPI():
   
pass

class Node(om.MPxNode):
    nodeName
= 'test'
    nodeClass
= 'general'
    nodeId
= om.MTypeId(0xeff)

    aMatrix
= om.MObject()
    aOut
= om.MObject()

   
def __init__(self):
        om
.MPxNode.__init__(self)

   
@staticmethod
   
def create():
       
return Node()

   
@staticmethod
   
def initialize():
        tAttr
= om.MFnTypedAttribute()
        nAttr
= om.MFnNumericAttribute()

       
Node.aMatrix = tAttr.create(
           
'matrix', 'mat', om.MFnMatrixData.kMatrix)
        tAttr
.readable = False
       
Node.addAttribute(Node.aMatrix)

       
Node.aOut = nAttr.create(
           
'out', 'o', om.MFnNumericData.kFloat, 0.0)
        nAttr
.writable = False
       
Node.addAttribute(Node.aOut)

       
Node.attributeAffects(Node.aMatrix, Node.aOut)

   
def compute(self, plug, data):

       
if plug == Node.aOut:

            mat
= data.inputValue(Node.aMatrix).asMatrix()
           
print mat

            s
= 0
           
for i in range(15):
               
print mat[i]
                s
+= mat[i]

            outHandle
= data.outputValue(Node.aOut)
            outHandle
.setFloat(s)

            data
.setClean(plug)

# Plug-in initialization omitted

To me, this seems ok, but Maya does not appear to like it. In fact if I connect the matrix attribute to a transform at the origin with zero rotation, scale and shear, the matrix values are completely wrong:
(((6.92524e-310, 1.39063e-309, 2.3342e-312, 0), (3.95253e-322, 1.66006e-321, 3.32254e-316, 3.32813e-316), (2.52962e-321, 0, 1.39063e-309, 0), (6.92524e-310, 1.39065e-309, 0, 1)))

6.92524378327e-310
1.39062872165e-309
2.33419537006e-312
0.0
3.95252516673e-322
1.66006057003e-321
3.32253771394e-316
3.32813251331e-316
2.52961610671e-321
0.0
1.39062872165e-309
0.0
6.92523883349e-310
1.39064994161e-309
0.0

What am I missing?

Thanks in advance,
Giulio

Angelo Sta. Catalina

no leída,
23 nov 2017, 7:42:49 p. m.23/11/17
para python_in...@googlegroups.com
you're getting the matrix successfully when you call "print mat"
If you compare your input matrix and the values printed from "print mat", they should be identical.

As far as the other stuff goes, I'm not exactly sure what you're trying to accomplish.
if you're trying to return the input matrix as an array of elements from your input matrix, then you need to use MDataBuilder to build the array before your set it to the outHandle. The outHandle also has to be declared an array as well.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/b333a5fd-494d-47ea-8e55-a610c2547bcc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Giulio Martella

no leída,
23 nov 2017, 8:01:03 p. m.23/11/17
para Python Programming for Autodesk Maya
Nope, the "print mat" instruction should have printed the identity matrix in that case; the thing printed is a matrix filled with values very near to zero, not the identity for sure.
What I'm trying to accomplish is far more complex than this barebone plugin I've written just to explain my problem easily. However, to be precise, I need to read a matrix and then multiply it by a point. So long, I've obtained only matrices filled with zeroes, resulting in zeroing also the point I was multiplying.

--
Giulio

Angelo Sta. Catalina

no leída,
23 nov 2017, 8:19:10 p. m.23/11/17
para python_in...@googlegroups.com
Oh. I see a problem. In your initialize method, you shouldn't use om.MFnTypedAttribute to create a matrix attribute. Use om.MFnMatrixAttribute.

mAttr = om.MFnMatrixAttribute()

Node.aMatrix = mAttr.create('matrix', 'mat')

Try doing that.

Giulio

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

Angelo Sta. Catalina

no leída,
23 nov 2017, 8:22:58 p. m.23/11/17
para python_in...@googlegroups.com
Also, to multiply a point, you can simply just cast an MPoint and multply that directly into your MMatrix variable: mat. Maya supports MPoint to MMatrix multiplication.

om.MPoint() * om.MMatrix() will work.

On Thu, Nov 23, 2017 at 7:19 PM, Angelo Sta. Catalina <angelost...@gmail.com> wrote:
Oh. I see a problem. In your initialize method, you shouldn't use om.MFnTypedAttribute to create a matrix attribute. Use om.MFnMatrixAttribute.

mAttr = om.MFnMatrixAttribute()

Node.aMatrix = mAttr.create('matrix', 'mat')

Try doing that.
On Thu, Nov 23, 2017 at 7:01 PM, Giulio Martella <giuliomar...@gmail.com> wrote:
Nope, the "print mat" instruction should have printed the identity matrix in that case; the thing printed is a matrix filled with values very near to zero, not the identity for sure.
What I'm trying to accomplish is far more complex than this barebone plugin I've written just to explain my problem easily. However, to be precise, I need to read a matrix and then multiply it by a point. So long, I've obtained only matrices filled with zeroes, resulting in zeroing also the point I was multiplying.

--
Giulio

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsubscribe@googlegroups.com.

Giulio Martella

no leída,
24 nov 2017, 11:56:36 a. m.24/11/17
para Python Programming for Autodesk Maya
Thanks for your help.
Now everything works. Damn, I feel ashamed I've wasted so much time to figure out a mistake so trivial...

About the multiplication: yeah that's cool. Fortunately OpenMaya has a fairly good interface for its numerical types.

Bye,
Giulio


Il giorno venerdì 24 novembre 2017 02:22:58 UTC+1, Angelo ha scritto:
Also, to multiply a point, you can simply just cast an MPoint and multply that directly into your MMatrix variable: mat. Maya supports MPoint to MMatrix multiplication.

om.MPoint() * om.MMatrix() will work.
On Thu, Nov 23, 2017 at 7:19 PM, Angelo Sta. Catalina <angelost...@gmail.com> wrote:
Oh. I see a problem. In your initialize method, you shouldn't use om.MFnTypedAttribute to create a matrix attribute. Use om.MFnMatrixAttribute.

mAttr = om.MFnMatrixAttribute()

Node.aMatrix = mAttr.create('matrix', 'mat')

Try doing that.
On Thu, Nov 23, 2017 at 7:01 PM, Giulio Martella <giuliomar...@gmail.com> wrote:
Nope, the "print mat" instruction should have printed the identity matrix in that case; the thing printed is a matrix filled with values very near to zero, not the identity for sure.
What I'm trying to accomplish is far more complex than this barebone plugin I've written just to explain my problem easily. However, to be precise, I need to read a matrix and then multiply it by a point. So long, I've obtained only matrices filled with zeroes, resulting in zeroing also the point I was multiplying.

--
Giulio

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
Responder a todos
Responder al autor
Reenviar
0 mensajes nuevos