2
$\begingroup$

I'll start this off by saying I'm not the most knowledgeable person with Python, or Blender's API. I'm trying to write a script that loops through all the mesh objects in a scene and does the following:

  1. Creates an image designated to be the baked texture for the object
  2. Inserts image texture nodes into every material in that object
  3. Does a Lightmap Pack on the object
  4. Does a Diffuse bake with Color only
  5. After that it saves the baked texture image and an exported fbx of the current object mesh to a given file path

Here's what I have so far:

import bpy

scene = bpy.context.scene

# deselect all to make sure select one at a time
bpy.ops.object.select_all(action='DESELECT')

for obj in scene.objects:


    if (obj.type == 'MESH'):
        imageName = "Texture" #Just a filler name for now
        img = bpy.data.images.new(imageName,1024,1024)
        
        for m in obj.data.materials:
            m.use_nodes = True
            nodes = m.node_tree.nodes

            # Add a image texture node and set its location:    
            tex = nodes.new("ShaderNodeTexImage")
            tex.location = (100,100)
            tex.image = img


        bpy.context.view_layer.objects.active = obj
        obj.select_set(True) 
        bpy.ops.object.shade_flat()
        
        lm =  obj.data.uv_layers.get("LightMap")
        if not lm:
            lm = obj.data.uv_layers.new(name="LightMap")
            
        lm.active = True
        bpy.ops.object.editmode_toggle()
        bpy.ops.mesh.select_all(action = 'SELECT') # for all faces

        bpy.ops.uv.lightmap_pack(PREF_CONTEXT='SEL_FACES', PREF_PACK_IN_ONE=True, PREF_NEW_UVLAYER=False, PREF_APPLY_IMAGE=False, PREF_IMG_PX_SIZE=1024, PREF_BOX_DIV=12, PREF_MARGIN_DIV=0.24)
        bpy.ops.object.editmode_toggle()
        bpy.context.scene.render.engine = 'CYCLES'
        bpy.ops.object.bake(type='DIFFUSE', pass_filter={'COLOR'}, save_mode='EXTERNAL')

        img.save_render(filepath='C:\\Users\\TestUser\\Desktop\\TestImage.png')
        

So far I've only been testing it in scenes with 1 object so not how it works for multiple, but it's sort of working. It creates a proper (or at least proper looking) baked texture image from all the materials and properly saves it to the desktop. No exporting the obj as an FBX yet as I haven't been able to figure out the chunk of code for that.

One issue is that the texture map doesn't properly apply to the object, it winds up looking tiled. Here's a photo to show what I mean:

Tiled Texture

The biggest issue however is that it doesn't seem like UV map is actually being "connected" to the object. I'm not a pro at Blender so I don't know exactly how to word this, but I've manually exported the obj as an FBX before and tried inserting into a game engine, only to have it error telling me the texture couldn't be found.

Very important note is if I do all this manually everything works properly, so my code is definitely off. Here's a reference photo for what the end result is supposed to look like (this is one material with it's base color set to be the baked image texture):

enter image description here

Any help is much appreciated!

$\endgroup$
1

0

Browse other questions tagged .