This repository has been archived by the owner on Sep 3, 2019. It is now read-only.
/
Py-TextureBaker.pyp
212 lines (163 loc) · 6.77 KB
/
Py-TextureBaker.pyp
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
"""
Texture Baker
Copyright: MAXON Computer GmbH
Written for Cinema 4D R18.020
Last Modified Date: 23/08/2016
"""
import c4d
from c4d import bitmaps, documents, gui, plugins, threading, utils
# Be sure to use a unique ID obtained from www.plugincafe.com
PLUGIN_ID = 1037872
# Thread for the TextureBaker Command Plugin
class TextureBakerThread(threading.C4DThread):
# Initializes the Texture Baker thread
def __init__(self, doc, textags, texuvws, destuvws):
self.doc = doc
self.textags = textags
self.texuvws = texuvws
self.destuvws = destuvws
self.bakeDoc = None
self.bakeData = None
self.bakeBmp = bitmaps.MultipassBitmap(512, 512, c4d.COLORMODE_RGB)
self.bakeError = c4d.BAKE_TEX_ERR_NONE
# Setups and starts the texture baking thread
def Begin(self):
bakeData = c4d.BaseContainer()
bakeData[c4d.BAKE_TEX_WIDTH] = 512
bakeData[c4d.BAKE_TEX_HEIGHT] = 512
bakeData[c4d.BAKE_TEX_PIXELBORDER] = 1
bakeData[c4d.BAKE_TEX_CONTINUE_UV] = False
bakeData[c4d.BAKE_TEX_SUPERSAMPLING] = 0
bakeData[c4d.BAKE_TEX_FILL_COLOR] = c4d.Vector(1)
bakeData[c4d.BAKE_TEX_USE_BUMP] = False
bakeData[c4d.BAKE_TEX_USE_CAMERA_VECTOR] = False
bakeData[c4d.BAKE_TEX_AUTO_SIZE] = False
bakeData[c4d.BAKE_TEX_NO_GI] = False
bakeData[c4d.BAKE_TEX_GENERATE_UNDO] = False
bakeData[c4d.BAKE_TEX_PREVIEW] = False
bakeData[c4d.BAKE_TEX_COLOR] = True
bakeData[c4d.BAKE_TEX_UV_LEFT] = 0.0
bakeData[c4d.BAKE_TEX_UV_RIGHT] = 1.0
bakeData[c4d.BAKE_TEX_UV_TOP] = 0.0
bakeData[c4d.BAKE_TEX_UV_BOTTOM] = 1.0
#bakeData[c4d.BAKE_TEX_OPTIMAL_MAPPING] = c4d.BAKE_TEX_OPTIMAL_MAPPING_CUBIC
self.bakeData = bakeData
# Initializes bake process
bakeInfo = utils.InitBakeTexture(self.doc, self.textags, self.texuvws, self.destuvws, self.bakeData, self.Get())
self.bakeDoc = bakeInfo[0]
self.bakeError = bakeInfo[1]
if self.bakeError != c4d.BAKE_TEX_ERR_NONE or self.bakeDoc is None:
return False
# Starts bake thread
self.Start(c4d.THREADMODE_ASYNC, c4d.THREADPRIORITY_BELOW)
return True
# Texture Baker hook
# Prints the baker progress information
def BakeTextureHook(self, info):
#print info
pass
# Bake Texture Thread Main routine
def Main(self):
self.bakeError = utils.BakeTexture(self.bakeDoc, self.bakeData, self.bakeBmp, self.Get(), self.BakeTextureHook)
c4d.SpecialEventAdd(PLUGIN_ID) # Send core message once baking has finished
# Checks for user break
def TestDBreak(self):
return False
# Main dialog for the Texture Baker
class TextureBakerDlg(gui.GeDialog):
BUTTON_BAKE = 1000
BUTTON_ABORT = 1001
aborted = False
textureBakerThread = None
infoText = None
def CreateLayout(self):
self.SetTitle("Texture Baker")
self.GroupBegin(id=0, flags=c4d.BFH_SCALEFIT, rows=1, title="", cols=2, groupflags=0)
self.AddButton(id=self.BUTTON_BAKE, flags=c4d.BFH_LEFT, initw=100, inith=25, name="Bake")
self.AddButton(id=self.BUTTON_ABORT, flags=c4d.BFH_LEFT, initw=100, inith=25, name="Abort")
self.GroupEnd()
self.GroupBegin(id=0, flags=c4d.BFH_SCALEFIT, rows=1, title="", cols=1, groupflags=0)
self.infoText = self.AddStaticText(id=0, initw=0, inith=0, name="", borderstyle=0, flags=c4d.BFH_SCALEFIT)
self.GroupEnd()
self.EnableButtons(False)
return True
def EnableButtons(self, baking):
self.Enable(self.BUTTON_BAKE, not baking)
self.Enable(self.BUTTON_ABORT, baking)
def Command(self, id, msg):
if id==self.BUTTON_BAKE:
self.Bake()
elif id==self.BUTTON_ABORT:
self.Abort()
return True
def Bake(self):
doc = documents.GetActiveDocument()
if doc is None:
return
textags = []
texuvws = []
destuvws = []
obj = doc.GetActiveObject()
if obj is None:
self.SetString(self.infoText, "Bake Init Failed: Select one single object")
return
# Gather texture and UVW tags from the selected object
uvwTag = obj.GetTag(c4d.Tuvw)
tags = obj.GetTags()
for tag in tags:
if tag.CheckType(c4d.Ttexture):
textags.append(tag)
texuvws.append(uvwTag)
destuvws.append(uvwTag)
# Initialize and start texture baker thread
self.aborted = False
self.textureBakerThread = TextureBakerThread(doc, textags, texuvws, destuvws)
if not self.textureBakerThread.Begin():
print "Bake Init Failed: Error " + str(self.textureBakerThread.bakeError)
self.SetString(self.infoText, str("Bake Init Failed: Error " + str(self.textureBakerThread.bakeError)))
else:
self.EnableButtons(True)
self.SetString(self.infoText, "Baking")
def Abort(self):
if self.textureBakerThread and self.textureBakerThread.IsRunning():
self.aborted = True
self.textureBakerThread.End()
self.textureBakerThread = None
def CoreMessage(self, id, msg):
# Checks if texture baking has finished
if id==PLUGIN_ID:
self.EnableButtons(False)
# Aborted?
if not self.aborted:
# Not aborted
#print "Baking Finished"
self.SetString(self.infoText, str("Baking Finished"))
bmp = self.textureBakerThread.bakeBmp
if bmp:
bitmaps.ShowBitmap(bmp)
self.textureBakerThread = None
return True
else:
# Aborted
#print "Baking Aborted"
self.SetString(self.infoText, str("Baking Aborted"))
return True
return gui.GeDialog.CoreMessage(self, id, msg)
def AskClose(self):
self.Abort() # Abort on close
return False
# Texture Baker Command Plugin
class TextureBakerData(c4d.plugins.CommandData):
dialog = None
def Execute(self, doc):
if self.dialog is None:
self.dialog = TextureBakerDlg()
return self.dialog.Open(dlgtype=c4d.DLG_TYPE_ASYNC, pluginid=PLUGIN_ID, defaultw=250, defaulth=50)
def RestoreLayout(self, sec_ref):
if self.dialog is None:
self.dialog = TextureBakerDlg()
return self.dialog.Restore(pluginid=PLUGIN_ID, secret=sec_ref)
if __name__ == "__main__":
plugins.RegisterCommandPlugin(id=PLUGIN_ID, str="Py-TextureBaker",
help="Py - Texture Baker", info=0,
dat=TextureBakerData(), icon=None)