Skip to content

Commit

Permalink
Some minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
lucianjames committed Aug 26, 2021
1 parent b4ca711 commit 83eaad9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
22 changes: 18 additions & 4 deletions ALT_CC0AssetDownloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,27 @@
try:
import requests
except:
print('Module "requests" not found, attempting to install...')
import subprocess
print('Module "requests" not found, attempting to install...')
subprocess.check_call([sys.executable, "-m", "pip", "install", "requests"])
import requests


# Converts the string "None" to Nonetype
def strToNoneType(x):
if x == 'None':
x = None
return x


# Converts string "True"/"False" to actual boolean True/False values
def strToBool(x):
if x == 'True':
return True
if x == 'False':
return False


# Removes any items from the assets list that dont contain the specified keyword in assetId
def filterByKeyword(assets, keyword):
i = 0
Expand All @@ -34,6 +38,7 @@ def filterByKeyword(assets, keyword):
i+=1
return assets


# Removes any items from the assets list that dont have the specified attribute
def filterByDownloadAttribute(assets, attribute):
i = 0
Expand All @@ -44,6 +49,7 @@ def filterByDownloadAttribute(assets, attribute):
i+=1
return assets


# Removes any items from the assets list that dont have the specified file extension
def filterByFileExtension(assets, extension):
i = 0
Expand All @@ -54,6 +60,7 @@ def filterByFileExtension(assets, extension):
i+=1
return assets


# Calls the above filtering functions if the inputs for those filters are not None
def getAssetsByFilters(assets, assetfilters):
assetsCopy = copy.deepcopy(assets) # Deepcopy to avoid modifying original assets list (Not really important to do this)
Expand All @@ -65,6 +72,7 @@ def getAssetsByFilters(assets, assetfilters):
assetsCopy = filterByFileExtension(assetsCopy, assetfilters[2])
return assetsCopy


def download(assets, saveLocation, unZip, deleteZips, skipDuplicates):
print("Downloading...")
for i in assets: # i = ['assetId', 'downloadAttribute', 'filetype', 'size', 'downloadLink', 'rawLink']
Expand Down Expand Up @@ -101,8 +109,8 @@ def download(assets, saveLocation, unZip, deleteZips, skipDuplicates):
yesInputs = ["y", "yes", "yes please"]
noInputs = ["n", "no", "no thank you"]


#AssetLibraryTools will do the input checking for this script, we just need to do some conversions
#strToNoneType converts a string to None if the string is 'None'
print(sys.argv)
saveLocation = sys.argv[1] + '/'
keywordFilter = strToNoneType(sys.argv[2])
Expand All @@ -112,34 +120,39 @@ def download(assets, saveLocation, unZip, deleteZips, skipDuplicates):
deleteZips = strToBool(sys.argv[6])
skipDuplicates = strToBool(sys.argv[6])


# Download asset data csv file
# CSV file is formatted like this:
#['assetId', 'downloadAttribute', 'filetype', 'size', 'downloadLink', 'rawLink']
# For some reason it wont download the file unless you send a "User-Agent" header
print("Downloading asset data from https://ambientcg.com/api/v2/downloads_csv\n")
headers = {'User-Agent' : 'LJ3DSCRIPT'}
url = 'https://ambientcg.com/api/v2/downloads_csv'
print("Downloading asset data from https://ambientcg.com/api/v2/downloads_csv\n")
r = requests.get(url, allow_redirects=True, headers=headers)
filename = re.findall('filename=(.+)', r.headers.get('content-disposition'))[0]
open(filename, 'wb').write(r.content) # Save downloaded file to disk


# Open downloaded asset data csv file
with open(filename, newline='') as f:
reader = csv.reader(f)
assets = list(reader)
assets.pop(0) # Remove the 1st item since its not asset data, its column info
print("Loaded csv file and found {0} assets\n".format(len(assets)))

# Filter the assets

# Filter and sort the assets
filteredAssets = getAssetsByFilters(assets, [keywordFilter, attributeFilter, extensionFilter])
filteredAssets.sort()


# Get the total size in bytes of the filtered assets
filteredTotalSize = 0
for i in filteredAssets:
filteredTotalSize += int(i[3])
print("=====\nFound {0} assets that match the filters, with a combined size of {1} bytes ({2} gigabytes)".format(len(filteredAssets), filteredTotalSize, filteredTotalSize/1e+9))


