0
$\begingroup$

I want to make a script that bake a curvature texture at the resolution set automatically (i am creating a script that bakes every maps at once).

The script needs to create a textures, use the resolution entered, create a material with an emit shader and for the input a Geometry > Pointiness then a Color Ramp at 0.4 then 0.6.

It needs also to check is there is an existing node and texture with the same name already, if yes it should remove it. Then the script save the texture.

I have started the beginning of the script, the material part is missing:

import bpy

size = 2048, 2048

# create the texture, delete the old texture
image = bpy.data.images.new("curvature", width=size[0], height=size[1])

# create the material nodes, delete the old texture
bpy.data.materials.new("CURVATURE")

# to use once the material is created correctly
#bpy.data.objects['HP'].active_material = bpy.data.materials['CURVATURE']
#bpy.data.objects['LP'].active_material = bpy.data.materials['CURVATURE']
#bpy.ops.object.bake(type="EMIT", use_selected_to_active = True, use_cage = True, cage_extrusion = cage_size, margin = edge_padding, use_clear = True)

#need to get the name of the image on the variable image
image.filepath_raw = "//curvature.png"
image.file_format = 'PNG'
image.save()
$\endgroup$
  • $\begingroup$ Just to note: If you need real curvature map (1st surface derivative), you won't get it from Pointiness or Dirty Vertex Colors. Blender does generate these maps a bit differently (looks like it's 2nd surface derivative). $\endgroup$ – Jaroslav Jerryno Novotny Jul 20 '17 at 0:32
  • $\begingroup$ How can i get a better curvature map? $\endgroup$ – Danyl Bekhoucha Jul 20 '17 at 1:00
  • $\begingroup$ You can see info here: wiki.polycount.com/wiki/Curvature_map. But these are codes for Max, Maya or Lightway. You may find an OSL shader to use or you will have to write your own. $\endgroup$ – Jaroslav Jerryno Novotny Jul 20 '17 at 19:14
1
$\begingroup$

zero025 helped me to code it:

import bpy

#Generate the textures and shader
#################################
#Get texture in data-block
###########################
tex = bpy.data.images.get("curvature")


#If exist or not
#################
if  tex is None:
    #create texture in data block
    tex = bpy.data.images.new("curvature", width=size[0], height=size[1], alpha = True)


    #Create material nodes "CURVATURE"
    ################################################################

    # Test if material exists
    # If it does not exist, create it:
    mat = bpy.data.materials.get("CURVATURE")

    if  mat is None:
       mat = bpy.data.materials.new("CURVATURE")

       # Enable 'Use nodes':
       mat.use_nodes = True
       nt = mat.node_tree
       nodes = nt.nodes
       links = nt.links

       # clear
       while(nodes): nodes.remove(nodes[0])

       d_geometry  = nodes.new("ShaderNodeNewGeometry")
       d_colorramp = nodes.new("ShaderNodeValToRGB")
       d_emission = nodes.new("ShaderNodeEmission")
       d_output   = nodes.new("ShaderNodeOutputMaterial")
       d_image   = nodes.new("ShaderNodeTexImage")

       d_geometry.location = (-100,-100)
       d_colorramp.location = (200,-100)      
       d_emission.location = (500,-100)
       d_output.location = (700,-100)
       d_image.location = (300,300)

       d_image.image = tex
       d_colorramp.color_ramp.elements[0].position = 0.4
       d_colorramp.color_ramp.elements[1].position = 0.6

       links.new( d_output.inputs['Surface'], d_emission.outputs['Emission'])
       links.new( d_emission.inputs['Color'], d_colorramp.outputs['Color'])
       links.new( d_colorramp.inputs['Fac'], d_geometry.outputs['Pointiness'])

#BAKING
#########################

#Curvature map

#remove every material slot of the high poly
bpy.ops.object.select_all(action = 'DESELECT')

bpy.ops.object.select_pattern(pattern="tmpHP")
bpy.context.scene.objects.active = bpy.data.objects["tmpHP"]

for ob in bpy.context.selected_editable_objects:
    ob.active_material_index = 0
    for i in range(len(ob.material_slots)):
        bpy.ops.object.material_slot_remove({'object': ob})

bpy.ops.object.select_pattern(pattern="tmpLP")
bpy.context.scene.objects.active = bpy.data.objects["tmpLP"]

print("\n> Baking: curvature map\n")

bpy.data.objects['tmpHP'].active_material = bpy.data.materials['CURVATURE']
bpy.data.objects['tmpLP'].active_material = bpy.data.materials['CURVATURE']
bpy.ops.object.bake(type="EMIT", use_selected_to_active = True, use_cage = True, cage_extrusion = cage_size, margin = edge_padding, use_clear = True)
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.