Spaces:
Running
Running
Download pydraw3d/models.py from HuggingFaceSwarms/pydraw: direct link, hf CLI and curl.
- Browser
- Download file 14.7 kB
-
https://huggingface.co/spaces/HuggingFaceSwarms/pydraw/resolve/main/pydraw3d/models.py
- Command line
-
hf download hf://spaces/HuggingFaceSwarms/pydraw/pydraw3d/models.py
-
curl -L -o models.py https://huggingface.co/spaces/HuggingFaceSwarms/pydraw/resolve/main/pydraw3d/models.py
14.7 kB
| """3D concept models built from SDF primitives. Local frame: base at y=0, height 1, centred on x=z=0, the | |
| long side of animals/vehicles along x (side view). `col` is the prompt colour (or the concept default).""" | |
| import numpy as np | |
| from . import sdf as S | |
| MODELS = {} | |
| def model(*names, aspect=1.0): | |
| def deco(fn): | |
| for n in names: | |
| MODELS[n] = (fn, aspect) | |
| return fn | |
| return deco | |
| class Part: | |
| def __init__(self, fn, color, k=0.0, spec=.2, rough=.5, tex=None, emit=0.0, pat=None, **light): | |
| self.fn, self.color, self.k, self.spec, self.rough, self.tex = fn, np.asarray(color, float), k, spec, rough, tex | |
| self.emit = emit # 0..1: glows with its own colour (screens, lamps) | |
| # pat: coat / surface pattern (kind, second colour, scale): kind in stripes, spots, rosettes, patches, | |
| # bands, checker, scales. See pydraw3d.pattern. | |
| self.pat = pat | |
| # light: optional material flags read by pydraw3d/light.py (glass, ior, frost, metal, sss) | |
| self.__dict__.update(light) | |
| def V(*a): | |
| return np.array(a, np.float32) | |
| def darker(c, k): | |
| return tuple(min(255, max(0, v * k)) for v in c) | |
| # ---------------- shapes ---------------- | |
| def m_sphere(col, inst=None): | |
| col = col or (200, 60, 60) | |
| return [Part(lambda p: S.sphere(p, V(0, .5, 0), .5), col, spec=.5, rough=.25)] | |
| def m_cube(col, inst=None): | |
| col = col or (70, 120, 200) | |
| return [Part(lambda p: S.rbox(p, V(0, .5, 0), (.5, .5, .5), .04), col, spec=.3, rough=.4)] | |
| def m_cone(col, inst=None): | |
| col = col or (230, 120, 50) | |
| return [Part(lambda p: S.cone(p, V(0, 0, 0), .4, 1.0) * .8, col, spec=.3)] | |
| def m_cyl(col, inst=None): | |
| col = col or (60, 170, 110) | |
| return [Part(lambda p: S.cylinder(p, V(0, .5, 0), .35, .5, .03), col, spec=.35, rough=.35)] | |
| # ---------------- food ---------------- | |
| def m_apple(col, inst=None): | |
| col = col or (200, 40, 40) | |
| def body(p): | |
| q = p - V(0, .45, 0) | |
| r = S.length(q) | |
| # rule: an apple is a sphere pinched at top and bottom (dimples where stem and calyx sit) | |
| dimple = .12 * np.exp(-((q[:, 0] ** 2 + q[:, 2] ** 2) / .02)) * (q[:, 1] > 0) | |
| dimple2 = .06 * np.exp(-((q[:, 0] ** 2 + q[:, 2] ** 2) / .02)) * (q[:, 1] < 0) | |
| return (r - .45 + dimple + dimple2) * .8 | |
| return [Part(body, col, spec=.6, rough=.25, tex="speckle"), | |
| Part(lambda p: S.capsule(p, (0, .8, 0), (.04, 1.0, 0), .025), (90, 60, 30)), | |
| Part(lambda p: S.ellipsoid(p, V(.12, .97, 0), V(.13, .025, .06)), (70, 150, 50), spec=.2)] | |
| def m_orange(col, inst=None): | |
| col = col or (240, 140, 30) | |
| return [Part(lambda p: S.sphere(p, V(0, .48, 0), .48), col, spec=.35, rough=.5, tex="pores"), | |
| Part(lambda p: S.sphere(p, V(0, .96, 0), .04), (80, 120, 40))] | |
| # ---------------- nature ---------------- | |
| def m_tree(col, inst=None): | |
| leaf = col or (70, 140, 60) | |
| def crown(p): | |
| d = S.sphere(p, V(0, .66, 0), .27) | |
| for c, r in (((-.2, .55, .05), .2), ((.2, .57, -.04), .21), ((0, .85, 0), .2), ((.1, .5, .18), .16), ((-.08, .52, -.18), .17)): | |
| d = S.smin(d, S.sphere(p, V(*c), r), .08) | |
| # rule: leafy crowns are lumpy, not smooth spheres | |
| return d + .03 * (S.noise3(p * 14) - .5) | |
| return [Part(lambda p: S.capsule(p, (0, 0, 0), (0, .5, 0), .055) - .02 * p[:, 1] * 0, (110, 75, 45), tex="bark"), | |
| Part(crown, leaf, k=.02, spec=.05, rough=.9, tex="leaves")] | |
| def m_pine(col, inst=None): | |
| leaf = col or (40, 100, 60) | |
| def needles(p): | |
| d = S.cone(p, V(0, .18, 0), .32, .5) | |
| d = np.minimum(d, S.cone(p, V(0, .42, 0), .25, .42)) | |
| d = np.minimum(d, S.cone(p, V(0, .64, 0), .17, .36)) | |
| return d * .8 + .015 * (S.noise3(p * 20) - .5) | |
| return [Part(lambda p: S.cylinder(p, V(0, .1, 0), .05, .1), (100, 70, 45)), | |
| Part(needles, leaf, spec=.05, rough=.9, tex="leaves")] | |
| def m_rock(col, inst=None): | |
| col = col or (130, 128, 122) | |
| return [Part(lambda p: S.ellipsoid(p, V(0, .35, 0), V(.7, .5, .5)) + .06 * (S.fbm(p * 5, 3) - .5), col, rough=.9, tex="stone")] | |
| # ---------------- buildings ---------------- | |
| def m_house(col, inst=None): | |
| wall = col or (232, 214, 186) | |
| roof = (170, 70, 55) | |
| def roof_fn(p): | |
| q = p - V(0, .58, 0) | |
| # rule: a gable roof is a box clipped by two slanted planes | |
| slab = np.maximum(np.abs(q[:, 2]) - .42, -q[:, 1]) | |
| slope = (np.abs(q[:, 0]) * .62 + q[:, 1] * .78) - .42 * .78 | |
| return np.maximum(np.maximum(slab, slope), np.abs(q[:, 0]) - .66) | |
| return [Part(lambda p: S.rbox(p, V(0, .3, 0), (.5, .3, .36), .01), wall, rough=.8, tex="plaster"), | |
| Part(roof_fn, roof, rough=.7, tex="tiles"), | |
| Part(lambda p: S.rbox(p, V(.0, .16, -.365), (.07, .16, .012), .005), (110, 70, 45)), | |
| Part(lambda p: S.rbox(p, V(-.3, .33, -.365), (.09, .08, .012), .005), (150, 190, 220), spec=.9, rough=.1), | |
| Part(lambda p: S.rbox(p, V(.3, .33, -.365), (.09, .08, .012), .005), (150, 190, 220), spec=.9, rough=.1), | |
| Part(lambda p: S.rbox(p, V(.3, .9, .1), (.05, .14, .05)), (140, 90, 70))] | |
| # ---------------- animals ---------------- | |
| def m_dog(col, inst=None): | |
| fur = col or (196, 140, 80) | |
| dark = darker(fur, .55) | |
| def body(p): | |
| d = S.ellipsoid(p, V(0, .52, 0), V(.42, .2, .18)) | |
| d = S.smin(d, S.ellipsoid(p, V(.44, .8, 0), V(.17, .16, .15)), .08) # head | |
| d = S.smin(d, S.ellipsoid(p, V(.6, .74, 0), V(.12, .07, .08)), .05) # snout | |
| for x in (-.3, .28): | |
| for z in (-.1, .1): | |
| d = S.smin(d, S.capsule(p, (x, .5, z), (x + .02, .05, z), .055), .06) # legs | |
| d = S.smin(d, S.capsule(p, (-.4, .58, 0), (-.58, .8, 0), .04), .04) # tail | |
| return d | |
| return [Part(body, fur, k=0, spec=.1, rough=.8, tex="fur"), | |
| Part(lambda p: np.minimum(S.ellipsoid(p, V(.4, .86, -.15), V(.06, .13, .03)), | |
| S.ellipsoid(p, V(.4, .86, .15), V(.06, .13, .03))), dark, k=.02, rough=.8), # ears | |
| Part(lambda p: S.sphere(p, V(.715, .76, 0), .035), (25, 20, 20), spec=.8, rough=.15), # nose | |
| Part(lambda p: np.minimum(S.sphere(p, V(.56, .86, -.085), .026), S.sphere(p, V(.56, .86, .085), .026)), | |
| (20, 15, 15), spec=1.0, rough=.05)] | |
| def m_cat(col, inst=None): | |
| fur = col or (150, 150, 150) | |
| def body(p): | |
| d = S.ellipsoid(p, V(-.05, .42, 0), V(.34, .2, .16)) | |
| d = S.smin(d, S.sphere(p, V(.36, .72, 0), .16), .1) # head | |
| for x in (-.25, .22): | |
| for z in (-.08, .08): | |
| d = S.smin(d, S.capsule(p, (x, .4, z), (x, .04, z), .045), .05) | |
| d = S.smin(d, S.capsule(p, (-.36, .5, 0), (-.55, .95, .05), .035), .03) # tail up | |
| # rule: cat ears are small sharp triangles on top of a round head | |
| for z in (-.09, .09): | |
| d = np.minimum(d, S.cone(p - V(0, 0, z), V(.36, .82, 0), .07, .16) * .8) | |
| return d | |
| return [Part(body, fur, spec=.1, rough=.8, tex="fur"), | |
| Part(lambda p: np.minimum(S.sphere(p, V(.5, .76, -.065), .028), S.sphere(p, V(.5, .76, .065), .028)), (60, 160, 60), spec=1, rough=.05), | |
| Part(lambda p: S.sphere(p, V(.52, .7, 0), .02), (230, 140, 150))] | |
| # ---------------- vehicles ---------------- | |
| def m_car(col, inst=None): | |
| paint = col or (200, 40, 40) | |
| def body(p): | |
| d = S.rbox(p, V(0, .36, 0), (1.0, .2, .4), .1) | |
| d = S.smin(d, S.rbox(p, V(-.1, .66, 0), (.52, .16, .36), .12), .1) | |
| for x in (-.62, .62): | |
| d = np.maximum(d, -S.cylinder(p[:, [0, 2, 1]], V(x, 0, .2), .26, .6)) # rule: wheel arches cut into the body | |
| return d | |
| def glass(p): | |
| return S.rbox(p, V(-.1, .68, 0), (.5, .12, .37), .1) | |
| wheels = lambda p: np.minimum(S.cylinder(p[:, [0, 2, 1]], V(-.62, 0, .22), .22, .42, .05), | |
| S.cylinder(p[:, [0, 2, 1]], V(.62, 0, .22), .22, .42, .05)) | |
| return [Part(body, paint, spec=.9, rough=.15), | |
| Part(glass, (60, 80, 100), spec=1.0, rough=.05), | |
| Part(wheels, (30, 30, 32), rough=.7), | |
| Part(lambda p: np.minimum(S.sphere(p, V(1.02, .42, -.28), .06), S.sphere(p, V(1.02, .42, .28), .06)), (255, 250, 220), spec=1, rough=.1)] | |
| def m_mug(col, inst=None): | |
| col = col or (240, 240, 235) | |
| def body(p): | |
| outer = S.cylinder(p, V(0, .45, 0), .36, .45, .03) | |
| inner = S.cylinder(p, V(0, .55, 0), .31, .45) | |
| handle = S.torus(p[:, [0, 2, 1]], V(.4, 0, .45), .17, .05) | |
| return np.minimum(np.maximum(outer, -inner), handle) | |
| return [Part(body, col, spec=.8, rough=.2), | |
| Part(lambda p: S.cylinder(p, V(0, .78, 0), .31, .02), (70, 40, 25), spec=.9, rough=.1)] | |
| def m_table(col, inst=None): | |
| wood = col or (150, 100, 62) | |
| legs = lambda p: np.minimum.reduce([S.rbox(p, V(x, .46, z), (.035, .46, .035), .01) for x in (-.7, .7) for z in (-.35, .35)]) | |
| return [Part(lambda p: S.rbox(p, V(0, .95, 0), (.8, .045, .45), .015), wood, spec=.45, rough=.3, tex="wood"), | |
| Part(legs, darker(wood, .8), spec=.3, rough=.4, tex="wood")] | |
| def load_all(): | |
| """import every pydraw3d/models_*.py so their @model registrations run""" | |
| import importlib, pkgutil, os | |
| for m in pkgutil.iter_modules([os.path.dirname(__file__)]): | |
| if m.name.startswith("models_"): | |
| importlib.import_module("pydraw3d." + m.name) | |
| def fallback(col, aspect): | |
| col = col or (150, 150, 160) | |
| return [Part(lambda p: S.rbox(p, V(0, .5, 0), (.5 * aspect, .5, .3), .08), col, rough=.6)] | |
| # ---------------- night wave (render3d) ---------------- | |
| def m_snowman(col, inst=None): | |
| """rule: a snowman is three stacked snowballs (big, medium, small) with coal eyes and buttons, a carrot nose | |
| pointing at the camera, twig arms and a scarf at the neck (the generator family gave a cone-bodied person)""" | |
| snow = col or (246, 248, 252) | |
| def body(p): | |
| d = S.sphere(p, V(0, .25, 0), .26) | |
| d = S.smin(d, S.sphere(p, V(0, .6, 0), .19), .04) | |
| return S.smin(d, S.sphere(p, V(0, .86, 0), .135), .03) | |
| def coal(p): | |
| pts = [(-.045, .9, -.12), (.045, .9, -.12), (0, .66, -.185), (0, .58, -.19), (0, .5, -.175)] | |
| d = S.sphere(p, V(*pts[0]), .017) | |
| for c in pts[1:]: | |
| d = np.minimum(d, S.sphere(p, V(*c), .017)) | |
| return d | |
| nose = lambda p: S.capsule(p, (0, .86, -.12), (.012, .845, -.25), .016) | |
| arms = lambda p: np.minimum(S.capsule(p, (-.17, .64, 0), (-.34, .8, -.02), .012), | |
| S.capsule(p, (.17, .64, 0), (.33, .76, -.03), .012)) | |
| scarf = lambda p: np.minimum(S.torus(p, V(0, .745, 0), .115, .028), | |
| S.capsule(p, (.06, .73, -.1), (.08, .6, -.14), .022)) | |
| return [Part(body, snow, spec=.25, rough=.75, tex="speckle", sss=.4), Part(coal, (25, 25, 28), spec=.4, rough=.4), | |
| Part(nose, (235, 120, 30), rough=.6), Part(arms, (95, 65, 40), rough=.9, tex="bark"), | |
| Part(scarf, (200, 40, 40), rough=.85)] | |
| def m_nest(col, inst=None): | |
| """night characters 3 (nestfeed): a bird nest - a shallow bowl of twigs (a fat ring over a round bottom), three | |
| pale blue eggs inside, or (chicks_in, set by pydraw.characters) fluffy chicks with open orange beaks raised. It was | |
| the generic brown blob in 3D.""" | |
| import os as _os | |
| if dict(kv.split("=") for kv in _os.environ.get("PYDRAW_EXP", "").split(",") if "=" in kv).get("nest3d", "1") == "0": | |
| return [Part(lambda p: S.sphere(p, V(0, .5, 0), .5), col or (140, 100, 60), rough=.9)] | |
| twig = col or (140, 100, 60) | |
| parts = [Part(lambda p: S.ellipsoid(p, V(0, .32, 0), (.66, .32, .5)), darker(twig, .8), rough=.95, tex="wood"), | |
| Part(lambda p: S.torus(p, V(0, .55, 0), .56, .17), twig, rough=.9, tex="wood")] | |
| n = int(getattr(inst, "chicks_in", 0) or 0) if inst is not None else 0 | |
| if n: | |
| fl = (236, 214, 120) | |
| for k in range(n): | |
| x = (k - (n - 1) / 2) * .3 | |
| z = .06 * ((k % 2) * 2 - 1) | |
| parts.append(Part(lambda p, c=V(x, .68, z): S.sphere(p, c, .16), darker(fl, .92), rough=.95)) | |
| parts.append(Part(lambda p, c=V(x, .9, z): S.sphere(p, c, .13), fl, rough=.95)) | |
| parts.append(Part(lambda p, a=V(x, .98, z - .05), b=V(x + .02, 1.18, z - .12): S.capsule(p, a, b, .045), | |
| (240, 120, 40), spec=.4, rough=.4)) | |
| parts.append(Part(lambda p, c=V(x + .06, .94, z - .1): S.sphere(p, c, .025), (20, 16, 16), spec=.8, rough=.2)) | |
| else: | |
| for k in range(3): | |
| x = (k - 1) * .22 | |
| parts.append(Part(lambda p, c=V(x, .62, .05 * (k - 1)): S.ellipsoid(p, c, (.11, .14, .11)), (150, 205, 220), | |
| spec=.5, rough=.3)) | |
| return parts | |
| def m_puddle(col, inst=None): | |
| """night characters 2 (puddle): a flat lens of water on the ground - a dark wet lip, a glossy sky-mirroring | |
| surface (brown when muddy); a splasher throws a ring of drops (attrs splash, set by pydraw.characters).""" | |
| at = (getattr(inst, "attrs", None) or {}) if inst is not None else {} | |
| muddy = at.get("muddy") or "mud" in (getattr(inst, "word", "") or "") | |
| water = col or ((110, 82, 55) if muddy else (120, 150, 185)) | |
| lip = (70, 52, 38) if muddy else (60, 64, 70) | |
| parts = [Part(lambda p: S.ellipsoid(p, V(0, 0, 0), (2.1, .09, 1.05)), lip, rough=.8), | |
| Part(lambda p: S.ellipsoid(p, V(0, .01, 0), (1.95, .1, .95)), water, spec=1.0, rough=.06)] | |
| if at.get("splash"): | |
| drops = [(x * 1.4, .6 + .5 * (1 - abs(x)), z) for x, z in ((-1, .2), (-.7, -.4), (-.4, .5), (.4, .45), (.7, -.35), (1, .15), (-.15, -.6), (.15, .6))] | |
| def d_(p): | |
| d = None | |
| for x, y, z in drops: | |
| e = S.sphere(p, V(x, y, z), .09) | |
| d = e if d is None else np.minimum(d, e) | |
| return d | |
| parts.append(Part(d_, (150, 115, 80) if muddy else (215, 230, 245), spec=.9, rough=.1)) | |
| return parts | |