Message 1 of 4
Modify all UVs using Python API 2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I'm having a problem updating all the UVs of an object using the python api. After running the below sample script, if I select the target object, the UVs will update to their new positions in the UV editor. BUT, once I attempt to move a singe UV in the editor, all the UVs will pop back to their original position. Even if run the script, verify that that UVs are updated in the UV editor, save the file and load it back in, all the UVs will be back at their original locations as if they were never modified. So where am I going wrong?
Note: For the script to work, make a cube in the scene with the default name of pCube1.
import maya.api.OpenMaya as om
def scale_UVs_on_u(obj_name, scale=2):
uvset = 'map1'
# get mf mesh obj
dgpa = om.MDagPath.getAPathTo(om.MSelectionList().add(obj_name).getDependNode(0))
MFnMesh = om.MFnMesh(dgpa)
# get original uv data
uvs = MFnMesh.getUVs(uvset)
uv_counts, uvIDs = MFnMesh.getAssignedUVs(uvset)
# modify uv values
for i in range(len(uvs[0])):
uvs[0][i] = (uvs[0][i] - 0.5) * scale + 0.5
# apply modified uv values back to the mesh obj
MFnMesh.setUVs(uvs[0], uvs[1], uvset)
MFnMesh.assignUVs(uv_counts, uvIDs, uvset)
# updateSurface() doesn't solve the problem either
# MFnMesh.updateSurface()
scale_UVs_on_u('pCube1')
Cheers,
Paul
Maya 2017 update 5/Maya 2018.2
Windows 10