Jump to content

get all possible file extensions with python


celd

Recommended Posts

Hi,

I am trying to get a list of all the extensions that file sop can read/write, for example: bgeo.sc, vdb, abc, etc - with python. Is there a way to load them all into an array without writing it manually one by one?

Thanks

 

Link to comment
Share on other sites

I don't know reliable future-proof way to get the exact list of geometry files for File node.

1. To make hou.ui.selectFile() ask for geometry, pass file_type=hou.fileType.Geometry  parameter.

2. You can parse data from "$HH/GEOfiles" and "$HH/GEOio.json", which appear to provide most common geometry formats.

3. To get the most extensive list of supported geometry, I'd parse the help message of gconvert utility:

import re
import subprocess

formats = {'Read': set(),
           'Write': set()}

# Capture output of gconvert utility.
path = 'gconvert'
try:
    out = subprocess.check_output(path, shell=True, stderr=subprocess.STDOUT)
except:
    pass

# Split output into major pieces.
_, builtin, _, external_translators = re.split(r'-- Built-In --|-- Translators --|-- External Translators --', out)

# Parse "Built-In" piece
builtin = set(builtin.partition('Recognized Extensions:')[2].replace(',', '').split())
formats['Read'] |= builtin
formats['Write'] |= builtin

# Parse "External Translators" piece
external_translators = re.findall(r'\((Read)-(?:Only|(Write))\)\n.*: ([\w., ]+)', external_translators)
for read, write, extensions in external_translators:
    extensions = set(extensions.split(', '))
    if read:
        formats['Read'] |= extensions
    if write:
        formats['Write'] |= extensions

# Manually add 2 formats from "Translators" piece and also add FBX read.
formats['Read'] |= {'.hgt', '.fbx', '.vdb'}
formats['Write'] |= {'.vdb'}

It will output this:

{'Read': {'.abc',
  '.ai',
  '.bgeo',
  '.bgeo.bz2',
  '.bgeo.gz',
  '.bgeo.lzma',
  '.bgeo.sc',
  '.bgeogz',
  '.bgeosc',
  '.bhclassic',
  '.bhclassic.bz2',
  '.bhclassic.gz',
  '.bhclassic.lzma',
  '.bhclassic.sc',
  '.bhclassicgz',
  '.bhclassicsc',
  '.bjson',
  '.bjson.gz',
  '.bjson.sc',
  '.bjsongz',
  '.bjsonsc',
  '.bstl',
  '.dxf',
  '.eps',
  '.fbx',
  '.geo',
  '.geo.bz2',
  '.geo.gz',
  '.geo.lzma',
  '.geo.sc',
  '.geogz',
  '.geosc',
  '.hbclassic',
  '.hbclassic.gz',
  '.hbclassic.sc',
  '.hbclassicgz',
  '.hbclassicsc',
  '.hclassic',
  '.hclassic.bz2',
  '.hclassic.gz',
  '.hclassic.lzma',
  '.hclassic.sc',
  '.hclassicgz',
  '.hclassicsc',
  '.hgt',
  '.iges',
  '.igs',
  '.json',
  '.json.gz',
  '.json.sc',
  '.jsongz',
  '.jsonsc',
  '.lw',
  '.lwo',
  '.obj',
  '.off',
  '.pc',
  '.pdb',
  '.ply',
  '.pmap',
  '.stl',
  '.vdb'},
 'Write': {'.abc',
  '.bgeo',
  '.bgeo.bz2',
  '.bgeo.gz',
  '.bgeo.lzma',
  '.bgeo.sc',
  '.bgeogz',
  '.bgeosc',
  '.bhclassic',
  '.bhclassic.bz2',
  '.bhclassic.gz',
  '.bhclassic.lzma',
  '.bhclassic.sc',
  '.bhclassicgz',
  '.bhclassicsc',
  '.bjson',
  '.bjson.gz',
  '.bjson.sc',
  '.bjsongz',
  '.bjsonsc',
  '.bstl',
  '.dxf',
  '.geo',
  '.geo.bz2',
  '.geo.gz',
  '.geo.lzma',
  '.geo.sc',
  '.geogz',
  '.geosc',
  '.hbclassic',
  '.hbclassic.gz',
  '.hbclassic.sc',
  '.hbclassicgz',
  '.hbclassicsc',
  '.hclassic',
  '.hclassic.bz2',
  '.hclassic.gz',
  '.hclassic.lzma',
  '.hclassic.sc',
  '.hclassicgz',
  '.hclassicsc',
  '.iges',
  '.igs',
  '.json',
  '.json.gz',
  '.json.sc',
  '.jsongz',
  '.jsonsc',
  '.lw',
  '.lwo',
  '.obj',
  '.off',
  '.pc',
  '.pdb',
  '.ply',
  '.pmap',
  '.stl',
  '.vdb'}}

Difference may not worth it, but it exist:

>>> formats['Read'] - formats['Write']
{'.ai', '.eps', '.fbx', '.hgt'}

>>> formats['Write'] - formats['Read']
set()

 

  • Like 2
Link to comment
Share on other sites

Thanks a lot for this! I tried the python gconvert method and the odd thing is that I cant get it to write the output of subprocess.check_output() into a variable. No matter what I do it errors out into a "CalledProcessError: Command 'gconvert' returned non-zero status 1".

I actually managed to get it to write the output into the console by removing the "stderr" flag, but it always ends with an error and does not assign the output into a variable.

Do you have any ideas what could be the reason for this/how to get around it?

Link to comment
Share on other sites

Hmm, weird that it did work for me earlier. stderr parameter is required to skip console popup (on Windows). Try to catch the subprocess.CalledProcessError exception:

try:
    out = subprocess.check_output(path, shell=True, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError, e:
    out = e.output

This now works for me, in clean scene.

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...