Last active
July 22, 2022 16:41
-
-
Save 5agado/9478e9ee0445b41ae754460b028d3ba6 to your computer and use it in GitHub Desktop.
Synthetic-Data-Generation in Blender
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
from numpy.random import uniform | |
def rand_camera_loc(camera, xloc_range, yloc_range, zloc_range): | |
""" Randomize camera location for each axis based on the provided ranges """ | |
new_loc = camera.location | |
new_loc[0] = uniform(low=xloc_range[0], high=xloc_range[1]) | |
new_loc[1] = uniform(low=yloc_range[0], high=yloc_range[1]) | |
new_loc[2] = uniform(low=zloc_range[0], high=zloc_range[1]) | |
camera.location = new_loc | |
camera = bpy.data.objects['Camera'] # target camera to use | |
rand_camera_loc(camera, [-1,1], [-1,1], [-0.5,0.5]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy # Blender specific import | |
import numpy as np | |
from datetime import datetime | |
def randomize_colors(material, color_nodes_names): | |
""" Randomize RGB nodes for the given Blender material """ | |
# apply colors in target material | |
# naive way to generate multiple colors (as there is no guarantee of enough difference) | |
for i, color_nodes_name in enumerate(color_nodes_names): | |
rand_rgb = np.random.uniform(low=0., high=1., size=3) | |
rand_rgb = np.append(rand_rgb, 1.) | |
material.node_tree.nodes[color_nodes_name].outputs[0].default_value = tuple(rand_rgb) | |
def randomize_plain_pattern(): | |
material = bpy.data.materials["plain"] | |
randomize_colors(material, ["RGB"]) | |
def randomize_striped_pattern(): | |
material = bpy.data.materials["striped"] | |
wave_texture = material.node_tree.nodes["Wave Texture"] | |
wave_texture.inputs[1].default_value = np.random.uniform(low=2, high=12) # rand scale | |
wave_texture.bands_direction = np.random.choice(['Y', 'X']) # rand bands direction | |
randomize_colors(material, ["RGB", "RGB.001"]) | |
def randomize_floral_pattern(texture_img_paths): | |
material = bpy.data.materials["floral"] | |
# load random texture image | |
image = bpy.data.images.load(filepath=str(np.random.choice(texture_img_paths))) | |
material.node_tree.nodes["Image Texture"].image = image | |
# rand texture scale | |
rand_scale = np.random.uniform(low=1.5, high=8) | |
material.node_tree.nodes["Mapping"].inputs[3].default_value[0] = rand_scale | |
material.node_tree.nodes["Mapping"].inputs[3].default_value[1] = rand_scale | |
material.node_tree.nodes["Mapping"].inputs[3].default_value[2] = rand_scale | |
randomize_colors(material, ["RGB"]) | |
randomize_plain_pattern() | |
randomize_striped_pattern() | |
randomize_floral_pattern(['list', 'of', 'floral', 'image-paths']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
import numpy as np | |
from numpy.random import uniform | |
def randomize_world(bg_strength_range): | |
""" Randomize World settings """ | |
world = bpy.data.worlds["World"].node_tree | |
# random world color value | |
world_rgba = uniform(low=0, high=1, size=3) | |
world_rgba = np.append(world_rgba, 1.) | |
world.nodes["RGB"].outputs[0].default_value = world_rgba | |
# random world background strength | |
rand_bg = uniform(low=bg_strength_range[0], high=bg_strength_range[1]) | |
world.nodes["Background"].inputs[1].default_value = rand_bg | |
randomize_world(bg_strength_range=[0.2, 0.4]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
from datetime import datetime | |
def render_still(filepath: str, file_format='PNG'): | |
""" Render a still frame for the current Blender scene """ | |
bpy.context.scene.render.filepath = filepath | |
bpy.context.scene.render.image_settings.file_format = file_format | |
bpy.ops.render.render(write_still=True) | |
render_still(f"{datetime.now().strftime('%Y%m%d_%H%M')}.png")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment