Example #1
0
#
# See ReadMe-XPlane2Blender.html for usage.
#

from Blender import Draw, Image, Window
from math import log

from XPlaneUtils import PanelRegionHandler

image = Image.GetCurrent()  # may be None
h = PanelRegionHandler()

opts = []
if not image:
    pass
elif h.isRegion(image):
    opts.append('Delete this region%x1')
    opts.append('Reload all regions%x3')
elif h.isPanel(image):
    if h.countRegions() < PanelRegionHandler.REGIONCOUNT:
        opts.append('Create new region...%x2')
    else:
        opts.append(
            'Can\'t create new region - already using maximum of %d regions%%x0'
            % PanelRegionHandler.REGIONCOUNT)
    opts.append('Reload all regions%x3')
elif image and 'panel.' in image.name.lower(
) and '.region' not in image.name.lower():
    opts.append('Create new region...%x2')

if not opts:
#
# See ReadMe-XPlane2Blender.html for usage.
#

from Blender import Draw, Image, Window
from math import log

from XPlaneUtils import PanelRegionHandler

image=Image.GetCurrent()	# may be None
h=PanelRegionHandler()

opts=[]
if not image:
    pass
elif h.isRegion(image):
    opts.append('Delete this region%x1')
    opts.append('Reload all regions%x3')
elif h.isPanel(image):
    if h.countRegions()<PanelRegionHandler.REGIONCOUNT:
        opts.append('Create new region...%x2')
    else:
        opts.append('Can\'t create new region - already using maximum of %d regions%%x0' % PanelRegionHandler.REGIONCOUNT)
    opts.append('Reload all regions%x3')
elif image and 'panel.' in image.name.lower() and '.region' not in image.name.lower():
    opts.append('Create new region...%x2')

if not opts:
    r=Draw.PupMenu('This is not a Panel Texture or Region%t')
else:
    r=Draw.PupMenu('X-Plane panel regions%t|'+('|'.join(opts)))
