[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [1293] trunk/py/scripts/addons/ io_import_images_as_planes.py: basically completely recoded in order to use add_utils and io_utils.

Florian Meyer florianfelix at web.de
Sun Dec 19 00:11:10 CET 2010


Revision: 1293
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-extensions&revision=1293
Author:   testscreenings
Date:     2010-12-19 00:11:10 +0100 (Sun, 19 Dec 2010)

Log Message:
-----------
basically completely recoded in order to use add_utils and io_utils. Also the rest of the thing is recoded. Since this was like my first script this new version is much more readable and easier to debug. As a bonus there is also a new option to align the planes in a row on import to justify a version bumb. I think i tested everything and it should be bug free. Hopefully :)

Modified Paths:
--------------
    trunk/py/scripts/addons/io_import_images_as_planes.py

Modified: trunk/py/scripts/addons/io_import_images_as_planes.py
===================================================================
--- trunk/py/scripts/addons/io_import_images_as_planes.py	2010-12-18 14:11:07 UTC (rev 1292)
+++ trunk/py/scripts/addons/io_import_images_as_planes.py	2010-12-18 23:11:10 UTC (rev 1293)
@@ -18,10 +18,10 @@
 
 bl_addon_info = {
     "name": "Import Images as Planes",
-    "author": "Florian Meyer (testscreenings)",
-    "version": (0,7),
-    "blender": (2, 5, 3),
-    "api": 31998,
+    "author": "Florian Meyer (tstscr)",
+    "version": (1,0),
+    "blender": (2, 5, 5),
+    "api": 33754,
     "location": "File > Import > Images as Planes",
     "description": "Imports images and creates planes with the appropriate aspect ratio. The images are mapped to the planes.",
     "warning": "",
@@ -31,43 +31,12 @@
         "func=detail&aid=21751&group_id=153&atid=469",
     "category": "Import/Export"}
 
-"""
-This script imports images and creates Planes with them as textures.
-At the moment the naming for objects, materials, textures and meshes
-is derived from the imagename.
-
-One can either import a single image, or all images in one directory.
-When imporing a directory one can either check the checkbox or leave
-the filename empty.
-
-As a bonus one can choose to import images of only one type.
-Atm this is the list of possible extensions:
-extList =
-    ('jpeg', 'jpg', 'png', 'tga', 'tiff', 'tif', 'exr',
-    'hdr', 'avi', 'mov', 'mp4', 'ogg', 'bmp', 'cin', 'dpx', 'psd')
-
-If someone knows a better way of telling if a file is an image which
-Blender can read, please tell so ;)
-
-when importing images that are allready referenced they are not
-reimported but the old ones reused as not to clutter the materials,
-textures and image lists.
-Instead the plane gets linked against an existing material.
-
-If one reimports images but chooses different material/texture mapping
-new materials are created.
-So one doesn't has to go through everything if one decides differently
-after importing 236 images.
-
-It also has an option to translate pixeldimensions into Blenderunits.
-"""
-
-import bpy
+import bpy, os, mathutils
 from bpy.props import *
-from os import listdir
-from mathutils import Vector
+from add_utils import *
+from io_utils import ImportHelper, load_image
 
-
+## GLOBAL VARS ##
 EXT_LIST = {
     'jpeg': ['jpeg', 'jpg', 'jpe'],
     'png': ['png'],
@@ -83,317 +52,195 @@
     'cin': ['cin'],
     'dpx': ['dpx'],
     'psd': ['psd']}
+EXT_VALS = [val for val in EXT_LIST.values()]
+EXTENSIONS = []
+for i in EXT_VALS:
+    EXTENSIONS.extend(i)
+    
+## FUNCTIONS ##
+def set_image_options(self, image):
+    image.use_premultiply = self.use_premultiply
+    
+def create_image_textures(self, image):
+    #look for texture with importsettings
+    for texture in bpy.data.textures:
+        if texture.type == 'IMAGE'\
+        and texture.image\
+        and texture.image.filepath == image.filepath:
+            if self.use_transparency:
+                texture.use_alpha = True
+            else:
+                texture.use_alpha = False
+            return texture
+    
+    #if no texture is found: create one
+    texture = bpy.data.textures.new(name=os.path.split(image.filepath)[1],
+                                    type='IMAGE')
+    texture.image = image
+    if self.use_transparency:
+        texture.use_alpha = True
+    else:
+        texture.use_alpha = False
+    return texture
 
+def create_material_for_texture(self, texture):
+    #look for material with the needed texture
+    for material in bpy.data.materials:
+        if material.texture_slots[0]\
+        and material.texture_slots[0].texture == texture:
+            if self.use_transparency:
+                material.alpha = 0
+                material.specular_alpha = 0
+                material.texture_slots[0].use_map_alpha = True
+            else:
+                material.alpha = 1
+                material.specular_alpha = 1
+                material.texture_slots[0].use_map_alpha = False
+            material.use_transparency = self.use_transparency
+            material.transparency_method = self.transparency_method
+            material.use_shadeless = self.use_shadeless
+            return material
+            
+    # if no material found: create one
+    material = bpy.data.materials.new(name=os.path.split(texture.image.filepath)[1])
+    slot = material.texture_slots.add()
+    slot.texture = texture
+    slot.texture_coords = 'UV'
+    if self.use_transparency:
+        slot.use_map_alpha = True
+        material.alpha = 0
+        material.specular_alpha = 0
+    else:
+        material.alpha = 1
+        material.specular_alpha = 1
+        slot.use_map_alpha = False
+    material.use_transparency = self.use_transparency
+    material.transparency_method = self.transparency_method
+    material.use_shadeless = self.use_shadeless
+    
+    return material
 
-# Apply view rotation to objects if "Align To" for new objects
-# was set to "VIEW" in the User Preference.
-def apply_view_rotation(ob):
-    context = bpy.context
-    align = bpy.context.user_preferences.edit.object_align
-
-    if (context.space_data.type == 'VIEW_3D'
-        and align == 'VIEW'):
-            view3d = context.space_data
-            region = view3d.region_3d
-            viewMatrix = region.view_matrix
-            rot = viewMatrix.rotation_part()
-            ob.rotation_euler = rot.invert().to_euler()
-
-
-# Create plane mesh
-def createPlaneMesh(dimension, img):
-    # x is the x-aspectRatio.
+def create_image_plane(self, context, material):
+    img = material.texture_slots[0].texture.image
     x = img.size[0] / img.size[1]
     y = 1
 
-    if dimension[0]:
-        x = (img.size[0] * (1.0 / dimension[1])) * 0.5
-        y = (img.size[1] * (1.0 / dimension[1])) * 0.5
+    if self.use_dimension:
+        x = (img.size[0] * (1.0 / self.factor)) * 0.5
+        y = (img.size[1] * (1.0 / self.factor)) * 0.5
 
-    verts = []
-    faces = []
+    verts = [(-x, -y, 0),
+             (x, -y, 0),
+             (x, y, 0),
+             (-x, y, 0)]
+    faces = [[0, 1, 2, 3]]
 
-    v1 = (-x, -y, 0)
-    v2 = (x, -y, 0)
-    v3 = (x, y, 0)
-    v4 = (-x, y, 0)
-
-    verts.append(v1)
-    verts.append(v2)
-    verts.append(v3)
-    verts.append(v4)
-
-    faces.append([0, 1, 2, 3])
-
-    return verts, faces
-
-
-# Create plane object
-def createPlaneObj(img, dimension):
-    scene = bpy.context.scene
-
-    verts, faces = createPlaneMesh(dimension, img)
-
-    me = bpy.data.meshes.new(img.name)
-    me.from_pydata(verts, [], faces)
-    me.update()
-
-    plane = bpy.data.objects.new(img.name, me)
+    mesh_data = bpy.data.meshes.new(img.name)
+    mesh_data.from_pydata(verts, [], faces)
+    mesh_data.update()
+    add_object_data(context, mesh_data, operator=self)
+    plane = context.scene.objects.active
     plane.data.uv_textures.new()
-
-    scene.objects.link(plane)
-    plane.location = scene.cursor_location
-    apply_view_rotation(plane)
-
+    plane.data.materials.append(material)
+    plane.data.uv_textures[0].data[0].image = img
+    plane.data.uv_textures[0].data[0].use_image = True
+    plane.data.uv_textures[0].data[0].blend_type = 'ALPHA'
+    plane.data.uv_textures[0].data[0].use_twoside = True
     return plane
 
+def generate_paths(self):
+    directory, file = os.path.split(self.filepath)
 
-# Check if a file extension matches any
-# valid (i.e. recognized) image/movie format.
-def isImageFile(extension):
-    for ext, ext_list in EXT_LIST.items():
-        if extension in ext_list:
-            return True
+    if file and not self.all_in_directory:
+        #test for extension
+        if not os.path.splitext(file)[1].lstrip('.').lower() in EXTENSIONS:
+            return [], directory
+        
+        return [self.filepath], directory
+        
+    if not file or self.all_in_directory:
+        imagepaths = []
+        files_in_directory = os.listdir(directory)
+        #clean files from nonimages
+        files_in_directory = [file for file in files_in_directory
+                              if os.path.splitext(file)[1].lstrip('.').lower()
+                              in EXTENSIONS]
+        #clean from unwanted extensions
+        if self.extension != '*':
+            files_in_directory = [file for file in files_in_directory
+                                  if os.path.splitext(file)[1].lstrip('.').lower()
+                                  in EXT_LIST[self.extension]]
+        #create paths
+        for file in files_in_directory:
+            imagepaths.append(os.path.join(directory, file))
+            
+        #print(imagepaths)
+        return imagepaths, directory
 
-    return False
-
-
-# Get imagepaths from directory
-def getImageFilesInDirectory(directory, extension):
-    import os
-
-    # Get all files in the directory.
-    allFiles = listdir(directory)
-    allImages = []
-
-    extensions = []
-
-    # Import all images files?
-    if extension == '*':
-        all = True
-
-    else:
-        all = False
-        # Get the possible extensions
-        extensions = EXT_LIST[extension]
-
-    # Put all image files in the list.
-    for file in allFiles:
-        # Get the file extension (includes the ".")
-        e = os.path.splitext(file)[1]
-
-        # Separate by "." and get the last list-entry.
-        e = e.rpartition(".")[-1]
-
-        # Convert to lower case
-        e = e.lower()
-
-        if (e in extensions
-            or (all and isImageFile(e))):
-            allImages.append(file)
-
-    return allImages
-
-
-# Get image datablock from the (image's) filepath.
-def getImage(path):
-    img = []
-
-    # Check every Image if it is already there.
-    for image in bpy.data.images:
-        # If image with same path exists take that one.
-        if image.filepath == path:
-            img = image
-
-    # Else create new Image and load from path.
-    if not img:
-        name = path.rpartition('\\')[2].rpartition('.')[0]
-        img = bpy.data.images.new(name)
-        img.source = 'FILE'
-        img.filepath = path
-
-    return img
-
-
-# Create/get Material
-def getMaterial(tex, mapping):
-    mat = []
-
-    # Check all existing materials.
-    for material in bpy.data.materials:
-        # If a material with name and mapping
-        # texture with image exists, take that one...
-        if (material.name == tex.image.name
-        and tex.name in material.texture_slots

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-extensions-cvs mailing list