Skip to content

Instantly share code, notes, and snippets.

@nitheeshkl
Created April 21, 2022 01:07
Show Gist options
  • Save nitheeshkl/5e406eec35d4eee591039837c9d4dc68 to your computer and use it in GitHub Desktop.
Save nitheeshkl/5e406eec35d4eee591039837c9d4dc68 to your computer and use it in GitHub Desktop.
Pytorch3d Load model
DATA_DIR = "dataset/"
model_file = "03001627/8d458ab12073c371caa2c06fded3ca21"
obj_file = os.path.join(
DATA_DIR, "ShapeNetCore.v2", model_file, "models/model_normalized.obj"
)
mesh = load_objs_as_meshes([obj_file], device=device)
# We scale normalize and center the target mesh to fit in a sphere of radius 1
# centered at (0,0,0). (scale, center) will be used to bring the predicted mesh
# to its original center and scale. Note that normalizing the target mesh,
# speeds up the optimization but is not necessary!
verts = mesh.verts_packed()
N = verts.shape[0]
center = verts.mean(0)
scale = max((verts - center).abs().max(0)[0])
mesh.offset_verts_(-center)
mesh.scale_verts_((1.0 / float(scale)))
# change obj mesh textures to rainbow colors
verts = mesh.verts_padded()
faces = mesh.faces_padded()
texture_rgb = (verts - verts.min()) / (verts.max() - verts.min())
texture = TexturesVertex(texture_rgb)
obj_mesh = Meshes(verts, faces, texture)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment