set attribute value in matrix type

set attribute value in matrix type

egoldid1
Advocate Advocate
1,314 Views
8 Replies
Message 1 of 9

set attribute value in matrix type

egoldid1
Advocate
Advocate
Hello, How I can create a matrix for control position of an object and save it in  attributes?
I write this codes but it crashed. i have rotate and translate:
 
MPlug outMatrixAttrPlug(oThis, outMatrixAttr);
MDataHandle outMatrixHandle = outMatrixAttrPlug.asMDataHandle();
 
MTransformationMatrix m;
double scale[3] = { 1,1,1 };
m.setScale(scale, MSpace::kTransform);
MEulerRotation rr(MVector(0,45,0), MEulerRotation::RotationOrder::kXYZ);
m.rotateTo(rr);
MVector translate(0, 11, 0);
m.setTranslation(translate, MSpace::kTransform);
//
outMatrixHandle.setMMatrix(m.asMatrix());

 

1,315 Views
8 Replies
Replies (8)
Message 2 of 9

jmreinhart
Advisor
Advisor

What's the context for this code? Is this code from the ::compute method of a custom node or something like that? If so you should be using the MDataBlock class to get your dataHandles.

0 Likes
Message 3 of 9

egoldid1
Advocate
Advocate

hi, yes I wrote it in ::compute method of a custom node.

at first I wrote below code:
outMatrixAttr= matAttr.create("outCCMatrix", "outCCMatrix", MFnMatrixAttribute::kFloat);

 

when I change MFnMatrixAttribute::kFloat in to MFnMatrixAttribute::kDouble, it now does not crash. 

outMatrixAttr = matAttr.create("outCCMatrix", "outCCMatrix", MFnMatrixAttribute::kDouble);

 

but now in the outMatrixAttr attribute only saving zero values.

 

Message 4 of 9

jmreinhart
Advisor
Advisor

Could you please share the full code?

Message 5 of 9

egoldid1
Advocate
Advocate

the full code is so long.

-------------

I wrote below code for create attribute:

#...

MFnMatrixAttribute matAttr;
outMatrixAttr = matAttr.create("outCCMatrix", "outCCMatrix", MFnMatrixAttribute::kDouble);
matAttr.setStorable(true);
matAttr.setReadable(true);
matAttr.setKeyable(true);
matAttr.setWritable(false);
matAttr.setHidden(false);
stat = addAttribute(outMatrixAttr );
if (!stat) { stat.perror("addAttribute"); return stat; }
 
-----------
and in the compute part I wrote below code:

MPlug outMatrixAttrPlug(oThis, outMatrixAttr);
MDataHandle outMatrixHandle = outMatrixAttrPlug.asMDataHandle();
#
MTransformationMatrix m;
double scale[3] = { 1.0,21.0,1.0 };
m.setScale(scale, MSpace::kTransform);
MEulerRotation rr(MVector(0.0, 20.0, 0.0), MEulerRotation::RotationOrder::kXYZ);
m.rotateTo(rr);
MVector translate(110.0, 11.0, 22.0);
m.setTranslation(translate, MSpace::kTransform);
outMatrixHandle .setMMatrix(m.asMatrix());
 
and result is zero value:
Screenshot 2023-11-15 171946.png
 
I am not sure that I can share full code. so much thanks.
0 Likes
Message 6 of 9

jmreinhart
Advisor
Advisor

I think that you might be misunderstanding how the compute function works. This is a good example to look at https://help.autodesk.com/view/MAYAUL/2022/ENU/?guid=Maya_SDK_Dependency_graph_plug_ins_Implementing... 

 

The important bit is this. This is the usual way of getting the dataHandle for a plug during the compute method.

 

 

MDataHandle stateData = data.outputValue( state, &status );

 

 

I've never used 

 

 

MPlug().asMDataHandle();

 

 

but the documentation says: 

 

Value of plug as an MDataHandle. When done using the returned handle, the caller must destroy it using MPlug::destructHandle().

 

Hopefully one of those two things will resolve your problem.

Message 7 of 9

egoldid1
Advocate
Advocate

thank you. I sued this code and it worked.

MDataHandle outMatrixHandle = data.outputValue(outMatrixAttr);
outMatrixHandle.setMMatrix(m.asMatrix());

0 Likes
Message 8 of 9

egoldid1
Advocate
Advocate

now if the outMatrixAttr be child of the outMatrixGRPAttr , how I can write values or how I can use data.outputValue()?

 

outMatrixGRPAttr = compoundAttr.create("outMatrixGRP", "outMatrixGRP");
compoundAttr.setArray(true);
compoundAttr.setReadable(true);
compoundAttr.setWritable(false);
compoundAttr.setKeyable(true);
compoundAttr.setStorable(true);
compoundAttr.setHidden(false);

 

outMatrixAttr= matAttr.create("outCCMatrix", "outCCMatrix", MFnMatrixAttribute::kDouble);
matAttr.setStorable(true);
matAttr.setReadable(true);
matAttr.setKeyable(true);
matAttr.setWritable(false);
matAttr.setHidden(false);
compoundAttr.addChild(outMatrixAttr);

 

stat = addAttribute(outMatrixGRPAttr);
if (!stat) { stat.perror("addAttribute"); return stat; }

 

0 Likes
Message 9 of 9

jmreinhart
Advisor
Advisor

It should work the same way. I would recommend clicking on the link and reading about the compute function. Your question is really general, and you'll make more progress referring to the documentation and making an attempt on your own, and posting to the forum if that doesn't work.

 

Also, small tip if you use the "</>" when making a post that contains code it makes it more readable. Best of luck