Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

With rigid body dynamics, it is often necessary to slightly change physical properties of an object. Good examples are random differences in mass or elasticity. Though the variations are sometimes very subtle it is a good idea to think about this possibility, because it adds some extra realism to a simulation. Changing the properties for a few dozens or even hundreds of nodes is not a fun job. Doing it once is already a hassle, but what if you have to present several simulations with different values? Here a script is the only solution!

 

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:

...

Now it is easy to loop through the individual elements of “userSelection” userSelection. A simple “for for … in …” loop will do the job. The process of checking whether rigid body dynamics is already activated or not should not be difficult, as it is simply an if-condition. Reading out the current “@ mass” value is also no mysteryIn case  an object is already an active rigid body, the script will change its mass. So the body of the script could look like this:

Code Block
themeEclipse
languagepython
linenumberstrue
userSelection = scene.getSelectedNodes()

for node in userSelection:
    rbdState = node.getParameter("Dynamics")

    if (rbdState != "RigidActive rigid body"):
        node.setParameter("Dynamics", "RigidActive 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
themeEclipse
languagepython
linenumberstrue
import random

percentVariation = 10
range     rangeMass        = (currentMass / 100) * (percentVariation / 2)
randomValue randomMass      = random.uniform(-rangerangeMass, rangerangeMass)
newMass          = currentMass + randomValuerandomMass
Info
Please note that the operation above might fail for very small mass settings! 

 

The last action is to print out a little message together with the time the script needed for applying the new mass value. Since this little program is a batch script it is not possible to use RealFlow’s simulation time. The scene.getCurrentTime() statements has no effect here, but fortunately Python provides a module called “time”. This module comes with Python’s standard distribution and should be installed by default. To access the specific clock() function a new notation is required:

...

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
themeEclipse
languagepython
linenumberstrue
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")

...

Code Block
themeEclipse
languagepython
scene.message("\nActionnProcess completedfinished...\nElapsed time: "+str(diffTime)+" seconds")

...

So the entire script looks like this:

...

Code Block
themeEclipse
languagepython
linenumberstrue
from time import *
import random

startTime         = clock()
percentVariation  = 10
userSelection     = scene.getSelectedNodes()

for node in userSelection:
    rbdState = node.getParameter("Dynamics")

    if (rbdState != "RigidActive rigid body"):
        node.setParameter("Dynamics", "RigidActive rigid body")
        currentMass = node.getParameter("@ mass")
        range   rangeMass     = (currentMass / 100) * (percentVariation / 2)
        randomValuerandomMass = random.uniform(-rangerangeMass, rangerangeMass)
   
    newMass     = currentMass + randomValuerandomMass
        node.setParameter("@ mass", newMass)

endTime  = clock()
diffTime = endTime - startTime
 
scene.message("\nProcess finshedfinished...\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.

 

Image Added