Advertisement
thureinfree

Untitled

Jun 6th, 2024 (edited)
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.13 KB | None | 0 0
  1. import hou
  2. from pxr import Usd, UsdGeom, Gf, Vt, Sdf
  3.  
  4. # Access the USD stage from the LOPS context
  5. stage = hou.pwd().editableStage()
  6.  
  7. # Function to create a box mesh with ST attributes
  8. def create_box_mesh(stage, path, translation):
  9.     mesh = UsdGeom.Mesh.Define(stage, path)
  10.    
  11.     # Define the vertices of a box
  12.     points = Vt.Vec3fArray([
  13.         (-1.0, -1.0, -1.0), (1.0, -1.0, -1.0), (1.0, 1.0, -1.0), (-1.0, 1.0, -1.0),
  14.         (-1.0, -1.0, 1.0), (1.0, -1.0, 1.0), (1.0, 1.0, 1.0), (-1.0, 1.0, 1.0)
  15.     ])
  16.    
  17.     # Translate points
  18.     points = Vt.Vec3fArray([p + translation for p in points])
  19.    
  20.     # Define the face vertex counts and indices for a box (left-handed winding order)
  21.     face_vertex_counts = Vt.IntArray([4, 4, 4, 4, 4, 4])
  22.     face_vertex_indices = Vt.IntArray([
  23.         0, 3, 2, 1,  # Bottom face
  24.         4, 5, 6, 7,  # Top face
  25.         0, 1, 5, 4,  # Front face
  26.         2, 3, 7, 6,  # Back face
  27.         0, 4, 7, 3,  # Left face
  28.         1, 2, 6, 5   # Right face
  29.     ])
  30.    
  31.     # Define the ST coordinates
  32.     st = Vt.Vec2fArray([
  33.         (0, 0), (1, 0), (1, 1), (0, 1),
  34.         (0, 0), (1, 0), (1, 1), (0, 1)
  35.     ])
  36.    
  37.     # Set the attributes on the mesh
  38.     mesh.GetPointsAttr().Set(points)
  39.     mesh.GetFaceVertexCountsAttr().Set(face_vertex_counts)
  40.     mesh.GetFaceVertexIndicesAttr().Set(face_vertex_indices)
  41.    
  42.     # Create and set the ST attribute
  43.     st_attr = mesh.CreatePrimvar("st", Sdf.ValueTypeNames.TexCoord2fArray, UsdGeom.Tokens.varying)
  44.     st_attr.Set(st)
  45.    
  46.     return mesh
  47.  
  48. # Function to get mesh data including ST attributes
  49. def get_mesh_data(mesh_prim):
  50.     print(f"processing prim: {mesh_prim}")
  51.     if mesh_prim:
  52.         points = mesh_prim.GetPointsAttr().Get()
  53.         face_vertex_counts = mesh_prim.GetFaceVertexCountsAttr().Get()
  54.         face_vertex_indices = mesh_prim.GetFaceVertexIndicesAttr().Get()
  55.         primvars_api = UsdGeom.PrimvarsAPI(mesh_prim)
  56.         st = primvars_api.GetPrimvar("st").Get()
  57.         return points, face_vertex_counts, face_vertex_indices, st
  58.  
  59. # Function to merge multiple mesh prims into a single mesh prim
  60. def merge_mesh_prims(stage, prim_paths, new_prim_path):
  61.     combined_points = Vt.Vec3fArray()
  62.     combined_face_vertex_counts = Vt.IntArray()
  63.     combined_face_vertex_indices = Vt.IntArray()
  64.     combined_st = Vt.Vec2fArray()
  65.    
  66.     offset = 0
  67.    
  68.     for prim_path in prim_paths:
  69.         mesh_prim = stage.GetPrimAtPath(prim_path)
  70.         points, face_vertex_counts, face_vertex_indices, st = get_mesh_data(UsdGeom.Mesh(mesh_prim))
  71.        
  72.         combined_points = Vt.Vec3fArray(list(combined_points) + list(points))
  73.         combined_face_vertex_counts = Vt.IntArray(list(combined_face_vertex_counts) + list(face_vertex_counts))
  74.        
  75.         adjusted_face_vertex_indices = [index + offset for index in face_vertex_indices]
  76.         combined_face_vertex_indices = Vt.IntArray(list(combined_face_vertex_indices) + adjusted_face_vertex_indices)
  77.        
  78.         combined_st = Vt.Vec2fArray(list(combined_st) + list(st))
  79.        
  80.         offset += len(points)
  81.    
  82.     # Create a new mesh prim
  83.     new_mesh_prim = UsdGeom.Mesh.Define(stage, new_prim_path)
  84.    
  85.     # Set the combined points, face_vertex_counts, face_vertex_indices, and ST attributes
  86.     new_mesh_prim.GetPointsAttr().Set(combined_points)
  87.     new_mesh_prim.GetFaceVertexCountsAttr().Set(combined_face_vertex_counts)
  88.     new_mesh_prim.GetFaceVertexIndicesAttr().Set(combined_face_vertex_indices)
  89.    
  90.     # Create and set the ST attribute
  91.     primvars_api = UsdGeom.PrimvarsAPI(new_mesh_prim)
  92.     st_attr = primvars_api.CreatePrimvar("st", Sdf.ValueTypeNames.TexCoord2fArray, UsdGeom.Tokens.faceVarying)
  93.     st_attr.Set(combined_st)
  94.    
  95.     print(f"New merged mesh prim created at: {new_prim_path}")
  96.  
  97. # Create two box meshes for testing
  98. #create_box_mesh(stage, "/World/Box1", Gf.Vec3f(-2, 0, 0))
  99. #create_box_mesh(stage, "/World/Box2", Gf.Vec3f(2, 0, 0))
  100.  
  101. # List of prim paths to merge
  102. prim_paths = ["/World/auto", "/World/single"]
  103. new_prim_path = "/New/MergedBox"
  104.  
  105. # Merge the mesh prims
  106. merge_mesh_prims(stage, prim_paths, new_prim_path)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement