Forum: Poser Python Scripting


Subject: Useful Code Snippets

structure opened this issue on Jun 27, 2019 ยท 94 posts


structure posted Tue, 02 November 2021 at 12:27 PM Forum Coordinator

get libraries (outside of poser) : 

import os
import xml.etree.cElementTree as ElementTree

def PrefsLocation(version):
    if version > 10:
        return os.path.join(os.environ["APPDATA"], "poser", str(Version()))
    else:
        return os.path.join(os.environ["APPDATA"], "poser pro", str(Version()))

def get_libraries( xml_file ):
    try:
        tree = ElementTree.parse(xml_file)
    except Exception as inst:
        print( "Unexpected error opening %s: %s" % (xml_file, inst) )
        return None
   
    libraries = []
   
    if os.path.exists(xml_file):
        root=tree.getroot()
        for child in root:
            if child.tag=='ContentFolder':
                libraries.append(str(child.attrib['folder']))
   
    return libraries

def Version(): # edit to suit your needs
    # return 10
    return 12

def get_libraries( xml_file ):
    try:
        tree = ElementTree.parse(xml_file)
    except Exception as inst:
        print( "Unexpected error opening %s: %s" % (xml_file, inst) )
        return None
    libraries = []
   
    if os.path.exists(xml_file):
        root=tree.getroot()
        for child in root:
            if child.tag=='ContentFolder':
                libraries.append(str(child.attrib['folder']))
   
    return libraries

PrefsLocation = PrefsLocation(Version())
PrefsFile = "LibraryPrefs.xml"
xml_file = os.path.join(PrefsLocation, PrefsFile)
libraries = get_libraries( xml_file)

for library in libraries:
    print(library)

Locked Out