Created
February 25, 2021 15:22
-
-
Save LuanAdemi/6aac83f06d8d4394abc22e450af18a41 to your computer and use it in GitHub Desktop.
A script for rendering random photo-realistic go boards using 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
# A python script for rendering random photorealistic go board setups with domain randomization in blender 2.8.X. | |
# This includes the randomization of the current board position, camera angle, lighting (via hdri and a sun object), board material, | |
# surface material and random probs on the surface. | |
# Copyright © 2021 Luan Ademi | |
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), | |
# to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
# and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | |
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
import random | |
import bpy | |
import numpy as np | |
from numpy import savetxt | |
######## Domain Randomization ######## | |
# TODOs: | |
# implement random lighting and camera angles (Done) | |
# implement random material change (Done) | |
# implement random probs on the table (0/1) | |
# NOTES: | |
# Might need to transpose grountTruth according to the camera position-> orientation problem? | |
######################################## | |
# the go stone template objects, which we will copy | |
blackStoneTemplate = bpy.data.objects['BlackStoneTemplate'] | |
whiteStoneTemplate = bpy.data.objects['WhiteStoneTemplate'] | |
# our objects we want to randomize | |
ground = bpy.data.objects['Ground'] | |
board = bpy.data.objects['Board'] | |
world = bpy.data.worlds.get("World.001") | |
camera = bpy.data.objects['Camera'] | |
# store the current materials, hdris and positions | |
baseGroundMaterial = ground.active_material | |
baseBoardMaterial = board.active_material | |
baseHdri = world.node_tree.nodes['Environment Texture'].image | |
baseCameraPoition = camera.location | |
# random materials | |
groundMaterials = ["Plywood","Kitchenwood","Fabric34","Fabric31","Fabric42","Leather27","Marble6","Plastic6","Wood23","Wood24","Wood34","Wood51"] | |
boardMaterials = ["Wood23","Wood24","Wood34"] | |
# random lighting | |
hdris = ["veranda_2k.hdr","st_fagans_interior_2k.hdr","stone_alley_03_2k.exr","ulmer_muenster_2k.hdr","lythwood_room_2k.hdr","lilienstein_2k.hdr"] | |
# an array for all generated stone objects | |
stones = [] | |
groundTruth = np.zeros((13,13)) | |
# @procedure | |
# Creates a copy of the whiteStoneTemplate object at the given board coordinate | |
def addWhiteStone(x,y): | |
stone = whiteStoneTemplate.copy() | |
stone.data = whiteStoneTemplate.data.copy() | |
stone.name = f"WhiteStone_{x}_{y}" | |
stone.location = (0.99-(x*0.165), -0.99+(y*0.165), 0.0627) | |
stone.animation_data_clear() | |
bpy.data.collections['Stones'].objects.link(stone) | |
stones.append(stone) | |
# @procedure | |
# Creates a copy of the blackStoneTemplate object at the given board coordinate | |
def addBlackStone(x,y): | |
stone = blackStoneTemplate.copy() | |
stone.data = blackStoneTemplate.data.copy() | |
stone.name = f"BlackStone_{x}_{y}" | |
stone.location = (0.99-(x*0.165), -0.99+(y*0.165), 0.0627) | |
stone.animation_data_clear() | |
bpy.data.collections['Stones'].objects.link(stone) | |
stones.append(stone) | |
# @function | |
# Returns a random camera position on an orbit around the board | |
def randomizeCameraPosition(): | |
r = random.random() * 3 | |
angle = 1+ random.random()*1.2 | |
x = np.cos(angle)*r; | |
y = np.sin(angle)*r; | |
z = 2.5+ random.random() * 3 | |
return (x, y, z) | |
# @procedure | |
# Removes all generated stones from the board | |
def clearBoard(): | |
for s in stones: | |
bpy.data.objects.remove(s, do_unlink = True) | |
del stones[:] | |
# @procedure | |
# Resets the current scene to it's initial state | |
def resetScene(): | |
clearBoard() | |
bpy.data.objects['Ground'].active_material = baseGroundMaterial | |
bpy.data.objects['Board'].active_material = baseBoardMaterial | |
world.node_tree.nodes['Environment Texture'].image = baseHdri | |
camera.location = baseCameraPoition | |
# @procedure | |
# Generates a random board. | |
def randomBoard(): | |
# pick a random ground material | |
ground.active_material = bpy.data.materials[np.random.choice(groundMaterials)] | |
# pick a random board material | |
board.active_material = bpy.data.materials[np.random.choice(boardMaterials)] | |
# random hdr environment | |
world.node_tree.nodes['Environment Texture'].image = bpy.data.images[np.random.choice(hdris)] | |
# randomize camera position | |
camera.location = randomizeCameraPosition() | |
# random stone placement | |
for x in range(13): | |
for y in range(13): | |
r = random.randint(0, 2) | |
if r == 0: | |
groundTruth[x][y] = 0 | |
continue | |
elif r == 1: | |
addBlackStone(x,y) | |
groundTruth[x][y] = -1 | |
elif r == 2: | |
addWhiteStone(x,y) | |
groundTruth[x][y] = 1 | |
# @procedure | |
# Renders the current scene to a file | |
def render(filename): | |
sceneKey = bpy.data.scenes.keys()[0] | |
bpy.data.scenes[sceneKey].render.filepath = filename + ".jpg" | |
bpy.ops.render.render(write_still = True) | |
savetxt(filename + ".csv", groundTruth, delimiter=',') | |
for i in range(100, 200): | |
randomBoard() | |
render("/home/luan/Schreibtisch/Programmieren/ML-Workshop/goBoards/board_"+str(i)) | |
resetScene() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment