Datasets:
Download code/robohome/src/tools/asset_curation/render6.py from RoboParty/RoboHome: direct link, hf CLI and curl.
- Browser
- Download file 3.1 kB
-
https://huggingface.co/datasets/RoboParty/RoboHome/resolve/main/code/robohome/src/tools/asset_curation/render6.py
- Command line
-
hf download hf://datasets/RoboParty/RoboHome/code/robohome/src/tools/asset_curation/render6.py
-
curl -L -o render6.py https://huggingface.co/datasets/RoboParty/RoboHome/resolve/main/code/robohome/src/tools/asset_curation/render6.py
3.1 kB
| import bpy,sys,json,math,os,mathutils,time | |
| argv=sys.argv[sys.argv.index("--")+1:] | |
| spec=json.load(open(argv[0])); outdir=argv[1]; os.makedirs(outdir,exist_ok=True) | |
| AXV={"X":mathutils.Vector((1,0,0)),"Y":mathutils.Vector((0,1,0)),"Z":mathutils.Vector((0,0,1))} | |
| RES=448 | |
| for a in spec: | |
| key=a["key"] | |
| if os.path.exists(f"{outdir}/{key}_views.json"): continue | |
| t0=time.time() | |
| bpy.ops.wm.read_factory_settings(use_empty=True); sc=bpy.context.scene | |
| try: | |
| if a["path"].lower().endswith((".glb",".gltf")): bpy.ops.import_scene.gltf(filepath=a["path"]) | |
| else: bpy.ops.wm.usd_import(filepath=a["path"]) | |
| except Exception as e: | |
| json.dump(dict(key=key,error="import:"+str(e)[:200]),open(f"{outdir}/{key}_views.json","w")); continue | |
| meshes=[o for o in sc.objects if o.type=="MESH"] | |
| if not meshes: json.dump(dict(key=key,error="no_mesh"),open(f"{outdir}/{key}_views.json","w")); continue | |
| # neutral fallback material: the source materials rendered pure black | |
| mat=bpy.data.materials.new("fallback_grey"); mat.use_nodes=True | |
| b=mat.node_tree.nodes.get("Principled BSDF"); b.inputs["Base Color"].default_value=(0.62,0.60,0.57,1); b.inputs["Roughness"].default_value=0.75 | |
| for o in meshes: | |
| o.data.materials.clear(); o.data.materials.append(mat) | |
| pts=[o.matrix_world@mathutils.Vector(c) for o in meshes for c in o.bound_box] | |
| mn=mathutils.Vector((min(p.x for p in pts),min(p.y for p in pts),min(p.z for p in pts))) | |
| mx=mathutils.Vector((max(p.x for p in pts),max(p.y for p in pts),max(p.z for p in pts))) | |
| ctr=(mn+mx)/2; size=(mx-mn).length; ext=[mx[i]-mn[i] for i in range(3)] | |
| up=AXV[a["vertical_axis"]]; hor=a["horizontal_axes"] | |
| dirs=[AXV[hor[0]],-AXV[hor[0]],AXV[hor[1]],-AXV[hor[1]]]; names=["+"+hor[0],"-"+hor[0],"+"+hor[1],"-"+hor[1]] | |
| sc.render.engine="CYCLES"; sc.cycles.device="CPU"; sc.cycles.samples=16; sc.cycles.use_denoising=False | |
| sc.render.resolution_x=RES; sc.render.resolution_y=RES; sc.render.image_settings.file_format="PNG" | |
| w=bpy.data.worlds.new("w"); sc.world=w; w.use_nodes=True; w.node_tree.nodes["Background"].inputs[0].default_value=(0.78,0.78,0.78,1) | |
| cam=bpy.data.objects.new("cam",bpy.data.cameras.new("cam")); sc.collection.objects.link(cam); sc.camera=cam | |
| cam.data.type="ORTHO"; cam.data.ortho_scale=size*1.15; cam.data.clip_start=0.01; cam.data.clip_end=size*20 | |
| lt=bpy.data.objects.new("sun",bpy.data.lights.new("sun","SUN")); lt.data.energy=3.0; sc.collection.objects.link(lt) | |
| for i,d in enumerate(dirs): | |
| el=math.radians(20); pos=ctr+(d*math.cos(el)+up*math.sin(el))*size*2.0; cam.location=pos | |
| fwd=(ctr-pos).normalized(); right=fwd.cross(up).normalized(); upv=right.cross(fwd).normalized() | |
| m=mathutils.Matrix((right,upv,-fwd)).transposed(); cam.rotation_euler=m.to_euler(); lt.rotation_euler=m.to_euler() | |
| sc.render.filepath=f"{outdir}/{key}_p{i}.png"; bpy.ops.render.render(write_still=True) | |
| json.dump(dict(key=key,view_axis=names,center=list(ctr),size=size,ext=ext,secs=round(time.time()-t0,2)),open(f"{outdir}/{key}_views.json","w")) | |