API: How to set a matrix attribute value in C++ outside compute()?


#1

Hi,

I have a custom node that has a (dynamic) matrix attribute created with MFnMatrixAttribute::create(). Now I want to set a value on that attribute, but looking at the documentation for MPlug, there is no setValue() method that takes a MMatrix or MFloatMatrix as input (whereas MDataHandle has such a method, but I’m outside the compute() method, so I can’t use that one, right?). So how can I set the value?

What I’m actually trying to do is set the default value for the matrix. I noticed that when I set it using MFnMatrixAttribute::setDefault(), the new value is actually used but Maya doesn’t store it when saving the scene. This means when I store a node that still has (my own) default value, Maya won’t store the value (as it’s the default one). But as it doesn’t store the default value, the matrix will be the identity matrix when I load the scene again and not my actual default value. This is why I wanted to set my default value as a real value and not as an attribute default value.

  • Matthias -

#2

Will this help ?

http://www.comet-cartoons.com/3ddocs/mayaAPI/

If you scroll down a little bit there is an example for dealing with Matrix Attributes


#3

if you go further down “isStorable” is inherited, isn’t it? sorry, but I’ve only done 3d textures where the texture comes via input from a place3dTexture…so I’ve never stored that, but try…
CHECK_MSTATUS(yourMatrixAttr.setStorable(true) );


#4

duplicate post…sorry


#5

Ah, thanks for that! I wasn’t aware of the MFnMatrixData class! It looks like this should do the trick, but alas, it doesn’t work…? One minor nuisance is that it doesn’t have a set() method for a MFloatMatrix, but neither converting it to MMatrix nor using MMatrix in the first place worked for me…? I don’t get an error on any of the calls, but the value just doesn’t update.
As a last resort, I even tried constructing a string containing a MEL command that sets the attribute and actually this works, but it’s quite awkward in my opinion…

tbaypaul: The attribute is stored in the files, I can see it in the *.ma file and when I do assign a value (using MEL) that is different from the default, then everything is fine. It’s just that Maya doesn’t store the default value of the matrix, so loading a scene uses the “default default” value (so to speak) which is the identity matrix, and so you don’t get back what you have saved.

Thanks for your help!

  • Matthias -

#6

Call MFnMatrixData::create() instead of set. Pass the returned MObject to the setValue method of the MPlug for the matrix. You can do the following to convert matrices:


 void convMatrices(MMatrix& o,const MFloatMatrix& fm)
 {
   float temp[4][4];
   fm.get(temp);
   o.set(temp);
 }
  void convMatrices(MFloatMatrix& o,const MMatrix& m)
  {
    float temp[4][4];
    m.get(temp);
    o.set(temp);
  }

#7

I have it working now (without MEL)! I guess it just was too late yesterday… :wink:
What I did first was this:

[ol]
[li]Obtain the plug[/li][li]Get the matrix value from the plug as an MObject[/li][li]Set a new value on the MObject using MFnMatrixData[/li][/ol]Somehow I was thinking that the MObject is a reference to the actual value and changing its value changes the attribute value. Obviously, this is not the case. So I just had to do another plug.setValue() with the modified object and then it was fine.
And as Robert pointed out, this sequence of steps can be shortened by just creating a new MObject containing a matrix instead of reading the value and overwriting it anyway.

So the bottomline simply is: The matrix must be available as a MObject and not as a MMatrix/MFloatMatrix to be able to set it on the plug. And the creation/conversion of such an MObject can be done using the MFnMatrixData class.

Thanks again for that!

  • Matthias -

#8

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.