Forum: Poser Python Scripting


Subject: RenderCacheLocation?

FVerbaas opened this issue on Dec 20, 2020 ยท 9 posts


FVerbaas posted Mon, 21 December 2020 at 1:58 PM Forum Coordinator

OK, here is the end?? product. Let's see if dev team gives us the pointers to the files. Krita accepts Poser's .exr files and supports editing the 16 bit float per channel.

#
# Script to kick off a session with a .exr image processing program (Krita for example) with the last image from the RenderCache. . 
#
# This script reads the location where the image processing app can be found and launches the app with the file  
# location of rendercache and favorite app is written in small 'cookie' text files in poser.PrefsLocation in oreder to be re-used
# (mental note: need some prefsManagement system)
#
# note: Not written for Mac yet. I have no Mac so could not test. 
#
# F. Verbaas, 21-Dec-2020

import os, sys
import subprocess
import platform
from datetime import datetime
import glob

#
# breaks a path into a list of its components
# from: https://www.oreilly.com/library/view/python-cookbook/0596001673/ch04s16.html
def splitall(path):
    allparts = []
    while 1:
        parts = os.path.split(path)
        if parts[0] == path:  # sentinel for absolute paths
            allparts.insert(0, parts[0])
            break
        elif parts[1] == path: # sentinel for relative paths
            allparts.insert(0, parts[1])
            break
        else:
            path = parts[0]
            allparts.insert(0, parts[1])
    return allparts

scene = poser.Scene()
good = True

#
#-------------------------------------------------------------------
# Check out if preferred executable was saved, if not ask and set up
#-------------------------------------------------------------------
AppPathpointer  = os.path.join(poser.PrefsLocation(), 'ImgAppsLocations.txt')
if not os.access(AppPathpointer, os.R_OK):
    # make default location
    osflavor = platform.system()
    if osflavor in 'Windows': 
        AppPathproposal = 'C:/Program Files'
        AppLocationChooser = poser.DialogFileChooser(poser.kDialogFileChooserOpen, None, "Select Imsage Editing Application", AppPathproposal , ".exe")
        AppLocationChooser.Show()

        if '.exe'in AppLocationChooser.Path():
            app = open(AppPathpointer, 'w')
            AppPath = app.write(AppLocationChooser.Path())
            app.close
        else:
            poser.DialogSimple.MessageBox('Selected path ' + AppLocationChooser.Path() + 'does not lead to an executable file')
            good = False

    elif osflavor in 'Darwin': 
        poser.DialogSimple.MessageBox( 'Sorry No Mac support yet.')
        good = False

if not os.access(AppPathpointer, os.R_OK):
    poser.DialogSimple.MessageBox('Link to app was not set correctly.n Please re-run script.n If problem persists check manual.')
    good = False
else:
    with open(AppPathpointer, 'r') as app:
        AppPath = app.read()

#
#--------------------------------------------------------------------
# check if location of cache was saved already, if not ask and set up
# use PrefsLocation as a cunning way to get the User's name  
#--------------------------------------------------------------------
CachePathpointer  = os.path.join(poser.PrefsLocation(), 'RenderCacheLocation.txt')
if not os.access(CachePathpointer, os.R_OK):
    # make default location
    osflavor = platform.system()
    if osflavor in 'Windows': 
        parts = splitall(CachePathpointer)
        CachePathProposal = os.path.join(parts[0],parts[1],parts[2])   # we guess the first three parts of the path are C:users
        CachePathChooser = poser.DialogDirChooser(None, "Select location  of RenderCache", CachePathProposal)
        CachePathChooser.Show()
        CachePath = CachePathChooser.Path()
        if len(CachePath)> 0 :
            cache = open(CachePathpointer, 'w')
            cache.write(CachePath)
            cache.close()
        else:
            poser.DialogSimple.MessageBox('Selected folder not correct')
            good = False
    elif osflavor in 'Darwin': 
        poser.DialogSimple.MessageBox( 'Sorry No Mac support yet.')
        good = False

# 
#-------------------------------------------------
# should now have both app and folder
# Double-check and read final value of Cachepath
#-----------------------------------------------
if not os.access(CachePathpointer, os.R_OK):
    poser.DialogSimple.MessageBox('Link to cache was not set correctly.n Please re-run script.n If problem persists check manual.')
    good = False
else:
    with open(CachePathpointer, 'r') as cache:
        CachePath = cache.read()

#
#-------------------------------------------------
# finally all should be known. Process! 
#-------------------------------------------------
if good:
    #
    #------------------------------------------------
    # Get latest rendered image 
    # would need a parameter in Poser of course
    #------------------------------------------------
    list_of_files = glob.glob(os.path.join(CachePath,'*.exr'))
    if len(list_of_files) > 0: # user may have flushed the cache
        imagepath = max(list_of_files, key=os.path.getctime)
        if len(imagepath)>0: # who knows what else may have happened
            subprocess.call([AppPath, imagepath])

enjoy and thank you all who helped me thru.