Last active
December 24, 2021 14:02
-
-
Save iinfin/0dbf1225d48944ed2bff0663ddeb42ab to your computer and use it in GitHub Desktop.
generate render tiles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import hou | |
| import os | |
| def fit(input, oldmin, oldmax, newmin, newmax): | |
| return (((input - oldmin) * (newmax - newmin)) / (oldmax - oldmin)) + newmin | |
| def tile_camera(path_cam, path_rop, tiles_x=2, tiles_y=2): | |
| debug = False | |
| root = hou.node("/obj") | |
| path_cam = hou.node(path_cam).path() | |
| path_rop = hou.node(path_rop).path() | |
| if debug: | |
| hou.ui.displayMessage( | |
| text="tiles_x: {tiles_x}\ntiles_y: {tiles_y}\npath_cam: {path_cam}\npath_rop: {path_rop}".format( | |
| tiles_x=str(tiles_x), | |
| tiles_y=str(tiles_y), | |
| path_cam=str(path_cam), | |
| path_rop=str(path_rop), | |
| ) | |
| ) | |
| # delete old containers if they exist | |
| _old_container_cam = hou.node("{root}/_TILES_CAM".format(root=str(root.path()))) | |
| if _old_container_cam: | |
| _old_container_cam.destroy() | |
| _old_container_rop = hou.node("{root}/_TILES_ROP".format(root=str(root.path()))) | |
| if _old_container_rop: | |
| _old_container_rop.destroy() | |
| # cam container | |
| container_cam = root.createNode("subnet", "_TILES_CAM") | |
| container_cam.setDisplayFlag(False) | |
| container_cam.setColor(hou.Color((0, 0, 0))) | |
| fetch = container_cam.createNode("fetch") | |
| fetch.parm("useinputoffetched").set(1) | |
| fetch.parm("fetchobjpath").set(path_cam) | |
| fetch.setDisplayFlag(0) | |
| # rop container | |
| container_rop = root.createNode("ropnet", "_TILES_ROP") | |
| container_rop.setColor(hou.Color((0, 0, 0))) | |
| rop_merge = container_rop.createNode("merge", "OUT") | |
| for y in range(0, tiles_y): | |
| for x in range(0, tiles_x): | |
| # duplicate camera and reset transform | |
| newcam = container_cam.copyItems( | |
| [hou.node(path_cam)], channel_reference_originals=True | |
| )[0] | |
| newcam.setInput(0, fetch) | |
| # clear channel references for things we have to modify | |
| newcam.parm("winx").deleteAllKeyframes() | |
| newcam.parm("winy").deleteAllKeyframes() | |
| newcam.parm("winsizex").deleteAllKeyframes() | |
| newcam.parm("winsizey").deleteAllKeyframes() | |
| zoomx = 1.0 / tiles_x | |
| zoomy = 1.0 / tiles_y | |
| newcam.parm("winsizex").set(zoomx) | |
| newcam.parm("winsizey").set(zoomy) | |
| newcam.parm("winsizey").set(zoomy) | |
| # clear transform channel references | |
| for ch in ["t", "r"]: | |
| for dim in ["x", "y", "z"]: | |
| newcam.parm(ch + dim).deleteAllKeyframes() | |
| newcam.parm(ch + dim).set(0) | |
| centerx = (float(x) * zoomx) + (zoomx / 2) | |
| centery = (float(y) * zoomy) + (zoomy / 2) | |
| nx = fit(centerx, 0, 1, -1, 1) * 0.5 | |
| ny = fit(centery, 0, 1, -1, 1) * 0.5 | |
| newcam.parm("winx").set(nx) | |
| newcam.parm("winy").set(ny) | |
| # set camera resolution to be a single tile width | |
| orig_resx = hou.node(path_cam).evalParm("resx") | |
| orig_resy = hou.node(path_cam).evalParm("resy") | |
| newcam.parm("resx").deleteAllKeyframes() | |
| newcam.parm("resy").deleteAllKeyframes() | |
| newcam.parm("resx").set(orig_resx / tiles_x) | |
| newcam.parm("resy").set(orig_resy / tiles_y) | |
| # set camera name | |
| tile_num = "u{}_v{}".format(str(x).zfill(2), str(y).zfill(2)) | |
| tile_name = hou.node(path_cam).name() + "_{}".format(tile_num) | |
| newcam.setName(tile_name, True) | |
| # # duplicate rop and set camera | |
| newrop = container_rop.copyItems( | |
| [hou.node(path_rop)], channel_reference_originals=True | |
| )[0] | |
| newrop.setName(tile_name, True) | |
| newrop.parm("HO_renderCamera").deleteAllKeyframes() | |
| newrop.parm("HO_renderCamera").set(newcam.path()) | |
| # set output to match input, with UVtile as containing directory | |
| outpathparm = hou.node(path_rop).evalParm("HO_img_fileName") | |
| outpath = os.path.join( | |
| os.path.dirname(outpathparm), | |
| "{tiles_x}x{tiles_y}".format(tiles_x=tiles_x, tiles_y=tiles_y), | |
| "$F4", | |
| tile_name, | |
| ).replace("\\", "/") | |
| newrop.parm("HO_img_fileName").deleteAllKeyframes() | |
| newrop.parm("HO_img_fileName").set(outpath) | |
| if debug: | |
| hou.ui.displayMessage( | |
| text="outpath: {outpath}".format(outpath=str(outpath)) | |
| ) | |
| # merge with ROP network's output | |
| rop_merge.setNextInput(newrop) | |
| container_cam.layoutChildren() | |
| container_rop.layoutChildren() | |
| root.layoutChildren() | |
| def main(): | |
| tiles_x = hou.parm("./tiles_x").eval() | |
| tiles_y = hou.parm("./tiles_y").eval() | |
| path_cam = hou.parm("./path_cam").eval() | |
| path_rop = hou.parm("./path_rop").eval() | |
| if tiles_x and tiles_y and path_cam and path_rop: | |
| tile_camera(path_cam, path_rop, tiles_x, tiles_y) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment