|
Script Coder |
|
Joined: May 07, 2014 Posts: 3975 Location: Canada
|
I think I made pretty good progress (other than funky ones that has "pads" which are more than 2) The below code generates code from result1.txt (mentioned in post post #3) typelines = set() def extract_distinct_types(content): types = set() # Split the content into lines lines = content.split('\n') for line in lines: # Find " type: " in the line index = line.find(" type: ") if index != -1: # Extract the type following " type: " type_start = index + len(" type: ") type_end = line.find(" ", type_start) # If no space is found, consider the type until the end of the line if type_end == -1: type_end = len(line) # Extract and add the type to the set item_type = line[type_start:type_end] currentsize = len(types) types.add(item_type) newsize = len(types) if (newsize>currentsize): typelines.add(line) return list(types)
with open("result1.txt", "r") as file: # Read the entire content of the file content = file.read() # Split the content by "=====" #SHOW DIFFERENT TYPES for US to deal with distinct_types = extract_distinct_types(content)
#print some header for code print ("#!/usr/bin/env python") print ("from gimpfu import *")
# example data of a section ============================================ # MAIN_NAME:svg:lighten # PADS:pads: aux input output # name: srgb type: boolean default: False sections = content.split("=====") print("#Distinct Types:", ",".join(distinct_types)) print("#Type Lines:", "\n#".join(typelines)) # Distinct Types: enum,pointer,color,double,string,seed,audiofragment,boolean,curve,path,uri,object,format,int,filepath # Type Lines: # name: space-name type: string default: sRGB # name: value type: double default: 0.00 minimum: -inf maximum: +inf ui-minimum: -1.00 ui-maximum: 1.00 ui-gamma: 1.00 ui-step-small: 0.00 ui-step-big: 0.10 ui-digits: 3 # name: background-color type: color default: rgba(0.000, 0.000, 0.000, 0.000) sensitive:! keep_surroundings role:color-secondary # name: buffer type: object # name: curve type: curve # name: uri type: uri default: # name: audio type: audiofragment # name: color-model type: enum # name: seed type: seed # name: subdivisions type: int default: 1 minimum: 0 maximum: 15 ui-minimum: 0 ui-maximum: 15 ui-gamma: 1.00 ui-step-small: 1 ui-step-big: 5 # name: buffer type: pointer # name: path type: filepath default: # name: d type: path # name: cpn-1-keep type: boolean default: False label:[color-model {rgb} : rgb-label, color-model {hsl} : hsl-label] rgb-label:Keep red component hsl-label:Keep hue component # name: input-format type: format #FUNCTION_NAME# #VARIABLES# to pass into FUNCTION_NAME #GIMP_MESSAGES# if there's errors or unhandled/unimplemented cases #GEGLSTRINGLINE# #gegl_graph_string="#GEGL_STRING#" #GEGLCALLLINE# #pdb.python_fu_dinasset_gegl_graph(image, layer, gegl_graph_string) #code_to_replaced_below code = '''#--------------------------------------------------------------------------------------------- def #FUNCTION_NAME#(image, layer, #VARIABLES#): #GIMP_MESSAGES# #GEGLSTRINGLINE# pdb.python_fu_dinasset_gegl_graph(image, layer, gegl_graph_string) register( "python_fu_#FUNCTION_NAME#", "GEGL Testing", "GEGL Testing", "Auto Author", "Auto Author", "Auto Generated Dated", "<Image>/Python-Fu/GEGL-Automated/#FUNCTION_NAME_MENU#", #Menu path "*", [ #PFVARIABLES# ], [], #FUNCTION_NAME#) '''
#now we generate code here for i in range(0,len(sections)): section = sections[i] lines = section.split("\n") #break each section into lines PFVARIABLES = [] VARIABLES = [] GIMP_MESSAGES = [] function_name = "" GEGLSTRING = "" for j in range(0,len(lines)): spaceitems = lines[j].strip().replace(" "," ").split(" ") #replace double spaces with single spaces colonitems = lines[j].strip().replace(" "," ").split(":") if colonitems[0] == "MAIN_NAME": function_name = colonitems[1] + "_" + colonitems[2].replace("-","_") #so that MAIN_NAME:svg:lighten becomes "svg_lighten" GEGLSTRING += colonitems[1] + ":" + colonitems[2] + " " #print("MAIN_NAME FOUND" + function_name + "=>"+ GEGLSTRING) if spaceitems[0] == "PADS:pads:": pads = spaceitems[1:] #so that PADS:pads: aux input output becomes ["aux",input","output"] if len(pads) > 2: GIMP_MESSAGES.append(" pdb.gimp_message('Not Implemented for pads:"+",".join(pads)+"')"); if spaceitems[0] == "name:": #parse variables name and types var_name = spaceitems[1] #Set variable name safe_var_name = var_name.replace('-', '_') #so python doesn't blow up #name: space-name type: string default: sRGB if spaceitems[3] == "string": try: default_value = spaceitems[5] except Exception as e: #if this fails we fall back on just minimum as maximum default_value = "" PFVARIABLES.append(' (PF_STRING, "'+safe_var_name+'", "'+var_name+':", "'+default_value+'")') GEGLSTRING += var_name + "=" + "%"+"s " VARIABLES.append(safe_var_name) #name: value type: double default: 0.00 minimum: -inf maximum: +inf ui-minimum: -1.00 ui-maximum: 1.00 ui-gamma: 1.00 ui-step-small: 0.00 ui-step-big: 0.10 ui-digits: 3 elif spaceitems[3] == "double": default_value = float(spaceitems[5]) try: ui_minimum = spaceitems[11] ui_maximum = spaceitems[13] except Exception as e: #if this fails we fall back on just minimum as maximum ui_minimum = spaceitems[7] ui_maximum = spaceitems[9] if ui_minimum == '-inf': ui_minimum = -10000 else: ui_minimum = float(ui_minimum) if ui_maximum == '+inf': ui_maximum = 10000 else: ui_maximum = float(ui_maximum) PFVARIABLES.append(' (PF_SPINNER, "'+safe_var_name+'", "'+var_name+':", '+str(default_value)+', ('+str(ui_minimum)+', '+str(ui_maximum)+', 0.1))') GEGLSTRING += var_name + "=" + "%"+"f " VARIABLES.append(safe_var_name)
#name: background-color type: color default: rgba(0.000, 0.000, 0.000, 0.000) sensitive:! keep_surroundings role:color-secondary #name: buffer type: object #name: curve type: curve #name: uri type: uri default: #name: audio type: audiofragment #name: color-model type: enum #name: seed type: seed #name: subdivisions type: int default: 1 minimum: 0 maximum: 15 ui-minimum: 0 ui-maximum: 15 ui-gamma: 1.00 ui-step-small: 1 ui-step-big: 5 elif spaceitems[3] == "int": default_value = int(spaceitems[5]) try: ui_minimum = spaceitems[11] ui_maximum = spaceitems[13] except Exception as e: #if this fails we fall back on just minimum as maximum ui_minimum = spaceitems[7] ui_maximum = spaceitems[9] if ui_minimum == '-inf': ui_minimum = -10000 else: ui_minimum = int(ui_minimum) if ui_maximum == '+inf': ui_maximum = 10000 else: ui_maximum = int(ui_maximum) PFVARIABLES.append(' (PF_SPINNER, "'+safe_var_name+'", "'+var_name+':", '+str(default_value)+', ('+str(ui_minimum)+', '+str(ui_maximum)+', 1))') GEGLSTRING += var_name + "=" + "%"+"d " VARIABLES.append(safe_var_name) #name: buffer type: pointer #name: path type: filepath default: #name: d type: path #name: cpn-1-keep type: boolean default: False label:[color-model {rgb} : rgb-label, color-model {hsl} : hsl-label] rgb-label:Keep red component hsl-label:Keep hue component elif spaceitems[3] == "boolean": default_value = bool(spaceitems[5]) PFVARIABLES.append(' (PF_TOGGLE, "'+safe_var_name+'","'+var_name+':",'+str(default_value)+')') GEGLSTRING += var_name + "=" + "%"+"d " VARIABLES.append(safe_var_name) #name: input-format type: format else: GIMP_MESSAGES.append(" pdb.gimp_message('Not Implemented for type:"+spaceitems[3]+"')"); #TODO SHOW MESSAGE that it's not yet implemented VARIABLES = ",".join(VARIABLES) GEGLSTRINGLINE = ('gegl_graph_string="%s' % GEGLSTRING) + '" % (' + VARIABLES + ')'; FUNCTION_NAME_MENU = function_name + "..." FUNCTION_NAME = function_name # print(VARIABLES) # print(GEGLSTRINGLINE) # print(FUNCTION_NAME) # print(FUNCTION_NAME_MENU) # print(PFVARIABLES) # Code Replacement here if function_name != "" and len(GIMP_MESSAGES) == 0: #only print some code if we actually have a function_name and there's no GIMP_MESSAGES to report newcode = code newcode = newcode.replace("#FUNCTION_NAME#",FUNCTION_NAME) newcode = newcode.replace("#FUNCTION_NAME_MENU#",FUNCTION_NAME_MENU) newcode = newcode.replace("#VARIABLES#",VARIABLES) newcode = newcode.replace("#GEGLSTRINGLINE#",GEGLSTRINGLINE) newcode = newcode.replace("#PFVARIABLES#",",\n".join(PFVARIABLES)) newcode = newcode.replace("#GIMP_MESSAGES#","\n".join(GIMP_MESSAGES)) print(newcode) #call main to close off the code print("main()") #(PF_INT, "p0", "_INT:", 0), # PF_INT8, PF_INT16, PF_INT32 similar but no difference in Python. # (PF_FLOAT, "p02", "_FLOAT:", 3.141), # (PF_STRING, "p03", "_STRING:", "foo"), # alias PF_VALUE # (PF_TEXT, "p04", "TEXT:", "bar"), # # PF_VALUE # # Pick one from set of choices # (PF_OPTION,"p1", "OPTION:", 0, ["0th","1st","2nd"]), # initially 0th is choice # (PF_RADIO, "p16", "RADIO:", 0, (("0th", 1),("1st",0))), # note bool indicates initial setting of buttons # # PF_RADIO is usually called a radio button group. # # SLIDER, ADJUSTMENT types require the extra parameter of the form (min, max, step). # (PF_TOGGLE, "p2", "TOGGLE:", 1), # initially True, checked. Alias PF_BOOL # # PF_TOGGLE is usually called a checkbox. # (PF_SLIDER, "p3", "SLIDER:", 0, (0, 100, 10)), # (PF_SPINNER, "p4", "SPINNER:", 21, (1, 1000, 50)), # alias PF_ADJUSTMENT # # Pickers ie combo boxes ie choosers from lists of existing Gimp objects # (PF_COLOR, "p14", "_COLOR:", (100, 21, 40) ), # extra param is RGB triple # # PF_COLOUR is an alias by aussie PyGimp author lol # (PF_IMAGE, "p15", "IMAGE:", None), # should be type gimp.image, but None works # (PF_FONT, "p17", "FONT:", 0), # (PF_FILE, "p18", "FILE:", 0), # (PF_BRUSH, "p19", "BRUSH:", 0), # (PF_PATTERN, "p20", "PATTERN:", 0), # (PF_GRADIENT, "p21", "GRADIENT:", 0), # (PF_PALETTE, "p22", "PALETTE:", 0), # (PF_LAYER, "p23", "LAYER:", None), # (PF_CHANNEL, "p24", "CHANNEL:", None), # ??? Usually empty, I don't know why. # (PF_DRAWABLE, "p25", "DRAWABLE:", None), # # Mostly undocumented, but work # (PF_VECTORS, "p26", "VECTORS:", None), # (PF_FILENAME, "p27", "FILENAME:", 0), # (PF_DIRNAME, "p28", "DIRNAME:", 0) # # PF_REGION might work but probably of little use. See gimpfu.py.
attached file is the generated py code to expose gegl functions. There are right now 72 exposed. If I learn more in the future about how to map params and deal with other "pads" I'll add changes to above code to generate more. When I generated all of them an put gimp messages to say "Not Yet Implemented" I ran into an issue when the file is too large GIMP wouldn't load so I had to break it into 2 files about 3000 lines each then GIMP was happy with it (I don't know what the real limit is though). But yeah this one is small enough to have in one file that exposes all 72 functions basically any functions that uses "string","int","double","boolean" and has pads 2 or less pads. Oh yeah you have to look at the first post and uncomment dinnaset register call (that's currently being commented out) because this uses that. See friendlier labelled version (of gegl-python-fu.zip)
Attachments: |
File comment: uncommented code that paynekj wrote that this auto generated code uses.
dinasset_gegl.zip [1.41 KiB]
Downloaded 84 times
|
File comment: auto generated code that exposes 72 gegl functions to python-fu (also see friendly labelled version)
gegl-python-fu (2).zip [6.73 KiB]
Downloaded 72 times
|
_________________ TinT
Last edited by trandoductin on Mon Nov 06, 2023 8:12 pm, edited 2 times in total.
|
|