...
Name | ChangeRBDMass.rfs |
Type | Batch script |
Description | This program automatically activates the rigid body property for a custom selection of nodes and randomly changes the "@ mass" parameter. |
What the script should do:
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
userSelection = scene.getSelectedNodes() for node in userSelection: rbdState = node.getParameter("Dynamics") if (rbdState != "Rigid body"): node.setParameter("Dynamics", "Rigid body") currentMass = node.getParameter("@ mass") |
...
The core function of this script is to apply a certain amount of randomness. This value should be within a given range based on the original “@ mass” setting, e.g. vary the current mass within 10% of the current value. Let’s say the initial mass is 100 for each object. This means that the new mass should be somewhere between 95 and 105. The statement for this operation uses the random module and actually the code should already look familiar to you:
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
import random percentVariation = 10 range = (currentMass / 100) * (percentVariation / 2) randomValue = random.uniform(-range, range) newMass = currentMass + randomValue |
...
Here you can see a different notation for the "import" command. If you would like to learn more about advanced techniques to load modules, we suggest that you do some research online. The clock() function from this module simply measures and stores the current time during function call. Keeping this in mind it is easy to create a time difference:
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
from time import * startTime = clock() ... go through the selected nodes and calculate the new mass values here stopTime = clock() diffTime = stopTime - startTime scene.message("Elapsed time: "+str(diffTime)+" seconds") |
...
So the entire script looks like this:
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
from time import * import random startTime = clock() percentVariation = 10 userSelection = scene.getSelectedNodes() for node in userSelection: rbdState = node.getParameter("Dynamics") if (rbdState != "Rigid body"): node.setParameter("Dynamics", "Rigid body") currentMass = node.getParameter("@ mass") range = (currentMass / 100) * (percentVariation / 2) randomValue = random.uniform(-range, range) newMass = currentMass + randomValue node.setParameter("@ mass", newMass) endTime = clock() diffTime = endTime - startTime scene.message("\nProcess finshed...\nElapsed time: "+str(diffTime)+" seconds") |
...
You can extend this script to perform more than one parameter change or add a nice little GUI. With ChangeRBDMass.rfs, a simulation looks much better, because the different masses cause “instabilities”, forcing the bodies to act in a different way and the result looks moch more vivid. The example below shows a fixed mass of 1,000, the second uses a “percentVariation” value of 25.