Is it possible to modify material properties defined via keyword snippet for multiple bodies at once
When using Workbench Dyna, if I have a complicated material model defined in a keyword snippet under a body (as per image), is it possible to re-use that material definition across many bodies without having to copy and paste the same snippet over and over? i.e. I want to change say the material density for all the keyword snippets under all the bodies at once.
Best Answer
-
You can take advantage of Python Code object to modify all the Keyword Snippets inserted under all bodies at once. The below script modifies the Density value for all snippets at once.
Please note that this is a generic example and this can be ofcourse further expanded to more material parameters and/or restrict to only specific Keyword snippets with further scripting.
Steps:
1. RMB on Analysis --> Insert --> Python Code
2. Change the "Target Callback" property to "After Object Changed"
3. Paste the below script under the "Script" tab (Python code object has two tabs. Script tab to create an automation script, Property Provider tab to create custom properties to input data)def after_object_changed(this, object_changed, property_name):# Do not edit this line """ Called after an object is changed. Keyword Arguments : this -- the datamodel object instance of the python code object you are currently editing in the tree object_changed -- The object that was changed property_name -- The property that was changed """ def convert_to_float(s): # Check if 'e' or 'E' is present for scientific notation if 'e' in s or 'E' in s: if 'e' in s: parts = s.split('e') if 'E' in s: parts = s.split('E') base = float(parts[0]) # If the base is an integer, format it as a float with .0 before 'e' if base.is_integer(): return "{:.1f}e{}".format(base, parts[1]) else: return s else: # Handle non-scientific numbers (no 'e') num = float(s) if num.is_integer(): return "{:.1f}".format(num) return s import re # To access properties created using the Property Provider, please use the following command. # this.GetCustomPropertyByPath("your_property_group_name/your_property_name") # To access scoping properties use the following to access geometry scoping and named selection respectively: # this.GetCustomPropertyByPath("your_property_group_name/your_property_name/Geometry Selection") # this.GetCustomPropertyByPath("your_property_group_name/your_property_name/Named Selection") #If decimal point is comma Density_Val = this.GetCustomPropertyByPath("Properties/Density").ValueString.Replace(",",".") #Density_Val = this.GetCustomPropertyByPath("Properties/Density").ValueString Density_Val_Float = convert_to_float(str(Density_Val)) if object_changed == this: keywordSnippetObjs = [child for child in DataModel.GetObjectsByType(DataModelObjectCategory.CommandSnippet) if child.Parent.DataModelObjectCategory == DataModelObjectCategory.Body] for keywordSnippetObj in keywordSnippetObjs: keywordText = keywordSnippetObj.Input lines = keywordText.splitlines() line_of_interest = lines[22].strip() numbers = line_of_interest.split() orig_density = numbers[1] keywordSnippetObj.Input = re.sub(orig_density, str(Density_Val_Float),keywordSnippetObj.Input)
- Replace the function "reload_properties" with the below script under "Property Provider" tab
def reload_props(): this.PropertyProvider = None # Create the property instance provider = Provider() # Create a group named Group 1. group = provider.AddGroup("Properties") # Create a property with control type Expression and a property with control type Double, and add it to the Group 1 double_prop = group.AddProperty("Density", Control.Double) # Connects the provider instance back to the object by setting the PropertyProvider member on this, 'this' being the # current instance of the Python Code object. this.PropertyProvider = provider
- RMB on Python Code --> Reload. This should show a new property under Python Code called Density.
- RMB on Python Code --> Connect
- Also go to WB --> Tools --> Options --> Mechanical --> "Reconnect Python Code Object when Mechanical is launched"
Now, once these steps are completed, try changing the Density property in Python Code. This should update all Keyword Snippets that are under Bodies automatically.
0