示例#1
0
def pointLight():
    world = bpy.data.worlds['World']
    world.use_nodes = True
    wnodes = world.node_tree.nodes
    bg_node = wnodes['Background']
    bg_node.inputs[1].default_value = 0

    d = random.uniform(3, 5)
    litpos = Vector(config["litpos"])
    eul = Euler((0, 0, 0), 'XYZ')
    eul.rotate_axis('Z', config["litEulerZ"])
    eul.rotate_axis('X', config["litEulerX"])
    litpos.rotate(eul)

    bpy.ops.object.add(type='LIGHT', location=litpos)
    lamp = bpy.data.lights[0]
    lamp.use_nodes = True
    nodes = lamp.node_tree.nodes
    links = lamp.node_tree.links
    for node in nodes:
        if node.type == 'OUTPUT':
            output_node = node
        elif node.type == 'EMISSION':
            lamp_node = node
    strngth = config["litStr"]
    lamp_node.inputs[1].default_value = strngth
    # Change warmness of light to simulate more natural lighting
    bbody = nodes.new(type='ShaderNodeBlackbody')
    color_temp = config["litColorTemp"]
    bbody.inputs[0].default_value = color_temp
    links.new(bbody.outputs[0], lamp_node.inputs[0])
示例#2
0
def add_lighting():
    world=bpy.data.worlds['World']
    world.use_nodes = True
    wnodes=world.node_tree.nodes
    wlinks=world.node_tree.links
    bg_node=wnodes['Background']
    # hdr lighting
    # remove old node
    for node in wnodes:
        if node.type in ['OUTPUT_WORLD', 'BACKGROUND']:
            continue
        else:
            wnodes.remove(node)
    # hdr world lighting
    if random.random() > 0.3:
        texcoord = wnodes.new(type='ShaderNodeTexCoord')
        mapping = wnodes.new(type='ShaderNodeMapping')
        # print( 'mapping : ',  mapping.rotation[2])
        mapping.rotation[2] = random.uniform(0, 6.28)
        wlinks.new(texcoord.outputs[0], mapping.inputs[0])
        envnode=wnodes.new(type='ShaderNodeTexEnvironment')
        wlinks.new(mapping.outputs[0], envnode.inputs[0])
        idx = random.randint(0, len(envlist) - 1)
        envp = envlist[idx]
        envnode.image = bpy.data.images.load(envp[0])
        envstr = int(envp[1])
        bg_node.inputs[1].default_value=random.uniform(0.4 * envstr, 0.6 * envstr)
        wlinks.new(envnode.outputs[0], bg_node.inputs[0])
    else:
        # point light
        bg_node.inputs[1].default_value=0

        d = random.uniform(3, 5)
        litpos = Vector((0, d, 0))
        eul = Euler((0, 0, 0), 'XYZ')
        eul.rotate_axis('Z', random.uniform(0, 3.1415))
        eul.rotate_axis('X', random.uniform(math.radians(45), math.radians(135)))
        litpos.rotate(eul)

        bpy.ops.object.add(type='LIGHT', location=litpos)
        lamp = bpy.data.lights[0]
        lamp.use_nodes = True
        nodes=lamp.node_tree.nodes
        links=lamp.node_tree.links
        for node in nodes:
            if node.type=='OUTPUT':
                output_node=node
            elif node.type=='EMISSION':
                lamp_node=node
        strngth=random.uniform(200,500)
        lamp_node.inputs[1].default_value=strngth
        #Change warmness of light to simulate more natural lighting
        bbody=nodes.new(type='ShaderNodeBlackbody')
        color_temp=random.uniform(2700,10200)
        bbody.inputs[0].default_value=color_temp
        links.new(bbody.outputs[0],lamp_node.inputs[0])
示例#3
0
def randCam(mesh):
    bpy.ops.object.select_all(action='DESELECT')
    camera = bpy.data.objects['Camera']

    # sample camera config until find a valid one
    id = 0
    vid = False
    # focal length
    bpy.data.cameras['Camera'].lens = random.randint(25, 35)
    # cam position
    d = random.uniform(2.3, 3.3)
    campos = Vector((0, d, 0))
    eul = Euler((0, 0, 0), 'XYZ')
    eul.rotate_axis('Z', random.uniform(0, 3.1415))
    eul.rotate_axis('X', random.uniform(math.radians(60), math.radians(120)))

    campos.rotate(eul)
    camera.location = campos

    while id < 50:
        # look at pos
        st = (d - 2.3) / 1.0 * 0.2 + 0.3
        lookat = Vector((random.uniform(-st, st), random.uniform(-st, st), 0))
        eul = Euler((0, 0, 0), 'XYZ')

        eul.rotate_axis('X', math.atan2(lookat.y - campos.y, campos.z))
        eul.rotate_axis('Y', math.atan2(campos.x - lookat.x, campos.z))
        st = (d - 2.3) / 1.0 * 15 + 5.
        eul.rotate_axis(
            'Z', random.uniform(math.radians(-90 - st),
                                math.radians(-90 + st)))

        camera.rotation_euler = eul
        bpy.context.view_layer.update()

        if isVisible(mesh, camera):
            vid = True
            break

        id += 1
    return vid