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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
#!BPY
"""
Name: 'Save UV Face Layout...'
Blender: 232
Group: 'UV'
Tooltip: 'Export the UV face layout of the selected object to a .TGA file'
"""
__author__ = "Martin 'theeth' Poirier"
__url__ = ("http://www.blender.org", "http://www.elysiun.com")
__version__ = "1.4"
__bpydoc__ = """\
This script exports the UV face layout of the selected mesh object to
a TGA image file. Then you can, for example, paint details in this image using
an external 2d paint program of your choice and bring it back to be used as a
texture for the mesh.
Usage:
Open this script from UV/Image Editor's "UVs" menu, make sure there is a mesh
selected, define size and wire size parameters and push "Export" button.
There are more options to configure, like setting export path, if image should
use object's name and more.
Notes:<br>
Jean-Michel Soler (jms) wrote TGA functions used by this script.<br>
Zaz added the default path code and Selected Face option.<br>
Macouno fixed a rounding error in the step calculations<br>
"""
# $Id: uv_export.py,v 1.14 2006/06/27 13:26:00 campbellbarton Exp $
#
# --------------------------------------------------------------------------
# ***** BEGIN GPL LICENSE BLOCK *****
#
# Copyright (C) 2003: Martin Poirier, theeth@yahoo.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# ***** END GPL LICENCE BLOCK *****
# --------------------------------------------------------------------------
# thanks to jms for the tga functions:
# Writetga and buffer functions
# (c) 2002-2004 J-M Soler released under GPL licence
# Official Page :
# http://jmsoler.free.fr/didacticiel/blender/tutor/write_tga_pic.htm
# Communicate problems and errors on:
# http://www.zoo-logique.org/3D.Blender/newsportal/thread.php?group=3D.Blender
# --------------------------
# Version 1.1
# Clear a bug that crashed the script when UV coords overlapped in the same faces
# --------------------------
# Version 1.2
# Now with option to use the object's name as filename
# --------------------------
# Version 1.3 Updates by Zaz from Elysiun.com
# Default path is now the directory of the last saved .blend
# New options: Work on selected face only & Scale image when face wraps
# --------------------------
# Version 1.3a
# Corrected a minor typo and added the tga extension to both export call
# --------------------------
# Version 1.4 Updates by Macouno from Elysiun.com
# Fixed rounding error that can cause breaks in lines.
# --------------------------
# Version 2.0
# New interface using PupBlock and FileSelector
# Save/Load config to Registry
# Edit in external program
# --------------------------
FullPython = False
import Blender
try:
import os
FullPython = True
except:
pass
from math import *
def ExportConfig():
conf = {}
conf["SIZE"] = bSize.val
conf["WSIZE"] = bWSize.val
conf["OBFILE"] = bObFile.val
conf["WRAP"] = bWrap.val
conf["ALLFACES"] = bAllFaces.val
conf["EDIT"] = bEdit.val
conf["EXTERNALEDITOR"] = bEditPath.val
Blender.Registry.SetKey("UVEXPORT", conf, True)
def ImportConfig():
global bSize, bWSize, bObFile, bWrap, bAllFaces
conf = Blender.Registry.GetKey("UVEXPORT", True)
if not conf:
return
try:
bSize.val = conf["SIZE"]
bWSize.val = conf["WSIZE"]
bObFile.val = conf["OBFILE"]
bWrap.val = conf["WRAP"]
bAllFaces.val = conf["ALLFACES"]
bEdit.val = conf["EDIT"]
editor = conf["EXTERNALEDITOR"]
if editor:
bEditPath.val = editor
except KeyError:
pass
def Export(f):
obj = Blender.Scene.getCurrent().getActiveObject()
if not obj:
Blender.Draw.PupMenu("ERROR%t|No Active Object!")
return
if obj.getType() != "Mesh":
Blender.Draw.PupMenu("ERROR%t|Not a Mesh!")
return
mesh = obj.getData()
if not mesh.hasFaceUV():
Blender.Draw.PupMenu("ERROR%t|No UV coordinates!")
return
if bObFile.val:
name = f + obj.name + ".tga"
else:
name = f + ".tga"
UV_Export(mesh, bSize.val, bWSize.val, bWrap.val, bAllFaces.val, name)
if FullPython and bEdit.val and bEditPath.val:
filepath = os.path.realpath(name)
print filepath
os.spawnl(os.P_NOWAIT, bEditPath.val, "", filepath)
def Buffer(height=16, width=16, profondeur=3,rvb=255 ):
"""
reserve l'espace memoire necessaire
"""
p=[rvb]
b=p*height*width*profondeur
return b
def write_tgafile(loc2,bitmap,width,height,profondeur):
f=open(loc2,'wb')
Origine_en_haut_a_gauche=32
Origine_en_bas_a_gauche=0
Data_Type_2=2
RVB=profondeur*8
RVBA=32
entete0=[]
for t in range(18):
entete0.append(chr(0))
entete0[2]=chr(Data_Type_2)
entete0[13]=chr(width/256)
entete0[12]=chr(width % 256)
entete0[15]=chr(height/256)
entete0[14]=chr(height % 256)
entete0[16]=chr(RVB)
entete0[17]=chr(Origine_en_bas_a_gauche)
#Origine_en_haut_a_gauche
for t in entete0:
f.write(t)
for t in bitmap:
f.write(chr(t))
f.close()
def UV_Export(mesh, size, wsize, wrap, allface, file):
vList = []
faces = []
minx = 0
miny = 0
scale = 1.0
step = 0
if allface:
faces = mesh.faces
else:
faces = mesh.getSelectedFaces ()
for f in faces:
vList.append(f.uv)
img = Buffer(size+1,size+1)
if wrap:
wrapSize = size
else:
wrapSize = size
maxx = -100000
maxy = -100000
for f in vList:
for v in f:
x = int(v[0] * size)
maxx = max (x, maxx)
minx = min (x, minx)
y = int(v[1] * size)
maxy = max (y, maxy)
miny = min (y, miny)
wrapSize = max (maxx - minx + 1, maxy - miny + 1)
scale = float (size) / float (wrapSize)
fnum = 0
fcnt = len (vList)
for f in vList:
fnum = fnum + 1
if not fnum % 100:
print "Face " + str (fnum) + " of " + str (fcnt)
for index in range(len(f)):
co1 = f[index]
if index < len(f) - 1:
co2 = f[index + 1]
else:
co2 = f[0]
step = int(ceil(size*sqrt((co1[0]-co2[0])**2+(co1[1]-co2[1])**2)))
if step:
for t in range(step):
x = int(floor((co1[0] + t*(co2[0]-co1[0])/step) * size))
y = int(floor((co1[1] + t*(co2[1]-co1[1])/step) * size))
if wrap:
x = x % wrapSize
y = y % wrapSize
else:
x = int ((x - minx) * scale)
y = int ((y - miny) * scale)
co = x * 3 + y * 3 * size;
img[co] = 0
img[co+1] = 0
img[co+2] = 0
if wsize > 1:
for x in range(-1*wsize + 1,wsize):
for y in range(-1*wsize,wsize):
img[co + 3 * x + y * 3 * size] = 0
img[co + 3 * x + y * 3 * size +1] = 0
img[co + 3 * x + y * 3 * size +2] = 0
for v in f:
x = int(v[0] * size)
y = int(v[1] * size)
if wrap:
x = x % wrapSize
y = y % wrapSize
else:
x = int ((x - minx) * scale)
y = int ((y - miny) * scale)
co = x * 3 + y * 3 * size
img[co] = 0
img[co+1] = 0
img[co+2] = 255
write_tgafile(file,img,size,size,3)
def SetEditorAndExport(f):
global bEditPath
bEditPath.val = f
ExportConfig()
Blender.Window.FileSelector(Export, "Save UV Image")
# ###################################### MAIN SCRIPT BODY ###############################
bSize = Blender.Draw.Create(512)
bWSize = Blender.Draw.Create(1)
bObFile = Blender.Draw.Create(1)
bWrap = Blender.Draw.Create(1)
bAllFaces = Blender.Draw.Create(1)
bEdit = Blender.Draw.Create(0)
bEditPath = Blender.Draw.Create("")
ImportConfig()
Block = []
Block.append(("Size: ", bSize, 64, 8192, "Size of the exported image"))
Block.append(("Wire: ", bWSize, 1, 5, "Size of the wire of the faces"))
Block.append(("Wrap", bWrap, "Wrap to image size, scale otherwise"))
Block.append(("All Faces", bAllFaces, "Export all or only selected faces"))
Block.append(("Ob", bObFile, "Use object name in filename"))
if FullPython:
Block.append(("Edit", bEdit, "Edit resulting file in an external program"))
Block.append(("Editor: ", bEditPath, 0, 399, "Path to external editor (leave blank to select a new one)"))
retval = Blender.Draw.PupBlock("UV Image Export", Block)
if retval:
ExportConfig()
if bEdit.val and not bEditPath.val:
Blender.Window.FileSelector(SetEditorAndExport, "Select Editor")
else:
Blender.Window.FileSelector(Export, "Save UV Image")
|