Skip to content

Commit

Permalink
Merge pull request #71 from beersandrew/custom-thumbnails
Browse files Browse the repository at this point in the history
Custom thumbnails
  • Loading branch information
jcowles authored Feb 20, 2024
2 parents e713e6e + 28179f3 commit dea8091
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#usda 1.0
(
framesPerSecond = 24
metersPerUnit = 1
timeCodesPerSecond = 24
defaultPrim = "Elephant"
upAxis = "Y"
)

def "Elephant" (
kind = "component"
prepend references = @SoC-ElephantWithMonochord.usdc@
)
{
matrix4d xformOp:transform
matrix4d xformOp:transform:xform1 = ( (1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 0, 1) )
uniform token[] xformOpOrder = ["xformOp:transform", "xformOp:transform:xform1"]
}

def Xform "cameras"
{
def Camera "thumbnail_camera"
{
float2 clippingRange = (1, 1000000)
float exposure = 0
float focalLength = 0.5
float focusDistance = 5
float fStop = 0
float horizontalAperture = 0.11787187
float horizontalApertureOffset = 0
token projection = "perspective"
double shutter:close = 0.25
double shutter:open = -0.25
float verticalAperture = 0.11787187
float verticalApertureOffset = 0
matrix4d xformOp:transform = ( (0.9910474834212168, -1.570123107919041e-9, -0.13350987081288368, 0), (-0.026176251550166015, 0.9805914400857846, -0.1943070546461696, 0), (0.13091863679115867, 0.19606230548090348, 0.9718126789203039, 0), (17.011857294812835, 39.108272051296815, 132.87990243980298, 1) )
uniform token[] xformOpOrder = ["xformOp:transform"]
}
}

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions full_assets/UsdCookie/UsdCookie_thumbnail.usda
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#usda 1.0
(
framesPerSecond = 24
metersPerUnit = 1
timeCodesPerSecond = 24
defaultPrim = "Cookie"
upAxis = "Y"
)

def "Cookie" (
kind = "component"
prepend references = @UsdCookie.usdz@
)
{
matrix4d xformOp:transform = ( (1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 0, 1) )
uniform token[] xformOpOrder = ["xformOp:transform"]
}

def Xform "cameras"
{
def Camera "thumbnail_camera"
{
float2 clippingRange = (1, 1000000)
float exposure = 0
float focalLength = 0.5
float focusDistance = 5
float fStop = 0
float horizontalAperture = 0.11787187
float horizontalApertureOffset = 0
token projection = "perspective"
double shutter:close = 0.25
double shutter:open = -0.25
float verticalAperture = 0.11787187
float verticalApertureOffset = 0
matrix4d xformOp:transform = ( (-0.9985086386209923, 1.2336458397248059e-8, -0.054593942880619695, 0), (0.0007695292388446494, 0.9999006566035167, -0.014074258367913763, 0), (0.054588519159280015, -0.014095280197856132, -0.9984094433909071, 0), (0.21233206980445954, -0.0450005203593666, -3.5206879489797465, 1) )
uniform token[] xformOpOrder = ["xformOp:transform"]
}
}

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions scripts/thumbnail-generator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ optional arguments:
- `--render-purposes` : A comma separated list of render purposes to include in the thumbnail. Valid values are: default, render, proxy, guide.
- `--directory` : A directory to generate thumbnails for all .usd, .usda, .usdc, and .usdz files. When a directory is supplied, usd-file is ignored.
- `--recursive` : Will recursively search all directories underneath a given directory, requires a directory to be set.
- `--camera` : The path to the camera prim to use for the thumbnail image

