Paste Code
Paste Blends
Paste Images
import bpy
import math
import array
import sys
#import Mathutils

from bpy.props import BoolProperty
from bpy.props import FloatVectorProperty

# Custom properties
bpy.types.Scene.realsize_forceunwrap = BoolProperty(name="Force Unwrap",description = "Forces the script to unwrap the mesh again" , default = True)
bpy.types.Scene.realsize_materialSize = FloatVectorProperty(name="Material Size",description = "The world dimensions of a texture (metric)" , default = (1.0,1.0),
min=0.00001,
max= 1000000000,
soft_min = 0,
soft_max = 10000,
step=3,
precision=2,
options={'SKIP_SAVE'},
subtype ='NONE',
size = 2)

UVMapName = "UVMapWorldSize"

UVToolsObject = 0
_Debug = True
bl_info = {
"name": "Realsize Unwrap",
"author": "Gertjan Van den BRoek",
"version": (0, 1),
"blender": (2, 6, 5),
"location": "3D View, Toolbox",
"description": "Creates an new UVmap OR updates an existing UVmap called 'UVMapWorldSize',this set of UV's is scaled to match the world size. The UV square is treated as a 1mx1m square",
"warning": "Not 100% reliable, but 100% safe for your mesh",
"wiki_url": ""
"",
"tracker_url": ""
"",
"support": 'TESTING',
"category": "Object"}

# Debug
def printIDs(meshObj):
print('----------ID------------')
for poly in meshObj.data.polygons:
print( poly.vertices[0])
print( poly.vertices[1])
print( poly.vertices[2])
print('----------ID------------')
print('----------UV------------')
for vertice in meshObj.data.uv_layers.data.vertices:
print(vertice.index)
print('----------UV------------')

#length of a Vector2
def VectorMagnitude2 (first, last):
# A² + B² = C²
X = first[0] - last[0]
Y = first[1] - last[1]
returnVal = math.pow(X,2) + math.pow(Y,2)
returnVal = math.sqrt(returnVal)
if(_Debug):
print("Vector2 magnitude called! Returning " + "%s" %returnVal)
return returnVal

#length of a Vector3
def VectorMagnitude3 (first, last):
# A² + B² + C² = D²
X = first[0] - last[0]
Y = first[1] - last[1]
Z = first[2] - last[2]
returnVal = math.pow(X,2) + math.pow(Y,2) + math.pow(Z,2)
returnVal = math.sqrt(returnVal)
if(_Debug):
print("Vector3 magnitude called! Returning " + "%s" %returnVal)
return returnVal

#not used, but could be usefull
def TriangleArea(x,y,z):
sumOfSides=x+y+z
#angleX represents the angle between side y and side z
#The value of asine() is in radian
#The value of angle is calculated from angle/opposite_site ratio
angleX=math.acos((pow(y,2)+pow(z,2)-pow(x,2))*3.14/(360*y*z))
#Height of apex Y from side y
h=z*math.sin(angleX)
#area of triangle
area=(y*h)/2.0
if(_Debug):
print ("Area of the triangle is %s"%area)
return area

# Input: worldPoint1, WorldPoint2 , UVPoint 1, UVPoint2
# Purpose: Calculates the scaling needed to convert the UV length between UV1 and UV2 to the length between WP1 and WP2
# Output: ScaleFactor , ERROR = 0
def getEdgeScale(wPoint1, wPoint2, tPoint1, tPoint2):
# world length
wLength = VectorMagnitude3(wPoint1, wPoint2)
# texcoord length
tLength = VectorMagnitude2(tPoint1, tPoint2)

# default scale factor = 0 = error code
returVal= 0
if(tLength == 0): # if the texcoord length = 0 then the 2 points are on top of eachother = not scaleable.
print("Warning, texture has 0 edge length, has it been unwrapped properly?") # prints warning
else:
returVal = wLength / tLength
if(_Debug):
print(returVal)
return returVal

# Input: MeshData, FaceData
# Purpose: Calculates the average scalefactor of every edge in a face.
# Output: Face scalefactor, ERROR = 0
def getFaceUVScale(obj, face):

# polygons can consists out of multiple vertices and not just 3 ( ffs )
numVerts = len(face.vertices)

# create containers for each vert, ' Verts ' is a refference to the vertex Id's for the other 2 lists
Verts = [None]*numVerts
wCoords = [None]*numVerts # World Coordinates
tCoords = [None]*numVerts # Texture Coordinates

# needs to be filled
numEdges = numVerts
# keep a list of every individual edge scale
edgeScales = [None]*numVerts

# fill arrays with data
ArrayIndex = 0
for i in face.loop_indices:
# store the face Index in the faceIndexArray called Verts
Verts[ArrayIndex] = i
# store the position data in the wCoords and tCoords
wCoords[ArrayIndex] = obj.data.uv_layers.data.vertices[face.vertices[ArrayIndex]].co
tCoords[ArrayIndex] = obj.data.uv_layers[UVMapName].data[Verts[ArrayIndex]].uv

if(_Debug): # print debug info
print (wCoords[ArrayIndex])
print (tCoords[ArrayIndex])
ArrayIndex += 1 # manually increase array index for next loop

# get all edge scales
for i in range(numVerts):
thisVertArrayIndex = i
nextVertArrayIndex = 0
if(i < numVerts -1): # if i is not the end of the array
nextVertArrayIndex = i+1
# get edge scale
edgeScale = getEdgeScale(wCoords[thisVertArrayIndex], wCoords[nextVertArrayIndex],tCoords[thisVertArrayIndex], tCoords[nextVertArrayIndex])
#
if(_Debug): # print debug info
print ("\t\tEdge scale: %s" %edgeScale)
# in case I get an error, ignore that edge scale and dont count it. This way it doesnt interfere with other results.
if(edgeScale == 0): # error code
numEdges = numEdges -1
else:
edgeScales[i] = edgeScale

# get average face,edge scale
totalScale = 0
for i in range(numVerts):
if(edgeScales[i] != None):
totalScale += edgeScales[i]
scaleFactor = 1
if(numEdges < 1):#error code
print("ERROR: Not a single edge has a scale. Returning scale factor 0 #ERROR" )
else:
scaleFactor = totalScale / numEdges
if(_Debug):
print ("\tFace scalefactor: %s" %scaleFactor)
return scaleFactor


# Input: Mesh Data
# Purpose: To go over every face and get its individual average scale factor. Then combine it into a single number
# Output: Object's scale factor
def GetObjectUVScaleFactor (obj):

scaleFactor = 1 # default = 1 = failsafe.
totalScaleFactor = 0 # Sum of all face scales; divided by calculatedFaceCount in the end to get scalefactor

faceCount = len(obj.data.polygons)
originalFaceCount = faceCount # actual face count
calculatedFaceCount = faceCount # face count after excluding badfaces
# go over every face
for face in obj.data.polygons:
faceScale = getFaceUVScale(obj, face) # attempt to get single face scale factor

if(faceScale == 0): # error code
calculatedFaceCount -=1 # exclude this face from equasion
else:
totalScaleFactor += faceScale # add this scale to equasion

if(calculatedFaceCount == 0): # not a single face was usable
print("ERROR: Not a single face was useable in calculations, setting scale to 1")
else:
scaleFactor = totalScaleFactor / calculatedFaceCount

return scaleFactor

# Input: None
# Purpose: Iterate over every valid mesh in selection, calculate and apply a realsize UV scale
# Output: None
def DoRealWorldUnwrap ():
# check if an object is selected
SelectedObjects = bpy.context.selected_objects
isObjectSelected = False
if ( SelectedObjects != [] ):
isObjectSelected = True
print ( "Object selected = " + ("%s" %isObjectSelected ))

# if selected, iterate over each selected object
if ( isObjectSelected == True ):
for selectedObj in SelectedObjects:

# DEBUG
if(_Debug):
printIDs (selectedObj)
# select mesh
bpy.context.scene.objects.active = selectedObj

# -> EDIT MODE
bpy.ops.object.mode_set(mode='EDIT' , toggle=False)
# select all
bpy.ops.mesh.select_all(action='SELECT')

# select or add a RealSize UV map to the object
alreadyHasCoords = False
for uvs in selectedObj.data.uv_textures:
if (uvs.name == UVMapName):
alreadyHasCoords = True
print("Mesh already has worldsize UV map, skipping creation")
# check if use has decided to unwrap anyway , set hascoords to false

if( alreadyHasCoords == False ): # if there are no UV coords yet, add a new on with given scripted name
bpy.ops.mesh.uv_texture_add()
bpy.context.active_object.data.uv_textures.active.name = UVMapName
alreadyHasCoords = True

# unwrap
if(bpy.context.scene.realsize_forceunwrap == True):
alreadyHasCoords = False
bpy.ops.uv.smart_project(angle_limit=45, island_margin=0, user_area_weight=0)

# activate image editor
bpy.ops.object.mode_set(mode='OBJECT' , toggle=False)
scalefactor = GetObjectUVScaleFactor ( selectedObj )
bpy.ops.object.mode_set(mode='EDIT' , toggle=False)

# store which area the user is looking at ( probably VIEW_3D )
originalActiveArea = bpy.context.area.type
bpy.context.area.type = 'IMAGE_EDITOR' # change to Image_Editor
#coulddo: delete existing texture of mesh.
bpy.ops.uv.select_all(action='SELECT')

# scaling the UV
print ("resizing UV scale to " + "%s" %scalefactor)
bpy.ops.transform.resize(value=(scalefactor, scalefactor, scalefactor), constraint_axis=(False, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1, snap=False, snap_target='CLOSEST', snap_point=(0, 0, 0), snap_align=False, snap_normal=(0, 0, 0), texture_space=False, release_confirm=False)

# back to original area type (probably 3D view though )
bpy.context.area.type = originalActiveArea
# exit edit mode
bpy.ops.object.mode_set(mode='OBJECT' , toggle=False)
# done
print ("Script ended!")

def Do_SetMaterialSize():
print("Setting Material Size")
# get selected material
mat = bpy.context.object.active_material
if(mat == None):
print("Object has no materials set, ending!")
return {'FAILED'}
# iterate textures
for texSlot in mat.texture_slots:
if(texSlot != None):
# calc scale
texScale = [0]*2
texScale[0] = 1 / bpy.context.scene.realsize_materialSize[0]
texScale[1] = 1 / bpy.context.scene.realsize_materialSize[1]
print("\tFound: " + texSlot.name + " scaling to x: %s, y: %s" %(texScale[0], texScale[1]))

texSlot.texture_coords = 'UV' # set to UV mapping mode
texSlot.scale[0] = texScale[0] # set scale X
texSlot.scale[1] = texScale[1] # set scale Y
texSlot.uv_layer = UVMapName # set UV to realsize UV
# done
print ("Script Ended!")

class UVTOOLSPanel(bpy.types.Panel):
"Creates a Panel in the viewport toolbox"
bl_label = "UV Tools"
bl_idname = "object_ot_uvtools.dorealsizeunwrap"
bl_space_type = 'VIEW_3D'
bl_region_type = 'TOOLS'
bl_context = "objectmode"

def draw(self, context):
layout = self.layout
sce = context.scene

layout.prop(sce, "realsize_forceunwrap")
layout.operator("real_size.button")

layout.prop(sce, "realsize_materialSize" )
layout.operator("real_size.set_material_size")
layout.label("v0.1")


class OBJECT_OT_RealsizeSetMaterialSize(bpy.types.Operator):
bl_idname = "real_size.set_material_size"
bl_label = "Set Material Size"
bl_description = "Sets all textures in the material to a given scale using formule : size = 1 / givenscale"

def execute(self, context):
Do_SetMaterialSize()
return {'FINISHED'}

class OBJECT_OT_RealsizeButton(bpy.types.Operator):
bl_idname = "real_size.button"
bl_label = "Scale UV to world size"
bl_description = "Scales model UVs to world size"
def execute(self,context):
DoRealWorldUnwrap()
return {'FINISHED'}

def register():
bpy.utils.register_module(__name__)
# bpy.utils.register_class(UVTOOLSPanel)


def unregister():
bpy.utils.unregister_module(__name__)
# bpy.utils.unregister_class(UVTOOLSPanel)


if __name__ == "__main__":
register()
  1. import bpy
  2. import math
  3. import array
  4. import sys
  5. #import Mathutils
  6.  
  7. from bpy.props import BoolProperty
  8. from bpy.props import FloatVectorProperty
  9.  
  10. # Custom properties
  11. bpy.types.Scene.realsize_forceunwrap = BoolProperty(name="Force Unwrap",description = "Forces the script to unwrap the mesh again" , default = True)
  12. bpy.types.Scene.realsize_materialSize = FloatVectorProperty(name="Material Size",description = "The world dimensions of a texture (metric)" , default = (1.0,1.0),
  13.                                                              min=0.00001,
  14.                                                              max= 1000000000,
  15.                                                              soft_min = 0,
  16.                                                              soft_max = 10000,
  17.                                                              step=3,
  18.                                                              precision=2,
  19.                                                              options={'SKIP_SAVE'},
  20.                                                              subtype ='NONE',
  21.                                                              size = 2)
  22.  
  23. UVMapName = "UVMapWorldSize"
  24.  
  25. UVToolsObject = 0
  26. _Debug = True
  27. bl_info = {
  28.     "name": "Realsize Unwrap",
  29.     "author": "Gertjan Van den BRoek",
  30.     "version": (0, 1),
  31.     "blender": (2, 6, 5),
  32.     "location": "3D View, Toolbox",
  33.     "description": "Creates an new UVmap OR updates an existing UVmap called 'UVMapWorldSize',this set of UV's is scaled to match the world size. The UV square is treated as a 1mx1m square",
  34.     "warning": "Not 100% reliable, but 100% safe for your mesh",
  35.     "wiki_url": ""
  36.                 "",
  37.     "tracker_url": ""
  38.                    "",
  39.     "support": 'TESTING',
  40.     "category": "Object"}
  41.  
  42. # Debug
  43. def printIDs(meshObj):
  44.     print('----------ID------------')
  45.     for poly in meshObj.data.polygons:
  46.         print( poly.vertices[0])
  47.         print( poly.vertices[1])
  48.         print( poly.vertices[2])
  49.     print('----------ID------------')
  50.     print('----------UV------------')
  51.     for vertice in meshObj.data.uv_layers.data.vertices:
  52.         print(vertice.index)
  53.     print('----------UV------------')
  54.  
  55. #length of a Vector2
  56. def VectorMagnitude2 (first, last):
  57.         # A² + B² = C²
  58.     X = first[0] - last[0]
  59.     Y = first[1] - last[1]
  60.     returnVal = math.pow(X,2) + math.pow(Y,2)
  61.     returnVal = math.sqrt(returnVal)
  62.     if(_Debug):
  63.         print("Vector2 magnitude called! Returning " + "%s" %returnVal)
  64.     return returnVal
  65.  
  66. #length of a Vector3  
  67. def VectorMagnitude3 (first, last):
  68.         # A² + B² + C² = D²
  69.     X = first[0] - last[0]
  70.     Y = first[1] - last[1]
  71.     Z = first[2] - last[2]
  72.     returnVal = math.pow(X,2) + math.pow(Y,2) + math.pow(Z,2)
  73.     returnVal = math.sqrt(returnVal)
  74.     if(_Debug):
  75.         print("Vector3 magnitude called! Returning " + "%s" %returnVal)
  76.     return returnVal
  77.  
  78. #not used, but could be usefull
  79. def TriangleArea(x,y,z):
  80.     sumOfSides=x+y+z
  81.     #angleX represents the angle between side y and side z
  82.     #The value of asine() is in radian
  83.     #The value of angle is calculated from angle/opposite_site ratio
  84.     angleX=math.acos((pow(y,2)+pow(z,2)-pow(x,2))*3.14/(360*y*z))
  85.     #Height of apex Y from side y
  86.     h=z*math.sin(angleX)
  87.     #area of triangle
  88.     area=(y*h)/2.0
  89.     if(_Debug):
  90.         print ("Area of the triangle is %s"%area)
  91.     return area
  92.  
  93. # Input: worldPoint1, WorldPoint2 , UVPoint 1, UVPoint2
  94. # Purpose: Calculates the scaling needed to convert the UV length between UV1 and UV2 to the length between WP1 and WP2
  95. # Output: ScaleFactor , ERROR = 0
  96. def getEdgeScale(wPoint1, wPoint2, tPoint1, tPoint2):
  97.     # world length
  98.     wLength = VectorMagnitude3(wPoint1, wPoint2)
  99.         # texcoord length
  100.     tLength = VectorMagnitude2(tPoint1, tPoint2)
  101.    
  102.         # default scale factor = 0 = error code
  103.     returVal= 0
  104.     if(tLength == 0): # if the texcoord length = 0 then the 2 points are on top of eachother =  not scaleable.
  105.         print("Warning, texture has 0 edge length, has it been unwrapped properly?") # prints warning
  106.     else:
  107.         returVal = wLength / tLength
  108.     if(_Debug):
  109.         print(returVal)
  110.     return returVal
  111.  
  112. # Input: MeshData, FaceData
  113. # Purpose: Calculates the average scalefactor of every edge in a face.
  114. # Output: Face scalefactor, ERROR = 0
  115. def getFaceUVScale(obj, face):
  116.        
  117.     # polygons can consists out of multiple vertices and not just 3 ( ffs )
  118.     numVerts = len(face.vertices)
  119.  
  120.     # create containers for each vert, ' Verts ' is a refference to the vertex Id's for the other 2 lists
  121.     Verts = [None]*numVerts
  122.     wCoords = [None]*numVerts # World Coordinates
  123.     tCoords = [None]*numVerts # Texture Coordinates
  124.    
  125.     # needs to be filled
  126.     numEdges = numVerts
  127.         # keep a list of every individual edge scale
  128.     edgeScales = [None]*numVerts
  129.    
  130.     # fill arrays with data
  131.     ArrayIndex = 0
  132.     for i in face.loop_indices:
  133.                 # store the face Index in the faceIndexArray called Verts
  134.         Verts[ArrayIndex] = i
  135.         # store the position data in the wCoords and tCoords
  136.         wCoords[ArrayIndex] = obj.data.uv_layers.data.vertices[face.vertices[ArrayIndex]].co
  137.         tCoords[ArrayIndex] = obj.data.uv_layers[UVMapName].data[Verts[ArrayIndex]].uv
  138.        
  139.         if(_Debug): # print debug info
  140.             print (wCoords[ArrayIndex])
  141.             print (tCoords[ArrayIndex])
  142.         ArrayIndex += 1 # manually increase array index for next loop
  143.                
  144.         # get all edge scales
  145.     for i in range(numVerts):
  146.         thisVertArrayIndex = i
  147.         nextVertArrayIndex = 0
  148.         if(i < numVerts -1): # if i is not the end of the array
  149.             nextVertArrayIndex = i+1
  150.         # get edge scale
  151.         edgeScale = getEdgeScale(wCoords[thisVertArrayIndex], wCoords[nextVertArrayIndex],tCoords[thisVertArrayIndex], tCoords[nextVertArrayIndex])
  152.                 #       
  153.         if(_Debug): # print debug info
  154.             print ("\t\tEdge scale: %s" %edgeScale)
  155.                 # in case I get an error, ignore that edge scale and dont count it. This way it doesnt interfere with other results.
  156.         if(edgeScale == 0): # error code
  157.             numEdges = numEdges -1
  158.         else:
  159.             edgeScales[i] = edgeScale
  160.            
  161.     # get average face,edge scale
  162.     totalScale = 0
  163.     for i in range(numVerts):
  164.         if(edgeScales[i] != None):
  165.             totalScale += edgeScales[i]
  166.     scaleFactor = 1
  167.     if(numEdges < 1):#error code
  168.         print("ERROR: Not a single edge has a scale. Returning scale factor 0 #ERROR" )
  169.     else:
  170.         scaleFactor = totalScale / numEdges
  171.     if(_Debug):
  172.             print ("\tFace scalefactor: %s" %scaleFactor)
  173.     return scaleFactor
  174.        
  175.        
  176. # Input: Mesh Data
  177. # Purpose: To go over every face and get its individual average scale factor. Then combine it into a single number
  178. # Output: Object's scale factor
  179. def GetObjectUVScaleFactor (obj):
  180.        
  181.     scaleFactor = 1 # default = 1 = failsafe.
  182.     totalScaleFactor = 0 # Sum of all face scales; divided by calculatedFaceCount in the end to get scalefactor
  183.    
  184.     faceCount = len(obj.data.polygons)
  185.     originalFaceCount = faceCount # actual face count
  186.     calculatedFaceCount = faceCount # face count after excluding badfaces
  187.     # go over every face
  188.     for face in obj.data.polygons:
  189.         faceScale = getFaceUVScale(obj, face) # attempt to get single face scale factor
  190.        
  191.         if(faceScale == 0): # error code
  192.             calculatedFaceCount -=1 # exclude this face from equasion
  193.         else:
  194.             totalScaleFactor += faceScale # add this scale to equasion
  195.    
  196.     if(calculatedFaceCount == 0): # not a single face was usable
  197.         print("ERROR: Not a single face was useable in calculations, setting scale to 1")
  198.     else:
  199.         scaleFactor = totalScaleFactor / calculatedFaceCount
  200.        
  201.     return scaleFactor
  202.  
  203. # Input: None
  204. # Purpose: Iterate over every valid mesh in selection, calculate and apply a realsize UV scale
  205. # Output: None
  206. def DoRealWorldUnwrap ():
  207.   # check if an object is selected
  208.     SelectedObjects = bpy.context.selected_objects  
  209.     isObjectSelected = False
  210.     if ( SelectedObjects != [] ):
  211.         isObjectSelected = True    
  212.     print ( "Object selected = " + ("%s" %isObjectSelected ))
  213.  
  214.         # if selected, iterate over each selected object
  215.     if ( isObjectSelected == True ):
  216.         for selectedObj in SelectedObjects:
  217.        
  218.         # DEBUG
  219.             if(_Debug):
  220.                 printIDs (selectedObj)
  221.         # select mesh
  222.             bpy.context.scene.objects.active = selectedObj
  223.        
  224.         # -> EDIT MODE
  225.             bpy.ops.object.mode_set(mode='EDIT' , toggle=False)
  226.         # select all
  227.             bpy.ops.mesh.select_all(action='SELECT')
  228.        
  229.         # select or add a RealSize UV map to the object
  230.             alreadyHasCoords = False
  231.             for uvs in selectedObj.data.uv_textures:
  232.                 if (uvs.name == UVMapName):
  233.                     alreadyHasCoords = True
  234.                     print("Mesh already has worldsize UV map, skipping creation")
  235.             # check if use has decided to unwrap anyway , set hascoords to false
  236.  
  237.             if( alreadyHasCoords == False ): # if there are no UV coords yet, add a new on with given scripted name
  238.                 bpy.ops.mesh.uv_texture_add()
  239.                 bpy.context.active_object.data.uv_textures.active.name = UVMapName
  240.                 alreadyHasCoords = True
  241.        
  242.         # unwrap
  243.             if(bpy.context.scene.realsize_forceunwrap == True):
  244.                 alreadyHasCoords = False
  245.                 bpy.ops.uv.smart_project(angle_limit=45, island_margin=0, user_area_weight=0)        
  246.  
  247.         # activate image editor
  248.             bpy.ops.object.mode_set(mode='OBJECT' , toggle=False)
  249.             scalefactor = GetObjectUVScaleFactor ( selectedObj )
  250.             bpy.ops.object.mode_set(mode='EDIT' , toggle=False)
  251.        
  252.                 # store which area the user is looking at ( probably VIEW_3D )
  253.             originalActiveArea = bpy.context.area.type
  254.             bpy.context.area.type = 'IMAGE_EDITOR' # change to Image_Editor
  255.                         #coulddo: delete existing texture of mesh.
  256.             bpy.ops.uv.select_all(action='SELECT')
  257.  
  258.                 # scaling the UV
  259.             print ("resizing UV scale to " + "%s" %scalefactor)
  260.             bpy.ops.transform.resize(value=(scalefactor, scalefactor, scalefactor), constraint_axis=(False, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1, snap=False, snap_target='CLOSEST', snap_point=(0, 0, 0), snap_align=False, snap_normal=(0, 0, 0), texture_space=False, release_confirm=False)
  261.  
  262.        # back to original area type (probably 3D view though )
  263.             bpy.context.area.type = originalActiveArea
  264.        # exit edit mode
  265.             bpy.ops.object.mode_set(mode='OBJECT' , toggle=False)
  266.        # done
  267.     print ("Script ended!")
  268.  
  269. def Do_SetMaterialSize():
  270.     print("Setting Material Size")
  271.     # get selected material
  272.     mat = bpy.context.object.active_material
  273.     if(mat == None):
  274.         print("Object has no materials set, ending!")
  275.         return {'FAILED'}
  276.     # iterate textures
  277.     for texSlot in mat.texture_slots:
  278.         if(texSlot != None):
  279.             # calc scale            
  280.             texScale = [0]*2
  281.             texScale[0] = 1 / bpy.context.scene.realsize_materialSize[0]
  282.             texScale[1] = 1 / bpy.context.scene.realsize_materialSize[1]
  283.             print("\tFound: " + texSlot.name + " scaling to x: %s, y: %s"  %(texScale[0], texScale[1]))
  284.  
  285.             texSlot.texture_coords = 'UV' # set to UV mapping mode
  286.             texSlot.scale[0] = texScale[0] # set scale X
  287.             texSlot.scale[1] = texScale[1] # set scale Y
  288.             texSlot.uv_layer = UVMapName # set UV to realsize UV
  289.     # done
  290.     print ("Script Ended!")
  291.    
  292. class UVTOOLSPanel(bpy.types.Panel):
  293.     "Creates a Panel in the viewport toolbox"
  294.     bl_label = "UV Tools"
  295.     bl_idname = "object_ot_uvtools.dorealsizeunwrap"
  296.     bl_space_type = 'VIEW_3D'
  297.     bl_region_type = 'TOOLS'
  298.     bl_context = "objectmode"
  299.  
  300.     def draw(self, context):
  301.         layout = self.layout
  302.         sce = context.scene
  303.  
  304.         layout.prop(sce, "realsize_forceunwrap")
  305.         layout.operator("real_size.button")
  306.        
  307.         layout.prop(sce, "realsize_materialSize" )
  308.         layout.operator("real_size.set_material_size")
  309.         layout.label("v0.1")
  310.  
  311.  
  312. class OBJECT_OT_RealsizeSetMaterialSize(bpy.types.Operator):
  313.     bl_idname = "real_size.set_material_size"
  314.     bl_label = "Set Material Size"
  315.     bl_description = "Sets all textures in the material to a given scale using formule : size = 1 / givenscale"
  316.    
  317.     def execute(self, context):
  318.         Do_SetMaterialSize()
  319.         return {'FINISHED'}
  320.    
  321. class OBJECT_OT_RealsizeButton(bpy.types.Operator):
  322.     bl_idname = "real_size.button"
  323.     bl_label = "Scale UV to world size"    
  324.     bl_description = "Scales model UVs to world size"
  325.     def execute(self,context):
  326.         DoRealWorldUnwrap()
  327.         return {'FINISHED'}
  328.  
  329. def register():
  330.     bpy.utils.register_module(__name__)
  331. #    bpy.utils.register_class(UVTOOLSPanel)
  332.  
  333.  
  334. def unregister():
  335.     bpy.utils.unregister_module(__name__)
  336. #    bpy.utils.unregister_class(UVTOOLSPanel)
  337.  
  338.  
  339. if __name__ == "__main__":
  340.     register()
go to heaven