Announcements

Between mid-October and November, the content on AREA will be relocated to the Autodesk Community M&E Hub and the Autodesk Community Gallery. Learn more HERE.

Modify all UVs using Python API 2

Modify all UVs using Python API 2

robotProdigy
Advocate Advocate
2,907 Views
3 Replies
Message 1 of 4

Modify all UVs using Python API 2

robotProdigy
Advocate
Advocate

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

2,908 Views
3 Replies
Replies (3)
Message 2 of 4

lanh.hong
Alumni
Alumni

Hi Paul,

 

This is supposed to happen when a mesh has history. All that you’re doing in your script is setting the UVs on the mesh, but any modifications to them will be erased when the mesh updates itself. 

 

What you need to modify it correctly is to have a node that takes in the mesh, modifies it, and give resulting mesh to the final shape. An easy way to do this it to create a polyTweakUV node and modify the UVs using the uvTweak attribute.

 

Regards,

Lanh


Lanh Hong
Developer Technical Services
Autodesk Developer Network



Message 3 of 4

robotProdigy
Advocate
Advocate

Hi Lahn,

Yes, the part about history resetting UVs back to their original position makes sense, and I can now set UVs so long as I delete history first. Thank you.

 

But I'm still not clear on how to get the polyTweakUV node working properly when there is history. When I create a cube, and modify its UVs in the UV editor, I see a polyTweakUV node appear between the polyCube and shape node just like below. This allows me to modify UVs on the cube with history intact (so long as I do it manually in the UV editor). BUT when I run my script on it,  it doesn't work in the exact same way as before (that is - with no polyTweak node). It will eventually reset UVs back to their original positions. Is the below example not what you described regarding creating a polyTweakUV node, and "modifying UVs using the uvTweak attribute" as you mentioned? If not, could you elaborate a little more. FYI, I've scoured Google a second time, and can't seem to find a solution to this.

 

Also, so that we are on the same page, when you say "have a node that takes in the mesh" are you referring to a shape node (its type is "mesh"), or magenta colored connections that generally feed into some sort of "in Mesh" attribute?

 

image.png

0 Likes
Message 4 of 4

lanh.hong
Alumni
Alumni

Hi Paul,

 

Sorry for being unclear. The node I was referring to is the polyTweakUV node. That node takes in an input mesh, modifies the UV using the uvTweak attribute, and give the output mesh to the shape. Just like what you see in your screenshot.

 

If you manually edit the UVs in the UV editor, the polyTweakUV node is created for you automatically. This is how Maya normally handles it if mesh has a history.

 

Using the API to edit the UVs can be a bit complicated when there's history. Read the Polygon API section in Maya documentation (http://help.autodesk.com/view/MAYAUL/2018/ENU//?guid=__files_Polygon_API_htm). It explains how polygons are handled internally and provides examples on how to handle history and tweaks. For Python examples, take a look at polyModifier.py and splitUVCmd.py.

 

If you don't want to deal with history, it's fine to delete the history before running your script. When the history is deleted and turned off, you can operate on the UVs directly.

 

Regards,

Lanh


Lanh Hong
Developer Technical Services
Autodesk Developer Network



0 Likes