Skip to content

Commit

Permalink
Updated for Blender 2.80
Browse files Browse the repository at this point in the history
  • Loading branch information
Banbury committed Nov 16, 2019
1 parent 9711a71 commit 4f30d4c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 36 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Import image as mesh

This is an add-on for [Blender](http://www.blender.org), that imports images as meshes. If the image contains alpha
This is an add-on for [Blender 2.80](http://www.blender.org), that imports images as meshes. If the image contains alpha
transparency, the mesh will have the shape of the opaque parts of the image. The resulting image is scaled according
to its resolution in inches.

Expand All @@ -9,7 +9,7 @@ to its resolution in inches.
Download the repository as Zip file. Then install the add-on in Blender with
File/User Preferences/Add-ons/Install from file.

Rename the directory in the 'addons' folder to 'io_image_as_mesh'.
Rename the directory in the folder `C:\Users\<username>\AppData\Roaming\Blender Foundation\Blender\2.80\scripts\addons` to `io_image_as_mesh`.

## Usage

Expand All @@ -19,7 +19,7 @@ Check `Subdivide` to create a mesh with mostly equally sized triangles.
Currently only PNGs are allowed. Images without alpha information (like JPGs) should be imported with
with the 'Images as Planes' addon.

> If you cannot see the image, change the viewport shading to 'Texture'.
> If you cannot see the image, change the viewport shading to 'Look Dev' or 'Rendered'.
## Implementation details

Expand All @@ -31,11 +31,14 @@ Finally uv coordintes are calculated, a material is created and the mesh is scal

## License information

The [RDP library](https://github.com/sebleier/RDP) is licensed under the BSD.
The [RDP library](https://github.com/sebleier/RDP) is licensed under the MIT.

Everything else is GPL licensed.

## Changelog
**v1.3.0:**
* Updated for Blender 2.80
* If you're still using Blender 2.79, use the previous version.

**v1.2.0:**
* Better subdivision with Subdivision and Decimate modifiers
Expand Down
20 changes: 10 additions & 10 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"name": "Image as Mesh Importer",
"description": "Import Image as Mesh.",
"author": "Banbury",
"version": (1, 2, 0),
"blender": (2, 79, 0),
"version": (1, 3, 0),
"blender": (2, 80, 0),
"location": "File > Import",
"wiki_url": "",
"category": "Import-Export"}
Expand All @@ -28,16 +28,16 @@ class ImageAsMeshOps(bpy.types.Operator, ImportHelper):
bl_options = {'REGISTER', 'UNDO', 'PRESET'}

# File type filter in file browser
filename_ext = ".png"
filename_ext: ".png"

filter_glob = bpy.props.StringProperty(
filter_glob: bpy.props.StringProperty(
default="*.png",
options={'HIDDEN'}
)

files = CollectionProperty(type=bpy.types.PropertyGroup)
files: CollectionProperty(type=bpy.types.PropertyGroup)

subdivide = bpy.props.BoolProperty(
subdivide: bpy.props.BoolProperty(
name="Subdivide", description="Creates smaller triangles.")

def execute(self, context):
Expand Down Expand Up @@ -69,14 +69,14 @@ def menu_import(self, context):

def register():
"""Register Menu Listing"""
bpy.utils.register_module(__package__)
bpy.types.INFO_MT_file_import.append(menu_import)
bpy.utils.register_class(ImageAsMeshOps)
bpy.types.TOPBAR_MT_file_import.append(menu_import)


def unregister():
"""Unregister Menu Listing"""
bpy.utils.unregister_module(__package__)
bpy.types.INFO_MT_file_import.remove(menu_import)
bpy.utils.unregister_class(ImageAsMeshOps)
bpy.types.TOPBAR_MT_file_import.remove(menu_import)


if __name__ == "__main__":
Expand Down
27 changes: 5 additions & 22 deletions image_as_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,24 @@ def create_sprite(poly, img):
mesh_data.update()

obj = bpy.data.objects.new(img.name, mesh_data)
scene.objects.link(obj)
obj.select = True
scene.objects.active = obj
scene.collection.objects.link(obj)
obj.select_set(True)
bpy.context.view_layer.objects.active = obj
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')

bm = bmesh.from_edit_mesh(obj.data)
bm.verts.index_update()

bmesh.ops.triangle_fill(bm, edges=bm.edges[:], use_dissolve=False, use_beauty=True)
bmesh.ops.triangle_fill(
bm, edges=bm.edges[:], use_dissolve=False, use_beauty=True)

if hasattr(bm.verts, "ensure_lookup_table"):
bm.verts.ensure_lookup_table()

uvtex = bm.faces.layers.tex.new("UVMap")
uv_lay = bm.loops.layers.uv.new("UVMap")

for face in bm.faces:
face[uvtex].image = img
for loop in face.loops:
uv = loop[uv_lay].uv
index = loop.vert.index
Expand All @@ -100,9 +99,6 @@ def create_sprite(poly, img):
mat = create_blender_material(obj, img)
create_cycles_material(mat, img)

if scene.render.engine != 'CYCLES':
mat.use_nodes = False

return obj


Expand All @@ -111,21 +107,8 @@ def create_blender_material(obj, img):
tex.image = img

mat = bpy.data.materials.new(name="Material")
mat.use_transparency = True
mat.emit = 1.0
mat.alpha = 0.0
obj.data.materials.append(mat)

texslot = mat.texture_slots.add()
texslot.texture = tex
texslot.texture_coords = 'UV'
texslot.use_map_color_diffuse = True
texslot.use_map_color_emission = True
texslot.emission_color_factor = 0.5
texslot.use_map_density = True
texslot.mapping = 'FLAT'
texslot.use_map_alpha = True

return mat


Expand Down

0 comments on commit 4f30d4c

Please sign in to comment.