Skip to content

Instantly share code, notes, and snippets.

View esnosy's full-sized avatar

Eyad Ahmed esnosy

  • Egypt
  • 12:13 (UTC +03:00)
View GitHub Profile
@esnosy
esnosy / read_render_result.py
Last active February 26, 2024 10:28
Read render result (or any image) in Blender fast
# Reading from render result without heavy workarounds
# Supports 2.93
# Author Iyad Ahmed (Twitter: @cgonfire, email: iyadahmed@cgonfire.com)
# Licensed under GPL-3
import bgl
import bpy
import gpu
import numpy as np
@esnosy
esnosy / 3d_minimum_volume_bounding_box.py
Last active December 15, 2023 14:45
3D Minimum Volume Bounding Box in Blender
import math
from timeit import default_timer as timer
import bmesh
import bpy
import numpy as np
from mathutils import Matrix
DEBUG = False
@esnosy
esnosy / faster_ray_cast_blender.py
Created June 3, 2021 16:43
How to efficiently ray cast static meshes Blender Python
import bpy
from mathutils.bvhtree import BVHTree
C = bpy.context
# Build BVH once
bvh = BVHTree.FromObject(C.object, C.evaluated_depsgraph_get())
for i in range(999999):
# Slower, this possibly builds BVH everytime
@esnosy
esnosy / uv_edges_blender.py
Last active November 23, 2023 06:55
Example Blender script for getting/setting UV edge selection for large meshes using NumPy and foreach_get/set
# Author: Iyad Ahmed
import bpy
# importing numpy is slow for first time
import numpy as np
prev_mode = bpy.context.object.mode
bpy.ops.object.mode_set(mode="OBJECT")
@esnosy
esnosy / write_eigen_matrix_as_pgm_image.cpp
Created October 4, 2023 19:31
Write Eigen Matrix as PGM image
constexpr size_t N = 900;
Eigen::Matrix<uint8_t, -1, -1> output(N, N);
output.fill(255);
std::ofstream ofs("output.pgm", std::ofstream::binary);
ofs << "P5\n"
<< N << ' ' << N << "\n255\n";
// Otherwise the image will be flipped due to how PGM file format is layed out
output.rowwise().reverseInPlace();
ofs.write(reinterpret_cast<char *>(output.data()), N * N * sizeof(uint8_t));
@esnosy
esnosy / asan.cmake
Last active October 1, 2023 18:26
CMake snipped to enable ASAN for MSVC and non-MSVC in Debug config correctly 😊
if(MSVC)
target_compile_options(eigen_vis_cpp PRIVATE $<$<CONFIG:RelWithDebInfo,Debug>:/fsanitize=address>)
elseif()
target_compile_options(eigen_vis_cpp PRIVATE $<$<CONFIG:RelWithDebInfo,Debug>:-fsanitize=address -fno-omit-frame-pointer>)
target_link_options(eigen_vis_cpp PRIVATE $<$<CONFIG:RelWithDebInfo,Debug>-fsanitize=address>)
endif()
@esnosy
esnosy / skip_line.cpp
Created September 28, 2023 01:05
A skip_line function for std::ifstream
static void skip_line(std::ifstream &ifs)
{
// Thanks to Eljay and CPPReference
// https://stackoverflow.com/questions/53693218/how-to-skip-a-line-of-standard-input#comment94242047_53693218
// https://en.cppreference.com/w/cpp/string/basic_string/getline#Notes
// ifs.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
// also thanks to https://stackoverflow.com/a/72365435/8094047
// and https://jdhao.github.io/2018/12/08/newline_vim_python_sublime_notepad/
// Skip all characters until newline or carriage return
@esnosy
esnosy / timer.hpp
Created June 30, 2023 22:40
Neat TIMER macro for C++11
# GPLv3 iyadahmed430@gmail.com (Iyad Ahmed)
#pragma once
#include <chrono>
#include <functional>
void time_function(const char *message, std::function<void()> function)
{
auto start = std::chrono::high_resolution_clock::now();
@esnosy
esnosy / fast_foreach_set_vertices.py
Created September 16, 2023 12:02
Ultra fast setting vertices locations of Blender mesh in Python, faster than foreach_set
import numpy as np
import ctypes
def fast_foreach_set_vertices(mesh: bpy.types.Mesh, array: NDArray):
# Ultra fast vertices setting
# Thanks a ton!: https://blender.stackexchange.com/a/298488/53664
# beware all things could go wrong here!
assert array.dtype == np.float32
assert len(array.shape) == 1
@esnosy
esnosy / naf_simple_jump.js
Last active August 25, 2023 02:18
networked-aframe simple jump component
// License Public Domain
// These constants are arbitrary but make sure they do not accidentally collide with other things
const animationAttributeName = 'animation__onkeydown_space';
const animationStartEvent = 'onkeydown_space';
AFRAME.registerComponent('simple-jump', {
schema: {},
init: function () {
var el = this.el;
// Thank you!!!: https://stackoverflow.com/a/47019444/8094047
// https://stackoverflow.com/a/51050251/8094047