如何在Maya API中从MObject获取Node类实例 [英] How to get Node class instance from MObject in Maya API

查看:150
本文介绍了如何在Maya API中从MObject获取Node类实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在cpp插件中我正在开发Maya API,在initializePlugin函数中注册一个自定义的MPxTransform节点:

  status = pluginFn.registerTransform(mympxtransform,
myMPxTransformClass :: id,
& myMPxTransformClass :: creator,
& myMPxTransformClass :: initialize,
& myMPxTransformMatrixClass :: creator ,
myMPxTransformMatrixClass :: id);

然后以编程方式创建节点:

  MDagModifier mdagmod; 
MObject MyMObject;
MyMObject = mdagmod.createNode(mympxtransform,MObject :: kNullObj,& status);

我可以看到在大纲中正确创建的节点。

现在,如何从获取的MyMObject访问我的自定义myMPxTransformClass?

解决方案

解决方案:



你只需要做:

  myMPxTransformClass * newDerived =(myMPxTransformClass *)& transformedObj; // 1)

//用于调试
MGlobal :: displayInfo(newDerived-> className());

1)我们这里做的基本上是创建一个指针 '类型并为其分配类型转换指针到创建的 MObject 。编译器不允许类型转换 MObject 本身,但是指针可以被类型转换为类的指针( myMPxTransformClass * )。



我们可以使用指针访问类的方法等。 >

ps在 dynamic_cast 的情况下,尝试直接强制转换 MObject 将不起作用,因为 MObject






副建议:

在旁注中,我想添加这个。这主要是我的意见。由于我不知道您要如何处理您的代码,因此请使用一粒盐的建议。



根据Maya的原则,


您不应访问Maya中节点/ MObject 的成员函数。


创建之后,它完全由Maya控制。你可以控制它的唯一方法是使用Maya的提供的函数集( MFn Classes)用于该节点类型(在你的情况下 MFnTransform 设置,因为您的节点是一个 kPluginTransformNode ,它是一个变换节点)。



case你有类数据成员,你可能想要操作和以编程方式操作,你必须使他们实际Maya属性的节点(从而使他们暴露给Maya)。然后,你将能够得到他们 MPlugs ,并做你的事情。



查看你的代码,我感觉有两个组件,你主要瞄准产生,一个节点(即你的自定义 MPxTransform );和一个类型的函数类型(即 UnitClass )与你的节点进行某些操作。我建议将插件分成两个单独的组件:节点和命令。这样,责任明确分开。因为,因为它现在,只是加载你的插件创建节点,并对它们进行操作。用户可能会对可能发生的事情感到困惑。如果你把它们分成一个节点和一个命令,它们可以以用户认为合适的许多不同的方式使用。该命令可以是用户使用插件的入口点。



希望这有助于!


In a cpp plugin I am developing in Maya API, I register a custom MPxTransform Node in the initializePlugin function:

status=pluginFn.registerTransform("mympxtransform",
                                      myMPxTransformClass::id,
                                      &myMPxTransformClass::creator,
                                      &myMPxTransformClass::initialize,
                                      &myMPxTransformMatrixClass::creator,
                                      myMPxTransformMatrixClass::id);

And then create the node programmatically:

MDagModifier mdagmod;
MObject MyMObject;
MyMObject=mdagmod.createNode("mympxtransform",MObject::kNullObj,&status);

I can see the node properly created in the outliner.
But now, how can I access my custom myMPxTransformClass from the obtained MyMObject ?

解决方案

Solution To This Problem:

You would just have to do:

myMPxTransformClass* newDerived = (myMPxTransformClass*)&transformedObj;  // 1)

// For debugging
MGlobal::displayInfo(newDerived->className());

1) What we do here is basically create a pointer of your class' type and assign to it the type casted pointer to the created MObject. The compiler wouldn't allow type-casting MObject itself, but the pointer to it can be type-casted into your class' pointer (myMPxTransformClass*).

We can then just use the pointer to access the class' methods and so on.

p.s. In the case of the dynamic_cast, attempting to cast MObject directly won't work because MObject is not a polymorphic type (intentionally).


Side Recommendation:

On a side note, I wanted to add this. This is mostly my opinion. Since I don't know what you are trying to do with your code, please take this recommendation with a grain of salt.

According to core Maya's principle,

Thou shall never access member functions of a node/MObject in Maya.

Once it has been created, it is solely under Maya's control. The only way you can (sort of) control it is using Maya's provided function sets (MFn Classes) for that node type (in your case MFnTransform set because your node is a kPluginTransformNode which is a transform node).

In the case you have class data members that you might want to operate with and manipulate programmatically, you will have to make them actual Maya attributes for that node (and thereby expose them to Maya). Then, you will be able to get them as MPlugs and do your thing.

Looking at your code, I feel that there are two components you are majorly aiming to produce, a node(s) (i.e. your custom MPxTransform); and a functor kind of class (i.e. UnitClass) that does something with/to your node(s). I would recommend splitting your plugin then into two separate components: a node and a command. That way there is clear separation of responsibilities. Because, as it stands right now, just loading your plugin creates the node(s) and also operates on them. The user might be confused as to what might be happening. If you separated them into a node and a command that does the thing, they can be used in many different ways as the user sees fit. The command could be the entry point for the user to use your plugin.

Hope this helps!

这篇关于如何在Maya API中从MObject获取Node类实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