Hello everyone,

I'm having fun with xGen and his exporter for UnrealEngine.
I am trying to create the uv mesh it generates. So I made a script that 
detects each mesh and calculates the position in the UV space.
Only I have a strange behavior in the UV editor. The indexes are good but 
the polygons are not correct after the first mesh.

Do you know why?

I enclose my script and scene test in this message.


-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to python_inside_maya+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/1da41c6b-d130-4a04-a726-482d50e7c95c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Attachment: xGen_test.ma
Description: Binary data

# ====================================
#    Import Modules
# ====================================

import time
import math

from maya import cmds, OpenMaya


# ====================================
#    Misc function
# ====================================

def get_path(s_node):
    """
    !@Brief Retrieve MObject from node name

    @type s_node: str / unicode
    @param s_node: Node name

    @rtype: OpenMaya.MDagPath
    @return: Maya object.
    """

    m_sel_list = OpenMaya.MSelectionList()
    m_sel_list.add(s_node)
    dp_node = OpenMaya.MDagPath()
    m_sel_list.getDagPath(0, dp_node)

    return dp_node


# ====================================
#   Get Mesh
# ====================================

a_sel = cmds.ls(selection=True, long=True)
if a_sel is None or len(a_sel) == 0:
    raise Exception("Select one mesh node")

dp_node = get_path(a_sel[0])
if dp_node.hasFn(OpenMaya.MFn.kTransform) is True:
    dp_node.extendToShape()

if dp_node.hasFn(OpenMaya.MFn.kMesh) is False:
    raise TypeError("Node selected must be a mesh not {0}".format(dp_node.node().apiTypeStr()))

# ====================================
#   Retrieve separate mesh
# ====================================

f_time = time.time()

mfn_mesh = OpenMaya.MFnMesh(dp_node)
mit_mesh = OpenMaya.MItMeshPolygon(dp_node)

d_separate = dict()
i_mesh_id = 0

mit_mesh.reset()
while mit_mesh.isDone() is False:

    #   Check if this face is new mesh
    if d_separate.get(i_mesh_id) is not None:
        if len(d_separate[i_mesh_id]["faces"]) != 0 and mit_mesh.index() not in d_separate[i_mesh_id]["faces"]:
            i_mesh_id += 1

    #    Add element to dict
    if d_separate.get(i_mesh_id) is None:
        d_separate[i_mesh_id] = {"vertices": list(), "faces": list()}

    #   Add faces
    if len(d_separate[i_mesh_id]["faces"]) == 0:
        d_separate[i_mesh_id]["faces"].append(mit_mesh.index())

    mia_connected_face = OpenMaya.MIntArray()
    mit_mesh.getConnectedFaces(mia_connected_face)
    for i in range(mia_connected_face.length()):
        if mia_connected_face[i] in d_separate[i_mesh_id]["faces"]:
            continue
        d_separate[i_mesh_id]["faces"].append(mia_connected_face[i])

    #    Get connected Face from current vertices
    mia_face_vertices = OpenMaya.MIntArray()
    mit_mesh.getVertices(mia_face_vertices)
    for i in range(mia_face_vertices.length()):

        if mia_face_vertices[i] in d_separate[i_mesh_id]["vertices"]:
            continue
        d_separate[i_mesh_id]["vertices"].append(mia_face_vertices[i])

    mit_mesh.next()

print ("Retrieve separate mesh take -- {0}".format(time.time() - f_time))


def mesh_check(i_mesh_id):
    a_vtx = ["{0}.vtx[{1}]".format(dp_node.fullPathName(), vid) for vid in d_separate[i_mesh_id]["vertices"]]
    cmds.select(a_vtx)


# mesh_check(2)

# # ====================================
# #   Flatten hair
# # ====================================

# f_time = time.time()

# mpa_points = OpenMaya.MPointArray()
# mfn_mesh.getPoints(mpa_points, OpenMaya.MSpace.kObject)

# for separate_id, d_data in d_separate.items():

#     a_vtx = d_data["vertices"]
#     i_first = min(a_vtx, key=int)
#     i_second = i_first + 1

#     for vid in a_vtx:

#         if vid < 2:
#             continue

#         if vid%2 == 0:
#             mpa_points.set(mpa_points[i_first], vid)
#         else:
#             mpa_points.set(mpa_points[i_second], vid)

# print ("Flatten hair take -- {0}".format(time.time() - f_time))

# f_time = time.time()
# mfn_mesh.setPoints(mpa_points, OpenMaya.MSpace.kObject)
# print ("Set Points take -- {0}".format(time.time() - f_time))


# ====================================
#   Build UVs
# ====================================

f_time = time.time()

#   Init list
i_num_vertices = mfn_mesh.numVertices()
mfa_u = OpenMaya.MFloatArray(i_num_vertices, 0.0)
mfa_v = OpenMaya.MFloatArray(i_num_vertices, 0.0)

#   Init iteration
i_num_mesh = len(d_separate.keys())
i_split = int(math.sqrt(i_num_mesh) + 0.5)
f_space = 1.0 / (i_split * 1.5)

i_vtx_id = 0
#   Iter row
for i in range(i_split):
    #   Iter coloumn
    for j in range(i_split):

        i_id = (i_split * i) + j
        if i_id > i_num_mesh - 1:
            break

        d_mesh = d_separate[i_id]
        i_num_mesh_vertices = len(d_mesh["vertices"])

        if i_vtx_id >= i_num_vertices:
            break

        #   Iter mesh vertices
        for k in range(0, i_num_mesh_vertices, 2):
            f_mesh_split = f_space * 0.5
            u_pos = (f_space * j) + (f_mesh_split * j)
            v_split = f_space / (i_num_mesh_vertices - 1)
            v_pos = (v_split * k) + (f_space * i) + (f_mesh_split * i)

            mfa_u.set(u_pos, i_vtx_id)
            mfa_v.set(v_pos, i_vtx_id)
            mfa_u.set(mfa_u[i_vtx_id] + f_space, i_vtx_id + 1)
            mfa_v.set(mfa_v[i_vtx_id], i_vtx_id + 1)

            i_vtx_id += 2

print "Build UV array take -- {0}".format(time.time() - f_time)

f_time = time.time()
mfn_mesh.setUVs(mfa_u, mfa_v, "map1")
print "Set UV take -- {0}".format(time.time() - f_time)

Reply via email to