Community
FBX Forum
Welcome to Autodesk’s FBX Forums. Share your knowledge, ask questions, and explore popular FBX topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

SDK Python UV coordinates work uncorrectly

0 REPLIES 0
Reply
Message 1 of 1
Anonymous
729 Views, 0 Replies

SDK Python UV coordinates work uncorrectly

I got Python script, which can get Vertex Position, Vertex Indices, but UV and Normals it get uncorrectly, i don't understand what is the problem. Can u help me please guys, i'm figured, really don't know what to do.

Script:

import sys, os, json, urlparse
sys.path.append( '/home/o/oddbear/fairmotion/public_html/engine/utils/fbx/lib/Python27_ucs4_x64/' )
from fbx import *

def application( environ, start_response ):

    #url path

    value = environ[ 'HTTP_HOST' ]+environ[ 'REQUEST_URI' ]
    query = urlparse.urlparse(value).query.decode( 'UTF-8', 'strict' )
    url = urlparse.parse_qs(query)[ 'url' ][0]

    path = '/home/o/oddbear/fairmotion/public_html/engine/'+url

    #initializing file

    manager = FbxManager.Create()

    IOSettings = FbxIOSettings.Create( manager, 'IOSettings' )
    manager.SetIOSettings( IOSettings )

    scene = FbxScene.Create( manager, 'Scene' )

    importer = FbxImporter.Create( manager, 'Importer' )

    status = importer.Initialize( path, -1, IOSettings )

    JsonReq = []

    if( status != False ):

        #if file exist

        importer.Import( scene )
        importer.Destroy()

        pFbxRootNode = scene.GetRootNode()

        objcount = pFbxRootNode.GetChildCount()

        for i in range( objcount ):

            #for every object

            pFbxChildNode = pFbxRootNode.GetChild( i )
            pMesh = pFbxChildNode.GetNodeAttribute()

            if( pMesh == None ):
                continue

            geometry = FbxGeometryConverter( manager )
            pMesh = geometry.Triangulate( pMesh, True )

            attributeType = pMesh.GetAttributeType();

            if( attributeType != FbxNodeAttribute.eMesh ):
                continue

            polygonCount = pMesh.GetPolygonCount()
            layerCount = pMesh.GetLayerCount()

            indices = pMesh.GetPolygonVertices()
            pVertices = pMesh.GetControlPoints()

            m_vecIndices = indices
            m_vecVertices = []
            m_vecNormals = []
            m_vecUVs = []
            m_vecUVIndices = []
            UVs = []

            #transform

            node = pMesh.GetNode()

            lTmpVector = node.GetGeometricTranslation( FbxNode.eSourcePivot )
            Position = [ lTmpVector[ 0 ], lTmpVector[ 1 ], lTmpVector[ 2 ] ]
        
            lTmpVector = node.GetGeometricRotation( FbxNode.eSourcePivot )
            Rotation = [ lTmpVector[ 0 ], lTmpVector[ 1 ], lTmpVector[ 2 ] ]
        
            lTmpVector = node.GetGeometricScaling( FbxNode.eSourcePivot )
            Scale = [ lTmpVector[ 0 ], lTmpVector[ 1 ], lTmpVector[ 2 ] ]

            #vertices

            control_points_count = pMesh.GetControlPointsCount()
            control_points = pVertices
            
            positions = []

            for z in range( control_points_count ):
                tmp = control_points[ z ]
                tmp = [ tmp[ 0 ], tmp[ 1 ], tmp[ 2 ] ]
                positions.append( tmp )
            
            if node:
                t = node.GeometricTranslation.Get()
                t = FbxVector4( t[ 0 ], t[ 1 ], t[ 2 ], 1 )
                r = node.GeometricRotation.Get()
                r = FbxVector4( r[ 0 ], r[ 1 ], r[ 2 ], 1 )
                s = node.GeometricScaling.Get()
                s = FbxVector4( s[ 0 ], s[ 1 ], s[ 2 ], 1 )
                
                hasGeometricTransform = False
                if t[ 0 ] != 0 or t[ 1 ] != 0 or t[ 2 ] != 0 or \
                    r[ 0 ] != 0 or r[ 1 ] != 0 or r[ 2 ] != 0 or \
                    s[ 0 ] != 1 or s[ 1 ] != 1 or s[ 2 ] != 1:
                        hasGeometricTransform = True
                
                if hasGeometricTransform:
                    geo_transform = FbxMatrix( t, r, s )
                else:
                    geo_transform = FbxMatrix()
            
                transform = None
            
                if hasGeometricTransform:
                    transform = geo_transform
                    
                if transform:
                    for z in range( len( positions ) ):
                        v = positions[ z ]
                        position = FbxVector4( v[ 0 ], v[ 1 ], v[ 2 ] )
                        position = transform.MultNormalize( position )
                        positions[ z ] = [ position[ 0 ], position[ 1 ], position[ 2 ] ]
            
            for z in range( len( positions ) ):
                m_vecVertices.extend( positions[ z ] )

            #normals
        
            for z in range( layerCount ):

                mesh_normals = pMesh.GetLayer( z ).GetNormals()
                if( not mesh_normals ):
                    continue
                  
                normals_array = mesh_normals.GetDirectArray()
                normals_count = normals_array.GetCount()
          
                if normals_count == 0:
                    continue
        
                normal_values = []
        
                for l in range( normals_count ):
                    normal = normals_array.GetAt( l )
                    normal = [ normal[ 0 ], normal[ 1 ], normal[ 2 ] ]
                    normal_values.append( normal )
        
                node = pMesh.GetNode()
                if node:
                    t = node.GeometricTranslation.Get()
                    t = FbxVector4( t[ 0 ], t[ 1 ], t[ 2 ], 1 )
                    r = node.GeometricRotation.Get()
                    r = FbxVector4( r[ 0 ], r[ 1 ], r[ 2 ], 1 )
                    s = node.GeometricScaling.Get()
                    s = FbxVector4( s[ 0 ], s[ 1 ], s[ 2 ], 1 )
                    
                    hasGeometricTransform = False
                    if t[ 0 ] != 0 or t[ 1 ] != 0 or t[ 2 ] != 0 or \
                       r[ 0 ] != 0 or r[ 1 ] != 0 or r[ 2 ] != 0 or \
                       s[ 0 ] != 1 or s[ 1 ] != 1 or s[ 2 ] != 1:
                        hasGeometricTransform = True
                    
                    if hasGeometricTransform:
                        geo_transform = FbxMatrix( t, r, s )
                    else:
                        geo_transform = FbxMatrix()

                    transform = None

                    if hasGeometricTransform:
                        transform = geo_transform
                
                    if transform:
                        t = FbxVector4( 0, 0, 0, 1 )
                        transform.SetRow( 3, t )
        
                        for l in range( len( normal_values ) ):
                            n = normal_values[ l ]
                            normal = FbxVector4( n[ 0 ], n[ 1 ], n[ 2 ] )
                            normal = transform.MultNormalize( normal )
                            normal.Normalize()
                            normal = [ normal[ 0 ], normal[ 1 ], normal[ 2 ] ]
                            normal_values[ l ] = normal

                m_vecNormals.append( normal_values )

            #UVs

            for z in range( layerCount ):

                mesh_uvs = pMesh.GetLayer( z ).GetUVs()
                if( not mesh_uvs ):
                    continue

                uvs_array = mesh_uvs.GetDirectArray()
                uvs_count = uvs_array.GetCount()
        
                if( uvs_count == 0 ):
                    continue

                uv_values = []
                uv_indices = []

                for l in range( uvs_count ):
                    uv = uvs_array.GetAt( l )
                    uv = [ uv[ 0 ], uv[ 1 ] ]
                    uv_values.append( uv )

                vertexId = 0
                for p in range( polygonCount ):

                    poly_size = pMesh.GetPolygonSize( p )
                    poly_uvs = []
        
                    for v in range( poly_size ):

                        control_point_index = pMesh.GetPolygonVertex( p, v )
        
                        if mesh_uvs.GetMappingMode() == FbxLayerElement.eByControlPoint:
                            if mesh_uvs.GetReferenceMode() == FbxLayerElement.eDirect:
                                poly_uvs.append(control_point_index)
                            elif mesh_uvs.GetReferenceMode() == FbxLayerElement.eIndexToDirect:
                                index = mesh_uvs.GetIndexArray().GetAt( control_point_index )
                                poly_uvs.append( index )
                        elif mesh_uvs.GetMappingMode() == FbxLayerElement.eByPolygonVertex:
                            uv_texture_index = mesh_uvs.GetIndexArray().GetAt(vertexId)
                            
                            if mesh_uvs.GetReferenceMode() == FbxLayerElement.eDirect or \
                                mesh_uvs.GetReferenceMode() == FbxLayerElement.eIndexToDirect:
                                    poly_uvs.append( uv_texture_index )
                        elif mesh_uvs.GetMappingMode() == FbxLayerElement.eByPolygon or \
                            mesh_uvs.GetMappingMode() ==  FbxLayerElement.eAllSame or \
                            mesh_uvs.GetMappingMode() ==  FbxLayerElement.eNone:
                            return '[]'

                        vertexId += 1
                    uv_indices.append( poly_uvs )

                UVs.extend( uv_values )
                m_vecUVIndices.extend( uv_indices )

            for z in range( len( m_vecUVIndices ) ):
                for v in range( len( m_vecUVIndices[ z ] ) ):
                    m_vecUVs.extend( UVs[ m_vecUVIndices[ z ][ v ] ] )

            #Request

            JsonReq.append( {
                            'indices': m_vecIndices,
                            'normals': m_vecNormals,
                            'UV': m_vecUVs,
                            'vertices': m_vecVertices,
                            'transform': [ Position, Rotation, Scale ]
                            } )

        JsonReq = json.dumps( JsonReq )

    status = '200 OK'
    response_headers = [ ( 'Content-type','text/plain' ) ]
    start_response( status, response_headers )

    return JsonReq
0 REPLIES 0

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report