Hello, The code below will import a mesh and create a line through the mesh. I now want to twist the mesh using the line as the twist axis. But all of my attempts have failed. If anyone could give me some guidance on how to do this I would be grateful!
import os
import rhinoscriptsyntax as rs
import random
def import_random_mesh_and_process():
print("Step 1: Starting script.")
# Path to your folder containing OBJ files
folder_path = r"C:\Users\jdm037\Desktop\rhino_python_scripts\meshes_simple"
# Delete all visible geometry in the scene
print("Step 2: Deleting all visible geometry.")
visible_objects = rs.ObjectsByType(0, True, True) # 0 = all objects, include_hidden=False, include_lights=True
if visible_objects:
rs.DeleteObjects(visible_objects)
print("Step 2: All visible geometry deleted.")
else:
print("Step 2: No visible geometry to delete.")
# Get a list of all .OBJ files in the folder
print("Step 3: Gathering .OBJ files.")
mesh_files = [f for f in os.listdir(folder_path) if f.endswith('.obj')]
if not mesh_files:
print("Step 3: No OBJ files found in the specified folder.")
return
print("Step 3: OBJ files found.")
# Select a random OBJ file
print("Step 4: Selecting a random OBJ file.")
random_mesh_file = random.choice(mesh_files)
mesh_file_path = os.path.join(folder_path, random_mesh_file)
print("Step 4: Selected file - {}".format(random_mesh_file))
# Import the selected OBJ file into Rhino
print("Step 5: Importing the selected OBJ file.")
try:
rs.Command('-Import "{}" _Enter'.format(mesh_file_path))
print("Step 5: Successfully imported - {}".format(random_mesh_file))
except Exception as e:
print("Step 5: Failed to import mesh: {}".format(e))
return
# Find the longest dimension of the imported mesh and create a line
print("Step 6: Processing the imported mesh.")
try:
# Get the imported mesh
imported_mesh = rs.LastCreatedObjects(select=False)
if not imported_mesh:
print("Step 6: No mesh was imported.")
return
print("Step 6: Imported mesh detected.")
imported_mesh_id = imported_mesh[0]
bounding_box = rs.BoundingBox(imported_mesh_id)
if not bounding_box:
print("Step 6: Failed to calculate bounding box for the imported mesh.")
return
print("Step 6: Bounding box calculated.")
# Calculate the center of the bounding box
center_x = (bounding_box[0][0] + bounding_box[6][0]) / 2
center_y = (bounding_box[0][1] + bounding_box[6][1]) / 2
center_z = (bounding_box[0][2] + bounding_box[6][2]) / 2
center = (center_x, center_y, center_z)
# Calculate dimensions along X, Y, Z
x_length = abs(bounding_box[1][0] - bounding_box[0][0])
y_length = abs(bounding_box[3][1] - bounding_box[0][1])
z_length = abs(bounding_box[4][2] - bounding_box[0][2])
print("Step 6: Dimensions calculated.")
# Determine the longest axis
longest_length = max(x_length, y_length, z_length)
axis = ["X", "Y", "Z"][ [x_length, y_length, z_length].index(longest_length) ]
# Create a line along the longest axis, centered on the mesh
if axis == "X":
line_start = (center[0] - longest_length, center[1], center[2])
line_end = (center[0] + longest_length, center[1], center[2])
elif axis == "Y":
line_start = (center[0], center[1] - longest_length, center[2])
line_end = (center[0], center[1] + longest_length, center[2])
else: # Z
line_start = (center[0], center[1], center[2] - longest_length)
line_end = (center[0], center[1], center[2] + longest_length)
rs.AddLine(line_start, line_end)
print("Step 7: Created a line along the {}-axis through the center.".format(axis))
except Exception as e:
print("Step 7: Failed to process mesh: {}".format(e))
# Run the function
import_random_mesh_and_process()
1 post - 1 participant