Note: You must have usd installed and available in your path. [Install Steps Here](https://github.com/PixarAnimationStudios/OpenUSD#getting-and-building-the-code)

Expand Down
33 changes: 22 additions & 11 deletions scripts/thumbnail-generator/generate_thumbnail.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,16 @@ def parse_args():
parser.add_argument('--recursive',
action='store_true',
help='Will recursively search all directories underneath a given directory, requires a directory to be set.')

parser.add_argument('--camera',
type=str,
help='The path to the camera prim to use for the thumbnail image')

return parser.parse_args()

THUMBNAIL_LAYER_SUFFIX_USDA = "_Thumbnail.usda"
THUMBNAIL_LAYER_SUFFIX = "_Thumbnail"
DEFAULT_THUMBNAIL_FILENAME = "default_thumbnail.usda"
DEFAULT_CAMERA_NAME = 'MainCamera'
THUMBNAIL_FOLDER_NAME = "thumbnails"
EXTENSIONS = ('.usd', '.usda', '.usdc', '.usdz')
RENDER_PURPOSE_MAP = {
Expand All @@ -61,22 +65,23 @@ def parse_args():
"guide": UsdGeom.Tokens.guide
}

def generate_thumbnail(usd_file, verbose, extension, render_purpose_tokens):
def generate_thumbnail(usd_file, verbose, extension, render_purpose_tokens, camera):
if verbose:
print("Step 1: Setting up the camera...")

subject_stage = Usd.Stage.Open(usd_file)
subject_file = usd_file

setup_camera(subject_stage, subject_file, render_purpose_tokens)

file_to_snapshot = usd_file if camera else get_or_create_file_to_snapshot(subject_stage, subject_file, render_purpose_tokens)
image_path = create_image_filename(usd_file, extension)
camera_to_snapshot_from = camera if camera else DEFAULT_CAMERA_NAME

if verbose:
print("Step 2: Taking the snapshot...")

image_path = create_image_filename(usd_file, extension)
return take_snapshot(image_path)
return take_snapshot(file_to_snapshot, camera_to_snapshot_from, image_path)

def setup_camera(subject_stage, usd_file, render_purpose_tokens):
def get_or_create_file_to_snapshot(subject_stage, usd_file, render_purpose_tokens):
up_axis = UsdGeom.GetStageUpAxis(subject_stage)
is_z_up = up_axis == 'Z'

Expand All @@ -89,6 +94,9 @@ def setup_camera(subject_stage, usd_file, render_purpose_tokens):
sublayer_subject(camera_stage, usd_file)
set_camera_stage_draw_mode(camera_stage, subject_stage)

return DEFAULT_THUMBNAIL_FILENAME


def create_camera(up_axis):
stage = Usd.Stage.CreateNew(DEFAULT_THUMBNAIL_FILENAME)

Expand Down Expand Up @@ -222,11 +230,10 @@ def sublayer_subject(camera_stage, input_file):

return camera_stage

def take_snapshot(image_name):
def take_snapshot(file, camera, image_name):
renderer = get_renderer()
cmd = ['usdrecord', '--camera', 'MainCamera', '--imageWidth', str(args.width), '--renderer', renderer, DEFAULT_THUMBNAIL_FILENAME, image_name]
cmd = ['usdrecord', '--camera', camera, '--imageWidth', str(args.width), '--renderer', renderer, file, image_name]
run_os_specific_command(cmd)
os.remove(DEFAULT_THUMBNAIL_FILENAME)
return image_name

def get_renderer():
Expand Down Expand Up @@ -310,7 +317,11 @@ def generate_single_thumbnail(usd_file, args):

purpose_tokens = convert_render_purposes_to_tokens(args.render_purposes)

image_name = generate_thumbnail(usd_file, args.verbose, args.output_extension, purpose_tokens)
image_name = generate_thumbnail(usd_file, args.verbose, args.output_extension, purpose_tokens, args.camera)

if not args.camera:
os.remove(DEFAULT_THUMBNAIL_FILENAME)

subject_stage = create_usdz_wrapper_stage(usd_file) if is_usdz and args.apply_thumbnail else Usd.Stage.Open(usd_file)

if args.apply_thumbnail:
Expand Down

0 comments on commit dea8091

Please sign in to comment.