| import subprocess | |
| def create_scene(index: int, dataset_type="location", max_num_objects=1, retries=10, log_file="logs/error_log.txt", verbose=False) -> int: | |
| """ | |
| Attempts to create a data entry point in a dataset by running a Docker container that executes a Python script. | |
| This function tries to execute a command to create a scene up to a specified number of retries. It logs the attempts and errors to a specified file. If successful, it returns 1; if it fails after all retries, it returns 0. | |
| Args: | |
| index (int): The index of the scene to generate, which affects the generate_idx parameter in the command. | |
| dataset_type (str): The type of the dataset, which customizes the Python script to be executed. | |
| max_num_objects (int): The maximum number of objects to include in the scene. | |
| retries (int): The number of attempts to make in case of failure. | |
| log_file (str): Path to the file where error logs are appended. | |
| Returns: | |
| int: 0 if the scene was successfully created, 1 otherwise. | |
| Raises: | |
| None directly by the function, but subprocess.run may raise an exception if the command execution fails critically. | |
| Side Effects: | |
| Writes to a log file at `log_file` path if any errors occur during the execution. | |
| Tries to execute a Docker command which can affect system resources and Docker state. | |
| """ | |
| command = f""" | |
| sudo docker run --rm --interactive \ | |
| --user $(id -u):$(id -g) \ | |
| --volume "$(pwd):/kubric" kubricdockerhub/kubruntu \ | |
| /usr/bin/python3 eq-kubric/my_kubric_twoframe_{dataset_type}.py \ | |
| --sub_outputdir {dataset_type} \ | |
| --generate_idx {index+1} \ | |
| --max_num_objects {max_num_objects} | |
| """ | |
| attempt = 0 | |
| while attempt < retries: | |
| result = subprocess.run(command, shell=True, capture_output=True, text=True) | |
| if result.returncode == 0 and ("INFO:root:Done!" in result.stderr or "INFO:root:Done!" in result.stdout): | |
| return 0 | |
| attempt += 1 | |
| if verbose: | |
| print(f"Attempt {attempt} failed with return code {result.returncode}.") | |
| print(result.stderr) | |
| error_message = f"Failed to create a scene after {attempt} attempts: Index: {index+1}, Dataset Type: '{dataset_type}'." | |
| with open(log_file, "a") as file: | |
| file.write(f"{error_message}\n") | |
| return 1 | |