How to create a USD file with a sphere via Python

Hello devs,

After 20++ years in CG I was able to create a poly plane!

I am playing with USD Python API trying to understand the lowest level of this beast.

This is a quite messy process for me but, as usual, when I learn new exciting stuff, I record my findings in the form of an article or tutorial. Here is the USD Python API exploration which I would like to share.

Here is the code for the procedural plane:

def plane(row_points, column_points):
    """
    Create polygonal grid (size 2x2 units)

    int[] faceVertexCounts =  [4]
    int[] faceVertexIndices = [0, 1, 2, 3]
    point3f[] points =        [(-5, 0, -5), (5, 0, -5), (5, 0, 5), (-5, 0, 5)]
    """

    points = []  # List of point positions
    face_vertex_counts = []  # List of vertex count per face
    face_vertex_indices = []  # List of vertex indices

    # Spacing between points
    width = 2
    height = 2
    row_spacing = height / (row_points - 1)
    col_spacing = width / (column_points - 1)

    # Generate points for the grid
    for row_point in range(row_points):
        for column_point in range(column_points):
            x = column_point * col_spacing - width / 2
            z = row_point * row_spacing - height / 2
            points.append((x, 0, z))

    # Define faces using the indices of the grid points
    for row_point in range(row_points - 1):
        for column_point in range(column_points - 1):
            # Calculate the indices of the corners of the cell
            top_left = row_point * column_points + column_point
            top_right = top_left + 1
            bottom_left = top_left + column_points
            bottom_right = bottom_left + 1

            # Define the face using the indices of the 4 corners
            face_vertex_indices.extend([top_left, top_right, bottom_right, bottom_left])
            face_vertex_counts.append(4)

    geometry_data = {'points': points,
                     'face_vertex_counts': face_vertex_counts,
                     'face_vertex_indices': face_vertex_indices}

    return geometry_data

I started with the creation of a USD file, then I tried to export existing geometry to this USD file from Maya and Houdini and now I am reinventing the wheel, understanding how to create geometry with code. The latest milestone is a sphere:

And I hope to continue the exploration :slight_smile:

3 Likes

I updated the article, and now it looks more like a tutorial where we move from simplest to more complex stuff trying to reinvent the wheel in creating procedural geometry :slight_smile:

Creating Procedural Geometry with Python and recording it to a USD file.

Hey this is super useful to me! It’s something that I’ve been looking to dig into forever but never really figured out where to dig into it. Rad! Thanks for sharing it.

1 Like

Then you can find this guy even more useful:

He has several articles on that topic (check his blog).

Initially, I was trying to come up with the solution by myself, then I asked Chat GPT for help to get working solution.