Skip to content

Instantly share code, notes, and snippets.

@rdb
rdb / js_linux.py
Last active April 8, 2024 10:12
Access joysticks/game controllers from Python in Linux via the joystick driver. See https://discourse.panda3d.org/t/game-controllers-on-linux-without-pygame/14128
# Released by rdb under the Unlicense (unlicense.org)
# Based on information from:
# https://www.kernel.org/doc/Documentation/input/joystick-api.txt
import os, struct, array
from fcntl import ioctl
# Iterate over the joystick devices.
print('Available devices:')
@rdb
rdb / js_winmm.py
Last active February 17, 2024 10:04
Access game controllers from Python on Windows via the WinMM API. See https://discourse.panda3d.org/t/game-controllers-on-windows-without-pygame/14129
# Released by rdb under the Unlicense (unlicense.org)
# Further reading about the WinMM Joystick API:
# http://msdn.microsoft.com/en-us/library/windows/desktop/dd757116(v=vs.85).aspx
from math import floor, ceil
import time
import ctypes
import _winreg as winreg
from ctypes.wintypes import WORD, UINT, DWORD
from ctypes.wintypes import WCHAR as TCHAR
@rdb
rdb / custom_bam_types.py
Last active June 6, 2023 18:09
This gist demonstrates how to use the new .bam reading hooks to define custom types in Python or define a loader hook for existing types. Requires Panda 1.10.
"""
This example shows how to use the new Python factory function in the BamReader
in order to add a custom hook that is called when the BamReader encounters an
object of a certain type.
You can also use this to add a custom hook whenever the bam reader loads an
existing type, for example if you want to do some post-process task on every
GeomNode or Material loaded from the .bam file.
"""
@rdb
rdb / vertex-pulling.py
Last active April 12, 2023 19:47
Snippet demonstrating vertex pulling in Panda3D
from panda3d.core import *
#load_prc_file_data("", "gl-version 3 3")
from array import array
# Create two silly triangles.
data = array('f', [
0, 4, 0,
1, 4, 1,
0, 4, 1,
@rdb
rdb / optimizer.py
Created December 11, 2022 16:58
Collision scene optimizer for Panda3D
from panda3d.core import *
from array import array
def optimize_collisions(np, *, convert_geometry=False, ignore_z=False, preserve_name=True, preserve_tags=True, track_progress=None):
"""Organizes all "into" collision nodes below this level into an octree or
quadtree (actually an AABB tree, but never mind that) for greatly speeding
up collisions.
If convert_geometry is True, also converts all GeomNodes (unless you set
@rdb
rdb / mocap.py
Last active January 2, 2023 20:29
Motion capture animation recording in Panda3D
from panda3d.core import CharacterJoint, CharacterSlider, CharacterVertexSlider
from panda3d.core import AnimBundle, AnimGroup, AnimChannelScalarDynamic
from panda3d.core import AnimChannelMatrixXfmTable, AnimChannelScalarTable
from panda3d.core import PTA_float
class MotionCapture:
def __init__(self, actor, part_name="modelRoot", lod_name="lodRoot"):
self.part_bundle = actor.get_part_bundle(part_name, lod_name)
self.joint_tables = {}
@rdb
rdb / example.py
Created February 22, 2015 01:02
Example GLSL shader in Panda with simple texture mapping
from panda3d.core import Shader
vshader = """#version 140
// Vertex inputs
in vec4 p3d_Vertex;
in vec2 p3d_MultiTexCoord0;
// Uniform inputs
uniform mat4 p3d_ModelViewProjectionMatrix;
@rdb
rdb / cg2glsl.py
Last active September 28, 2022 11:28
Convert Cg shaders designed for Panda3D to GLSL shaders. Requires cgc.exe to be installed (part of the NVIDIA Cg Toolkit)
import subprocess
import sys
import os
glsl_extensions = {'v': '.vert', 'f': '.frag', 'g': '.geom'}
if len(sys.argv) != 2:
print("Usage: cg2glsl.py something.sha")
sys.exit(1)
@rdb
rdb / client.js
Last active January 9, 2022 11:19
Minimal Node.js-based PStats server implementation
const MSGTYPE_DATAGRAM = 0;
const MSGTYPE_HELLO = 1;
const MSGTYPE_DEFINE_COLLECTORS = 2;
const MSGTYPE_DEFINE_THREADS = 3;
/**
* Contains a single collector definition as sent by the client.
*/
class CollectorDef {
constructor() {
@rdb
rdb / points.py
Created March 17, 2021 12:06
Rendering many points in Panda3D
from panda3d.core import *
from random import random
num_points = 100000
vdata = GeomVertexData('points', GeomVertexFormat.get_v3(), GeomEnums.UH_static)
vdata.set_num_rows(num_points)
vertex = GeomVertexWriter(vdata, 'vertex')
for i in range(num_points):