-
-
Notifications
You must be signed in to change notification settings - Fork 404
Blender thumbnail support #273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
dac3464
Update thumb_renderer.py
050011-code a55be5d
Add files via upload
050011-code 4cb8723
Update thumb_renderer.py
050011-code e285b0d
Update blender_thumbnailer.py
050011-code 3e36556
Update thumb_renderer.py
050011-code 5fbff38
Update thumb_renderer.py
050011-code 699e7d4
Update thumb_renderer.py
050011-code 33940b5
Update blender_thumbnailer.py
050011-code 5b6fad9
Update blender_thumbnailer.py
050011-code 83390f7
Update blender_thumbnailer.py
050011-code 0ab4348
Update thumb_renderer.py
050011-code dbf10f2
Merge branch 'TagStudioDev:main' into main
050011-code 21573e8
Update constants.py
050011-code f7e9da3
Update blender_thumbnailer.py
050011-code efaa89b
Update thumb_renderer.py
050011-code acbeb20
Update blender_thumbnailer.py
050011-code a540c05
Update thumb_renderer.py
050011-code File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# ##### BEGIN GPL LICENSE BLOCK ##### | ||
# | ||
# This program is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU General Public License | ||
# as published by the Free Software Foundation; either version 2 | ||
# of the License, or (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program; if not, write to the Free Software Foundation, | ||
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
# | ||
# ##### END GPL LICENSE BLOCK ##### | ||
|
||
# <pep8 compliant> | ||
|
||
|
||
## This file is a modified script that gets the thumbnail data stored in a blend file | ||
|
||
|
||
import struct | ||
from PIL import ( | ||
Image, | ||
ImageOps, | ||
) | ||
import gzip | ||
import os | ||
|
||
|
||
def blend_extract_thumb(path): | ||
REND = b"REND" | ||
TEST = b"TEST" | ||
|
||
blendfile = open(path, "rb") | ||
|
||
head = blendfile.read(12) | ||
|
||
if head[0:2] == b"\x1f\x8b": # gzip magic | ||
blendfile.close() | ||
blendfile = gzip.GzipFile("", "rb", 0, open(path, "rb")) | ||
head = blendfile.read(12) | ||
|
||
if not head.startswith(b"BLENDER"): | ||
blendfile.close() | ||
return None, 0, 0 | ||
|
||
is_64_bit = head[7] == b"-"[0] | ||
|
||
# true for PPC, false for X86 | ||
is_big_endian = head[8] == b"V"[0] | ||
|
||
# blender pre 2.5 had no thumbs | ||
if head[9:11] <= b"24": | ||
return None, 0, 0 | ||
|
||
sizeof_bhead = 24 if is_64_bit else 20 | ||
int_endian = ">i" if is_big_endian else "<i" | ||
int_endian_pair = int_endian + "i" | ||
|
||
while True: | ||
bhead = blendfile.read(sizeof_bhead) | ||
|
||
if len(bhead) < sizeof_bhead: | ||
return None, 0, 0 | ||
|
||
code = bhead[:4] | ||
length = struct.unpack(int_endian, bhead[4:8])[0] # 4 == sizeof(int) | ||
|
||
if code == REND: | ||
blendfile.seek(length, os.SEEK_CUR) | ||
else: | ||
break | ||
|
||
if code != TEST: | ||
return None, 0, 0 | ||
|
||
try: | ||
x, y = struct.unpack(int_endian_pair, blendfile.read(8)) # 8 == sizeof(int) * 2 | ||
except struct.error: | ||
return None, 0, 0 | ||
|
||
length -= 8 # sizeof(int) * 2 | ||
|
||
if length != x * y * 4: | ||
return None, 0, 0 | ||
|
||
image_buffer = blendfile.read(length) | ||
|
||
if len(image_buffer) != length: | ||
return None, 0, 0 | ||
|
||
return image_buffer, x, y | ||
|
||
|
||
def blend_thumb(file_in): | ||
buf, width, height = blend_extract_thumb(file_in) | ||
image = Image.frombuffer( | ||
"RGBA", | ||
(width, height), | ||
buf, | ||
) | ||
image = ImageOps.flip(image) | ||
return image |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.