[Maya API] The global operation method of lesson21_Maya API-MGlobal

MGlobal global operation function

MGlobal

Function: a static class that provides general API functions

Features: no need to instantiate, you can use static methods directly

We can use its methods to do some functions that are not particularly targeted, but are just public. His methods are static methods, and we can call them directly.

1. First we import MGlobal in maya.OpenMaya

from maya.OpenMaya import MGlobal

2. You can use apiVersion to get his api version

from maya.OpenMaya import MGlobal

MGlobal.apiVersion()

3. We want to get the selected objects in the scene, similar to our ls command

This command will store the content of our current selection in this sel. If an object is selected as shown in the figure, there is one element in the list of MSelection.

from maya.OpenMaya import MGlobal

sel = OpenMaya.MSelectionList()
OpenMaya.MGlobal.getActiveSelectionList(sel)

sel.length()

4. We can setActiveSelectionList(), direct selection of objects is equivalent to our selection command, which is similar to select followed by a group of lists, which contains the names of a group of objects.

from maya.OpenMaya import MGlobal

sel = OpenMaya.MSelectionList()
OpenMaya.MGlobal.getActiveSelectionList(sel)

OpenMaya.MGlobal.setActiveSelectionList(sel)

5. We can also use it to execute a python command, such as OpenMaya.MGlobal.excuteCommand("createNode'joint'")

There is no output result by default, so we need to use a variable to accept the output result

If sometimes our plug-in needs to call her original commands, we can call these things in AP. In the API, we can use excuteCommand followed by MEL or Python commands, then call the python command. For excutePythonCommand, we can also use it to judge the state in the scene.

For example, to determine whether the Y axis is up or the Z axis is up, return a bool value

Use it to display part of the information, display output warning or error information, you can use displayInfo

Display normal prompt information: OpenMaya.MGlobal.displayInfo('info msg.....')

Display warning message: OpenMaya.MGlobal.displayWarning('info msg.....')

Display error message: OpenMaya.MGlobal.displayError('info msg.....')

Change the position of the frame: OpenMaya.MGlobal.viewFrame(30)

Guess you like

Origin blog.csdn.net/weixin_41363156/article/details/104157121