I need to create a terrain that moves under a vehicle, but some small meshes that appear on the terraine as it rolls must appear randomly. How could that be done or where should I start looking to find a solution?
1 Answer
You might find this useful. I used a script to step through the objects in a collection named "terrain obstacles" where I want the Visibility::Render to be enabled and disabled on stable random frames (frame numbers set up at random before rendering begins):
I then used a script that, for each object in the collection, add a scripted driver function that is based on the appear and disappear values:
The script I include in my file:
###
### initalize all "terrain obstacles" with a render driver based on random start and end frames
###
import bpy
import mathutils.noise as noise
for o in bpy.data.collections['terrain obstacles'].all_objects:
frameAppear = int(noise.random() * (bpy.context.scene.frame_end / 2))
# be sure that value for 'frame disappear' > 'frame appear'
frameDisappear = int(frameAppear + (noise.random() * (bpy.context.scene.frame_end / 4)))
#
# add a driver to the 'hide_render' property
print("adding driver to 'hide_render' for object", o.name )
try:
o.driver_remove('hide_render')
except:
pass # ignore possible exception
d = o.driver_add('hide_render')
v = d.driver.variables.new()
v.name = 'frameNumb'
t = v.targets[0]
t.id_type = 'SCENE'
t.id = bpy.context.scene
t.data_path='frame_current'
d.driver.type = 'SCRIPTED'
d.driver.expression = 'False if ((frameNumb > '+str(frameAppear)+') and (frameNumb < '+str(frameDisappear)+')) else True'
If you run the script again, it will change the appear and disappear times of the objects in the collection. You might want to adjust the formula I used to get the appear and disappear numbers. Note that the expression seems backwards (true/false logic): that's because the setting is hide render, not render.