Sat, Feb 8, 11:59 AM CST

Renderosity Forums / Poser Python Scripting



Welcome to the Poser Python Scripting Forum

Forum Moderators: Staff

Poser Python Scripting F.A.Q (Last Updated: 2025 Feb 05 6:41 am)

We now have a ProPack Section in the Poser FreeStuff.
Check out the new Poser Python Wish List thread. If you have an idea for a script, jot it down and maybe someone can write it. If you're looking to write a script, check out this thread for useful suggestions.

Also, check out the official Python site for interpreters, sample code, applications, cool links and debuggers. This is THE central site for Python.

You can now attach text files to your posts to pass around scripts. Just attach the script as a txt file like you would a jpg or gif. Since the forum will use a random name for the file in the link, you should give instructions on what the file name should be and where to install it. Its a good idea to usually put that info right in the script file as well.

Checkout the Renderosity MarketPlace - Your source for digital art content!



Subject: Simple return value bug in poser.DialogSimple.AskMenu()


3dcheapskate ( ) posted Sat, 26 September 2020 at 10:04 PM · edited Sat, 08 February 2025 at 11:51 AM

According to the Poser 11 PoserPython manual AskMenu returns a StringType.

This is correct, EXCEPT if you click the 'Cancel' button on the dialogue, when it returns a NoneType.

To catch that I found I had to cast (if that's the correct Pythonspeak) the return value as a string, which appears to convert NoneType to "None".


        ...
        action = ""
        while action == "":
            # According to the Poser 11 PoserPython manual AskMenu() returns .
            # If you select an option from the list and hit 'OK' it returns that option as a string.
            # If you hit 'OK' with 'None' selected it returns "".
            # BUT if you hit 'Cancel' it actually returns , so I have to cast the return value as a string just in case (it will be "None" for )
            action = poser.DialogSimple.AskMenu("Multiple 'SmartPlusData.txt' files found","Select which to use:",foundfilelist)
        locatedfile = str(action)
        ...

I don't recall having this problem with PP2014, although it's possible I never tested clicking the Cancel button from an AskMenu

(N.B. this darn forum s**tware removed anything between (and including) less-than and greater-than symbols, so the types in my code extract disappeared, despite being within a triple backtick block )`


The 3Dcheapskate* occasionally posts sensible stuff. Usually by accident.
And it usually uses Poser 11, with units set to inches. Except when it's using Poser 6 or PP2014, or when its units are set to PNU.

*also available in ShareCG, DAZ, and HiveWire3D flavours (the DeviantArt and CGBytes flavour have been discontinued).



structure ( ) posted Sat, 26 September 2020 at 10:13 PM · edited Sat, 26 September 2020 at 10:18 PM
Forum Coordinator

you could try

def askmenu( foundfilelist ) :
    return poser.DialogSimple.AskMenu("Multiple 'SmartPlusData.txt' files found","Select which to use:",foundfilelist) else False

if askmenu( filelist ): 
    do something

***UNTESTED***

or an alternative

import wx
class dialogs:
    def choose( self, title = "", prompt = "", OptionList = [], parent = None ):

        dialog = wx.SingleChoiceDialog( parent, prompt, title, OptionList )
        
        if not OptionList == []:

            return dialog.GetStringSelection() if dialog.ShowModal() == wx.ID_OK else False

you can choose to return anything if the cancel button is pressed ,

e.g.

return dialog.GetStringSelection() if dialog.ShowModal() == wx.ID_OK else "NOTHING"

Locked Out


3dcheapskate ( ) posted Sat, 26 September 2020 at 10:26 PM

Yes indeed, there are many ways to get round it, but you shouldn't need to as it's supposed to return a StringType.

Hence 'bug'.

This looks like sloppy coding of AskMenu() to me - trust me, I'm an expert on sloppy coding... ;o)


The 3Dcheapskate* occasionally posts sensible stuff. Usually by accident.
And it usually uses Poser 11, with units set to inches. Except when it's using Poser 6 or PP2014, or when its units are set to PNU.

*also available in ShareCG, DAZ, and HiveWire3D flavours (the DeviantArt and CGBytes flavour have been discontinued).



3dcheapskate ( ) posted Sat, 26 September 2020 at 10:45 PM · edited Sat, 26 September 2020 at 10:48 PM

However, I like your alternative workarounds better than my own, and this seems to get round the bug nicely for me:

       action = ""
        while action == "":
            action = poser.DialogSimple.AskMenu("Multiple 'SmartPlusData.txt' files found","Select which to use:",foundfilelist)
            # Handle NoneType return value if Cancel button pressed
            if action:
                locatedfile = action
            else:
               locatedfile = "CANCEL"
            print "TEST RESULT = "+locatedfile

P.S. I don't use wxPython - far too complicated for my decaying brain ! So I'm stuck with PoserPython's built in dialogues


The 3Dcheapskate* occasionally posts sensible stuff. Usually by accident.
And it usually uses Poser 11, with units set to inches. Except when it's using Poser 6 or PP2014, or when its units are set to PNU.

*also available in ShareCG, DAZ, and HiveWire3D flavours (the DeviantArt and CGBytes flavour have been discontinued).



adp001 ( ) posted Sun, 27 September 2020 at 1:31 AM · edited Sun, 27 September 2020 at 1:33 AM

Returning None is the correct Python way if nothing is selected (canceled). Returning nothing is different than returning an empty string.

result = poser.DialogSimple.AskMenu(...)
if result:
   do_something()
else:
    forget_it()

In Python None is assumed as False.

for item in emptylist or listwithcontent:
    print(item_from_listwithcontent)




3dcheapskate ( ) posted Sun, 27 September 2020 at 4:30 AM · edited Sun, 27 September 2020 at 4:31 AM

In that case it's probably not a bug then, just my lack of understanding of Python :)

So AskMenu() returns nothing (NoneType) if you click 'Cancel', but returns an empty/null string (StringType, "") if you click 'OK' with the AskMenu dialogue's default 'None' option, as shown below.

None.jpg

Thanks - that makes some sort of sense now.


The 3Dcheapskate* occasionally posts sensible stuff. Usually by accident.
And it usually uses Poser 11, with units set to inches. Except when it's using Poser 6 or PP2014, or when its units are set to PNU.

*also available in ShareCG, DAZ, and HiveWire3D flavours (the DeviantArt and CGBytes flavour have been discontinued).



an0malaus ( ) posted Sat, 03 October 2020 at 7:48 AM

It's also worth noting that BOTH None and '' (empty string) evaluate to False in a boolean expression. That provides a simple way to distinguish between a useful returned string to be parsed (any non-empty string will evaluate as True) and a non-selection ('' OK) or cancellation (None). Python design has its own logic, and it takes some getting used to when coming from other languages.

Similarly with lots of other Poser Python API methods which can return None instead of an empty list. As in adp's second example, if a method could return None instead of an empty list, make use of None evaluating as False, and iterate over a list conditional like:

for item in ListMethodThatMayReturnNone() or []: # The " or []" CAN be iterated over without error, while None cannot
    pass



My ShareCG Stuff

Verbosity: Profusely promulgating Graham's number epics of complete and utter verbiage by the metric monkey barrel.


Privacy Notice

This site uses cookies to deliver the best experience. Our own cookies make user accounts and other features possible. Third-party cookies are used to display relevant ads and to analyze how Renderosity is used. By using our site, you acknowledge that you have read and understood our Terms of Service, including our Cookie Policy and our Privacy Policy.