Skip to content

Instantly share code, notes, and snippets.

View serguei-k's full-sized avatar

Serguei Kalentchouk serguei-k

View GitHub Profile
@serguei-k
serguei-k / tbb_macro.h
Last active May 7, 2016 20:56
TBB Macro
// Macros
#define PARALLEL_FOR_BEGIN(type, variable, first, last, step) \
tbb::parallel_for(first, last, step, [&] (type variable)
#define PARALLEL_FOR_END );
// Example usage
PARALLEL_FOR_BEGIN(unsigned, index, 0u, count, 1u) {
// inner loop code
} PARALLEL_FOR_END
global proc AEFastExpression(string $nodeName)
{
editorTemplate -addControl "expression";
}
global proc AEFastExpression(string $nodeName)
{
editorTemplate -callCustom "CreateCustomControl" "UpdateCustomControl" "expression";
}
global proc CreateCustomControl(string $attrName)
{
// command to create custom control
}
// ExpressionControlCommand.h
#pragma once
#include <maya/MPxCommand.h>
class ExpressionControlCommand : public MPxCommand
{
public:
MStatus doIt(const MArgList& argList) override;
bool isUndoable() const override;
global proc CreateCustomControl(string $attrName)
{
expressionControl -a $attrName -l "Expression" "expressionControl";
}
global proc UpdateCustomControl(string $attrName)
{
expressionControl -e -a $attrName "expressionControl";
}
@serguei-k
serguei-k / translation_mean.cpp
Last active April 13, 2018 06:42
Translation Average
MVector translationAverage(const std::vector<MVector>& values)
{
if (values.empty()) return MVector::zero;
const MVector sum = std::accumulate(values.begin(), values.end(), MVector::zero);
return sum / values.size();
}
@serguei-k
serguei-k / scale_mean.cpp
Last active April 13, 2018 18:17
Scale Average
MVector scaleAverage(const std::vector<MVector>& values)
{
if (values.empty()) return MVector::zero;
const MVector sum = std::accumulate(values.begin(), values.end(), MVector::zero,
[](const MVector& a, const MVector& b)
{
// assumes [Vx, Vy, Vz] > 0
return MVector(a.x + std::log(b.x), a.y + std::log(b.y), a.z + std::log(b.z));
});
const MVector average = sum / values.length();
@serguei-k
serguei-k / rotation_mean.cpp
Created April 13, 2018 06:55
Rotation Average
MEulerRotation rotationAverage(const std::vector<MQuaternion>& values)
{
if (values.empty()) return MEulerRotation::identity;
const MQuaternion sum = std::accumulate(values.begin(), values.end(), MQuaternion::identity,
[](const MQuaternion& a, const MQuaternion& b)
{
return a + b.log();
});
const MQuaternion average(sum.x / values.size(), sum.y / values.size(), sum.z / values.size(), sum.w / values.size());
return average.exp().asEulerRotation();
@serguei-k
serguei-k / spherical_exp_01.mel
Last active May 4, 2018 08:06
Spherical Coordinates Tutorial Expression 1
float $rate = 3.14 / 2.0;
float $theta = offset.translateX * $rate + control.translateX * $rate;
float $phi = offset.translateY * $rate - control.translateY * $rate;
constraint.translateX = sin($theta) * sin($phi);
constraint.translateY = cos($phi);
constraint.translateZ = cos($theta) * sin($phi);
@serguei-k
serguei-k / stereographic.py
Created May 4, 2018 05:13
Spherical Coordinates Stereographic Projection
import maya.api.OpenMaya as om
from math import *
for i, p in enumerate(cmds.ls('unit.vtx[*]', fl=True)):
x, y, z = cmds.xform(p, q=True, t=True)
proj = om.MVector(x / (1 - z), y / (1 - z), 0)
cmds.xform('unit.vtx[{0}]'.format(i), t=proj)