Struggling to provide a correct value to 'OpenMaya.MItMeshPolygon.center()'

Am taking first steps into the Open Maya API, the Python version. I’ve gone through a Maya API series, and did some tasks such as printing to the script editor. I am now trying to write my own small bits of code. In this case, trying to solve a task I recently solved through maya commands, which is get the centre of a polygon face.

I am sure, I can accomplish this with just the OpenMaya.MItMeshPolygon.center() method, which was suggested to me by user Naughty on another thread, but am not having any success calling it. The documentation states that it is expecting a kObject as input. So with a face selected, I tried the following, which results in a error:

import maya.api.OpenMaya as om

coordValue = om.MItMeshPolygon.center(om.MSpace.kWorld)
print(coordValue)      #am expecting this to give me the center of the face I have selected
#Error: TypeError: descriptor 'center' for 'OpenMaya.MItMeshPolygon' objects doesn't apply to a 'int' object

Since this kObject, just seems to consist of a integer, I also tried passing a integer directly:

import maya.api.OpenMaya as om

print(om.MSpace.kWorld)                        #prints 4
coordValue = om.MItMeshPolygon.center(4)       #am expecting this to give me the center of the face I have selected
print(coordValue)

I have looked through the Devkit examples that Autodesk provides but have not been able to find an answer. What could I doing wrong? Am on Maya 2026.

Okay, after spending another hour on it and some more searching, and using AI to guide me… I figured it out! well partly.

Here is my code as it stands:

import maya.api.OpenMaya as om
import maya.cmds as cmds
def get_selected_center():
	selection = om.MGlobal.getActiveSelectionList()
	if selection.length() == 0:
		raise RuntimeError("Nothing selected")
	dag_path, component = selection.getComponent(0)
	if not dag_path.node().apiType() == om.MFn.kMesh:
		raise RuntimeError("Selected object is not a mesh")
	poly_iter = om.MItMeshPolygon(dag_path, component)
	
	center = poly_iter.center(om.MSpace.kWorld)
	return center

center = get_selected_center()     # this retuns a three part array, well maybe not an array, but definetly something
cmds.spaceLocator(p=(center.x, center.y, center.z))

The only issue am facing is that the above code, will only work as I expect it to, if I have a polygon face selected.
When I have a polygon face selected, the locator is created on its centre, but if I have a edge or vertex selected, the locator is still created at the centre of the face.

As shown HERE, but am looking to create the locator at the centre of a selected edge, face or at the position of a vertex.

Its probably because you use poly_iter = om.MItMeshPolygon, so you iterate over a polygon.
Instead you probably want to check the type of the component = selection.getComponent(0) first, and figure out from there if you’ll need to use MItMeshPolygon, MItMeshVertex or MItMeshEdge

1 Like

Aah, I see, very good idea, I will give that ago and see then, thank you