Skip to content

Instantly share code, notes, and snippets.

View mstevenson's full-sized avatar

Michael Stevenson mstevenson

View GitHub Profile
@mstevenson
mstevenson / IEnumeratorExtension.cs
Created April 10, 2016 00:31
Execute a Unity Coroutine in one frame without yielding
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public static class IEnumeratorExtensions
{
/// <summary>
/// Execute an entire Unity Coroutine in one frame.
/// This is useful for testing coroutines with NUnit.
///
@mstevenson
mstevenson / ConfigurableJointExtensions.cs
Created October 24, 2014 07:14
Extension methods for working with Configurable Joints for Unity
using UnityEngine;
public static class ConfigurableJointExtensions
{
/// <summary>
/// Sets a joint's targetRotation to match a given local rotation.
/// The joint transform's local rotation must be cached on Start and passed into this method.
/// </summary>
public static void SetTargetRotationLocal (this ConfigurableJoint joint, Quaternion targetLocalRotation, Quaternion startLocalRotation)
{
@mstevenson
mstevenson / Fps.cs
Last active April 19, 2024 03:04
An accurate FPS counter for Unity. Works in builds.
using UnityEngine;
using System.Collections;
public class Fps : MonoBehaviour
{
private float count;
private IEnumerator Start()
{
GUI.depth = 2;
@mstevenson
mstevenson / SaveFBX.cs
Created August 5, 2013 20:05
ASCII FBX file exporter for Unity. Extracted from Michal Mandrysz's Unity Summer of Code 2009 external lightmapping project.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;
/*
@mstevenson
mstevenson / ConfigurableJointExtensions.cs
Last active March 26, 2024 22:24
Unity extension methods for computing a ConfigurableJoint.TargetRotation value from a given local or world rotation.
using UnityEngine;
public static class ConfigurableJointExtensions {
/// <summary>
/// Sets a joint's targetRotation to match a given local rotation.
/// The joint transform's local rotation must be cached on Start and passed into this method.
/// </summary>
public static void SetTargetRotationLocal (this ConfigurableJoint joint, Quaternion targetLocalRotation, Quaternion startLocalRotation)
{
if (joint.configuredInWorldSpace) {
@mstevenson
mstevenson / CameraPlane.cs
Created January 17, 2013 00:47
A simple solution for grabbing and dragging physics objects in Unity. Attach a DragRigidbody component to an object that has a both collider and a rigidbody. Add the object to a layer named "Interactive".
using UnityEngine;
using System.Collections;
/// <summary>
/// Utility class for working with planes relative to a camera.
/// </summary>
public static class CameraPlane
{
/// <summary>
/// Returns world space position at a given viewport coordinate for a given depth.
@mstevenson
mstevenson / SepScript.mel
Created November 25, 2012 22:40
Maya MEL script that breaks up a mesh based on materials. Created by Andrew Coggeshall at Basenji Games.
// Poly Separate
// Copyright (C) 2012 Basenji Games
// Licensed under the MIT license
string $selection[] = `ls -sl`;
sepMat($selection[0]);
global proc sepMat(string $object){
string $shadingGroups[] = getSGsFromShape($object);
string $ParentName = ($object + "_lightMap_Group");
@mstevenson
mstevenson / MonoBehaviourSingleton.cs
Created December 18, 2012 04:51
Generic Singleton classes for Unity. Use MonoBehaviourSingletonPersistent for any singleton that must survive a level load.
using UnityEngine;
using System.Collections;
public class MonoBehaviourSingleton<T> : MonoBehaviour
where T : Component
{
private static T _instance;
public static T Instance {
get {
if (_instance == null) {
@mstevenson
mstevenson / image_caption_gui.py
Last active December 23, 2023 10:06
Manual image captioning tool for Stable Diffusion training
# Python GUI tool to manually caption images for machine learning.
# A sidecar file is created for each image with the same name and a .txt extension.
#
# [control/command + o] to open a folder of images.
# [page down] and [page up] to go to next and previous images. Hold shift to skip 10 images.
# [shift + home] and [shift + end] to go to first and last images.
# [shift + delete] to move the current image into a '_deleted' folder.
# [escape] to exit the app.
import os
@mstevenson
mstevenson / extract_video_frames.py
Created November 14, 2022 21:11
Extract frames every n seconds from a directory of videos
# extract frames every n seconds from a directory of videos
import cv2
import os
from pathlib import Path
video_base_path = Path('videos')
frames_root_path = Path('frames')
interval = 30 #seconds