Forum: Poser Python Scripting


Subject: Useful Code Snippets

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


structure posted Thu, 27 June 2019 at 4:56 PM Forum Coordinator

So I think with the advent of a new dawn for poser, this forum could use a sticky thread where snakeheads can supply snippets of poser python code that others may find useful, or indeed, be able to improve upon. Let me start the ball Rolling - the following is a complete library that I have built / collected over the years ( there are more to follow ) Some of the code was written by others, edited by myself and is now here - for the use of all of you fellow coders should you choose to.


# -*- coding: UTF8 -*-
# Dialog Library

import os
import wx
import wx.lib.imagebrowser as imagebrowser


class dialogs:
    def choose(self, title="", prompt="", OptionList=[], parent=None):
        # prepare the dialog
        dialog = wx.SingleChoiceDialog(parent, prompt, title, OptionList)
        # ensure the list is not empty
        if not OptionList == []:
            # check the dialog response
            return dialog.GetStringSelection() if dialog.ShowModal() == wx.ID_OK else None

    def choosemulti(self, title="", prompt="", OptionList=[], parent=None):
        # prepare the dialog
        dialog = wx.MultiChoiceDialog(parent, prompt, title, OptionList)
        # check the dialog response
        if dialog.ShowModal() == wx.ID_OK:
            # get the selections indices
            selections = dialog.GetSelections()
            # get the selections strings
            strings = [OptionList[x] for x in selections]
            dialog.Destroy()
            return (selections, strings)
        else:
            return (False, False)

    def colordialog(self, event=None):
        dialog = wx.ColourDialog(None)
        dialog.GetColourData().SetChooseFull(True)
        return dialog.GetColourData() if dialog.ShowModal() == wx.ID_OK else False

    def getfile(self, start="", filemustexist=True, parent=None):
        styles = [(wx.FD_OPEN | wx.FD_FILE_MUST_EXIST), (wx.FD_OPEN)]
        style = styles[0] if filemustexist else styles[1]
        types = "Camera         (*.cm2 , *.cmz)|*.cm2; *.cmz|"                                     \
            "Figure         (*.cr2 , *.crz)|*.cr2; *.crz|"                                    \
            "Expression     (*.fc2 , *.fcz)|*.fc2; *.fcz|"                                    \
            "Hand           (*.hd2 , *.hdz)|*.hd2; *.hdz|"                                    \
            "Hair           (*.hr2 , *.hrz)|*.hr2; *.hrz|"                                    \
            "Light          (*.lt2 , *.ltz)|*.lt2; *.ltz|"                                    \
            "Material       (*.mt5 , *.mtz , *.mc6 , *.mcz)|*.mt5; *.mtz; *.mc6; *.mcz|"      \
            "Object         (*.obj , *.obz)|*.obj; *.obz|"                                    \
            "Pose           (*.pz2 , *.p2z)|*.pz2; *.p2z|"                                    \
            "Prop           (*.pp2 , *.ppz)|*.pp2; *.ppz|"                                    \
            "Python Script  (*.py , *.pyc , *.pyd)|*.py; *.pyc; *.pyd|"                       \
            "Scene          (*.pz3 , *.pzz)|*.pz3; *.pzz|"                                    \
            "all files        (*.*)|*.*|"                                             # File Types

        # prepare dialog
        dialog = wx.FileDialog(parent, "Select a File",
                               start, "", types, style)
        # test dialog response
        return dialog.GetPath() if dialog.ShowModal() == wx.ID_OK else False

    def getfolder(self, start="", newdirbutton=True, parent=None):
        styles = [(wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON),
                  (wx.DD_DEFAULT_STYLE)]
        style = styles[0] if newdirbutton else styles[1]
        # prepare dialog
        dialog = wx.DirDialog(None, 'Select a Folder ', start, style)
        # test dialog response
        return dialog.GetPath() if dialog.ShowModal() == wx.ID_OK else False

    def get_desktop_path(self, event=None):
        import re
        # create variable
        D_paths = list()
        try:
            fs = open(os.sep.join((os.path.expanduser("~"),
                      ".config", "user-dirs.dirs")), 'r')
            data = fs.read()
            fs.close()
        except:
            data = ""

        D_paths = re.findall(r'XDG_DESKTOP_DIR="([^"]*)', data)

        if len(D_paths) == 1:
            D_path = D_paths[0]
            D_path = re.sub(r'$HOME', os.path.expanduser("~"), D_path)
        else:
            D_path = os.sep.join((os.path.expanduser("~"), 'Desktop'))

        return D_path if os.path.isdir(D_path) else None

    def getfont(self, f):
        fontdialog = wx.FontDialog(self, wx.FontData())

        if fontdialog.ShowModal() == wx.ID_OK:
            data = fontdialog.GetFontData()
            font = data.GetChosenFont()
            # self.text.SetFont(font)
            fontdialog.Destroy()
            return font
        fontdialog.Destroy()

    def getnumberentry(self, alertmessage, prompt, caption, value, min, max, pos=wx.DefaultPosition):
        NE_dialog = wx.NumberEntryDialog(
            None, alertmessage, prompt, caption, value, min, max, pos)
        return NE_dialog.GetValue() if NE_dialog.ShowModal() == wx.ID_OK else False

    def gettextentry(self, title="", message="", parent=None):
        TE_dialog = wx.TextEntryDialog(
            parent, message, title, "", style=wx.OK)                 # none=parent
        TE_dialog.SetValue("")
        return TE_dialog.GetValue() if TE_dialog.ShowModal() == wx.ID_OK else False

    def imageviewer(self, parent=None):
        dialog = imagebrowser.ImageDialog(parent)
        if dialog.ShowModal() == wx.ID_OK:
            print("You Selected File: ")+dialog.GetFile()

    def okcancel(self, title="", alertmessage="", okonly=False, parent=None):
        styles = [(wx.OK | wx.ICON_INFORMATION | wx.STAY_ON_TOP),
                  (wx.OK | wx.CANCEL | wx.ICON_INFORMATION | wx.STAY_ON_TOP)]
        style = styles[0] if okonly else styles[1]
        ok_dialog = wx.MessageDialog(parent, alertmessage, title, style)
        return True if ok_dialog.ShowModal() == wx.ID_OK else False

    def showalert(self, title="", alertmessage="", icon=0, Btype=0, bType=0, parent=None):
        icons = {
            0:  wx.ICON_ERROR,
            1:  wx.ICON_EXCLAMATION,
            2:  wx.ICON_HAND,
            3:  wx.ICON_QUESTION,
            4:  wx.ICON_INFORMATION,
            5:  wx.ICON_NONE
        }                                                                             # set up icons

        buttonType = {
            0:  wx.OK,
            # Must be combined with either OK or YES_NO
            1:  wx.CANCEL,
            2:  wx.YES_NO,
            3:  wx.HELP,
            # Can only be used with YES_NO
            4:  wx.NO_DEFAULT,
            # This style is currently not supported (and ignored) in wxOSX.
            5:  wx.CANCEL_DEFAULT,
            # Can only be used with YES_NO
            6:  wx.YES_DEFAULT,
            7:  wx.OK_DEFAULT
        }                                                                     # set up buttons

        behaviourType = {
            0:  wx.STAY_ON_TOP,
            1:  wx.CENTRE
        }                                                                     # set up behaviour

        # select icon to display
        icon = icons[icon]
        # select buttons to display
        atype = buttonType[Btype]
        # select behaviour
        btype = behaviourType[bType]

        wx.MessageDialog(parent, alertmessage, title, style=atype |
                         icon | btype).ShowModal()  # Show alert message

    def yesno(self, title="", message="", parent=None):
        style = (wx.YES | wx.NO)
        # prepare dialog
        YN_dialog = wx.MessageDialog(parent, message, title, style)
        # test dialog response
        return True if YN_dialog.ShowModal() == wx.ID_YES else False

    def onBusy(self,  message):
        busyDialog = wx.BusyInfo(message)
        busyDialog = None

Locked Out