1/30/2024

checking torch + cuda installed correctly

 

 

Run this script 

.

 

import torch
from torch.utils.cpp_extension import CUDAExtension, BuildExtension

def check_cuda_setup():
cuda_available = torch.cuda.is_available()
print(f"CUDA available: {cuda_available}")

if cuda_available:
cuda_version = torch.version.cuda
print(f"CUDA version (PyTorch): {cuda_version}")

try:
# Attempt to create a CUDA extension
ext = CUDAExtension(
name='test_ext',
sources=[]
)
print("CUDAExtension can be created successfully.")
except Exception as e:
print(f"Error creating CUDAExtension: {e}")

try:
# Attempt to create a BuildExtension object
build_ext = BuildExtension()
print("BuildExtension can be created successfully.")
except Exception as e:
print(f"Error creating BuildExtension: {e}")

if __name__ == "__main__":
check_cuda_setup()


..

If return 'False' then you need to fix your system.

Thank you.


1/16/2024

from pytorch3d.renderer import ( ModuleNotFoundError: No module named 'pytorch3d' (gw_text3d_py39...

1. requirements

conda create -n pytorch3d python=3.9
conda activate pytorch3d
conda install pytorch=1.13.0 torchvision pytorch-cuda=11.6 -c pytorch -c nvidia
conda install -c fvcore -c iopath -c conda-forge fvcore iopath

 

conda install -c bottler nvidiacub

 * otherwise

curl -LO https://github.com/NVIDIA/cub/archive/1.10.0.tar.gz
tar xzf 1.10.0.tar.gz
export CUB_HOME=$PWD/cub-1.10.0


 .

 

 2.Installing prebuilt binaries for PyTorch3D

# Anaconda Cloud
conda install pytorch3d -c pytorch3d

 

or 

# Anaconda Cloud
conda install pytorch3d -c pytorch3d-nightly


or install from whell

pip install --no-index --no-cache-dir pytorch3d -f https://dl.fbaipublicfiles.com/pytorch3d/packaging/wheels/py38_cu113_pyt1110/download.html



refer to this page: https://github.com/facebookresearch/pytorch3d/blob/main/INSTALL.md


Thank  you!



 

1/15/2024

unreal engine, create asset by python widget and copy asset to game env

 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


..

1/10/2024

set python env for unreal engine

go to project settings

find python in plugins category

and put your  "site-packages" path on addtional paths -> index[0] input box


refer to blew screen capture.