anti
anti

Reputation: 3125

Maya c++ API, moving an object?

I am attempting to write a Maya c++ plugin, and am having some trouble.

I simply want to create a camera, and then move it in the viewport. I have:

    MObject camera;
    MDagPath cameraPath;
    // create new camera
    MFnCamera fnCamera;
    fnCamera.create(camera);
    fnCamera.getPath(cameraPath);

    MFnTransform fn(camera);
    MVector trans(12, 12, 12);
    fn.setTranslation(trans, MSpace::kWorld);

This creates the camera fine, but does not move it. What am I missing to translate the created object? Thank you.

Upvotes: 1

Views: 924

Answers (2)

dadada
dadada

Reputation: 1

I find if you just change the space form MSpace::world to MSpace::object, the previous method you provided is ok. Maybe it is because the translate transform is a part of the final world matrix, and the transform space of this function may means the space which will add the transform matrix.

Change:

fn.setTranslation(trans, MSpace::kWorld);

to:

fn.setTranslation(trans, MSpace::kObject);

Sorry, I think I am wrong. Using MSpace::kWorld will return a failure because the MFnTransform object is not create by a dagPath object, and in this case use MSpace::kObject will not return failure.

Upvotes: 0

anti
anti

Reputation: 3125

Ah, i was doing it entirely wrong. This works:

MDagModifier dagModifier;

    //Create the camera transform node.
    MObject cameraTransformObj = dagModifier.createNode("transform");
    dagModifier.renameNode(cameraTransformObj, "myCameraTransform");

    //Create the camera shape node as a child of the camera transform node.
    MObject cameraShapeObj = dagModifier.createNode("camera", cameraTransformObj);
    dagModifier.renameNode(cameraShapeObj, "myCameraShape");

    dagModifier.doIt();

MFnTransform transformFn(cameraTransformObj);
transformFn.setTranslation(MVector(0, 5, 30), MSpace::kTransform);

Upvotes: 1

Related Questions