I am trying to create a locator at the centre of a face. The face belongs to the default maya cube, which is unmodified, and is at the world origin (0,0,0).
When I run the following, it creates the locator at the corner of the face, rather then its center:
import maya.cmds as cmds
pos = cmds.xform('pCube1.f[4]', q=True, ws=True, t=True)
cmds.spaceLocator(a=True, p=(pos[0], pos[1], pos[2]))
I figured, if divide the position coordinates into half, it will be placed at the face’s centre, but its placing it outside the boundaries of the cube. Which I am not expecting:
import maya.cmds as cmds
cmds.spaceLocator(a=True, p=(pos[0]/2, pos[1]/2, pos[2]/2))
Trying other variations just take me further away. Placing the lacator is incidental, this is just an excercise for me to understand positions and translation.
I would like to know what my solution is not working, or how else one could about this, thank you.
I don’t know if you can use xform
to query face position, if I had to guess without checking I’d say no. Because the xform
command is primarily designed to work with transform data (translation, rotation, scale) I don’t think it’s the most appropriate way to get component info.
There are two simple and one more complex way to approach this which will give you better results imo.
- convert the face to vertices (
polyListComponentConversion
), use pointPosition
to get the vertex co-ords, add them all up, divide by the amount of verts. This is their average position.
- simply switch to the
Move
tool and query the manipulator position. This has the benefit of being much faster with multiple selected items, of ANY type, and requires no maths.
- use the API to actually query the face’s “centroid” (
OpenMaya.MItMeshPolygon.center()
).
Sorry I don’t have capacity atm to provide you with any more working code to try, but hopefully this will give you something else to look into to make it work for you.
1 Like
I finally got some free time, and I used your second suggestion to solve my issue, which was simple enough, I appreciate the overview of how to best approach this, thank you.
Step 3, sounded much more interesting though, so decided to take a detour into the Maya API, apparently you can entirely bypass mel, (unlike standard python cmds and pymel), and Chris Zurbrigg makes it so much less daunting too.
A long time ago, I created this maya script that creates locators on anything. You can also bake the animation.
It can be downloaded for free from highend 3d - an ancient script sharing site in a deep dusty corner of the internet. Can be slow to access sometimes but should work.
https://www.highend3d.com/maya/script/loconloc-for-maya
1 Like
wow, how neat, i’ll get it and take a look, thanks!