
Manipulating Objects, Part 3
We’ve covered getting geometry data from an existing object and building a new object from raw data. Now we’ll start looking at non-geometry related elements of a polygon object. Continue using the cube example from the previous parts in this series lets see how to get its UV information.
Start by creating a cube called Box.
1 2 3 4 |
import maya.cmds as cmds # Create the box. cmds.polyCube( name='box' ) |
Now get Box’s shape MObject as shown in Part 1. This will be used to confirm that we’re dealing with a poly mesh object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import maya.OpenMaya as OpenMaya # Utility junk. def getMDagPath( name ): ''' Get the MDagPath of the object. @param name: String. Name of object. @return: MDagPath. ''' sel_list = OpenMaya.MSelectionList() sel_list.add( name ) dag = OpenMaya.MDagPath() component = OpenMaya.MObject() sel_list.getDagPath( 0, dag, component ) return dag # Create the box. cmds.polyCube( name='box' ) # Get the polyMesh of Box. box_dag = getMDagPath( 'box' ) # Get Box's mesh. box_dag.extendToShapeDirectlyBelow( 0 ) shape_obj = box_dag.node() |
The function set MFnMesh is used to retrieve the UV data from the mesh.
1 2 3 |
# Test to see what kind of shape we are dealing with. if shape_obj.apiType() == OpenMaya.MFn.kMesh: mfn_mesh = OpenMaya.MFnMesh( box_dag ) |
We’ll be using a few member functions from MFnMesh to get the UV information.
- numUVSets(), use this to get the number of UV sets applied to the object.
- numUVs(), use this to get the number of UVs the object contains.
- getUVs(), use this to get each UVs U and V values.
The first two are straight forward and don’t warrant a more detailed explanation. However, getUVs() does requires two parameters be passed into the function. These are both MFloatArrays with no elements which getUVs() fills with the U and V data. Elements of a MFloatArray can be accessed the same as a Python list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# Number of UV sets. print 'Number of UV Sets: {0}'.format(mfn_mesh.numUVSets()) # Number of UVs. print 'Number of UVs: {0}'.format(mfn_mesh.numUVs()) # Get the UVs. UArray = OpenMaya.MFloatArray() VArray = OpenMaya.MFloatArray() mfn_mesh.getUVs( UArray, VArray ) for n in xrange(len(UArray)): print 'UV {0}: {1}, {2}'.format(n+1, UArray[n], VArray[n]) |
Running this code on our box produces the following print out. I’ve also included a couple images showing the locations of each UV.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Number of UV Sets: 1 Number of UVs: 14 UV 1: 0.330000013113, 0.0 UV 2: 0.66333335638, 0.0 UV 3: 0.330000013113, 0.25 UV 4: 0.66333335638, 0.25 UV 5: 0.330000013113, 0.5 UV 6: 0.66333335638, 0.5 UV 7: 0.330000013113, 0.75 UV 8: 0.66333335638, 0.75 UV 9: 0.330000013113, 1.0 UV 10: 0.66333335638, 1.0 UV 11: 1.0, 0.0 UV 12: 1.0, 0.25 UV 13: 0.0, 0.0 UV 14: 0.0, 0.25 |
Here is the complete code for this post.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
import maya.cmds as cmds import maya.OpenMaya as OpenMaya # Utility junk. def getMDagPath( name ): ''' Get the MDagPath of the object. @param name: String. Name of object. @return: MDagPath. ''' sel_list = OpenMaya.MSelectionList() sel_list.add( name ) dag = OpenMaya.MDagPath() component = OpenMaya.MObject() sel_list.getDagPath( 0, dag, component ) return dag # Create the box. cmds.polyCube( name='box' ) # Get the polyMesh of Box. box_dag = getMDagPath( 'box' ) # Get Box's mesh. box_dag.extendToShapeDirectlyBelow( 0 ) shape_obj = box_dag.node() # Test to see what kind of shape we are dealing with. if shape_obj.apiType() == OpenMaya.MFn.kMesh: mfn_mesh = OpenMaya.MFnMesh( box_dag ) # Number of UV sets. print 'Number of UV Sets: {0}'.format(mfn_mesh.numUVSets()) # Number of UVs. print 'Number of UVs: {0}'.format(mfn_mesh.numUVs()) # Get the UVs. UArray = OpenMaya.MFloatArray() VArray = OpenMaya.MFloatArray() mfn_mesh.getUVs( UArray, VArray ) for n in xrange(len(UArray)): print 'UV {0}: {1}, {2}'.format(n+1, UArray[n], VArray[n]) |