diff options
author | Campbell Barton <ideasman42@gmail.com> | 2013-04-10 15:43:25 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2013-04-10 15:43:25 +0400 |
commit | 44a661e6a8293a1fddaca0cc1c47d21d792698ce (patch) | |
tree | a363701f6a2af0800fa6e61508418bc78e52821d /release | |
parent | 076932c7cbb5fdb9942ccb55baf4f813cc70ae08 (diff) |
py api: simple function to get an image from an objects for simple formats that only support one image per object.
Diffstat (limited to 'release')
-rw-r--r-- | release/scripts/modules/bpy_extras/object_utils.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/release/scripts/modules/bpy_extras/object_utils.py b/release/scripts/modules/bpy_extras/object_utils.py index 786b7b5460e..d1ff6c0eec7 100644 --- a/release/scripts/modules/bpy_extras/object_utils.py +++ b/release/scripts/modules/bpy_extras/object_utils.py @@ -201,3 +201,42 @@ def object_add_grid_scale(context): return space_data.grid_scale_unit return 1.0 + + +def object_image_guess(obj, bm=None): + """ + Return a single image used by the object, + first checking the texture-faces, then the material. + """ + # TODO, cycles/nodes materials + me = obj.data + if bm is None: + if obj.mode == 'EDIT': + bm = bmesh.from_edit_mesh(me) + + if bm is not None: + tex_layer = bm.faces.layers.tex.active + if tex_layer is not None: + for f in bm.faces: + image = f[tex_layer].image + if image is not None: + return image + else: + tex_layer = me.uv_textures.active + if tex_layer is not None: + for tf in tex_layer.data: + image = tf.image + if image is not None: + return image + + for m in obj.data.materials: + if m is not None: + # backwards so topmost are highest priority + for mtex in reversed(m.texture_slots): + if mtex and mtex.use_map_color_diffuse: + texture = mtex.texture + if texture and texture.type == 'IMAGE': + image = texture.image + if image is not None: + return image + return None |