<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic [Python] Script seemingly not returning UV IDs correctly in Maya Programming Forum</title>
    <link>https://forums.autodesk.com/t5/maya-programming-forum/python-script-seemingly-not-returning-uv-ids-correctly/m-p/6457207#M12961</link>
    <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is a difficult one to explain and I think that it probably arises from my lack of knowledge of how UVs actually work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've written a script that aligns a UV shell orthogonal to a selected edge, however it only seems to work when I select certain edges. When it doesn't work it's because the selected edge will only convert to one UV point (ID?) when I call&amp;nbsp;&lt;EM&gt;polyListComponentConversion&lt;/EM&gt;&lt;EM&gt;()&lt;/EM&gt;. What I want to know is why some edges that I select will convert to two UVs, and some will only convert to one. It seems like border edges happily convert to two UVs.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For instance, selecting this edge, between UV points 88 and 99 will return an error, because the conversion will only return one UV ID.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;IMG src="https://forums.autodesk.com/t5/image/serverpage/image-id/257577iADB282807B875A66/image-size/original?v=v2&amp;amp;px=-1" border="0" alt="8899.PNG" title="8899.PNG" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, selecting this edge will work fine, and I can't understand why.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;IMG src="https://forums.autodesk.com/t5/image/serverpage/image-id/257578iE4374D9E4FD8DAEE/image-size/original?v=v2&amp;amp;px=-1" border="0" alt="25.PNG" title="25.PNG" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As far as I can see, both of these edges are connected to two UV points, so would one work and the other not?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Code is attached in full below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;'''
Created on 18 Jul 2016

@author: BenjiA

About:
This tool attempts to rotate a UV shell so that it is oriented 90 degrees orthogonal to the edge that you select

Use:
Select an edge and run the tool, if you get a list index out of range error, try another edge
'''
import pymel.core as pm;
import maya.api.OpenMaya as om;

def GetSel():
    #Get the selected edge
    sel = pm.selected();
    if not sel:
        raise RuntimeError('Select an edge or a face, and run the tool again');
    else:
        selType = pm.nodeType(sel);
        #make sure its an edge by converting it to an edge
        selEdge = pm.polyListComponentConversion(te=1);
        return selEdge[0];

def GetVectorsAndRotate(sel):
    #convert the edge selection to uvs
    selUVs = pm.polyListComponentConversion(tuv=1);
    if len(selUVs) &amp;lt; 2:
        print 'selUvs length is ' + str(len(selUVs));
        raise RuntimeError('Select a face or an edge with more connected UV point');
    else:
        print 'selUvs length is ' + str(len(selUVs));
        #get the two UV points individually
        UV0 = pm.polyEditUV(selUVs[0], query=1, u=1, v=1);
        UV1 = pm.polyEditUV(selUVs[1], query=1, u=1, v=1);
        #work out the vector that represents the edge between the UV points
        edgeVect = pm.datatypes.Vector((UV0[0] - UV1[0]), (UV0[1] - UV1[1]));
        #normalize it
        edgeVect = edgeVect.normal();
        print 'Edge vector is:', edgeVect;
        #get the vector orthogonal to the edge vector
        orthVect = pm.datatypes.Vector(-edgeVect[1], edgeVect[0]);
        #normalize it
        orthVect = orthVect.normal();
        print 'Perpendicular vector is:', orthVect;
        #calculate the arctangent of the orthogonal vector
        atan = pm.datatypes.atan2(orthVect[1], orthVect[0]);
        print 'Arctangent is:', atan;
        #convert the arctangent to degrees
        degrees = pm.datatypes.degrees(atan);
        print 'Atan to degree conversion is:', degrees;
        #get the parent object of the selected uv points
        selObj = pm.PyNode(sel, s=1);
        selObj = selObj.split(".");
        selParent = pm.PyNode(selObj[0]).getParent();
        #select the whole shell
        selFaces = pm.polySelect(ets=1);
        #rotate the shell by the calculated degrees
        pm.polyEditUV(a=-degrees, pu=0.5, pv=0.5);
    
GetVectorsAndRotate(GetSel());&lt;/PRE&gt;</description>
    <pubDate>Mon, 25 Jul 2016 09:49:53 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2016-07-25T09:49:53Z</dc:date>
    <item>
      <title>[Python] Script seemingly not returning UV IDs correctly</title>
      <link>https://forums.autodesk.com/t5/maya-programming-forum/python-script-seemingly-not-returning-uv-ids-correctly/m-p/6457207#M12961</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is a difficult one to explain and I think that it probably arises from my lack of knowledge of how UVs actually work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've written a script that aligns a UV shell orthogonal to a selected edge, however it only seems to work when I select certain edges. When it doesn't work it's because the selected edge will only convert to one UV point (ID?) when I call&amp;nbsp;&lt;EM&gt;polyListComponentConversion&lt;/EM&gt;&lt;EM&gt;()&lt;/EM&gt;. What I want to know is why some edges that I select will convert to two UVs, and some will only convert to one. It seems like border edges happily convert to two UVs.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For instance, selecting this edge, between UV points 88 and 99 will return an error, because the conversion will only return one UV ID.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;IMG src="https://forums.autodesk.com/t5/image/serverpage/image-id/257577iADB282807B875A66/image-size/original?v=v2&amp;amp;px=-1" border="0" alt="8899.PNG" title="8899.PNG" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, selecting this edge will work fine, and I can't understand why.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;IMG src="https://forums.autodesk.com/t5/image/serverpage/image-id/257578iE4374D9E4FD8DAEE/image-size/original?v=v2&amp;amp;px=-1" border="0" alt="25.PNG" title="25.PNG" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As far as I can see, both of these edges are connected to two UV points, so would one work and the other not?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Code is attached in full below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;'''
