Skip to content

Instantly share code, notes, and snippets.

@alankent
Last active May 11, 2024 05:39
Show Gist options
  • Save alankent/828eff3919146af11ef470bc742ce7be to your computer and use it in GitHub Desktop.
Save alankent/828eff3919146af11ef470bc742ce7be to your computer and use it in GitHub Desktop.
// JavaScript code for Adobe Illustrator ExtendScript
// Get a handle to the current document
var doc = app.activeDocument;
// Start building up the USDA file
var usda = """#usda 1.0
(
defaultPrim = "World"
endTimeCode = 100
metersPerUnit = 0.01
startTimeCode = 0
timeCodesPerSecond = 60
upAxis = "Y"
)
def Xform "World"
{
""";
// Loop through all the page items (graphics) in the document
for (var i = 0; i < doc.pageItems.length; i++) {
var item = doc.pageItems[i];
// Extract position and rotation
// This gets corner, not center.
//var position = [item.position[0], item.position[1]];
// Get the bounding box of the object to work out center.
var bounds = item.geometricBounds;
var centerX = (bounds[0] + bounds[2]) / 2;
var centerY = (bounds[1] + bounds[3]) / 2;
var position = [centerX, centerY];
// Extract rotation
var rotation;
if (item.file) {
// For PlacedItems, get rotation from transformation matrix
rotation = Math.atan2(item.matrix.mValueC, item.matrix.mValueD) * (180 / Math.PI);
usda += ' def "' + item.name + '_' + i + '" (\n';
usda += ' prepend payload = @./' + item.name + '.usd@\n';
usda += ' )\n';
usda += ' {\n';
usda += ' float3 xformOp:rotateXYZ = (0, ' + rotation + ', 0)\n';
usda += ' float3 xformOp:scale = (1, 1, 1)\n';
usda += ' double3 xformOp:translate = (' + position[0] + ', 1, ' + -position[1] + ')\n';
usda += ' uniform token[] xformOpOrder = ["xformOp:translate", "xformOp:rotateXYZ", "xformOp:scale"]\n';
usda += ' }\n';
usda += '\n';
}
}
// Close out the USDA file. (I copied this from an Omniverse created stage.)
usda += '}\n';
usda += '\n';
usda += 'def Xform "Environment"\n';
usda += '{\n';
usda += ' int ground:size = 5000\n';
usda += ' string ground:type = "On"\n';
usda += ' double3 xformOp:rotateXYZ = (0, 0, 0)\n';
usda += ' double3 xformOp:scale = (1, 1, 1)\n';
usda += ' double3 xformOp:translate = (0, 0, 0)\n';
usda += ' uniform token[] xformOpOrder = ["xformOp:translate", "xformOp:rotateXYZ", "xformOp:scale"]\n';
usda += '\n';
usda += ' def DomeLight "Sky" (\n';
usda += ' prepend apiSchemas = ["ShapingAPI"]\n';
usda += ' )\n';
usda += ' {\n';
usda += ' float inputs:colorTemperature = 6250\n';
usda += ' bool inputs:enableColorTemperature = 1\n';
usda += ' float inputs:exposure = 9\n';
usda += ' float inputs:intensity = 1\n';
usda += ' float inputs:shaping:cone:angle = 180\n';
usda += ' float inputs:shaping:cone:softness\n';
usda += ' float inputs:shaping:focus\n';
usda += ' color3f inputs:shaping:focusTint\n';
usda += ' asset inputs:shaping:ies:file\n';
usda += ' asset inputs:texture:file = @https://omniverse-content-production.s3.us-west-2.amazonaws.com/Assets/Scenes/Templates/Default/SubUSDs/textures/CarLight_512x256.hdr@\n';
usda += ' token inputs:texture:format = "latlong"\n';
usda += ' token visibility = "inherited"\n';
usda += ' double3 xformOp:rotateXYZ = (0, -90, -90)\n';
usda += ' double3 xformOp:scale = (1, 1, 1)\n';
usda += ' double3 xformOp:translate = (0, 305, 0)\n';
usda += ' uniform token[] xformOpOrder = ["xformOp:translate", "xformOp:rotateXYZ", "xformOp:scale"]\n';
usda += ' }\n';
usda += '\n';
usda += ' def DistantLight "DistantLight" (\n';
usda += ' prepend apiSchemas = ["ShapingAPI"]\n';
usda += ' )\n';
usda += ' {\n';
usda += ' float inputs:angle = 2.5\n';
usda += ' float inputs:colorTemperature = 7250\n';
usda += ' bool inputs:enableColorTemperature = 1\n';
usda += ' float inputs:exposure = 10\n';
usda += ' float inputs:intensity = 1\n';
usda += ' float inputs:shaping:cone:angle = 180\n';
usda += ' float inputs:shaping:cone:softness\n';
usda += ' float inputs:shaping:focus\n';
usda += ' color3f inputs:shaping:focusTint\n';
usda += ' asset inputs:shaping:ies:file\n';
usda += ' token visibility = "inherited"\n';
usda += ' double3 xformOp:rotateXYZ = (-105, 0, 0)\n';
usda += ' double3 xformOp:scale = (1, 1, 1)\n';
usda += ' double3 xformOp:translate = (0, 305, 0)\n';
usda += ' uniform token[] xformOpOrder = ["xformOp:translate", "xformOp:rotateXYZ", "xformOp:scale"]\n';
usda += ' }\n';
usda += '\n';
usda += ' def Scope "Looks"\n';
usda += ' {\n';
usda += ' def Material "Grid"\n';
usda += ' {\n';
usda += ' token outputs:mdl:displacement.connect = </Environment/Looks/Grid/Shader.outputs:out>\n';
usda += ' token outputs:mdl:surface.connect = </Environment/Looks/Grid/Shader.outputs:out>\n';
usda += ' token outputs:mdl:volume.connect = </Environment/Looks/Grid/Shader.outputs:out>\n';
usda += '\n';
usda += ' def Shader "Shader"\n';
usda += ' {\n';
usda += ' uniform token info:implementationSource = "sourceAsset"\n';
usda += ' uniform asset info:mdl:sourceAsset = @OmniPBR.mdl@\n';
usda += ' uniform token info:mdl:sourceAsset:subIdentifier = "OmniPBR"\n';
usda += ' float inputs:albedo_add = 0\n';
usda += ' float inputs:albedo_brightness = 0.52\n';
usda += ' float inputs:albedo_desaturation = 1\n';
usda += ' asset inputs:diffuse_texture = @https://omniverse-content-production.s3.us-west-2.amazonaws.com/Assets/Scenes/Templates/Default/SubUSDs/textures/ov_uv_grids_basecolor_1024.png@ (\n';
usda += ' colorSpace = "sRGB"\n';
usda += ' customData = {\n';
usda += ' asset default = @@\n';
usda += ' }\n';
usda += ' )\n';
usda += ' bool inputs:project_uvw = 0\n';
usda += ' float inputs:reflection_roughness_constant = 0.333\n';
usda += ' float inputs:texture_rotate = 0 (\n';
usda += ' customData = {\n';
usda += ' float default = 0\n';
usda += ' }\n';
usda += ' )\n';
usda += ' float2 inputs:texture_scale = (0.5, 0.5) (\n';
usda += ' customData = {\n';
usda += ' float2 default = (1, 1)\n';
usda += ' }\n';
usda += ' )\n';
usda += ' float2 inputs:texture_translate = (0, 0) (\n';
usda += ' customData = {\n';
usda += ' float2 default = (0, 0)\n';
usda += ' }\n';
usda += ' )\n';
usda += ' bool inputs:world_or_object = 0 (\n';
usda += ' customData = {\n';
usda += ' bool default = 0\n';
usda += ' }\n';
usda += ' )\n';
usda += ' token outputs:out (\n';
usda += ' renderType = "material"\n';
usda += ' )\n';
usda += ' }\n';
usda += ' }\n';
usda += ' }\n';
usda += '\n';
usda += ' def Mesh "ground" (\n';
usda += ' prepend apiSchemas = ["MaterialBindingAPI"]\n';
usda += ' )\n';
usda += ' {\n';
usda += ' float3[] extent = [(-1400, -1400, 0), (1400, 1400, 0)]\n';
usda += ' int[] faceVertexCounts = [4]\n';
usda += ' int[] faceVertexIndices = [0, 1, 3, 2]\n';
usda += ' rel material:binding = </Environment/Looks/Grid> (\n';
usda += ' bindMaterialAs = "weakerThanDescendants"\n';
usda += ' )\n';
usda += ' normal3f[] normals = [(0, 0, 1), (0, 0, 1), (0, 0, 1), (0, 0, 1)] (\n';
usda += ' interpolation = "faceVarying"\n';
usda += ' )\n';
usda += ' point3f[] points = [(-700, -700, 0), (700, -700, 0), (-700, 700, 0), (700, 700, 0)]\n';
usda += ' bool primvars:isMatteObject = 0\n';
usda += ' texCoord2f[] primvars:st = [(0, 0), (14, 0), (14, 14), (0, 14)] (\n';
usda += ' interpolation = "faceVarying"\n';
usda += ' )\n';
usda += ' uniform token subdivisionScheme = "none"\n';
usda += ' token visibility = "inherited"\n';
usda += ' double3 xformOp:rotateXYZ = (0, -90, -90)\n';
usda += ' double3 xformOp:scale = (1, 1, 1)\n';
usda += ' double3 xformOp:translate = (0, 0, 0)\n';
usda += ' uniform token[] xformOpOrder = ["xformOp:translate", "xformOp:rotateXYZ", "xformOp:scale"]\n';
usda += ' }\n';
usda += '\n';
usda += ' def Plane "groundCollider" (\n';
usda += ' prepend apiSchemas = ["PhysicsCollisionAPI"]\n';
usda += ' )\n';
usda += ' {\n';
usda += ' uniform token axis = "Y"\n';
usda += ' uniform token purpose = "guide"\n';
usda += ' }\n';
usda += '}\n';
// Get the path of the current document
var currentFilePath = doc.fullName;
// Extract the directory path from the file path
var directoryPath = currentFilePath.path;
// Extract the filename without extension
var currentFileNameWithoutExtension = currentFilePath.displayName.replace(/\.[^\.]+$/, "");
// Create a new file object for the JSON file in the same directory
var usdaFile = new File(currentFilePath.path + "/" + currentFileNameWithoutExtension + ".usda");
// Open the file in write mode
usdaFile.open("w");
// Write the text content to the file
usdaFile.write(usda);
// Close the file
usdaFile.close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment