refer to code:
.
import unreal
import os
def main_process(input_args):
# Use a directory within your user's documents or another location you have write access to
local_directory = "/Users/user/Documents/Unreal Projects/prj_name/Content/prj_name/Scripts"
# Example usage
filename = "chamfered_cube.obj"
file_path = create_chamfered_cube_obj_file(filename, local_directory, 100.0, 0.1)
imported_asset_path = import_obj_to_unreal(file_path, "/Game/prj_name")
place_static_mesh_in_world('/Game/prj_name/chamfered_cube', (1000, 1000, 100))
def place_static_mesh_in_world(mesh_asset_path, location, rotation=(0, 0, 0), scale=(1, 1, 1)):
# Load the Static Mesh asset
static_mesh = unreal.load_asset(mesh_asset_path, unreal.StaticMesh)
# Get the current editor world
editor_world = unreal.EditorLevelLibrary.get_editor_world()
# Spawn a new StaticMeshActor in the world
static_mesh_actor = unreal.EditorLevelLibrary.spawn_actor_from_class(
unreal.StaticMeshActor, location, rotation
)
if static_mesh_actor:
# Access the StaticMeshComponent property and set the static mesh
static_mesh_component = static_mesh_actor.get_component_by_class(unreal.StaticMeshComponent)
if static_mesh_component:
static_mesh_component.set_static_mesh(static_mesh)
# Set the scale if necessary
static_mesh_actor.set_actor_scale3d(unreal.Vector(*scale))
print(f"Placed Static Mesh at location: {location}")
return static_mesh_actor
else:
print("Failed to access StaticMeshComponent.")
return None
else:
print("Failed to place Static Mesh in the world.")
return None
def import_obj_to_unreal(obj_file_path, unreal_asset_path):
# Set up the import task
import_task = unreal.AssetImportTask()
import_task.filename = obj_file_path # The full path to the OBJ file on disk
import_task.destination_path = unreal_asset_path # The path in Unreal where to import the asset
import_task.automated = True
import_task.save = True
# Set up the import options for Static Mesh
options = unreal.FbxImportUI()
# Set various options on the options object here...
import_task.options = options
# Execute the import task
unreal.AssetToolsHelpers.get_asset_tools().import_asset_tasks([import_task])
# Return the imported asset path if successful, None otherwise
return import_task.imported_object_paths[0] if import_task.imported_object_paths else None
import os
def create_chamfered_cube_obj_file(filename, directory, scale=1.0, chamfer_ratio=0.1):
# Calculate the chamfer size
chamfer_size = scale * chamfer_ratio
half_scale = scale / 2
inner_size = half_scale - chamfer_size
# Define the vertices for a chamfered cube
vertices = [
# Bottom vertices (4 corners)
f"v {-inner_size} {-inner_size} {-half_scale}", f"v {inner_size} {-inner_size} {-half_scale}",
f"v {inner_size} {inner_size} {-half_scale}", f"v {-inner_size} {inner_size} {-half_scale}",
# Top vertices (4 corners)
f"v {-inner_size} {-inner_size} {half_scale}", f"v {inner_size} {-inner_size} {half_scale}",
f"v {inner_size} {inner_size} {half_scale}", f"v {-inner_size} {inner_size} {half_scale}",
# Chamfer vertices on the bottom (4)
f"v {-half_scale} {-half_scale} {-inner_size}", f"v {half_scale} {-half_scale} {-inner_size}",
f"v {half_scale} {half_scale} {-inner_size}", f"v {-half_scale} {half_scale} {-inner_size}",
# Chamfer vertices on the top (4)
f"v {-half_scale} {-half_scale} {inner_size}", f"v {half_scale} {-half_scale} {inner_size}",
f"v {half_scale} {half_scale} {inner_size}", f"v {-half_scale} {half_scale} {inner_size}",
]
# Define the faces for a chamfered cube (using the vertex indices)
faces = [
# Bottom square
"f 1 2 3 4",
# Top square
"f 5 6 7 8",
# Side squares (4 sides)
"f 1 2 6 5", "f 2 3 7 6",
"f 3 4 8 7", "f 4 1 5 8",
# Chamfer triangles (8 triangles)
"f 1 9 2", "f 2 10 3",
"f 3 11 4", "f 4 12 1",
"f 5 13 6", "f 6 14 7",
"f 7 15 8", "f 8 16 5",
# Chamfer squares (connecting the triangles - 4 squares)
"f 9 10 14 13", "f 10 11 15 14",
"f 11 12 16 15", "f 12 9 13 16",
]
# Ensure the directory exists
if not os.path.exists(directory):
os.makedirs(directory)
# Create a full system file path
file_path = os.path.join(directory, filename)
# Writing vertices and faces to the OBJ file
with open(file_path, 'w') as file:
for v in vertices:
file.write(f"{v}\n")
for f in faces:
file.write(f"{f}\n")
print(f"Chamfered Cube OBJ file created at {file_path}")
return file_path
def create_cube_obj_file(filename, directory):
# Create a full system file path
file_path = os.path.join(directory, filename)
# Cube vertices and faces
vertices = [
"v -0.5 -0.5 -0.5", "v -0.5 -0.5 0.5", "v -0.5 0.5 -0.5", "v -0.5 0.5 0.5",
"v 0.5 -0.5 -0.5", "v 0.5 -0.5 0.5", "v 0.5 0.5 -0.5", "v 0.5 0.5 0.5"
]
faces = [
"f 1 3 4 2", "f 5 7 8 6", "f 1 5 6 2", "f 3 7 8 4",
"f 1 5 7 3", "f 2 6 8 4"
]
# Ensure the directory exists
if not os.path.exists(directory):
os.makedirs(directory)
# Writing vertices and faces to the OBJ file
with open(file_path, 'w') as file:
for v in vertices:
file.write(f"{v}\n")
for f in faces:
file.write(f"{f}\n")
print(f"Cube OBJ file created at {file_path}")
return file_path
..