-
Chapter 5: lux.Material and Materials Management
- 5.1 Accessing and Manipulating the Material Graph
- 5.2 Accessing the Material Graph
- 5.3 Accessing the Root Node
- 5.4 Getting Input and Output Edges of a Node
- 5.5 Accessing and Modifying Node Parameters
- 5.6 Creating and Removing Nodes
- 5.7 Creating and Removing Edges
- 5.8 Best Practices for Material Graph Manipulation
KeyShot provides Python scripting support, enabling users to automate workflows, customize processes, and manipulate scene elements programmatically. With KeyShot scripting, you can access the KeyShot API to manipulate the scene tree, create animations, modify materials, and automate rendering tasks.
KeyShot scripting can be useful in several scenarios:
- Automating repetitive tasks.
- Customizing rendering and scene management.
- Enhancing workflows with custom tools and user interfaces.
To begin using Python scripting in KeyShot:
- Access the Python Console: In KeyShot, navigate to the
Scriptstab in the toolbar and open the Python Console. - Write and Execute Code: Python scripts can be executed within this console or saved as
.pyfiles for batch processing or automation. - Import KeyShot Libraries: The
luxlibrary contains functions for manipulating scene objects, materials, cameras, lights, and animations.
import lux
# Get the root of the scene tree
scene = lux.getSceneTree()
# Print the names of all nodes in the scene
for node in scene.getChildren():
print(node.getName())Familiarity with basic Python programming concepts is necessary for writing KeyShot scripts. Some concepts to focus on include:
- Variables: Used to store data.
- Functions: Reusable blocks of code.
- Conditionals: For controlling flow (
if-elsestatements). - Loops: For iterating over elements (
for,whileloops).
if scene.getChildren():
print("There are nodes in the scene.")
else:
print("The scene is empty.")In KeyShot scripting, accessing and manipulating scene elements is done via the scene tree. The scene tree contains all the models, parts, groups, cameras, and lights within the scene, and each node in this tree is of the type lux.SceneNode.
By using lux.getSceneTree(), you can retrieve the root node of the scene and perform various operations like hiding, showing, selecting, applying transformations, changing materials, and moving nodes.
# Get the root of the scene tree
root = lux.getSceneTree()
print(f"Root node: {root.getName()}")To find specific nodes in the scene, you can use the find() method on the root node. You can search nodes by name, type, material, or other attributes.
# Get the scene tree root node
root = lux.getSceneTree()
# Find and hide all nodes with "Cord" in their name
for node in root.find(name="Cord"):
node.hide()
print(f"Hiding node: {node.getName()}")# Find groups named "Padding" and select child nodes named "Ear Pad"
for node in root.find(name="Padding", types=lux.NODE_TYPE_GROUP):
for child in node.getChildren():
if child.getName() == "Ear Pad":
child.select()
print(f"Selected node: {child.getName()}")2.3 Showing All Hidden Nodes
If you've hidden some parts of the scene and want to show everything again, you can call show() on the root node to make all nodes visible.
Example: Show All Hidden Nodes
# Show all nodes in the scene
root.show()
print("All nodes are now visible.")You can change the material of any node by using the setMaterial() function. This is particularly useful for applying batch changes to nodes in the scene, such as simulating ambient occlusion by applying a uniform matte material.
# Apply the "Matte White" material to all nodes
for node in root.find(""):
node.setMaterial("Matte White")
print(f"Set material to Matte White for node: {node.getName()}")Tip: Use root.find("") to find all nodes in the scene.
KeyShot scripting allows you to move nodes between groups in the scene, just like using drag-and-drop in the KeyShot UI. This feature is especially useful when organizing complex scenes with many elements.
# Find the group "Headphone #1"
group_node = root.find(name="Headphone #1")[0] # Take the first group node
# Find all nodes using a material that contains "Padding" and move them to "Headphone #1"
for node in root.find(mat="Padding"):
node.moveToGroup(group_node)
print(f"Moved node: {node.getName()} to group: {group_node.getName()}")hide(): Hides the node in the scene.show(): Makes the node visible.select(): Selects the node in the scene (highlighted in the KeyShot UI).deselect(): Deselects the node.lock(): Locks the node, preventing further transformations or changes.unlock(): Unlocks the node.duplicate(): Duplicates the node.applyTransform(): Applies a transformation matrix to the node.getChildren(): Returns all child nodes of a group node.setMaterial(material_name): Sets the material of the node to the specified material.moveToGroup(group): Moves the node to the specified group.
# Find a node by name and lock it
node = root.find(name="MyNode")[0] # Assume only one node with this name
node.lock()
print(f"Locked node: {node.getName()}")
# Unlock the node later
node.unlock()
print(f"Unlocked node: {node.getName()}")- Efficient Node Management: Use
find()with specific parameters (name, type, material) to efficiently manage large scenes with many nodes. - Use Groups for Organization: Group related objects together and manipulate them as a unit for easier scene management.
- Batch Operations: Use loops to batch process node operations, such as hiding, showing, or applying materials.
- Visibility Control: Use
hide()andshow()to control visibility, especially in scenes with many objects.
The lux.Node class represents an element in the scene tree (a model, object, or group). This class provides methods to manipulate the node's properties, such as position, rotation, and scale.
Nodes have several key properties and methods:
getName(): Returns the name of the node.applyTransform(): Applies a transformation (e.g., translation, rotation) to the node.duplicate(): Creates a copy of the node.delete(): Removes the node from the scene.
if node:
print("Node name:", node.getName())Nodes can be one of the following types:
- MODEL: Represents an imported 3D model.
- OBJECT: Represents individual geometry or parts of a model.
- GROUP: Used to organize multiple nodes.
You can manipulate the position, rotation, and scale of a node using the applyTransform() method. Transformations are applied using matrices (covered in more detail in Chapter 4).
import luxmath
# Create a translation matrix to move the node by 10 units along the X-axis
translation_matrix = luxmath.Matrix().makeIdentity().translate(luxmath.Vector(10, 0, 0))
# Apply the translation to the node
if node:
node.applyTransform(translation_matrix)
print(f"Node '{node.getName()}' moved by 10 units along the X-axis.")- Organize models and parts early: Establish a clear hierarchy in your scene tree for easier manipulation.
- Use grouping: Use groups to bundle related parts and models together, making it easier to apply transformations or materials.
- Check node types: Always verify node types before applying transformations to avoid errors.
Matrices in 3D graphics are used to perform transformations such as translation (moving), rotation, and scaling. A matrix in KeyShot is a 4x4 matrix that allows for complex object manipulations.
import luxmath
# Create an identity matrix (no transformation)
identity_matrix = luxmath.Matrix().makeIdentity()Bounding boxes define the spatial limits of an object, which is crucial for transformations like translation, rotation, and scaling.
A bounding box is the smallest box that encloses an object, defined by its minimum and maximum points along the X, Y, and Z axes.
You can retrieve the bounding box of an object using the lux.getObjectBoundingBox() method.
bbox_min, bbox_max = lux.getObjectBoundingBox(node)
print(f"Bounding Box - Min: {bbox_min}, Max: {bbox_max}")Error: Misalignment after applying transformations. Solution: Ensure transformations are applied in the correct order: scale, rotate, then translate.
Translation moves an object along the X, Y, and Z axes. This can be done using a translation matrix.
translation_vector = luxmath.Vector(10, 5, 0)
translation_matrix = luxmath.Matrix().makeIdentity().translate(
translation_vector)
# Apply the translation to the node
node.applyTransform(translation_matrix)Rotation involves turning an object around a specified axis by a given angle. KeyShot uses rotation matrices to define the amount of rotation along the X, Y, or Z axis.
import math
# Rotate by 45 degrees around the Z-axis
rotation_angle = math.radians(45)
rotation_axis = luxmath.Vector(0, 0, 1)
rotation_matrix = luxmath.Matrix().makeIdentity().rotateAroundAxis(rotation_axis, rotation_angle)
# Apply the rotation to the node
node.applyTransform(rotation_matrix)
print(f"Node '{node.getName()}' rotated 45 degrees around the Z-axis.")Scaling changes the size of an object along the X, Y, and Z axes. You can scale uniformly (same factor in all directions) or non-uniformly (different factors for each axis).
# Scale the object by 2x along the X, Y, and Z axes
scaling_vector = luxmath.Vector(2.0, 2.0, 2.0)
scaling_matrix = luxmath.Matrix().makeIdentity().scale(scaling_vector)
# Apply the scaling transformation to the node
node.applyTransform(scaling_matrix)
print(f"Node '{node.getName()}' scaled by 2x.")In most cases, you’ll need to combine multiple transformations like scaling, rotation, and translation. The order of applying transformations matters (scaling should be applied before translation, for example).
# Create scaling, rotation, and translation matrices
scaling_matrix = luxmath.Matrix().makeIdentity().scale(luxmath.Vector(2.0, 2.0, 2.0))
rotation_matrix = luxmath.Matrix().makeIdentity().rotateAroundAxis(luxmath.Vector(0, 0, 1), math.radians(45))
translation_matrix = luxmath.Matrix().makeIdentity().translate(luxmath.Vector(10, 5, 0))
# Combine transformations: Scaling -> Rotation -> Translation
combined_matrix = scaling_matrix.mul(rotation_matrix).mul(translation_matrix)
# Apply the combined transformations to the node
node.applyTransform(combined_matrix)
print(f"Node '{node.getName()}' scaled, rotated, and translated.")Sometimes you need to arrange objects in specific patterns like grids, circles, or pyramids. You can calculate transformation matrices to position objects in these patterns.
In a spiral pattern, objects are arranged in an increasing radial and angular position from a central point.
def create_spiral_matrix(i, spacing, angle_increment, height_increment):
angle_rad = i * angle_increment * (math.pi / 180.0)
x_offset = math.sin(angle_rad) * spacing * i
y_offset = math.cos(angle_rad) * spacing * i + i * height_increment
z_offset = 0 # Maintain constant Z
return luxmath.Matrix().makeIdentity().translate(luxmath.Vector(x_offset, y_offset, z_offset))Objects are arranged in a grid pattern based on rows and columns.
def create_grid_matrix(i, spacing, rows, cols):
row = i // cols
col = i % cols
x_offset = col * spacing
y_offset = row * spacing
return luxmath.Matrix().makeIdentity().translate(luxmath.Vector(x_offset, 0, y_offset))Objects are arranged along the circumference of a circle.
def create_circle_matrix(i, radius, num_objects):
angle = (2 * math.pi / num_objects) * i
x_offset = math.cos(angle) * radius
y_offset = math.sin(angle) * radius
return luxmath.Matrix().makeIdentity().translate(luxmath.Vector(x_offset, 0, y_offset))Objects are arranged in a pyramid shape, with layers decreasing in size.
def create_pyramid_matrix(i, spacing, base_width):
layer = int((1 + math.sqrt(1 + 8 * i)) // 2)
index_in_layer = i - layer * (layer - 1) // 2
layer_width = max(base_width - layer + 1, 1)
row = index_in_layer // layer_width
col = index_in_layer % layer_width
x_offset = (col - (layer_width - 1) / 2) * spacing
z_offset = (row - (layer_width - 1) / 2) * spacing
return luxmath.Matrix().makeIdentity().translate(luxmath.Vector(x_offset, 0, z_offset))Managing multiple nodes efficiently is critical in large scenes. Scene tree management allows for easy duplication, deletion, and transformation of multiple objects.
root = lux.getSceneTree()
objects = root.find("", types=[lux.NODE_TYPE_OBJECT])
# Apply a scaling transformation to all objects
for obj in objects:
scaling_matrix = luxmath.Matrix().makeIdentity().scale(luxmath.Vector(1.5, 1.5, 1.5))
obj.applyTransform(scaling_matrix)
print(f"Scaled object: {obj.getName()}")The material graph in KeyShot allows you to manipulate materials in a more advanced and flexible way. By using the lux.getMaterialGraph() function, you can access and modify the material nodes, parameters, and connections. The following examples cover the material graph, nodes, edges, and parameters.
You can access the material graph of a specific material by using the lux.getMaterialGraph() function. The material graph is an instance of the lux.MaterialGraph class.
# Get the material graph of a material named "Matte"
graph = lux.getMaterialGraph("Matte")
# Check if the graph was retrieved successfully
if graph:
print(f"Material graph for 'Matte' retrieved.")
else:
print("Material graph not found.")Once you have the material graph, you can access the root node. The root node is an instance of the lux.ShaderNode class, which represents the primary shader of the material.
# Access the root node of the material graph
root_node = graph.getRoot()
# Display information about the root node
if root_node:
print(f"Root node: {root_node.getName()}, Type: {root_node.getType()}")Shader nodes in the material graph are connected by edges. You can retrieve the input and output edges of a node to understand how shaders are linked in the material.
# Get input edges of the root node
input_edges = root_node.getInputEdges()
# Print details of each input edge
for edge in input_edges:
print(f"Input edge from {edge.getSource().getName()} to {root_node.getName()}")# Get output edges of the root node
output_edges = root_node.getOutputEdges()
# Print details of each output edge
for edge in output_edges:
print(f"Output edge from {root_node.getName()} to {edge.getTarget().getName()}")Each shader node has parameters that define its behavior (e.g., color, reflectivity). You can retrieve and modify these parameters using the getParameter() and setParameter() functions.
# Get a specific parameter (e.g., "diffuse color") of the root node
diffuse_param = root_node.getParameter("diffuse")
# Check if the parameter exists and change its value
if diffuse_param:
print(f"Current diffuse color: {diffuse_param.getValue()}")
diffuse_param.setValue((0.8, 0.2, 0.2)) # Change to a red color
print(f"New diffuse color set to: {diffuse_param.getValue()}")
else:
print("Diffuse parameter not found.")Key Points:
- Parameters can have different types (e.g., booleans, colors, floats).
- Ensure that you provide valid values for the parameter types to avoid errors.
In KeyShot, you can create new shader nodes in the material graph to build more complex materials. For example, you can create a metal shader node and connect it to the graph.
# Create a new metal shader node in the material graph
metal_node = graph.newNode(lux.SHADER_TYPE_METAL)
if metal_node:
print(f"Created a new metal shader node: {metal_node.getName()}")
else:
print("Failed to create metal node.")# Remove the metal shader node by instance
if metal_node:
graph.removeNode(metal_node)
print(f"Removed node: {metal_node.getName()}")Edges in the material graph connect shader nodes. You can create edges between nodes to link shader outputs to inputs, such as connecting a texture node to the diffuse input of a shader.
# Assuming you have two nodes (e.g., root_node and metal_node)
if root_node and metal_node:
edge = graph.newEdge(source=metal_node, target=root_node, param="surface")
print(f"Created an edge from {metal_node.getName()} to {root_node.getName()} on the 'surface' parameter.")# Remove an edge by instance
if edge:
graph.removeEdge(edge)
print("Edge removed.")- Error Handling: Always check if nodes, parameters, and edges exist before performing operations. This ensures that your scripts handle cases where materials or connections may not be present.
- Testing Parameter Types: Use
getParameter()to retrieve the parameter type and ensure you are setting the correct values. - Performance Considerations: Complex material graphs with many nodes and connections can impact performance. Be mindful of how many nodes and edges you add to a material graph.
In KeyShot, while object transformations such as translation, rotation, and scaling cannot be animated directly via scripts, you can navigate the animation timeline, control time-based actions, and manipulate the existing keyframe animations through scripting.
Scripting in KeyShot for animations is primarily focused on:
- Navigating the Animation Timeline: Jumping to specific points in time.
- Rendering Frames at Specific Points in Time: This is useful for creating batch renders of an animation sequence.
- Playing, pausing, or stopping animations.
- Accessing and adjusting timeline parameters.
Using KeyShot’s Python API, you can control the timeline and access specific points within the animation for rendering or other purposes.
lux.setCurrentTime(time): Sets the current time in the animation timeline.lux.getCurrentTime(): Gets the current time in the animation timeline.lux.getAnimationDuration(): Returns the total duration of the animation.lux.play(): Starts playing the animation from the current time.lux.stop(): Stops the animation at the current time.lux.pause(): Pauses the animation at the current time.
import lux
# Set the animation timeline to 5 seconds
lux.setCurrentTime(5.0)
# Get the current time (should be 5.0)
current_time = lux.getCurrentTime()
print(f"Current animation time: {current_time} seconds")If an animation is already set up in the scene (created via the KeyShot UI), you can navigate through the timeline to access specific keyframes.
# Move the timeline to the 3-second mark
lux.setCurrentTime(3.0)
# Render the frame at the 3-second mark
lux.renderImage("output_at_3_seconds.png", width=1920, height=1080)You can retrieve the total duration of an animation sequence using lux.getAnimationDuration().
# Get the total animation duration
animation_duration = lux.getAnimationDuration()
print(f"Total animation duration: {animation_duration} seconds")One practical application of timeline navigation in KeyShot scripting is batch rendering animation frames. By iterating through the timeline and rendering frames at each step, you can generate images for the entire animation sequence.
# Define animation duration and time step between renders
animation_duration = lux.getAnimationDuration()
time_step = 1.0 # Render a frame every second
# Loop through the timeline, rendering frames
for t in range(0, int(animation_duration), int(time_step)):
lux.setCurrentTime(t)
output_filename = f"render_at_{t}_seconds.png"
lux.renderImage(output_filename, width=1920, height=1080)
print(f"Rendered frame at {t} seconds")Using the lux.play(), lux.pause(), and lux.stop() functions, you can control the playback of an animation directly in KeyShot.
# Play the animation
lux.play()
# Pause the animation after 5 seconds
lux.setCurrentTime(5.0)
lux.pause()
print("Animation paused at 5 seconds")- Batch Rendering: Use scripts to render frames at regular intervals, particularly for high-resolution animations.
- Time Control: Always ensure that the
lux.setCurrentTime()is correctly set before performing time-sensitive operations like rendering or saving. - Efficiency: When batch rendering, render only at necessary intervals to avoid performance issues.
KeyShot’s Python API allows you to create custom graphical user interfaces (GUIs) to collect user inputs via dialogs. These dialogs are crucial when building interactive scripts that require user input for executing actions like batch operations, material handling, and scene manipulation.
The lux.getInputDialog() function is the primary method for creating these dialogs. It allows you to define various types of inputs such as text, file paths, folder paths, and numerical inputs.
This example demonstrates how to use a GUI dialog to select multiple model files for batch importing into KeyShot. It utilizes the lux.DIALOG_FILE type to allow users to select files.
import lux
import os
def batch_import_models():
# Ask user to select model files for import
file_paths = lux.getInputDialog(
title="Batch Import Models",
desc="Select model files to import",
values=[("model_files", lux.DIALOG_FILE, "Model files:", None, True)]) # True allows multiple file selection
if file_paths:
for file_path in file_paths["model_files"]:
lux.importFile(file_path)
print(f"Imported: {file_path}")
batch_import_models()Key Concepts:
lux.DIALOG_FILEenables file selection dialogs, where the user can choose one or more files.- The batch import operation loops through the selected files and imports them into the scene.
This example demonstrates how to use a GUI to select a folder containing textures and batch-assign them to the multi-materials in a KeyShot scene.
import lux
import os
def batch_assign_textures():
# Ask user to select a folder containing textures
folder_path = lux.getInputDialog(
title="Batch Assign Textures",
desc="Select the folder containing textures",
values=[("texture_folder", lux.DIALOG_FOLDER, "Texture folder:", None)])
if folder_path:
texture_folder = folder_path["texture_folder"]
for texture_file in os.listdir(texture_folder):
# Here you would add the logic to assign textures to multi-materials
print(f"Assigning {texture_file} to a multi-material")
batch_assign_textures()Key Concepts:
lux.DIALOG_FOLDERallows for folder selection.- Use
os.listdir()to iterate over files in the selected folder, facilitating the batch assignment of textures.
You can rename materials in a scene to avoid name clashes or to improve organization. This example provides a GUI dialog that accepts find-and-replace terms for renaming materials.
import lux
def rename_materials():
rename_opts = lux.getInputDialog(
title="Rename Materials",
desc="Specify find and replace terms for material names",
values=[("find", lux.DIALOG_TEXT, "Find:", ""),
("replace", lux.DIALOG_TEXT, "Replace with:", "")])
if rename_opts:
find_term = rename_opts["find"]
replace_term = rename_opts["replace"]
# Logic to iterate through materials and rename them
materials = lux.getSceneMaterials()
for material in materials:
if find_term in material.getName():
new_name = material.getName().replace(find_term, replace_term)
material.setName(new_name)
print(f"Renamed material to: {new_name}")
rename_materials()Key Concepts:
- The
lux.DIALOG_TEXTtype allows users to input strings, which are used here for find-and-replace operations. - The
lux.getSceneMaterials()function retrieves all materials in the scene for iteration.
This script provides a GUI that retrieves and displays information about the currently active camera, such as its name, position, and field of view.
import lux
def get_camera_info():
# Get the active camera
camera = lux.getActiveCamera()
# Assuming camera properties like name, position, and FOV can be retrieved
camera_info = f"Camera Name: {camera.name}, Position: {camera.getPosition()}, FOV: {camera.getFieldOfView()}"
# Display camera info in a message box
lux.messageBox("Camera Information", camera_info)
get_camera_info()Key Concepts:
lux.getActiveCamera()retrieves the current active camera.lux.messageBox()displays a dialog with information for the user.
This example shows how to use a GUI to input a material name and retrieve its parameters for display or further manipulation.
import lux
def get_material_parameters():
# Ask user for the material name
material_name_input = lux.getInputDialog(
title="Get Material Parameters",
desc="Enter the name of the material",
values=[("material_name", lux.DIALOG_TEXT, "Material Name:", "")])
if material_name_input:
material_name = material_name_input["material_name"]
material = lux.getMaterial(material_name)
if material:
# Retrieve material parameters (replace with actual parameter retrieval)
material_params = f"Material Name: {material.getName()}\nType: {material.getType()}"
lux.messageBox("Material Parameters", material_params)
else:
lux.messageBox("Error", f"Material '{material_name}' not found")
get_material_parameters()Key Concepts:
- The script uses
lux.getInputDialog()to get a material name from the user. lux.getMaterial()fetches the material object for the given name, and its properties are retrieved and displayed.
The lux.getInputDialog() function cannot have empty fields. Ensure every lux.DIALOG_TEXT or similar input has a default value to prevent errors.
# Ensure default values are non-empty
values=[("material_name", lux.DIALOG_TEXT, "Material Name:", "DefaultMaterial")]- Validation: Always validate user inputs to ensure correct data types and values before processing them.
- Feedback: Provide clear feedback to the user through dialogs and message boxes to indicate success or failure.
- Error Handling: Handle errors gracefully by checking for null values, missing files, or invalid inputs.
- Use Defaults: Ensure that all dialog fields have sensible default values to prevent errors.
With these examples and explanations, you can efficiently create interactive and user-friendly scripts in KeyShot that make use of custom GUI dialogs. Now, we will proceed to Chapter 9: Headless Operations with lux for further scripting capabilities. If there are any other adjustments you'd like before moving on, feel free to let me know!
KeyShot can run in "headless" mode, which allows you to perform automated tasks such as batch rendering without the need for a graphical user interface. This is particularly useful for render farms or automated pipelines.
To run KeyShot in headless mode, you can use the following command in the terminal:
keyshot -script /path/to/script.py -headlessIn headless mode, batch processes such as rendering multiple scenes or exporting assets can be automated via scripts.
scenes = ["scene1.bip", "scene2.bip", "scene3.bip"]
for scene in scenes:
lux.loadScene(scene)
lux.renderImage(f"{scene}_render.png", width=1920, height=1080)
print(f"Rendered {scene}")Scripts can be used to integrate KeyShot with other software systems, such as asset management or content delivery systems. By combining KeyShot's headless mode with Python’s extensive library ecosystem, you can automate complex workflows involving external tools.
# Load a specific scene and set render parameters
lux.loadScene("my_scene.bip")
# Set up render options
render_options = lux.getRenderOptions()
render_options.setMaxSamples(128)
render_options.setResolution(1920, 1080)
# Render the image in headless mode
lux.renderImage("render_output.png", opts=render_options)In KeyShot, scripting allows you to create, manipulate, and save cameras, just like using the Camera tab in the GUI. You can also change the view, set up standard views, and switch between cameras programmatically.
You can create new cameras, adjust their orientation, and save their configurations.
# Create a new camera named "NewCamera"
lux.newCamera("NewCamera")
# Verify the camera was created
cameras = lux.getCameras()
print(f"Available Cameras: {cameras}")After creating a camera, you can manipulate its position, orientation, and field of view. The camera can be adjusted using the lux.setCameraLookAt() function to specify what the camera should focus on.
# Set the active camera to "NewCamera"
lux.setCamera("NewCamera")
# Adjust the camera to look at a specific point
lux.setCameraLookAt(pt=(0, 0, 0)) # Look at the origin
# Save the changes to the camera
lux.saveCamera()KeyShot allows you to switch between available cameras in the scene using the lux.setCamera() function.
# List available cameras
cameras = lux.getCameras()
print(f"Available Cameras: {cameras}")
# Switch to the "default" camera
lux.setCamera("default")You can remove any camera that has been created, except for the active camera. If you remove the active camera, KeyShot will automatically switch to the "Free Camera."
# Remove the "NewCamera"
lux.removeCamera("NewCamera")
# Verify that the camera was removed
cameras = lux.getCameras()
print(f"Available Cameras after removal: {cameras}")KeyShot provides several standard views (e.g., top, front, back) that you can use to manipulate the active camera. Setting a standard view moves the active camera to predefined positions.
# Set the camera to the top view
lux.setStandardView(lux.VIEW_TOP)
# Save the new camera configuration
lux.saveCamera()lux.VIEW_FRONTlux.VIEW_BACKlux.VIEW_TOPlux.VIEW_BOTTOMlux.VIEW_LEFTlux.VIEW_RIGHTlux.VIEW_ISOMETRIC
By using these standard views, you can quickly change the orientation of the active camera to predefined angles.
- Always Save Camera Changes: After making any adjustments to a camera (position, orientation, view), use
lux.saveCamera()to save those changes. - Use Standard Views for Consistency: Standard views provide a consistent way to set up cameras across different scenes.
- Switch Between Cameras: Use
lux.setCamera()to switch between different cameras quickly for rendering or scene inspection.
Chapter 9 covers advanced scripting topics in KeyShot, including more intricate operations involving material graphs, node management, scene optimization, and special use cases. These advanced functions are essential for streamlining workflows, automating processes, and handling complex scenes in both GUI and headless modes.
As scenes grow in complexity, managing performance becomes crucial, especially in KeyShot where large datasets and detailed materials can significantly impact rendering times. Below are strategies for optimizing performance through scripting:
Managing the scene tree efficiently can reduce the load on KeyShot, especially with large assemblies. You can group nodes, hide unused nodes, and lock elements to prevent unnecessary updates.
# Hide all nodes that are not currently needed for rendering
root = lux.getSceneTree()
for node in root.find():
if "unused" in node.getName():
node.hide()
print(f"Hiding node: {node.getName()}")One method to optimize a scene is to replace complex materials with simpler alternatives during certain stages of the workflow, such as test renders.
# Replace all complex materials with "Matte White" for test renders
for node in root.find(""):
node.setMaterial("Matte White")
print(f"Replaced material for node: {node.getName()}")In certain scenarios, you'll work with complex material graphs involving multiple shaders and connections. Handling these efficiently is crucial when creating advanced materials or performing batch operations.
You can combine multiple shader nodes into one material graph to create custom materials. This allows for more complex visual effects such as multi-layered textures, bump maps, or procedural materials.
# Access the material graph
graph = lux.getMaterialGraph("PlasticMaterial")
# Create a new bump map shader node
bump_node = graph.newNode(lux.SHADER_TYPE_BUMP)
bump_node.setParameter("bumpMap", "path/to/bump_map.png")
# Connect the bump node to the root node
root_node = graph.getRoot()
graph.newEdge(source=bump_node, target=root_node, param="bump")
print("Bump map added to material.")For repetitive tasks, such as applying certain material settings across multiple objects, automation via scripting can drastically reduce the time required.
# Apply a complex multi-layer material to nodes based on certain criteria
for node in root.find(name="ComplexObject"):
graph = lux.getMaterialGraph(node.getMaterial().getName())
# Create and assign additional shader nodes as needed
metal_node = graph.newNode(lux.SHADER_TYPE_METAL)
root_node = graph.getRoot()
graph.newEdge(source=metal_node, target=root_node, param="specular")
print(f"Assigned multi-layer material to: {node.getName()}")Running KeyShot in headless mode allows for full automation without the graphical interface, which is useful for batch rendering, material application, and other large-scale operations.
To run KeyShot in headless mode, use the following command in the terminal:
keyshot.exe -script path/to/script.py -scene path/to/scene.bip -renderoutput path/to/output.pngYou can automate batch rendering of multiple views or frames using scripting in headless mode. This is particularly useful for rendering product shots from different angles or producing animations.
# Define different camera angles
angles = [lux.VIEW_FRONT, lux.VIEW_BACK, lux.VIEW_TOP, lux.VIEW_LEFT, lux.VIEW_RIGHT]
# Loop through each angle and render the scene
for angle in angles:
lux.setStandardView(angle)
lux.renderImage(f"render_{angle}.png", width=1920, height=1080)
print(f"Rendered view: {angle}")Advanced scripting in KeyShot often involves complex scenarios where errors may occur due to node types, parameter mismatches, or file system issues. Proper error handling and debugging are essential for preventing script failures.
Use Python’s try-except blocks to catch exceptions and log errors for debugging purposes. This approach is useful for long-running scripts where failures can interrupt large workflows.
try:
# Example code that may cause an error
node = root.find("NonExistentNode")[0]
node.applyTransform(luxmath.Matrix().makeIdentity())
except IndexError as e:
lux.logError(f"Node not found: {e}")Use the lux.logInfo() function to output useful debugging information during the execution of scripts. This is especially important when working with complex scenes or material graphs.
# Log information about the material graph for debugging purposes
graph = lux.getMaterialGraph("PlasticMaterial")
root_node = graph.getRoot()
lux.logInfo(f"Root node: {root_node.getName()}, Type: {root_node.getType()}")
for edge in root_node.getInputEdges():
lux.logInfo(f"Input from: {edge.getSource().getName()} to {root_node.getName()}")There are various special cases in KeyShot scripting where advanced customization is required, such as working with procedural geometry, creating custom materials, or integrating with external systems.
You can use scripting to dynamically manipulate procedural geometry, such as adjusting parameters for custom shapes or generating patterns on-the-fly.
# Access and modify parameters of a procedural geometry shader node
graph = lux.getMaterialGraph("ProceduralShape")
root_node = graph.getRoot()
scale_param = root_node.getParameter("scale")
# Adjust the scale of the procedural shape
if scale_param:
scale_param.setValue(2.0)
lux.logInfo("Adjusted procedural shape scale.")KeyShot can be integrated with external systems via Python scripts to automate tasks such as importing CAD data, processing textures, or exporting renders to specific directories.
# Automate the process of importing a CAD file and rendering it
cad_file = "path/to/cadfile.step"
lux.importFile(cad_file)
# Set camera and render settings
lux.setCamera("default")
lux.renderImage("render_output.png", width=1920, height=1080)
lux.logInfo("CAD file imported and rendered successfully.")- Modularization: Break scripts into reusable functions to reduce complexity and improve maintainability.
- Error Handling: Always wrap critical sections in try-except blocks to prevent unexpected crashes during execution.
- Performance Optimization: Use efficient node and material management techniques to reduce script runtime, especially when working with large scenes.
- Testing: Test scripts on smaller datasets before applying them to large scenes or batch processes.
This chapter focuses on best practices when scripting in KeyShot to ensure efficiency, performance, maintainability, and reliability. Following these guidelines can help reduce script errors, improve execution time, and streamline your workflow.
Efficient scene management is critical when working with large, complex scenes in KeyShot. Proper organization and optimization of the scene tree can significantly reduce the impact on performance during rendering or animation tasks.
Grouping related objects together can make it easier to manage transformations, materials, and other properties.
# Find all objects with a common prefix and group them together
root = lux.getSceneTree()
group = root.newNode(lux.NODE_TYPE_GROUP, "GroupedObjects")
for node in root.find(name="Object_"):
node.moveToGroup(group)
print(f"Moved {node.getName()} to 'GroupedObjects'")Materials can quickly become a performance bottleneck, especially when dealing with high-resolution textures, complex shaders, and multi-layer materials. Simplifying materials for draft rendering or optimizing texture resolutions can greatly enhance performance.
For test renders or previews, you can replace complex materials with simpler ones to save rendering time.
# Replace all materials with "Matte" for faster test renders
for node in root.find(""):
node.setMaterial("Matte White")
print(f"Replaced material on {node.getName()}")Large texture files can increase memory usage and slow down rendering. By using lower-resolution textures during test phases, you can reduce the strain on system resources.
# Assign lower-resolution textures to all materials
for node in root.find(""):
mat = node.getMaterial()
if mat and mat.hasTexture():
mat.setTextureResolution(512) # Set texture resolution to 512px
print(f"Set low-res texture for {node.getName()}")When writing scripts for KeyShot, aim for modularity by breaking your code into small, reusable functions. This makes it easier to debug, maintain, and update scripts over time.
Breaking down complex scripts into smaller, more manageable functions increases readability and reusability.
# Function to apply a transformation to a node
def apply_transformation(node, translation_vector):
matrix = luxmath.Matrix().makeIdentity().translate(translation_vector)
node.applyTransform(matrix)
print(f"Applied transformation to {node.getName()}")
# Apply transformation to multiple nodes
for node in root.find(name="Object_"):
apply_transformation(node, luxmath.Vector(10, 0, 0))Create libraries of common functions that can be reused across multiple scripts or projects. This improves efficiency and ensures consistency in your scripts.
Effective error handling is key to ensuring that your scripts run smoothly, especially when working with large datasets or automated processes.
Wrapping code that might fail in a try-except block helps catch and log errors without breaking the script entirely.
try:
node = root.find("NonExistentNode")[0]
node.setMaterial("Matte White")
except IndexError as e:
lux.logError(f"Node not found: {e}")
except Exception as e:
lux.logError(f"An error occurred: {e}")Use lux.logInfo() and lux.logError() to output helpful debugging information when testing scripts.
for node in root.find(""):
lux.logInfo(f"Node: {node.getName()}, Type: {node.getKind()}")When dealing with large or complex scenes, performance optimization becomes a priority. Here are some tips to optimize performance while scripting in KeyShot:
Avoid constantly updating the scene or applying changes unless necessary. Apply bulk updates in a single operation where possible.
lux.beginUndoSession()
for node in root.find(""):
matrix = luxmath.Matrix().makeIdentity().translate(luxmath.Vector(10, 0, 0))
node.applyTransform(matrix)
lux.endUndoSession()Hide parts of the scene that are not currently being worked on to improve navigation and manipulation performance.
# Hide all nodes except the ones needed for current work
for node in root.find(""):
if not node.getName().startswith("Important"):
node.hide()
print(f"Hid node: {node.getName()}")When automating processes in KeyShot, especially in headless mode, it is important to manage memory, file handling, and error recovery efficiently.
Use batch processing scripts to automate rendering tasks, ensuring that KeyShot efficiently handles large numbers of images or views.
# Define multiple camera angles for batch rendering
angles = [lux.VIEW_FRONT, lux.VIEW_BACK, lux.VIEW_TOP, lux.VIEW_LEFT, lux.VIEW_RIGHT]
# Render each view in a loop
for angle in angles:
lux.setStandardView(angle)
lux.renderImage(f"view_{angle}.png", width=1920, height=1080)
print(f"Rendered view: {angle}")When running KeyShot in headless mode for long periods, make sure to clean up resources such as file handles and temporary data to avoid memory leaks.
When working on complex scripting projects, it's essential to maintain proper version control and documentation for future reference and team collaboration.
Always comment your code, especially when performing complex operations or using KeyShot-specific functions that may not be intuitive to other developers.
Consider using a version control system like Git to manage changes to your scripts. This helps track revisions, resolve conflicts, and collaborate with others.
- Scene Management: Use grouping, layers, and visibility controls to organize scenes and improve performance.
- Material Management: Replace complex materials with simpler ones for test renders, and reduce texture resolutions when appropriate.
- Modular Code: Write reusable, modular functions to improve script maintainability.
- Error Handling: Implement try-except blocks for error handling and use logging for debugging.
- Performance Optimization: Minimize scene updates, reduce the number of visible nodes, and apply bulk changes to improve performance.
- Automation: Automate batch processes and manage resources carefully, especially in headless mode.
- Documentation: Comment your code and use version control systems to manage script revisions.
This chapter highlights some of the most common issues that arise when scripting in KeyShot, along with solutions and troubleshooting tips. It will help you diagnose problems and provide guidance on how to resolve them effectively.
When trying to find or manipulate a node, you may encounter an error if the node does not exist or if the name is misspelled.
Before performing any operations on a node, always check if the node exists. Use proper error handling to avoid script failure.
node = root.find("NonExistentNode")
if node:
# Perform operations
print(f"Node {node.getName()} found.")
else:
print("Node not found. Please check the node name.")You may encounter issues when trying to apply a transformation or material to a node that is of the wrong type (e.g., trying to apply a material to a group instead of a part).
Verify the node type before applying any transformations or materials. This ensures that the operation is appropriate for that specific node.
node = root.find("MyNode")[0]
if node.getKind() == lux.NODE_TYPE_OBJECT:
node.setMaterial("Matte White")
else:
print("This operation can only be applied to object nodes.")When modifying material properties, you might encounter errors if the input value type is incorrect (e.g., setting a boolean value for a color parameter).
Check the parameter type before setting its value. This helps prevent invalid input types and reduces script errors.
material = lux.getMaterialGraph("PlasticMaterial")
root_node = material.getRoot()
color_param = root_node.getParameter("diffuse")
# Ensure the parameter accepts a color tuple
if color_param:
try:
color_param.setValue((0.5, 0.5, 0.5)) # RGB tuple
print("Diffuse color set successfully.")
except Exception as e:
print(f"Error: {e}")
else:
print("Diffuse color parameter not found.")When attempting to access the material graph of a node or material, you may encounter an issue where the graph is missing or not found.
Always check if the material graph exists before attempting to manipulate it.
graph = lux.getMaterialGraph("MyMaterial")
if graph:
print("Material graph found.")
else:
print("Material graph not found. Verify the material name.")Improper transformations, such as incorrect scaling or rotation, can lead to unexpected results like object distortion or misalignment.
The order in which transformations are applied (scaling, rotation, and translation) is critical for achieving the correct result.
scaling_matrix = luxmath.Matrix().makeIdentity().scale(luxmath.Vector(2.0, 2.0, 2.0)) # Scale first
rotation_matrix = luxmath.Matrix().makeIdentity().rotateAroundAxis(luxmath.Vector(0, 1, 0), math.radians(45)) # Rotate second
translation_matrix = luxmath.Matrix().makeIdentity().translate(luxmath.Vector(10.0, 0.0, 0.0)) # Translate last
# Combine transformations
transform_matrix = scaling_matrix.mul(rotation_matrix).mul(translation_matrix)
node.applyTransform(transform_matrix)When applying multiple transformations (e.g., scaling, rotation, translation), you might experience misalignment of objects in the scene.
Ensure you are applying transformations relative to the correct coordinate system, especially when rotating or translating objects.
Sometimes you may attempt to manipulate or render from a camera that does not exist in the scene.
Before applying transformations or rendering, check if the camera is present in the scene.
cameras = lux.getCameras()
if "MyCamera" in cameras:
lux.setCamera("MyCamera")
else:
print("Camera not found. Please check the camera name.")The camera might be positioned incorrectly, leading to undesired render outputs, especially in headless mode.
Use functions like setStandardView() or setCameraLookAt() to ensure the correct positioning of the camera.
# Set the camera to the front view
lux.setStandardView(lux.VIEW_FRONT)
lux.renderImage("front_view.png", width=1920, height=1080)In some cases, the render output might not be generated due to incorrect file paths or missing render settings.
Verify that the output path is valid and the render settings are correctly applied.
try:
lux.renderImage("output_image.png", width=1920, height=1080)
print("Render completed successfully.")
except Exception as e:
print(f"Render failed: {e}")Renders may appear pixelated or low quality if insufficient samples or incorrect settings are used.
Increase the sample count and ensure the resolution and other render settings are configured properly.
render_options = lux.getRenderOptions()
render_options.setMaxSamples(128) # Increase sample count for higher quality
lux.renderImage("high_quality_render.png", width=1920, height=1080, opts=render_options)Scripts that run correctly in the GUI may fail when executed in headless mode, often due to the absence of user input dialogs or missing render settings.
Remove or replace any GUI-related functionality (e.g., dialogs) with hardcoded values or file-based inputs to ensure compatibility with headless mode.
# In headless mode, hardcode the values that would normally be gathered from the GUI
file_path = "path/to/scene.bip"
lux.importFile(file_path)
# Set render options and render
render_options = lux.getRenderOptions()
lux.renderImage("headless_render.png", width=1920, height=1080, opts=render_options)A common error involves passing tuples or lists with incorrect lengths to functions that expect specific parameter types, such as RGB colors or transformation vectors.
Check that the tuple or list contains the correct number of elements and that they match the expected types.
# Incorrect: Only two values provided
# translation_vector = luxmath.Vector(10.0, 5.0)
# Correct: Three values provided for X, Y, and Z
translation_vector = luxmath.Vector(10.0, 5.0, 0.0)
node.applyTransform(luxmath.Matrix().makeIdentity().translate(translation_vector))If a parameter is provided in an unsupported format, an error will occur, such as passing a string where a number is expected.
Always validate the parameter type before passing it to a function.
parameter = node.getParameter("scale")
if isinstance(parameter.getValue(), (float, int)):
parameter.setValue(2.0)
else:
print("Invalid parameter type. Expected a number.")- Node Issues: Always check if nodes exist and ensure correct node types are used for operations.
- Material and Shader Issues: Verify parameter types and material graph existence before making changes.
- Transformation and Matrix Issues: Apply transformations in the correct order and check for misalignment.
- Camera Issues: Ensure that the camera is present and positioned correctly before rendering.
- Render Issues: Validate render settings and file paths to ensure successful output.
- Headless Scripting Issues: Replace GUI-dependent features with hardcoded values for headless mode.
- **Common
Errors**: Address tuple mismatches, type mismatches, and ensure valid input values.
This chapter provides an extensive reference guide to commonly used KeyShot scripting functions, constants, and resources. It also includes a glossary of important terms and a list of external resources for further learning. This reference will act as a quick lookup for script functions and offer resources to deepen your understanding of KeyShot’s scripting environment.
This section provides a quick reference for common classes, functions, and constants used in KeyShot scripting.
| Function/Class | Description |
|---|---|
lux.getSceneTree() |
Retrieves the root of the scene tree, enabling access to nodes and groups. |
lux.getMaterialGraph(name) |
Retrieves the material graph of a specific material by name. |
lux.renderImage(output_path, width, height, opts) |
Renders the scene to an image file with specified width and height. |
lux.getCameras() |
Returns a list of all available cameras in the scene. |
lux.setCamera(camera_name) |
Sets the active camera by name. |
lux.setStandardView(view_constant) |
Sets the camera to a standard view (e.g., front, top, left, etc.). |
lux.setAnimationTime(time) |
Sets the animation timeline to a specific time, where time is a float between 0 and 1. |
lux.newNode(node_type, name) |
Creates a new node in the scene tree with a specified type and name. |
lux.deleteObject(node) |
Deletes a specified node or object from the scene. |
lux.getMaterial(name) |
Retrieves a material by name from the scene. |
lux.createMaterial(material_type) |
Creates a new material of the specified type (e.g., diffuse, glossy, metal). |
lux.setMaterial(material) |
Assigns a material to a node. |
lux.getRenderOptions() |
Retrieves the current render options. |
lux.getInputDialog() |
Opens a dialog box for user input. |
| Constant/Enum | Description |
|---|---|
lux.NODE_TYPE_MODEL |
Represents a model node in the scene tree. |
lux.NODE_TYPE_OBJECT |
Represents an object node (geometry) in the scene tree. |
lux.NODE_TYPE_GROUP |
Represents a group node in the scene tree. |
lux.VIEW_FRONT |
Sets the camera to the front view. |
lux.VIEW_BACK |
Sets the camera to the back view. |
lux.VIEW_TOP |
Sets the camera to the top view. |
lux.VIEW_BOTTOM |
Sets the camera to the bottom view. |
lux.VIEW_LEFT |
Sets the camera to the left view. |
lux.VIEW_RIGHT |
Sets the camera to the right view. |
lux.SHADER_TYPE_METAL |
Represents the metal shader type in the material graph. |
lux.SHADER_TYPE_DIFFUSE |
Represents the diffuse shader type in the material graph. |
lux.SHADER_TYPE_BUMP |
Represents the bump shader type in the material graph. |
This glossary defines important terms used in KeyShot scripting and rendering.
| Term | Definition |
|---|---|
| Scene Tree | A hierarchical structure of nodes in KeyShot that represent models, objects, and groups. |
| Node | A fundamental element in the scene tree, representing a model, part, group, or object. |
| Material Graph | A graphical representation of a material’s shader nodes and how they are connected. |
| Shader Node | A node in the material graph that represents a specific type of shader (e.g., metal, bump). |
| Transformation Matrix | A 4x4 matrix used to translate, rotate, or scale objects in 3D space. |
| Viewport | The window in KeyShot where the 3D scene is displayed and manipulated. |
| Headless Mode | Running KeyShot without a graphical interface, used for batch processing and automation. |
| Render Options | Settings that control how an image is rendered (e.g., resolution, samples, output path). |
| Texture Mapping | The process of applying a 2D image (texture) to a 3D object’s surface. |
| Multi-Material | A material that contains multiple sub-materials, which can be swapped dynamically. |
| HDRI | High Dynamic Range Image, used for image-based lighting in KeyShot. |
Write modular, reusable functions to improve the maintainability of your scripts. Each function should perform a single task and should be tested independently.
def translate_node(node, x, y, z):
translation_matrix = luxmath.Matrix().makeIdentity().translate(luxmath.Vector(x, y, z))
node.applyTransform(translation_matrix)Use Python’s try-except blocks to handle errors and lux.logInfo() or lux.logError() to log useful information when debugging scripts.
try:
node = root.find("MyNode")[0]
lux.logInfo(f"Node found: {node.getName()}")
except Exception as e:
lux.logError(f"Error: {e}")Here is a list of helpful external resources to further your knowledge of KeyShot scripting and Python development.
- KeyShot Studio 2026.3 Python API
- Official KeyShot documentation with detailed information on the scripting API and examples.
- Python 3.13 documentation
- Python’s official documentation covering all language features, modules, and tutorials.
- Real-Time Rendering
- A comprehensive book and website covering topics related to 3D rendering, shaders, and graphics.
- KeyShot Forum
- The official KeyShot forum where users share tips, scripts, and troubleshooting advice.
- KeyShot Blog: Keep up-to-date with new releases, tips, and user stories on the KeyShot Blog.
- KeyShot Tutorials: Access tutorials on rendering, materials, and animation at KeyShot Learning.
- Python Programming for 3D: For those looking to dive deeper into Python scripting for 3D, explore resources like CG Cookie, which offers Python courses for 3D applications.
This reference chapter offers a wide range of resources and key functions to help you quickly navigate the KeyShot scripting API. By combining the functions and best practices from previous chapters, you will be able to efficiently create powerful automation workflows, optimize your scenes, and improve your render quality. Use this section as a quick guide when scripting in KeyShot, and continue expanding your knowledge through the additional resources provided.