-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path__init__.py
173 lines (124 loc) · 4.61 KB
/
__init__.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
bl_info = {
"name": "Bake UDIM Tiles",
"author": "Alfonso Annarumma",
"version": (0, 1),
"blender": (2, 80, 0),
"location": "Properties > Render Properties > Bake",
"description": "Baking UDIM Tiles with one click",
"warning": "",
"wiki_url": "",
"category": "Render",
}
import bpy
import os
import bmesh
from bpy.types import (
Operator,
Panel,
)
def uv_traslate(obj,i):
me = obj.data
bm = bmesh.new()
bm = bmesh.from_edit_mesh(me)
uv_layer = bm.loops.layers.uv.verify()
#bm.faces.layers.tex.verify() # currently blender needs both layers.
# scale UVs x2
for f in bm.faces:
for l in f.loops:
l[uv_layer].uv[0] -= i
#bm.to_mesh(me)
me.update()
def bake_udim(context):
obj = context.object
data = bpy.data
images = data.images
mat = obj.active_material
nodes = mat.node_tree.nodes
if nodes.active.type == 'TEX_IMAGE':
if nodes.active.image.source =='TILED':
udim_node = nodes.active
udim = udim_node.image
basename = bpy.path.basename(udim.filepath)
udim_name = basename.split('.')[0]
udim_dir = os.path.dirname(bpy.path.abspath(udim.filepath))
split = udim.filepath.split('.')
ext = split[-1]
list = []
for t in udim.tiles:
list.append(t.number)
i = 0
l = len(list)-1
for n in list:
if obj.mode != 'EDIT':
bpy.ops.object.editmode_toggle()
uv_traslate(obj,i)
if obj.mode == 'EDIT':
bpy.ops.object.editmode_toggle()
bake = images.new("bake", udim.size[0], udim.size[1], alpha=False, float_buffer=udim.is_float, stereo3d=False, is_data=False, tiled=False)
bake_node = nodes.new("ShaderNodeTexImage")
bake_node.name = "bake_image"
bake_node.image = bake
nodes.active = bake_node
bake_node.select = True
filepath = udim_dir+'/'+udim_name+'.'+str(n)+"."+ext
print(filepath)
bake.filepath = filepath
type = bpy.context.scene.cycles.bake_type
bpy.ops.object.bake(type = type, filepath=filepath, save_mode='EXTERNAL')
bake.save()
nodes.remove(bake_node)
images.remove(bake)
if obj.mode != 'EDIT':
bpy.ops.object.editmode_toggle()
uv_traslate(obj,-i)
if obj.mode == 'EDIT':
bpy.ops.object.editmode_toggle()
i += 1
# if i == l:
# f = -l
# if obj.mode != 'EDIT':
# bpy.ops.object.editmode_toggle()
# uv_traslate(obj,f)
# if obj.mode == 'EDIT':
# bpy.ops.object.editmode_toggle()
nodes.active = udim_node
udim.reload()
else:
print("Select Udim Node")
else:
print("Select Udim Node")
class SCENE_OT_Bake_Udim(Operator):
"""Select a UDIM Image Node"""
bl_idname = "object.bake_udim"
bl_label = "Bake for UDIM Image"
@classmethod
def poll(cls, context):
return context.object is not None
def execute(self, context):
print(context.object)
bake_udim(context)
return {'FINISHED'}
class SCENE_PT_BakeUDIM(Panel):
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = "Bake UDIM"
bl_label = "Bake UDIM"
#bl_options = {'DEFAULT_CLOSED'}
def draw(self, context):
scene = context.scene
layout = self.layout
layout.operator("object.bake_udim")
#def menu_func(self, context):
#
# layout = self.layout
# layout.operator("object.bake_udim")
def register():
bpy.utils.register_class(SCENE_OT_Bake_Udim)
bpy.utils.register_class(SCENE_PT_BakeUDIM)
#bpy.types.CYCLES_RENDER_PT_bake.append(menu_func)
def unregister():
bpy.utils.unregister_class(SCENE_OT_Bake_Udim)
bpy.utils.unregister_class(SCENE_PT_BakeUDIM)
#bpy.types.CYCLES_RENDER_PT_bake.remove(menu_func)
if __name__ == "__main__":
register()