Created on 18 Jul 2016

@author: BenjiA

About:
This tool attempts to rotate a UV shell so that it is oriented 90 degrees orthogonal to the edge that you select

Use:
Select an edge and run the tool, if you get a list index out of range error, try another edge
'''
import pymel.core as pm;
import maya.api.OpenMaya as om;

def GetSel():
    #Get the selected edge
    sel = pm.selected();
    if not sel:
        raise RuntimeError('Select an edge or a face, and run the tool again');
    else:
        selType = pm.nodeType(sel);
        #make sure its an edge by converting it to an edge
        selEdge = pm.polyListComponentConversion(te=1);
        return selEdge[0];

def GetVectorsAndRotate(sel):
    #convert the edge selection to uvs
    selUVs = pm.polyListComponentConversion(tuv=1);
    if len(selUVs) &amp;lt; 2:
        print 'selUvs length is ' + str(len(selUVs));
        raise RuntimeError('Select a face or an edge with more connected UV point');
    else:
        print 'selUvs length is ' + str(len(selUVs));
        #get the two UV points individually
        UV0 = pm.polyEditUV(selUVs[0], query=1, u=1, v=1);
        UV1 = pm.polyEditUV(selUVs[1], query=1, u=1, v=1);
        #work out the vector that represents the edge between the UV points
        edgeVect = pm.datatypes.Vector((UV0[0] - UV1[0]), (UV0[1] - UV1[1]));
        #normalize it
        edgeVect = edgeVect.normal();
        print 'Edge vector is:', edgeVect;
        #get the vector orthogonal to the edge vector
        orthVect = pm.datatypes.Vector(-edgeVect[1], edgeVect[0]);
        #normalize it
        orthVect = orthVect.normal();
        print 'Perpendicular vector is:', orthVect;
        #calculate the arctangent of the orthogonal vector
        atan = pm.datatypes.atan2(orthVect[1], orthVect[0]);
        print 'Arctangent is:', atan;
        #convert the arctangent to degrees
        degrees = pm.datatypes.degrees(atan);
        print 'Atan to degree conversion is:', degrees;
        #get the parent object of the selected uv points
        selObj = pm.PyNode(sel, s=1);
        selObj = selObj.split(".");
        selParent = pm.PyNode(selObj[0]).getParent();
        #select the whole shell
        selFaces = pm.polySelect(ets=1);
        #rotate the shell by the calculated degrees
        pm.polyEditUV(a=-degrees, pu=0.5, pv=0.5);
    
GetVectorsAndRotate(GetSel());&lt;/PRE&gt;</description>
      <pubDate>Mon, 25 Jul 2016 09:49:53 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-programming-forum/python-script-seemingly-not-returning-uv-ids-correctly/m-p/6457207#M12961</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2016-07-25T09:49:53Z</dc:date>
    </item>
    <item>
      <title>Re: [Python] Script seemingly not returning UV IDs correctly</title>
      <link>https://forums.autodesk.com/t5/maya-programming-forum/python-script-seemingly-not-returning-uv-ids-correctly/m-p/6461103#M12962</link>
      <description>&lt;P&gt;My guess is that the edge you picked has consecutive UV IDs.&amp;nbsp; If the UVs are consecutive, they will be returned as a single item.&amp;nbsp; For example, [u'myMesh.map[14:15]'].&amp;nbsp; You can use the "ls" command to separate out the results into individual components.&lt;/P&gt;&lt;PRE&gt;from maya import cmds&lt;BR /&gt;# Skipping a bunch of lines here&lt;BR /&gt;&lt;BR /&gt;selUVs = pm.polyListComponentConversion(tuv=1)
selUVs = cmds.ls(selUVs, flatten=True)&lt;/PRE&gt;</description>
      <pubDate>Tue, 26 Jul 2016 22:01:52 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-programming-forum/python-script-seemingly-not-returning-uv-ids-correctly/m-p/6461103#M12962</guid>
      <dc:creator>RFlannery1</dc:creator>
      <dc:date>2016-07-26T22:01:52Z</dc:date>
    </item>
    <item>
      <title>Re: [Python] Script seemingly not returning UV IDs correctly</title>
      <link>https://forums.autodesk.com/t5/maya-programming-forum/python-script-seemingly-not-returning-uv-ids-correctly/m-p/6461947#M12963</link>
      <description>Thanks buddy, that solved the issue - I didn't even know about flatten, thanks very much &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Wed, 27 Jul 2016 10:40:53 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-programming-forum/python-script-seemingly-not-returning-uv-ids-correctly/m-p/6461947#M12963</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2016-07-27T10:40:53Z</dc:date>
    </item>
  </channel>
</rss>

