File size: 2,976 Bytes
e27853f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import bpy
import random
import json
import os
import sys
from sys import platform

if __name__ == "__main__":

    code_fpath = sys.argv[6]  # Path to the code file
    rendering_dir = sys.argv[7] # Path to save the rendering from camera1
    if len(sys.argv) > 8:
        save_blend = sys.argv[8] # Path to save the blend file
    else:
        save_blend = None

    # Enable GPU rendering
    bpy.context.scene.render.engine = 'CYCLES'
    bpy.context.preferences.addons['cycles'].preferences.compute_device_type = 'CUDA'  # or 'OPTIX' if your GPU supports it
    bpy.context.preferences.addons['cycles'].preferences.get_devices()

    # Check and select the GPUs
    for device in bpy.context.preferences.addons['cycles'].preferences.devices:
        if device.type == 'GPU' and not device.use:
            device.use = True

    # Set the rendering device to GPU
    bpy.context.scene.cycles.device = 'GPU'

    # Setting up rendering resolution
    bpy.context.scene.render.resolution_x = 512
    bpy.context.scene.render.resolution_y = 512

    # Set max samples to 1024
    bpy.context.scene.cycles.samples = 512

    # Set color mode to RGB
    bpy.context.scene.render.image_settings.color_mode = 'RGB'

    # Read and execute the code from the specified file
    with open(code_fpath, "r") as f:
        code = f.read()
    try:
        exec(code)
    except:
        raise ValueError

    # Render from camera1
    if 'Camera1' in bpy.data.objects:
        bpy.context.scene.camera = bpy.data.objects['Camera1']
        bpy.context.scene.render.image_settings.file_format = 'PNG'
        bpy.context.scene.render.filepath = os.path.join(rendering_dir, 'render1.png')
        bpy.ops.render.render(write_still=True)

    # print(f'Camera1 location: {bpy.data.objects["Camera1"].location}')
    # print(f'Camera1 rotation: {bpy.data.objects["Camera1"].rotation_euler}')
    # print(f'Camera1 lens: {bpy.data.objects["Camera1"].data.lens}')
    
    # print(f"Camera2 location: {bpy.data.objects['Camera2'].location}")
    # print(f"Camera2 rotation: {bpy.data.objects['Camera2'].rotation_euler}")
    # print(f"Camera2 lens: {bpy.data.objects['Camera2'].data.lens}")
    
    # if 'Camera3' in bpy.data.objects:
    #     print(f"Camera3 location: {bpy.data.objects['Camera3'].location}")
    #     print(f"Camera3 rotation: {bpy.data.objects['Camera3'].rotation_euler}")

    # Render from camera2 (not used in hard tasks)
    # if 'Camera2' in bpy.data.objects:
    #     bpy.context.scene.camera = bpy.data.objects['Camera2']
    #     bpy.context.scene.render.image_settings.file_format = 'PNG'
    #     bpy.context.scene.render.filepath = os.path.join(rendering_dir, 'render2.png')
    #     bpy.ops.render.render(write_still=True)

    # Save the blend file
    if save_blend:
        # Set the save version to 0
        bpy.context.preferences.filepaths.save_version = 0
        # Save the blend file
        bpy.ops.wm.save_as_mainfile(filepath=save_blend)