forked from joejuma/Blender-Automatic-Normal-Map-Addon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Automap.py
134 lines (113 loc) · 4.3 KB
/
Automap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
bl_info = {
"name": "Automatic Normal & AO Maps",
"category": "Material"
}
import bpy
import os
# To Do:
# Add error detection clauses; Right now it just spits out debug if anything
# isn't properly configured. It should tell you why it's breaking.
def CreateImage():
''' Creates a new image in blender '''
image = bpy.data.images.new(name = 'Untitled',width = 4096, height = 4096)
return image
def AddImageToMaterial(myImage):
''' Adds an image texture to the active object material'''
# Create new image texture
texture = bpy.data.textures.new(name = 'Map', type = 'IMAGE')
# Assign my image to it
texture.image = myImage
# Acquire the active object
object = bpy.context.active_object
# Get the active object's first material
material = object.data.materials['Material']
# Add a new texture slot
materialTexture = material.texture_slots.add()
# Set to 'texture'
materialTexture.texture = texture
# Set image to active UV layer
for uv_face in object.data.uv_textures.active.data:
uv_face.image = myImage
# Fix the circular reference stack error by disabling this texture
material.use_textures[0] = False
def UVUnwrapActive():
''' UV Unwraps the active object '''
print('UV unwrapping active object...')
bpy.ops.object.mode_set(mode = 'EDIT')
bpy.ops.uv.unwrap()
print('Unwrapped successfully.')
bpy.ops.object.mode_set(mode = 'OBJECT')
def SaveImage(name,image):
''' Save provided image with provided name in file directory '''
print('Saving,"%s"...' % name)
image.filepath_raw = "//" + name + ".png"
image.file_format = 'PNG'
image.save()
print('"%s",Saved!' % name)
def BakeNormal(image):
''' Bake the selected objects normals to the active object texture '''
print('Baking normals...')
# Set bake type to normal
bpy.data.scenes["Scene"].render.bake_type = 'NORMALS'
# Set normal space to 'tangent'
bpy.data.scenes["Scene"].render.bake_normal_space = 'TANGENT'
# Enable 'Bake Selected to Active'
bpy.data.scenes["Scene"].render.use_bake_selected_to_active = True
# Bake Normal Map!
bpy.ops.object.bake_image()
print('Normals baked successfully.')
print('Saving normal map...')
# Save the normal map file
SaveImage('Normal',image)
print('Normal map saved.')
def BakeAO(image):
''' Bake the selected objects AO to the active object texture '''
print('Baking AO...')
# Set bake type to 'AO'
bpy.data.scenes["Scene"].render.bake_type = 'AO'
# Enable 'Bake Selected to Active'
bpy.data.scenes["Scene"].render.use_bake_selected_to_active = True
# Bake AO Map!
bpy.ops.object.bake_image()
print('AO baked successfully.')
print('Saving AO map...')
SaveImage('AO',image)
print('AO map saved.')
def GeneratePlane():
''' Creates a plane for the baking to be done to '''
# Get current active object
object = bpy.context.active_object
# Create plane, this will swap the plane to the new active
bpy.ops.mesh.primitive_plane_add()
aObject = bpy.context.active_object
aObject.scale = (2,2,1)
# Create material & add to plane
material = bpy.data.materials.new('Material')
aObject.data.materials.append(material)
# Set the old active to a selected object
object.select = True
class AutomaticMaps(bpy.types.Operator):
''' Automated Normal & AO Map Generation Operator '''
# ID for the UI
bl_idname = 'object.map_generation'
# Display name in the UI
bl_label = 'Automatic Normal & AO'
# Enable registering this operator
bl_options = {'REGISTER'}
def execute(self,context):
''' Execute this operator '''
GeneratePlane()
UVUnwrapActive()
mapImage = CreateImage()
AddImageToMaterial(mapImage)
BakeNormal(mapImage)
BakeAO(mapImage)
return {'FINISHED'}
def register():
''' Register 'AutomaticMaps' operator '''
bpy.utils.register_class(AutomaticMaps)
def unregister():
''' Unregister 'AutomaticMaps' operator '''
bpy.utils.unregister_class(AutomaticMaps)
if __name__ == '__main__':
register()