Advertisement
czeak6464

Color to Texture

May 15th, 2024
561
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | Gaming | 0 0
  1. import bpy
  2.  
  3. # Make sure you model has been UV unwrapped
  4.  
  5. # Set the render engine to Cycles
  6. bpy.context.scene.render.engine = 'CYCLES'
  7.  
  8. # Create a new image to bake to
  9. image_name = "BakedTexture"
  10. image = bpy.data.images.new(image_name, width=1024, height=1024)
  11.  
  12. # Iterate through all materials and assign the new image
  13. for mat in bpy.data.materials:
  14.     if mat.use_nodes:
  15.         for node in mat.node_tree.nodes:
  16.             if node.type == 'BSDF_PRINCIPLED':
  17.                 # Create an Image Texture node and assign the new image
  18.                 image_node = mat.node_tree.nodes.new('ShaderNodeTexImage')
  19.                 image_node.image = image
  20.                 mat.node_tree.nodes.active = image_node
  21.  
  22. # Bake the base colors
  23. bpy.ops.object.bake(type='DIFFUSE', pass_filter={'COLOR'})
  24.  
  25. # Save the baked image
  26. image.filepath_raw = "//baked_texture.png"
  27. image.file_format = 'PNG'
  28. image.save()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement