Forum: Poser Python Scripting


Subject: Useful Code Snippets

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


structure posted Fri, 14 August 2020 at 12:35 PM Forum Coordinator

call a function using a string as a variable.

for i in range( 0, 5 ):
    FUNCTIONNAME = str( i )
    getattr(globals()['CLASSNAME'](), FUNCTIONNAME )( args )
example script


from __future__ import print_function
import inspect
functionlist = []

class stringclass:
    def stripstring( self, i ):
        i=str(i).split(",")[0]
        return i.translate(None, "()'")

class materialclass:
    def get_all_scene_materials( self, item = None ):
        print("You are in the {} function".format( item ) )
        return

    def get_item_materials( self, item = None ):
        print("You are in the {} function".format( item ) )
        return

for i in (inspect.getmembers(materialclass, predicate=inspect.ismethod ) ):
    i = stringclass().stripstring( i )
    functionlist.append( i )

for f in functionlist:
    getattr(globals()['materialclass'](), f )( f ) # the 2nd f can be any argument


output

You are in the get_all_scene_materials function

You are in the get_item_materials function

Locked Out