Skip to content

Instantly share code, notes, and snippets.

@Syfaro
Last active November 11, 2023 01:44
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save Syfaro/583e6e1a949e0156fd022db0f05625eb to your computer and use it in GitHub Desktop.
Save Syfaro/583e6e1a949e0156fd022db0f05625eb to your computer and use it in GitHub Desktop.
Download OBJ previews of products from Bad Dragon
from typing import List, Iterator
import os
import urllib.request
import json
from dataclasses import dataclass
import argparse
PRODUCT_URL = "https://bad-dragon.com/api/products"
SAVE_DIR = "models"
@dataclass
class DownloadableProduct:
name: str
preview_object_url: str
preview_texture_map_url: str
preview_normal_map_url: str
def load_products() -> List[dict]:
with urllib.request.urlopen(PRODUCT_URL) as f:
return json.load(f)
def process_products(products: List[dict]) -> Iterator[DownloadableProduct]:
for product in products:
name = product.get("sku")
preview_object_url = product.get("previewObjModel", {}).get("url")
preview_texture_map_url = product.get("previewTextureMap", {}).get("url")
preview_normal_map_url = product.get("previewNormalMap", {}).get("url")
if (
not name
or not preview_object_url
or not preview_texture_map_url
or not preview_normal_map_url
):
continue
yield DownloadableProduct(
name, preview_object_url, preview_texture_map_url, preview_normal_map_url
)
def download_file(url: str, path: str) -> None:
if os.path.exists(path):
print("Already downloaded {path}".format(path=path))
return
print("Downloading {path}".format(path=path))
urllib.request.urlretrieve(url, path)
def download_product(
product: DownloadableProduct, save_dir: str, include_maps: bool = False
) -> None:
download_file(
product.preview_object_url,
os.path.join(save_dir, "{name}.obj".format(name=product.name)),
)
if include_maps:
download_file(
product.preview_texture_map_url,
os.path.join(save_dir, "{name}_texture_map.png".format(name=product.name)),
)
download_file(
product.preview_normal_map_url,
os.path.join(save_dir, "{name}_normal_map.png".format(name=product.name)),
)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Download Bad Dragon preview objects.")
parser.add_argument(
"--maps", help="include texture and normal maps", action="store_true"
)
parser.add_argument("dir", help="Path to save models", nargs="?", default=SAVE_DIR)
args = parser.parse_args()
if not os.path.exists(args.dir):
os.mkdir(args.dir)
print("Saving objects to {dir}".format(dir=args.dir))
products = load_products()
for product in process_products(products):
download_product(product, args.dir, args.maps)
@aztehkDEV
Copy link

how do you use this?

@Manni1000
Copy link

Also if you need a normal map go on this page https://bad-dragon.com/api/products . if you serch a bit you will find the normal maps

@jruwe
Copy link

jruwe commented Feb 18, 2021

If there is anyone who wants to use this but has an issue, I was able to use this after using the command "pip3 install requests" and rerunning the script.

@sweetthrills
Copy link

sweetthrills commented Mar 1, 2021

If there is anyone who wants to use this but has an issue, I was able to use this after using the command "pip3 install requests" and rerunning the script.

How do i do that command?

I tried to run the big script and it said Python Script Failed.

@sirThomasMoore
Copy link

sirThomasMoore commented Mar 8, 2021

In order to use this, you have to install python and create a dragon-download.py file somewhere on your machine (let's say your Desktop)

Setup and use python in Windows: https://docs.python.org/3/faq/windows.html
Setup and use python in OSX: https://docs.python.org/3/using/mac.html
If you are on linux, I assume you probably already know how to use python. :D

Once you have python installed, (OSX does have it by default) It's just a matter of running python dragon-download.py in the terminal
Just follow the tutorials I posted links for. I think that should be all you need.

@TailedCrusader
Copy link

Also if you need a normal map go on this page https://bad-dragon.com/api/products . if you serch a bit you will find the normal maps

For the unfamiliar, how might one go about extracting the normal maps from that link?

@foxwearingbox
Copy link

foxwearingbox commented Apr 26, 2021

I'm getting an error every time I run the script
"Downloading model for dukepaw
Traceback (most recent call last):
File "\Text", line 58, in
File "\Text", line 33, in download_objects
File "\Text", line 45, in download_object
FileNotFoundError: [Errno 2] No such file or directory: 'models\dukepaw.obj'
Error: Python script failed, check the message in the system console" Sorry if thats the wrong format, i dont use github much. Is there any way to fix that error?

(quick edit) im running it through blender
(another quick edit) i ran it with python and it works fine now, thanks for making this!

@Thorlian
Copy link

Thorlian commented Jun 10, 2021

For the unfamiliar, how might one go about extracting the normal maps from that link?

Don't know if you still need the info, but the document (https://bad-dragon.com/api/products) contains the link to every image used on the BD website, including the normal maps. If you're searching for a specific normalmap (or anything else): ctrl+f for "title":"productname" (e.g. "title":"nox"), you'll find a section that also contains a bunch of id's. Look for "previewNormalMapId" in that section, then ctrl+f for that ID (for nox it's 1600). This should get you the url of the normal map.

@cspaul89
Copy link

cspaul89 commented Aug 8, 2021

i have tried everything created the file and coppied the script nothing worked

@Syfaro
Copy link
Author

Syfaro commented Aug 8, 2021

This script has gotten a surprising amount of use over the years! I've just revised it with a few new options, the most useful being it can now automatically download texture and normal maps by running it with the --maps option (python dragon_download.py --maps). It should also give errors if it can't create a missing models directory (and has an option to download to a different folder). I believe it's only compatible with Python 3.7 and later now.

@TommyBoyio
Copy link

I'm not sure if this script just doesn't work anymore or if I'm doing something wrong.

@sirThomasMoore
Copy link

sirThomasMoore commented Feb 25, 2022

@TommyBoyio Just tested, it still works. It has to be ran in Python 3.x I believe. I didn't do anything special to make it work.

Python version: 3.9.10

I ran python3 dragon_download.py

It downloaded all the models for me.

@devynF0401
Copy link

hey uhm it isnt working for me could someone just send me like a folder full of all the models sorry

@PhazerRazer
Copy link

Works great, but is there any way to get the internal designs of the penetrables?

@human-beep
Copy link

how do i get the textures for the different colors?

@Zrksie
Copy link

Zrksie commented Apr 18, 2023

So I’m having a really hard time using this, I’m trying to get them into blender but it keeps coming up with an error and it doesn’t say what that error is but I think it has something to do with line 85. (I’m a complete noob when it comes to python scripts and blender)

@foxwearingbox
Copy link

foxwearingbox commented Apr 18, 2023 via email

@Zrksie
Copy link

Zrksie commented Apr 18, 2023

The script wont run in blender, get the newest version of python and run the downloaded script on your desktop. It'll create a folder with the models.

So I did that now I don’t know how to get the models into blender it’s saying it’s the wrong file type (the file type is .obj)

@foxwearingbox
Copy link

foxwearingbox commented Apr 18, 2023 via email

@Zrksie
Copy link

Zrksie commented Apr 19, 2023

you have to tell it to import .obj files, its one of the options in the "files" tab i think

Thank you it worked

@foxwearingbox
Copy link

foxwearingbox commented Apr 19, 2023 via email

@WellGoblin
Copy link

is there a way to construct the model with the texture maps to give it the high res look like the preveiw?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment