Skip to content

Provides a TypeScript library replicating elements of Blender, and matching export scripts.

License

Notifications You must be signed in to change notification settings

SUNRUSE/dreck-blender-plugin

Repository files navigation

Dreck Blender Plugin License Renovate enabled

Provides a TypeScript library replicating elements of Blender, and matching export scripts.

Dependencies

  • Blender 3.2

This must be available on the PATH (e.g. blender --help prints help text when executed in a Bash terminal).

Installing on common Linux distributions

wget https://download.blender.org/release/Blender3.2/blender-3.2.0-linux-x64.tar.xz
tar -xf blender-3.2.0-linux-x64.tar.xz
sudo ln -s $(pwd)/blender-3.2.0-linux-x64/blender /usr/bin/

Installation

Run the following in a Bash shell at the root of your project:

git submodule add https://github.com/sunruse/dreck-blender-plugin plugins/blender

Usage

Animation

An animation describes how a value changes over time.

Float animations

Float animations describe how a floating-point numerical value changes over time:

const animation: BlenderFloatAnimation = {
  keyframes: [
    {
      type: `linear`,
      atFrame: 25,
      value: 17,
    },
    {
      type: `constant`,
      atFrame: 30,
      value: 19,
    },
    {
      type: `constant`,
      atFrame: 35,
      value: 10,
    },
  ],
};

console.log(blenderSampleFloatAnimation(animation, 20));
console.log(blenderSampleFloatAnimation(animation, 27.5));
console.log(blenderSampleFloatAnimation(animation, 32.5));
console.log(blenderSampleFloatAnimation(animation, 37.5));
16
17
18
19
10

Boolean animations

Boolean animations describe how a boolean value changes over time:

const animation: BlenderBooleanAnimation = {
  keyframes: [
    {
      atFrame: 25,
      value: false,
    },
    {
      atFrame: 30,
      value: true,
    },
    {
      atFrame: 35,
      value: false,
    },
  ],
};

console.log(blenderSampleBooleanAnimation(animation, 20));
console.log(blenderSampleBooleanAnimation(animation, 27.5));
console.log(blenderSampleBooleanAnimation(animation, 32.5));
console.log(blenderSampleBooleanAnimation(animation, 37.5));
false
false
true
false

Transforms

A mutable BlenderTransform type is included describing a transformation matrix. For example:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]

Transform calculation

This function calculates a world transformation matrix matching that which would be generated by Blender in XYZ Euler mode:

const matrix: BlenderMatrix = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

blenderCalculateTransform(
  3, // Translation on X.
  -7, // Translation on Y.
  12, // Translation on Z.
  1.3, // Rotation around X axis, in radians.
  -0.5, // Rotation around Y axis, in radians.
  2.1, // Rotation around Z axis, in radians.
  4.5, // Scale factor on X axis.
  6, // Scale factor on Y axis.
  10.1, // Scale factor on Z axis.
  matrix,
);

// "matrix" now contains a world transformation matrix as would be generated by Blender meeting the above criteria.

Perspective calculation

This function calculates a perspective matrix matching that which would be generated by a Blender camera:

const matrix: BlenderMatrix = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

blenderCalculatePerspective(
  1170, // Width
  1576, // Height,
  24.87, // Focal length.
  2, // Shift on X axis.
  -1.09, // Shift on Y axis.
  2, // Near clip plane distance.
  5, // Far clip plane distance.
  36, // Sensor size.
  matrix,
);

// "matrix" now contains a perspective matrix as would be generated by a Blender camera meeting the above criteria.

Combination/multiplication

This function calculates the result of applying one matrix to another:

const a: BlenderMatrix = [
  1.3735369443893433, -2.974452495574951, 1.6522530317306519,
  -0.0720856785774231, 1.322471261024475, 6.1243109703063965,
  -0.5224795341491699, -0.7749859094619751, 0.8836144804954529,
  -4.5423712730407715, -1.7863743305206299, -0.9022345542907715, 0.0, 0.0,
  0.0, 1.0,
];

const b: BlenderMatrix = [
  -0.01299344003200531, 0.12425273656845093, 0.8930259346961975,
  2.3591322898864746, -0.06693699955940247, 0.18830831348896027,
  -0.023741465061903, 0.44092345237731934, -0.19957560300827026,
  -0.6888105869293213, 0.0446627140045166, -1.4445509910583496, 0.0, 0.0,
  0.0, 1.0,
];

const c: BlenderMatrix = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

blenderCombineMatrices(a, b, c);

// "c" now contains the result of applying "b" to "a".  "a" and "b" are unmodified.
const a: BlenderMatrix = [
  1.3735369443893433, -2.974452495574951, 1.6522530317306519,
  -0.0720856785774231, 1.322471261024475, 6.1243109703063965,
  -0.5224795341491699, -0.7749859094619751, 0.8836144804954529,
  -4.5423712730407715, -1.7863743305206299, -0.9022345542907715, 0.0, 0.0,
  0.0, 1.0,
];

const b: BlenderMatrix = [
  -0.01299344003200531, 0.12425273656845093, 0.8930259346961975,
  2.3591322898864746, -0.06693699955940247, 0.18830831348896027,
  -0.023741465061903, 0.44092345237731934, -0.19957560300827026,
  -0.6888105869293213, 0.0446627140045166, -1.4445509910583496, 0.0, 0.0,
  0.0, 1.0,
];

blenderCombineMatrices(a, b, a);

// "a" now contains the result of applying "b" to "a".  "b" is unmodified.
const a: BlenderMatrix = [
  1.3735369443893433, -2.974452495574951, 1.6522530317306519,
  -0.0720856785774231, 1.322471261024475, 6.1243109703063965,
  -0.5224795341491699, -0.7749859094619751, 0.8836144804954529,
  -4.5423712730407715, -1.7863743305206299, -0.9022345542907715, 0.0, 0.0,
  0.0, 1.0,
];

const b: BlenderMatrix = [
  -0.01299344003200531, 0.12425273656845093, 0.8930259346961975,
  2.3591322898864746, -0.06693699955940247, 0.18830831348896027,
  -0.023741465061903, 0.44092345237731934, -0.19957560300827026,
  -0.6888105869293213, 0.0446627140045166, -1.4445509910583496, 0.0, 0.0,
  0.0, 1.0,
];

blenderCombineMatrices(a, b, b);

// "b" now contains the result of applying "b" to "a".  "a" is unmodified.

Input

This plugin converts each file in the DRECK_BLENDER_INPUT_BLEND_PATHS Make variable.

Output

This plugin writes an equivalent TypeScript file for each input file. For example, if DRECK_BLENDER_INPUT_BLEND_PATHS contained ./example/file-path.blend, ./plugins/data-uri/generated/example-file-path.ts would be written to disk.

Their paths are listed in the DRECK_BLENDER_OUTPUT_TYPE_SCRIPT_PATHS Make variable, space separated.

About

Provides a TypeScript library replicating elements of Blender, and matching export scripts.

Resources

License

Stars

Watchers

Forks