Example #3
0
def getTexture (expobj, theObjects, iscsl, fileformat,want_draped=0):
    texture=None
    multierr=[]
    panelerr=(fileformat==7 and expobj.iscockpit)
    havepanel=False
    panelobjs=[]
    nobj=len(theObjects)
    texlist=[]
    layers=0

    if find_prop_list(theObjects,'panel_ok') != None:
        expobj.ispanelok=True

    if iscsl:
        expobj.lod=[0,1000,4000,100000]	# list of lod limits
    else:
        expobj.lod=[0,1000,4000,10000]		# list of lod limits
    thisdir=normpath(dirname(Blender.Get('filename')))
    h=PanelRegionHandler()

    lod_keys = [['lod_0', 0], ['lod_1',1], ['lod_2',2],['lod_3',3]];
    for l in lod_keys:
        src_o=find_prop_list(theObjects,l[0])
        if src_o != None:
            layer=l[1]
            #print "object %s layer %d found LOD %s" % (object.name,layer,get_prop(object,l[0],0))
            expobj.lod[layer] = float(get_prop(src_o,l[0],0))
            if layer<3 and expobj.lod[layer+1]<=expobj.lod[layer]:
                expobj.lod[layer+1]=expobj.lod[layer]+1
            if expobj.layermask!=7:
                expobj.layermask=7
                #print "Info:\tMultiple Levels Of Detail found"
                expobj.log.append(("Multiple Levels Of Detail found",[]))
            

    for o in range (nobj-1,-1,-1):
        object=theObjects[o]
        if h.isHandlerObj(object): continue
        objType=object.getType()

        if expobj.layermask==1 and not expobj.iscockpit:
            if layers==0:
                layers = object.Layer&7
            elif object.Layer&7 and layers^(object.Layer&7):
                expobj.layermask=7
                #print "Info:\tMultiple Levels Of Detail found"
                expobj.log.append(("Multiple Levels Of Detail found", []))

        if not object.Layer&expobj.layermask:
            continue
            
        if objType == "Mesh":
            mesh=object.getData(mesh=True)
            if isLight(object):
                material=mesh.materials[0]
                mtex=material.getTextures()[0]
                if mtex:
                    image=mtex.tex.image
                    if image:
                        texture=checkdup(image.filename, thisdir, texture, texlist, multierr, object)
                        continue
                print 'Warn:\tIgnoring custom light "%s" with no texture' % object.name
                expobj.log.append(('Ignoring custom light "%s" with no texture' % object.name, [object]))
                    
            elif mesh.faceUV:
                #print '%s (%d)' % (object.getName(), len(mesh.faces))
                for face in mesh.faces:
                    has_draped = (face.mode&Mesh.FaceModes.TILES) and has_prop(object,'ATTR_draped')

                    if (face.mode&Mesh.FaceModes.TEX) and face.image and len(face.v) in [3,4] and has_draped == want_draped:
                        #print '   this is face, image is %s.\n' % face.image.filename
                        if expobj.ispanelok:
                            isregion=h.isRegion(face.image)
                            if isregion: expobj.regions[face.image]=isregion
                            if fileformat>7 and isregion:
                                pass
                            elif 'panel.' in face.image.name.lower() or isregion:
                                if fileformat>7 and expobj.regions:
                                    raise ExportError('You can\'t use the panel texture since you\'ve used panel regions.\n\tI found a face using the panel texture in "%s"' % object.name, (object, mesh, [face]))
                                if fileformat==7 and len(face.v)==3:
                                    raise ExportError('Only quads can use the instrument panel texture,\n\tbut I found a tri using the panel texture in "%s"' % object.name, (object, mesh, [face]))
                                    
                                havepanel=True
                                if not object in panelobjs:
                                    panelobjs.append(object)
                                if panelerr:
                                    # Check that at least one panel texture is OK
                                    if isregion:
                                        #(n,xoff,yoff,width,height)=isregion
                                        panelerr=0    # Should check
                                    else:
                                        xoff=yoff=0
                                        try:
                                            (width,height)=face.image.getSize()
                                        except RuntimeError:
                                            raise ExportError("Can't load instrument panel texture file")
                                        for uv in face.uv:
                                            if uv[0]<0.0 or uv[0]>1.0 or (1-uv[1])*height>768 or uv[1]>1.0:
                                                break
                                        else:
                                            panelerr=0
                            else:
                                texture=checkdup(face.image.filename, thisdir, texture, texlist, multierr, object)
                        else:
                            texture=checkdup(face.image.filename, thisdir, texture, texlist, multierr, object)

        elif (expobj.iscockpit and objType == "Lamp"
              and object.getType() == Lamp.Types.Lamp):
            raise ExportError("Cockpit objects can't contain lights",[object])
                        
    if multierr:
        raise ExportError("The OBJ format supports one texture file, but you've used multiple texture files; see the Console for a list of the files", multierr)
                                
    if fileformat>7 and havepanel and expobj.regions:
        raise ExportError("You can't use the panel texture since you've used panel regions.\n\tUse the panel texture, or panel regions, but not both", panelobjs)

    if havepanel and panelerr:
        raise ExportError("At least one face that uses the instrument panel texture must be within the top 768 lines of the panel texture")

    if not texture:
        return None

#    try:
#        img=Image.Load(texture)
#        (width,height)=img.getSize()
#    except:
#    	raise ExportError("Can't load texture file \"%s\"" % texture)
#    if 2**int(log(width,2))!=width or 2**int(log(height,2))!=height:
#        raise ExportError("Texture file height and width must be powers of two.\n\tPlease resize the file. Use Image->Replace and fixup UV mapping\n\tto load the new file")

    l=texture.rfind(sep)
    if l!=-1:
        l=l+1
    else:
        l=0
    if texture[l:].find(' ')!=-1:
        raise ExportError('Texture filename "%s" contains spaces.\n\tPlease rename the file. Use Image->Replace to load the renamed file' % texture[l:])

    if texture[-4:].lower() in ['.dds', '.png', '.bmp']:
        if fileformat==7: texture=texture[:-4]
    else:
        raise ExportError("Texture file must be in DDS, PNG or BMP format.\n\tPlease convert the file. Use Image->Replace to load the new file")
    
    # try to guess correct texture path
    if expobj.iscockpit:
        print "Info:\tUsing algorithms appropriate for a cockpit object."
        expobj.log.append(("Using algorithms appropriate for a cockpit object",[]))
        return basename(texture)

    # v7 or v7-compatibility mode
    for prefix in ["custom object textures", "autogen textures"]:
        l=texture.lower().rfind(prefix)
        if l!=-1:
            texture = texture[l+len(prefix)+1:].replace(sep,'/')
            return texture
        
    # texture is relative to .obj file - find common prefix
    a=Blender.Get('filename').split(sep)
    b=texture.split(sep)
    for i in range(min(len(a),len(b))):
        if a[i].lower()!=b[i].lower():
            if i==0: break	# it's hopeless
            c=''
            for l in range(i,len(a)-1): c+='../'
            for l in range(i,len(b)-1): c+=b[l]+'/'
            if ' ' in c: break	# spaces not allowed
            if '/Custom Scenery' in c or '/Aircraft' in c or '/X-Plane' in c:
                break	# Can't step out of scenery or aircraft package
            return c+basename(texture)

    # just return bare filename
    print "Warn:\tCan't guess path for texture file. Please edit the .obj file to fix."
    expobj.log.append(("Can't guess path for texture file. Please edit the .obj file to fix", []))
    return basename(texture)
Example #4
0
def getTexture(expobj, theObjects, iscsl, fileformat, want_draped=0):
    texture = None
    multierr = []
    panelerr = (fileformat == 7 and expobj.iscockpit)
    havepanel = False
    panelobjs = []
    nobj = len(theObjects)
    texlist = []
    layers = 0

    if find_prop_list(theObjects, 'panel_ok') != None:
        expobj.ispanelok = True

    if iscsl:
        expobj.lod = [0, 1000, 4000, 100000]  # list of lod limits
    else:
        expobj.lod = [0, 1000, 4000, 10000]  # list of lod limits
    thisdir = normpath(dirname(Blender.Get('filename')))
    h = PanelRegionHandler()

    lod_keys = [['lod_0', 0], ['lod_1', 1], ['lod_2', 2], ['lod_3', 3]]
    for l in lod_keys:
        src_o = find_prop_list(theObjects, l[0])
        if src_o != None:
            layer = l[1]
            #print "object %s layer %d found LOD %s" % (object.name,layer,get_prop(object,l[0],0))
            expobj.lod[layer] = float(get_prop(src_o, l[0], 0))
            if layer < 3 and expobj.lod[layer + 1] <= expobj.lod[layer]:
                expobj.lod[layer + 1] = expobj.lod[layer] + 1
            if expobj.layermask != 7:
                expobj.layermask = 7
                #print "Info:\tMultiple Levels Of Detail found"
                expobj.log.append(("Multiple Levels Of Detail found", []))

    for o in range(nobj - 1, -1, -1):
        object = theObjects[o]
        if h.isHandlerObj(object): continue
        objType = object.getType()

        if expobj.layermask == 1 and not expobj.iscockpit:
            if layers == 0:
                layers = object.Layer & 7
            elif object.Layer & 7 and layers ^ (object.Layer & 7):
                expobj.layermask = 7
                #print "Info:\tMultiple Levels Of Detail found"
                expobj.log.append(("Multiple Levels Of Detail found", []))

        if not object.Layer & expobj.layermask:
            continue

        if objType == "Mesh":
            mesh = object.getData(mesh=True)
            if isLight(object):
                material = mesh.materials[0]
                mtex = material.getTextures()[0]
                if mtex:
                    image = mtex.tex.image
                    if image:
                        texture = checkdup(image.filename, thisdir, texture,
                                           texlist, multierr, object)
                        continue
                print 'Warn:\tIgnoring custom light "%s" with no texture' % object.name
                expobj.log.append(
                    ('Ignoring custom light "%s" with no texture' %
                     object.name, [object]))

            elif mesh.faceUV:
                #print '%s (%d)' % (object.getName(), len(mesh.faces))
                for face in mesh.faces:
                    has_draped = (face.mode
                                  & Mesh.FaceModes.TILES) and has_prop(
                                      object, 'ATTR_draped')

                    if (face.mode & Mesh.FaceModes.TEX) and face.image and len(
                            face.v) in [3, 4] and has_draped == want_draped:
                        #print '   this is face, image is %s.\n' % face.image.filename
                        if expobj.ispanelok:
                            isregion = h.isRegion(face.image)
                            if isregion: expobj.regions[face.image] = isregion
                            if fileformat > 7 and isregion:
                                pass
                            elif 'panel.' in face.image.name.lower(
                            ) or isregion:
                                if fileformat > 7 and expobj.regions:
                                    raise ExportError(
                                        'You can\'t use the panel texture since you\'ve used panel regions.\n\tI found a face using the panel texture in "%s"'
                                        % object.name, (object, mesh, [face]))
                                if fileformat == 7 and len(face.v) == 3:
                                    raise ExportError(
                                        'Only quads can use the instrument panel texture,\n\tbut I found a tri using the panel texture in "%s"'
                                        % object.name, (object, mesh, [face]))

                                havepanel = True
                                if not object in panelobjs:
                                    panelobjs.append(object)
                                if panelerr:
                                    # Check that at least one panel texture is OK
                                    if isregion:
                                        #(n,xoff,yoff,width,height)=isregion
                                        panelerr = 0  # Should check
                                    else:
                                        xoff = yoff = 0
                                        try:
                                            (width,
                                             height) = face.image.getSize()
                                        except RuntimeError:
                                            raise ExportError(
                                                "Can't load instrument panel texture file"
                                            )
                                        for uv in face.uv:
                                            if uv[0] < 0.0 or uv[0] > 1.0 or (
                                                    1 - uv[1]
                                            ) * height > 768 or uv[1] > 1.0:
                                                break
                                        else:
                                            panelerr = 0
                            else:
                                texture = checkdup(face.image.filename,
                                                   thisdir, texture, texlist,
                                                   multierr, object)
                        else:
                            texture = checkdup(face.image.filename, thisdir,
                                               texture, texlist, multierr,
                                               object)

        elif (expobj.iscockpit and objType == "Lamp"
              and object.getType() == Lamp.Types.Lamp):
            raise ExportError("Cockpit objects can't contain lights", [object])

    if multierr:
        raise ExportError(
            "The OBJ format supports one texture file, but you've used multiple texture files; see the Console for a list of the files",
            multierr)

    if fileformat > 7 and havepanel and expobj.regions:
        raise ExportError(
            "You can't use the panel texture since you've used panel regions.\n\tUse the panel texture, or panel regions, but not both",
            panelobjs)

    if havepanel and panelerr:
        raise ExportError(
            "At least one face that uses the instrument panel texture must be within the top 768 lines of the panel texture"
        )

    if not texture:
        return None

