Skip to content

Commit

Permalink
Update cgbookcase scraper
Browse files Browse the repository at this point in the history
  • Loading branch information
eliemichel committed Jun 24, 2021
1 parent cd7fd2f commit 471b4c8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 45 deletions.
69 changes: 25 additions & 44 deletions blender/LilySurfaceScraper/Scrapers/CgbookcaseScraper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2019 - 2020 Elie Michel
# Copyright (c) 2019 - 2021 Elie Michel
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the “Software”), to deal
Expand All @@ -21,6 +21,8 @@
# This file is part of LilySurfaceScraper, a Blender add-on to import materials
# from a single URL

import zipfile
import os
from .AbstractScraper import AbstractScraper

class CgbookcaseScraper(AbstractScraper):
Expand All @@ -41,75 +43,54 @@ def fetchVariantList(self, url):

# Get resolutions
resolutions = int(html.xpath("//meta[@name='tex1:resolution']/@content")[0])

# Has front or back-side?
# Checks for the "Front" text below the item name
# Example: https://www.cgbookcase.com/textures/autumn-leaf-22
double_sided = len(html.xpath("//div[@id='view-downloadSection']/h3")) != 0

# Get variants
variants_data = html.xpath("//div[@id='view-downloadLinks']/div")
variants = []
variants += [str(n) + "K" for n in range(resolutions, 0, -1)]
if double_sided:
front_variants = [v for v in variants]
variants += [v + " Backside" for v in front_variants]
variants += [v + " Twosided" for v in front_variants]
variants = [str(n) + "K" for n in range(resolutions, 0, -1)]

# Save some data for fetchVariant
self._html = html
self._variants_data = variants_data
self._base_name = str(html.xpath("//h1/text()")[0])
self._base_href = str(html.xpath("//div[@id='download-container']/a/@href")[0])[:-len("4K.zip")]
self._variants = variants
self._double_sided = double_sided
return variants

def fetchVariant(self, variant_index, material_data):
"""Fill material_data with data from the selected variant.
Must fill material_data.name and material_data.maps.
Return a boolean status, and fill self.error to add error messages."""
# Get data saved in fetchVariantList
html = self._html
variants_data = self._variants_data
base_name = self._base_name
base_href = self._base_href
variants = self._variants
double_sided = self._double_sided


if variant_index < 0 or variant_index >= len(variants):
self.error = "Invalid variant index: {}".format(variant_index)
return False

base_name = str(html.xpath("//h1/text()")[0])
variant_name = variants[variant_index]
material_data.name = "cgbookcase/" + base_name + "/" + variant_name

# If two sided, use several variants, and label them with is_back_side bool
n = len(variants_data)
selected_variants = [(variants_data[variant_index % n], False)]
if double_sided and variant_index >= n and variant_index < n * 1.5:
selected_variants.append((variants_data[variant_index % n + n // 2], True))
href = self._base_href + f"{variant_name}.zip"

zip_path = self.fetchZip(href, base_name, "textures.zip")
zip_dir = os.path.dirname(zip_path)
namelist = []
with zipfile.ZipFile(zip_path,"r") as zip_ref:
namelist = zip_ref.namelist()
zip_ref.extractall(zip_dir)

# Translate cgbookcase map names into our internal map names
# TODO: support two sided materials again
maps_tr = {
'Base Color': 'baseColor',
'BaseColor': 'baseColor',
'Normal': 'normal',
'Opacity': 'opacity',
'Roughness': 'roughness',
'Metallic': 'metallic',
'Height': 'height',
'AO': 'ambientOcclusion',
}
for variant_html_data, is_back_side in selected_variants:
for m in variant_html_data.xpath(".//a"):
map_url = m.attrib['href']
if not map_url.startswith("http"):
map_url = "https://www.cgbookcase.com" + map_url

temp = map_url[map_url.find("K_") + 2:-4].split("_")
map_name = " ".join(temp[1:]).title() if double_sided else " ".join(temp).title()

if map_name in maps_tr:
map_name = maps_tr[map_name]
if is_back_side:
map_name += "_back"
material_data.maps[map_name] = self.fetchImage(map_url, material_data.name, map_name)
for name in namelist:
base = os.path.splitext(name)[0]
map_type = base.split('_')[-1]
if map_type in maps_tr:
map_name = maps_tr[map_type]
material_data.maps[map_name] = os.path.join(zip_dir, name)

return True
2 changes: 1 addition & 1 deletion blender/LilySurfaceScraper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
bl_info = {
"name": "Lily Surface Scraper",
"author": "Élie Michel <elie.michel@exppad.com>",
"version": (1, 7, 1),
"version": (1, 7, 3),
"blender": (2, 82, 0),
"location": "Properties > Material",
"description": "Import material from a single URL",
Expand Down

0 comments on commit 471b4c8

Please sign in to comment.