2012年4月28日土曜日

MAXScript テキストファイルに書き出す

シーン内で使用しているテクスチャ画像の絶対パスをファイルに書き出す テスト

WriteDiffuseTexFilePath.ms

-- マテリアルのDiffuseテクスチャ画像の絶対パスをファイルに出力
fn WriteMtlInfo fs mtl = (
  d = mtl.DiffuseMap
  if undefined == d do return -2
  
  bm = d.bitmap
  if undefined == bm do return -3
  
  filePath = d.fileName
  
  -- ファイルの絶対パスを取得
  FileResolutionManager.getFullFilePath &filePath #Bitmap

  -- ¥ を /に変換
  strArray = filterString filePath "¥¥"
  filePath = ""
  for i = 1 to strArray.count do (
    filePath += strArray[i]
    if i < strArray.count do (
      filePath += "/"
    )
  )
  -- 画像の幅、高さ
  w = bm.width
  h = bm.height
  format "%¥n" filePath to:fs -- 絶対パスをファイルに出力
)

-- シーン内のマテリアル情報の取得
fn WriteDiffuseTexFilePath fileName = (
  if 0 == sceneMaterials.count do (
    format "sceneMaterials.count is 0¥n"
    return -1
  )

  -- ファイル出力先のディレクトリ
  fPath =  GetDir #export
  fPath += "¥¥" + fileName

  -- ファイルオープン
  fs = openFile fPath mode:"wt"
  if undefined  == fs do return -1
  
  for i = 1 to sceneMaterials.count do (
    mtl = sceneMaterials[ i ]   -- マテリアルを取得
    c = classof mtl     -- マテリアルのクラス名を取得

    if c == Standardmaterial do (
      WriteMtlInfo fs mtl
    )
    if c == Multimaterial do (
      nSubMtl = getNumSubMtls mtl -- マテリアルのサブマテリアル数を取得
      for j = 1 to nSubMtl do (
        subMtl = getSubMtl mtl j
        WriteMtlInfo fs subMtl
      )
    )
  )
  -- ファイルクローズ
  close fs  
)
-- exportフォルダにTexFilePath.txtが作成される
WriteDiffuseTexFilePath "TexFilePath.txt"

0 件のコメント: