[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [3534] trunk/py/scripts/addons/ io_import_images_as_planes.py: Update to import image as planes:

Bastien Montagne montagne29 at wanadoo.fr
Sun Jun 24 18:39:51 CEST 2012


Revision: 3534
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=3534
Author:   mont29
Date:     2012-06-24 16:39:42 +0000 (Sun, 24 Jun 2012)
Log Message:
-----------
Update to import image as planes:

*Mostly, makes much more intensive use of Blender's FileSelector, to only show relevant files, and allow any pict/video files when "all" is selected (previously e.g. importing mkv's was impossible!).
*Style cleaning/refactoring (esp. do not use 'self' for anythin else than a method's object, so made most func member of op class)...

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	2012-06-24 14:28:13 UTC (rev 3533)
+++ trunk/py/scripts/addons/io_import_images_as_planes.py	2012-06-24 16:39:42 UTC (rev 3534)
@@ -19,9 +19,9 @@
 bl_info = {
     "name": "Import Images as Planes",
     "author": "Florian Meyer (tstscr)",
-    "version": (1, 5),
+    "version": (1, 6),
     "blender": (2, 6, 3),
-    "location": "File > Import > Images as Planes",
+    "location": "File > Import > Images as Planes or Add > Mesh > Images as Planes",
     "description": "Imports images and creates planes with the appropriate "
                    "aspect ratio. The images are mapped to the planes.",
     "warning": "",
@@ -35,248 +35,121 @@
 from bpy.types import Operator
 import mathutils
 import os
+import collections
 
-from bpy.props import (BoolProperty,
+from bpy.props import (StringProperty,
+                       BoolProperty,
                        EnumProperty,
                        IntProperty,
                        FloatProperty,
+                       CollectionProperty,
                        )
 
 from bpy_extras.object_utils import AddObjectHelper, object_data_add
-from bpy_extras.io_utils import ImportHelper
 from bpy_extras.image_utils import load_image
 
 # -----------------------------------------------------------------------------
 # Global Vars
 
-EXT_LIST = {
-    'jpeg': ('jpeg', 'jpg', 'jpe'),
-    'png': ('png', ),
-    'tga': ('tga', 'tpic'),
-    'tiff': ('tiff', 'tif'),
-    'exr': ('exr', ),
-    'hdr': ('hdr', ),
-    'avi': ('avi', ),
-    'mov': ('mov', 'qt'),
-    'mp4': ('mp4', ),
-    'ogg': ('ogg', 'ogv'),
-    'bmp': ('bmp', 'dib'),
-    'cin': ('cin', ),
-    'dpx': ('dpx', ),
-    'psd': ('psd', ),
-    }
+EXT_FILTER = getattr(collections, "OrderedDict", dict)((
+    ("*", ((), "All image formats",
+           "Import all know image (or movie) formats.")),
+    ("jpeg", (("jpeg", "jpg", "jpe"), "JPEG ({})",
+              "Joint Photographic Experts Group")),
+    ("png", (("png", ), "PNG ({})", "Portable Network Graphics")),
+    ("tga", (("tga", "tpic"), "Truevision TGA ({})", "")),
+    ("tiff", (("tiff", "tif"), "TIFF ({})", "Tagged Image File Format")),
+    ("bmp", (("bmp", "dib"), "BMP ({})", "Windows Bitmap")),
+    ("cin", (("cin", ), "CIN ({})", "")),
+    ("dpx", (("dpx", ), "DPX ({})", "DPX (Digital Picture Exchange)")),
+    ("psd", (("psd", ), "PSD ({})", "Photoshop Document")),
+    ("exr", (("exr", ), "OpenEXR ({})", "OpenEXR HDR imaging image file format")),
+    ("hdr", (("hdr", "pic"), "Radiance HDR ({})", "")),
+    ("avi", (("avi", ), "AVI ({})", "Audio Video Interleave")),
+    ("mov", (("mov", "qt"), "QuickTime ({})", "")),
+    ("mp4", (("mp4", ), "MPEG-4 ({})", "MPEG-4 Part 14")),
+    ("ogg", (("ogg", "ogv"), "OGG Theora ({})", "")),
+    ))
 
-EXTENSIONS = [ext for ext_ls in EXT_LIST.values() for ext in ext_ls]
+# XXX Hack to avoid allowing videos with Cycles, crashes currently!
+VID_EXT_FILTER = {e for ext_k, ext_v in EXT_FILTER.items()
+                    if ext_k in {"avi", "mov", "mp4", "ogg"}
+                      for e in ext_v[0]}
 
+CYCLES_SHADERS = (
+    ('BSDF_DIFFUSE', "Diffuse", "Diffuse Shader"),
+    ('EMISSION', "Emission", "Emission Shader"),
+    ('BSDF_DIFFUSE_BSDF_TRANSPARENT', "Diffuse & Transparent",
+     "Diffuse and Transparent Mix"),
+    ('EMISSION_BSDF_TRANSPARENT', "Emission & Transparent",
+     "Emission and Transparent Mix")
+)
 
 # -----------------------------------------------------------------------------
-# misc
-def set_image_options(self, image):
-    image.use_premultiply = self.use_premultiply
-    if self.relative:
-        image.filepath = bpy.path.relpath(image.filepath)
+# Misc utils.
+def gen_ext_filter_ui_items():
+    return ((k,
+             name.format(", ".join("." + e for e in exts)) if "{}" in name else name,
+             desc)
+            for k, (exts, name, desc) in EXT_FILTER.items())
 
 
-def is_image_fn_any(fn):
+def is_image_fn(fn, ext_key):
+    if ext_key == "*":
+        return True  # Using Blender's image/movie filter.
     ext = os.path.splitext(fn)[1].lstrip(".").lower()
-    return ext in EXTENSIONS
+    return ext in EXT_FILTER[ext_key][0]
 
 
-def is_image_fn_single(fn, ext_key):
-    ext = os.path.splitext(fn)[1].lstrip(".").lower()
-    return ext in EXT_LIST[ext_key]
-
-
-def align_planes(self, planes):
-    gap = self.align_offset
-    offset = 0
-    for i, plane in enumerate(planes):
-        offset += (plane.dimensions.x / 2.0) + gap
-        if i == 0:
-            continue
-        move_local = mathutils.Vector((offset, 0.0, 0.0))
-        move_world = plane.location + move_local * plane.matrix_world.inverted()
-        plane.location += move_world
-        offset += (plane.dimensions.x / 2.0)
-
-
-def generate_paths(self):
-    directory, fn = os.path.split(self.filepath)
-
-    if fn and not self.all_in_directory:
-        # test for extension
-        if not is_image_fn_any(fn):
-            return [], directory
-
-        return [self.filepath], directory
-
-    if not fn or self.all_in_directory:
-        imagepaths = []
-        files_in_directory = os.listdir(directory)
-        # clean files from nonimages
-        files_in_directory = [fn for fn in files_in_directory
-                              if is_image_fn_any(fn)]
-        # clean from unwanted extensions
-        if self.extension != "*":
-            files_in_directory = [fn for fn in files_in_directory
-                                  if is_image_fn_single(fn, self.extension)]
-        # create paths
-        for fn in files_in_directory:
-            imagepaths.append(os.path.join(directory, fn))
-        #print(imagepaths)
-        return imagepaths, directory
-
 # -----------------------------------------------------------------------------
-# Blender
-def create_image_textures(self, image):
-    fn_full = os.path.normpath(bpy.path.abspath(image.filepath))
-
-    # look for texture with importsettings
-    for texture in bpy.data.textures:
-        if texture.type == 'IMAGE':
-            tex_img = texture.image
-            if (tex_img is not None) and (tex_img.library is None):
-                fn_tex_full = os.path.normpath(bpy.path.abspath(tex_img.filepath))
-                if fn_full == fn_tex_full:
-                    texture.use_alpha = self.use_transparency
-                    return texture
-
-    # if no texture is found: create one
-    name_compat = bpy.path.display_name_from_filepath(image.filepath)
-    texture = bpy.data.textures.new(name=name_compat, type='IMAGE')
-    texture.image = image
-    texture.use_alpha = self.use_transparency
-    return texture
-
-
-def create_material_for_texture(self, texture):
-    # look for material with the needed texture
-    for material in bpy.data.materials:
-        slot = material.texture_slots[0]
-        if slot and slot.texture == texture:
-            if self.use_transparency:
-                material.alpha = 0.0
-                material.specular_alpha = 0.0
-                slot.use_map_alpha = True
-            else:
-                material.alpha = 1.0
-                material.specular_alpha = 1.0
-                slot.use_map_alpha = False
-            material.use_transparency = self.use_transparency
-            material.transparency_method = self.transparency_method
-            material.use_shadeless = self.use_shadeless
-            material.use_transparent_shadows = self.use_transparent_shadows
-            return material
-
-    # if no material found: create one
-    name_compat = bpy.path.display_name_from_filepath(texture.image.filepath)
-    material = bpy.data.materials.new(name=name_compat)
-    slot = material.texture_slots.add()
-    slot.texture = texture
-    slot.texture_coords = 'UV'
-    if self.use_transparency:
-        slot.use_map_alpha = True
-        material.alpha = 0.0
-        material.specular_alpha = 0.0
-    else:
-        material.alpha = 1.0
-        material.specular_alpha = 1.0
-        slot.use_map_alpha = False
-    material.use_transparency = self.use_transparency
-    material.transparency_method = self.transparency_method
-    material.use_shadeless = self.use_shadeless
-    material.use_transparent_shadows = self.use_transparent_shadows
-
-    return material
-
-
-def create_image_plane(self, context, material):
-    engine = context.scene.render.engine
-    if engine == 'BLENDER_RENDER':
-        img = material.texture_slots[0].texture.image
-    if engine == 'CYCLES':
-        nodes = material.node_tree.nodes
-        img_node = [node for node in nodes if node.type == 'TEX_IMAGE'][0]
-        img = img_node.image
-        
-    px, py = img.size
-
-    # can't load data
-    if px == 0 or py == 0:
-        px = py = 1
-
-    x = px / py
-    y = 1.0
-
-    if self.use_dimension:
-        x = (px * (1.0 / self.factor)) * 0.5
-        y = (py * (1.0 / self.factor)) * 0.5
-
-    verts = ((-x, -y, 0.0),
-             (+x, -y, 0.0),
-             (+x, +y, 0.0),
-             (-x, +y, 0.0),
-             )
-    faces = ((0, 1, 2, 3), )
-
-    mesh_data = bpy.data.meshes.new(img.name)
-    mesh_data.from_pydata(verts, [], faces)
-    mesh_data.update()
-    object_data_add(context, mesh_data, operator=self)
-    plane = context.scene.objects.active
-    plane.data.uv_textures.new()
-    plane.data.materials.append(material)
-    plane.data.uv_textures[0].data[0].image = img
-
-    material.game_settings.use_backface_culling = False
-    material.game_settings.alpha_blend = 'ALPHA'
-    return plane
-
-
-# -----------------------------------------------------------------------------
-# Cycles
-def get_input_links(node, nodes, links):
-    input_links = []
-    for link in links:
-        if link.to_node == node:
-            input_links.append(link)
-    
-    sorted_links = []
-    while input_links:
-        for input in node.inputs:
-            for link in input_links:
-                if link.to_socket == input:
-                    sorted_links.append(link)
-                    input_links.remove(link)
-    return sorted_links
-
+# Cycles utils.
 def get_input_nodes(node, nodes, links):
-    input_nodes = []
-    input_links = get_input_links(node, nodes, links)
-    for link in input_links:
-        input_nodes.append(link.from_node)
-    return input_nodes
+    # Get all links going to node.

@@ Diff output truncated at 10240 characters. @@


More information about the Bf-extensions-cvs mailing list