wrl文件轉換成obj文件

在處理3D數據時發現有些文件是wrl的格式的。但要求輸入obj格式的文件,所以需要轉換一下。看了網上的一些教程,然後現在可以做到把點雲和面的信息提取出來。紋理信息還不知道如何對應。

wrl格式舉例:

#VRML V2.0 utf8
#3Q Technologies Ltd. Copyright 2002. http://www.3q.com 
DEF _3Q_object Transform {
  children [
    Shape {
      appearance Appearance {
        texture ImageTexture {
          url "170716093333.bmp"
          repeatS FALSE
          repeatT FALSE
        }
      }
      geometry IndexedFaceSet {
        ccw TRUE
        solid FALSE
        convex TRUE
        creaseAngle 1.57
        coord Coordinate {
          point [
            -2.73104 -17.342 62.2825,
            62.9104 24.8217 -18.5737
          ]
        }
        coordIndex [
          56240 56352 55946 -1,
          56444 56352 56240 -1
        ]
        texCoord TextureCoordinate {
          point [
            0.866707 0.480934,
            0.861055 0.485866
          ]
        }
        texCoordIndex [
          56240 56352 55946 -1,
          59187 60307 60308 -1
        ]
      }
    }
  ]
}
DEF _3Q_Camera_Front Viewpoint {
  position 1.79461 27.4489 562.665
  orientation 0 0 0 3.14159
  fieldOfView 0.5
  description "3Q Front"
}
DEF _3Q_Camera_Back Viewpoint {
  position 1.79461 27.4489 -472.268
  orientation 0 1 0 3.14159
  fieldOfView 0.5
  description "3Q Back"
}
DEF _3Q_Camera_Left Viewpoint {
  position -750.735 27.4489 45.1983
  orientation 0 1 0 -1.5708
  fieldOfView 0.5
  description "3Q Left"
}
DEF _3Q_Camera_Right Viewpoint {
  position 754.324 27.4489 45.1983
  orientation 0 1 0 1.5708
  fieldOfView 0.5
  description "3Q Right"
}
DEF _3Q_Camera_Top Viewpoint {
  position 1.79461 660.64 45.1983
  orientation 1 0 0 -1.5708
  fieldOfView 0.5
  description "3Q Top"
}
DEF _3Q_Camera_Bottom Viewpoint {
  position 1.79461 -605.742 45.1983
  orientation 1 0 0 1.5708
  fieldOfView 0.5
  description "3Q Bottom"
}

可以看到從coord Coordinate開始,先是點雲信息(point),然後是面信息(coordIndex)其中面的信息在obj中是從1開始的,我們這份wrl是從0開始的,所以在等下處理時要+1。但是我也不知道其他wrl文件是否需要加1。之後是texCoord TextureCoordinate,裏面分別有紋理座標和信息(現在還不知道怎麼對應)。

所以就提了pointcloud和faces的信息,用作後續處理。

貼代碼:

# -*- coding: utf-8 -*-
"""
Created on Fri Mar 29 10:23:59 2019

@author: DSY
"""

import numpy as np
import pandas as pd
import glob
import os 
#from re import *
import re
#import linecache
#%%
#points
wrls=glob.glob(os.path.join('E:\\3Dface\ZZ_3D','*.wrl'))
outpath="E:\\3Dface\\ZZ_3D\\ZZobj\\"
wrls.sort()
for wrl in wrls:
    f=open(wrl,"r")
    lines=f.readlines()
    f.close()
    keywords="coord Coordinate"
    for i in range(0,len(lines)):
        i+=1
        if re.search(keywords, lines[i]):
            break
    #skip point[
    i=i+2
    points=[]
    keywords2='coordIndex'
    for i in range(i,len(lines)):
        if re.search(keywords2, lines[i]):
            break
        points.append(lines[i].split(',')[0].split( ))
    points=points[0:-2]
    
    #%%
    faces=[]
    i=i+2
    keywords3='texCoord TextureCoordinate'
    for i in range(i,len(lines)):
        if re.search(keywords3,lines[i]):
            break
        faces.append(lines[i].split(',')[0].split( )[0:3])
    faces=faces[0:-1]
    faces=np.array(faces,dtype=int)+1
    #+1來符合obj的face格式
    
    #%%
    a=np.zeros((len(points),1),dtype='str')
    a[:]='v'
    points=np.hstack((a,points))
    a=np.zeros((len(faces),1),dtype='str')
    a[:]='f'
    faces=np.hstack((a,faces))
    files=np.vstack((points,faces))
    np.savetxt(outpath+wrl.split("\\")[-1].split(".")[0]+'.obj',files,delimiter=' ',fmt='%s')
    

不打註釋了…簡單的說就是通過三個關鍵詞來框選定區域,找到pointcloud和faces的信息,然後保存成obj的格式。

obj格式:

v x1 y1 z1

v x2 y2 z2

f i1 i2 i3

f i4 i5 i6

 

 

求打賞!!!謝謝老闆!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章