print("=====\nDisplay asset names? (y/n)")
while True:
userInput = input()
Expand All @@ -151,6 +164,7 @@ def download(assets, saveLocation, unZip, deleteZips, skipDuplicates):
break
print("Invalid input")


print("=====\nWould you like to download these assets? (y/n)")
while True:
userInput = input()
Expand Down
36 changes: 18 additions & 18 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"name": "AssetLibraryTools",
"description": "AssetLibraryTools is a free addon which aims to speed up the process of creating asset libraries with the asset browser, This addon is currently very much experimental as is the asset browser in blender.",
"author": "Lucian James (LJ3D)",
"version": (0, 1, 5),
"version": (0, 1, 6),
"blender": (3, 0, 0),
"location": "3D View > Tools",
"warning": "Developed in 3.0 ALPHA. May be unstable or broken in future versions", # used for warning icon and text in addons panel
"wiki_url": "",
"tracker_url": "",
"wiki_url": "https://github.com/LJ3D/AssetLibraryTools/wiki",
"tracker_url": "https://github.com/LJ3D/AssetLibraryTools",
"category": "3D View"
}

Expand Down Expand Up @@ -88,7 +88,7 @@ def draw(self, context):
self.layout.label(text=message)
bpy.context.window_manager.popup_menu(draw, title = title, icon = icon)

# class with functions for setting up shaders
# Class with functions for setting up shaders
class shaderSetup():

def createNode(mat, type, name="newNode", location=(0,0)):
Expand Down Expand Up @@ -495,9 +495,9 @@ class OT_BatchImportPBR(Operator):
def execute(self, context):
scene = context.scene
tool = scene.assetlibrarytools
i = 0 # Number of materials imported
i2 = 0 # Number of materials deleted (due to no textures after import)
i3 = 0 # Number of materials skipped due to them already existing
n_imp = 0 # Number of materials imported
n_del = 0 # Number of materials deleted (due to no textures after import)
n_skp = 0 # Number of materials skipped due to them already existing
existing_mat_names = []
subdirectories = [x for x in pathlib.Path(tool.mat_import_path).iterdir() if x.is_dir()] # Get subdirs in directory selected in UI
for sd in subdirectories:
Expand All @@ -521,27 +521,27 @@ def execute(self, context):
hasTex = True
if hasTex == False:
bpy.data.materials.remove(mat) # Delete material if it contains no textures
i2 += 1
n_del += 1
else:
i += 1
n_imp += 1
else:
i3 += 1
if (i2 > 0) and (i3 > 0):
DisplayMessageBox("Complete, {0} materials imported, {1} were deleted after import because they contained no textures (No recognised textures were found in the folder), {2} skipped because they already exist".format(i,i2,i3))
elif i3 > 0:
DisplayMessageBox("Complete, {0} materials imported. {1} skipped because they already exist".format(i, i3))
elif i2 > 0:
DisplayMessageBox("Complete, {0} materials imported, {1} were deleted after import because they contained no textures (No recognised textures were found in the folder)".format(i,i2))
n_skip += 1
if (n_del > 0) and (n_skp > 0):
DisplayMessageBox("Complete, {0} materials imported, {1} were deleted after import because they contained no textures (No recognised textures were found in the folder), {2} skipped because they already exist".format(n_imp,n_del,n_skp))
elif n_skp > 0:
DisplayMessageBox("Complete, {0} materials imported. {1} skipped because they already exist".format(n_imp, n_skp))
elif n_del > 0:
DisplayMessageBox("Complete, {0} materials imported, {1} were deleted after import because they contained no textures (No recognised textures were found in the folder)".format(n_imp,n_del))
else:
DisplayMessageBox("Complete, {0} materials imported".format(i))
DisplayMessageBox("Complete, {0} materials imported".format(n_imp))
return{'FINISHED'}


class OT_ImportModels(Operator):
bl_label = "Import models"
bl_idname = "alt.importmodels"

# hide new objects works by comparing a list of objects before (x) happened with the current list via bpy.context.scene.objects to get the list of new objects, then hides those new objects
# Hide new objects works by comparing a list of objects before (x) happened with the current list via bpy.context.scene.objects to get the list of new objects, then hides those new objects
def hideNewObjects(old_objects):
scene = bpy.context.scene
tool = scene.assetlibrarytools
Expand Down

0 comments on commit 83eaad9

Please sign in to comment.