Apply the bake normal map in script

In this script I use the addon blender for to make a terrain model with dem images.
The images dem are georeferenced.
The script imports dem images and makes the point cloud for every two meters, applies the delauny modifier.
And makes a simple plane for bake the normal map.
Select the simple plane and select the high mesh.
How can I apply the bake in this code?
Whith the options …Bake Mode = Normals
Selected to Active
And save the normalMap texture in the same folder of the original dem images, with 100 of quality.

This is the addon blenderGis:

Here there are some small test images.
https://mega.nz/#F!G1YhHbyI!bX73NHS-NmoECelKBr2GzA

And append the blender file:

This is the code:

import bpy, os

folder = “I:/catalunyaMetOrtoPru/demOrto/1/”
for f in os.listdir(folder):
pathFiles = folder + f
basename, ext = os.path.splitext(f)
if pathFiles.find(’_Dem.tif’) >= 1:
bpy.ops.importgis.georaster(importMode = ‘DEM_RAW’,filepath = pathFiles,rastCRS = ‘EPSG:3857’)
bpy.ops.tesselation.delaunay()
objetos_escena = bpy.data.objects
for objeto in objetos_escena:
objeto.select = False
for objeto in objetos_escena:
if objeto.name == basename:
objeto.select = True
bpy.ops.object.delete()
for objeto in objetos_escena:
if objeto.name.startswith(“TIN”):
nameHighDem = basename + “High”
objeto.name = nameHighDem
for f in os.listdir(folder):
pathFiles = folder + f
basename, ext = os.path.splitext(f)
if pathFiles.find(’_Dem.tif’) >= 1:
bpy.ops.importgis.georaster(importMode = ‘PLANE’,filepath = pathFiles,rastCRS = ‘EPSG:3857’)
objetos_escena = bpy.data.objects
for objeto in objetos_escena:
if objeto.name == basename:
nameLowDem = basename + “Low”
objeto.name = nameLowDem
objetos_escena = bpy.data.objects
for objeto in objetos_escena:
objeto.select = False
if objeto.name == nameHighDem:
objeto.select = True
if objeto.name == nameLowDem:
objeto.select = True

Attachments

importDemRawDelanuy_02.zip (608 Bytes)importApplyDelaunayBake.zip (2.24 MB)

the doc from the bake operator is almost self-explained…:

>>>bpy.ops.object.bake.doc

bpy.ops.object.bake(type=‘COMBINED’, pass_filter=set(), filepath="", width=512, height=512, margin=16, use_selected_to_active=False, cage_extrusion=0, cage_object="", normal_space=‘TANGENT’, normal_r=‘POS_X’, normal_g=‘POS_Y’, normal_b=‘POS_Z’, save_mode=‘INTERNAL’, use_clear=False, use_cage=False, use_split_materials=False, use_automatic_name=False, uv_layer="")

just change what you need when calling it to execute.

Thanks for your help Secrop but my knowledge about programming is very few.
I try to use your code but it isn’t work.
Do I have to fill all options?
I try whith this code but it isn’t work:

bpy.ops.object.bake(type=‘NORMAL’, pass_filter=set(), filepath=“I:/catalunyaMetOrtoPru/demOrto/1”, width=512, height=512, margin=16, use_selected_to_active=True, cage_extrusion=0, cage_object="", normal_space=‘TANGENT’, normal_r=‘POS_X’, normal_g=‘POS_Y’, normal_b=‘POS_Z’, save_mode=‘INTERNAL’, use_clear=False, use_cage=False, use_split_materials=False, use_automatic_name=False, uv_layer="")

Thanks for your help and time, cheers.

Theoretically, when a function call has parameters with a default (like 'type = ‘NORMAL’), you don’t need to add the parameter to the call (unless you want to use some other parameter value instead of the default one).

So ‘bpy.ops.object.bake(type=‘NORMAL’, use_selected_to_active=True)’ should be enough. In my custom blender, this however does not work correctly… it bakes something, but not any normal map I can use (can’t say it’s a blender problem or some thing I messed up in my source).

Confess that I don’t really know much about baking normals… I mean, I do bake normals all the time, but I don’t use Blender’s native ‘bake normals’, as I never got any good normal map from it.

In this scene I have eigth meshes, four meshes low Poly and four high Poly.
With this script I make a diccionari and select the elements first mesh high poly and then mesh low poly and bake de texture.
But the script don’t bake the texture.
code:
import bpy, os
bpy.context.scene.render.bake_type = ‘NORMALS’
bpy.context.scene.render.use_bake_selected_to_active = True

objetos_escena = bpy.data.objects

bpy.ops.object.select_all(False)
objetos_dict = {}
for objeto in objetos_escena:
if objeto.type ==“MESH”:
objeto.select = False
nombre = objeto.name
values = nombre.split(’_’)
if values[0] in objetos_dict.keys():
objetos_dict[values[0]].append(objeto)
else:
objetos_dict[values[0]] = [objeto]

for key in objetos_dict.keys():
bpy.ops.object.select_all(False)
list = objetos_dict[key]
list[0].select = True
list[1].select = True
bpy.ops.object.bake_image()
bpy.ops.object.select_all(False)

If I select manually the mesh hig poly and low poly and execute only this lines:
import bpy, os
bpy.context.scene.render.bake_type = ‘NORMALS’
bpy.context.scene.render.use_bake_selected_to_active = True
bpy.ops.object.bake_image()

The texture is bake.

why razon when I select whit script and apply bpy.ops.object.bake_image() in this code… don’t work the bake?

Thanks for your help and time.

Attachments

bakeNormals_01.zip (2.25 MB)