How can I dynamically update a list of options in a Python code or Python result?
I want a list of options to be actuated based on its content. Let say I am showing a list of coordinate systems to choose from. How can I make sure the list of options is the actual list of available CS?
Answers
-
Here's a solution, using 'GetOptionsCallback'. In the property provider, you define your option property like this:
options_prop = group.AddProperty("Coordinate systems", Control.Options) options_prop.GetOptionsCallback= get_options_handler
Then you add this definition to the main script:
def get_options_handler(prop): opt = {} num_opt=1 for comp in ExtAPI.DataModel.GetObjectsByType(DataModelObjectCategory.CoordinateSystem): opt[num_opt] = comp.Name num_opt+=1 return opt
2 -
Can you add a code-snippet example of what you mean?
2 -
Building on the script Pierre T provided, I modified it further to remove the small caveat (when you delete the system that is currently picked in the Python code, it will not invalidate it and show a number instead of the CS name.) associated with the previous solution.
In the property provider, you define your option property like this: In addition to using 'GetOptionsCallback', we would need to use 'IsValidCallback' as well.
options_prop = group.AddProperty("Coordinate systems", Control.Options) options_prop.GetOptionsCallback= get_options_handler options_prop.IsValidCallback = dropDownValid
Then you add these two function definitions to the main script:
def get_options_handler(prop): global opt try: numOptions_beforeRefresh = len(opt) except: ExtAPI.Log.WriteMessage("Opt is not yet defined") opt = {} num_opt=1 for comp in ExtAPI.DataModel.GetObjectsByType(DataModelObjectCategory.CoordinateSystem): opt[num_opt] = comp.Name num_opt+=1 if prop.Value != None: if str(prop.Value) > str(len(opt)): prop.Value = None if "numOptions_beforeRefresh" in locals(): if numOptions_beforeRefresh > len(opt): prop.Value = None return opt def dropDownValid(prop): if prop.Value==None: return False elif prop.ValueString.isnumeric() == True: return False else: return True
0