blender-colab
blender-colab copied to clipboard
Need help!!!, I'm creating code for baking textures, there is code but it doesn't work as expected
I made a code that bakes textures in Google colab
import os # Import module os
blender_url_dict = {'4.0.2': "https://ftp.nluug.nl/pub/graphics/blender/release/Blender4.0/blender-4.0.2-linux-x64.tar.xz"}
blender_version = '4.0.2'
blender_url = blender_url_dict[blender_version]
base_url = os.path.basename(blender_url) #Use the module os to access the function basename
!mkdir $blender_version
!wget -nc $blender_url
!tar -xkf $base_url -C ./$blender_version --strip-components=1
import shutil from google.colab import files, drive uploaded_filename = ""
!pip install bpy
import bpy
Finding the file path .blend
Folder selection
folder_path = "" #@param {type: 'string'}
Entering a file name
file_name = "" #@param {type: 'string'}
Forming the path to the file.blend
blend_file_path = f"/content/drive/MyDrive/{folder_path}/{file_name}"
Loading an existing .blend file
bpy.ops.wm.open_mainfile(filepath=blend_file_path)
Setting your preferred rendering devices(GPU и OptiX)
prefs = bpy.context.preferences cprefs = prefs.addons['cycles'].preferences
Setting up GPU rendering and activating devices
for compute_device_type in ('CUDA', 'OPTIX'): try: cprefs.compute_device_type = compute_device_type print('Device found:', compute_device_type) break except TypeError: pass
for device in cprefs.devices: if device.type == 'CUDA' or device.type == 'OPTIX': print('Activating', device) device.use = True
Starting the baking process
bpy.ops.object.bake(type='DIFFUSE')
Entering an image title
image_name = "No name" #@param {type: 'string'}
Forming a string to search for an image
image_object = bpy.data.images.get(image_name)
Checking that the image exists
if image_object: # Saving an image in PNG format image_object.file_format = 'PNG'
# Entering the name of the baked texture
Baked_image_name = "Baked texture" #@param {type: 'string'}
# Forming a path for saving an image
image_object.save_render(filepath=f"/content/drive/MyDrive/D/{Baked_image_name}.png")
else: print("Image not found")
but it needs to be completed, if you can help me, leave a comment here