#    try:
#        img=Image.Load(texture)
#        (width,height)=img.getSize()
#    except:
#    	raise ExportError("Can't load texture file \"%s\"" % texture)
#    if 2**int(log(width,2))!=width or 2**int(log(height,2))!=height:
#        raise ExportError("Texture file height and width must be powers of two.\n\tPlease resize the file. Use Image->Replace and fixup UV mapping\n\tto load the new file")

    l = texture.rfind(sep)
    if l != -1:
        l = l + 1
    else:
        l = 0
    if texture[l:].find(' ') != -1:
        raise ExportError(
            'Texture filename "%s" contains spaces.\n\tPlease rename the file. Use Image->Replace to load the renamed file'
            % texture[l:])

    if texture[-4:].lower() in ['.dds', '.png', '.bmp']:
        if fileformat == 7: texture = texture[:-4]
    else:
        raise ExportError(
            "Texture file must be in DDS, PNG or BMP format.\n\tPlease convert the file. Use Image->Replace to load the new file"
        )

    # try to guess correct texture path
    if expobj.iscockpit:
        print "Info:\tUsing algorithms appropriate for a cockpit object."
        expobj.log.append(
            ("Using algorithms appropriate for a cockpit object", []))
        return basename(texture)

    # v7 or v7-compatibility mode
    for prefix in ["custom object textures", "autogen textures"]:
        l = texture.lower().rfind(prefix)
        if l != -1:
            texture = texture[l + len(prefix) + 1:].replace(sep, '/')
            return texture

    # texture is relative to .obj file - find common prefix
    a = Blender.Get('filename').split(sep)
    b = texture.split(sep)
    for i in range(min(len(a), len(b))):
        if a[i].lower() != b[i].lower():
            if i == 0: break  # it's hopeless
            c = ''
            for l in range(i, len(a) - 1):
                c += '../'
            for l in range(i, len(b) - 1):
                c += b[l] + '/'
            if ' ' in c: break  # spaces not allowed
            if '/Custom Scenery' in c or '/Aircraft' in c or '/X-Plane' in c:
                break  # Can't step out of scenery or aircraft package
            return c + basename(texture)

    # just return bare filename
    print "Warn:\tCan't guess path for texture file. Please edit the .obj file to fix."
    expobj.log.append(
        ("Can't guess path for texture file. Please edit the .obj file to fix",
         []))
    return basename(texture)