ernestbofill / lego-image-dataset

Image dataset made of Lego parts synthetically generated from a 3D model. 10 parts with 6000 unique rendered images for each. A total of 60000 images
14 stars 2 forks source link

blender file shows error #1

Open bonylu opened 6 years ago

bonylu commented 6 years ago

Hi Ernest, I'm reading your master thesis, which is interesting and introduce your various trial runs. But when I open the .blend file, the software shows cannot open xxx file. My need is to use your python script to generate a lot of rendered images based on LDD/LDraw file automatically, including different angles, lights, etc. So could you provide your .py directly? Thanks

ernestbofill commented 6 years ago

Hi, I just opened the blender filed and copied the script. Here it is:

import bpy, sys, os, random, math

def get_override(area_type, region_type):
    for area in bpy.context.screen.areas: 
        if area.type == area_type:             
            for region in area.regions:                 
                if region.type == region_type:                    
                    override = {'area': area, 'region': region} 
                    return override
    #error message if the area or region wasn't found
    raise RuntimeError("Wasn't able to find", region_type," in area ", area_type,
                        "\n Make sure it's open while executing script.")

lights=['light_front', 'light_back', 'light_left', 'light_right', 'light_top', 'light_bottom']
numLights=len(lights)

random.seed()
for take in range(0,20):
    for i in range(0, numLights):
        bpy.data.materials[lights[i]].node_tree.nodes["Emission"].inputs[1].default_value = 2

    lightsToRandomise = random.randint(1,2)
    for i in range(0,lightsToRandomise):
        rndLight=random.randint(0,numLights-1)
        rndStrength=random.randint(1,20)
        bpy.data.materials[lights[rndLight]].node_tree.nodes["Emission"].inputs[1].default_value = rndStrength

    camera=bpy.data.objects['Camera']
    train=bpy.data.objects['Train']
    # ROTATE CAMERA
    camRotationX=math.radians(random.random()*360)
    camRotationY=math.radians(random.random()*360)
    camRotationZ=math.radians(random.random()*360)

    camera.select = True
    train.select = False

    #we need to override the context of our operator    
    override = get_override( 'VIEW_3D', 'WINDOW' )
    bpy.ops.transform.rotate(override, value=camRotationX, axis=(1,0,0))    
    bpy.ops.transform.rotate(override, value=camRotationY, axis=(0,1,0)) 
    bpy.ops.transform.rotate(override, value=camRotationZ, axis=(0,0,1)) 

    # ROTATE TRAIN
    camRotationX=math.radians(random.random()*360)
    camRotationY=math.radians(random.random()*360)
    camRotationZ=math.radians(random.random()*360)

    camera.select = False
    train.select = True

    # MOVE AND SCALE TRAIN
    scale = random.randint(80,120)/100  # +/- 20% scale
    for i in range(0,3):
        train.scale[i]=0.0001*scale
        delta_location=random.normalvariate(0, 0.08)
        #delta_location=(random.randint(0,6)-3)/10  # =/- 0.3 move
        train.location[i]=delta_location

    #we need to override the context of our operator    
    override = get_override( 'VIEW_3D', 'WINDOW' )
    bpy.ops.transform.rotate(override, value=camRotationX, axis=(1,0,0))    
    bpy.ops.transform.rotate(override, value=camRotationY, axis=(0,1,0)) 
    bpy.ops.transform.rotate(override, value=camRotationZ, axis=(0,0,1))     

    imagePath = '/Users/ernestbofill/Desktop/renders/take-' + str(take) + '.png'
    bpy.context.scene.render.filepath = imagePath
    # Render still image, automatically write to output path
    bpy.ops.render.render(write_still=True)