3DCoat Core API
The 3DCoat API documentation.
No Matches

The basic generator example, just to demonstrate the principles of generators, look the TreesGenerator.cpp for the more sophisticated example.

// The basic generator example, just to demonstrate the principles of generators, look the \ref TreesGenerator.cpp for the more sophisticated example.
#include <CoreAPI.h>
//@config: Release
using namespace coat;
// This is the simplest non-destructive generator example.
// It is registered as tool in the current room toolset as the first tool in list.
const char* file_for_settings = "data/Temp/SimpleGenerator.json";
class SimpleGenerator : public SculptGenerator{
SimpleGenerator() {
numSpheres = 200;
FigureRadius = 30;
FigureRadiusVariation = 0.1;
SpheresRadius = 10;
SpheresRadiusVariation = 0.5;
// load the sphere mesh
// returns the name/ID of the tool in the toolset
virtual const char* GetID() {
return "SimpleGenerator";
virtual const char* getDefaultObjectName() {
return "Spheres";
int numSpheres;
float FigureRadius;
float FigureRadiusVariation;
float SpheresRadius;
float SpheresRadiusVariation;
// we show parameters only if the object already created
if (getObject()) {
SLIDER(numSpheres, "numSpheres", 1, 1000);
FSLIDER(FigureRadius, "FigureRadius", 1, 100, 1, false);
FSLIDER(FigureRadiusVariation, "FigureRadiusVariation", 0, 1, 100, false);
FSLIDER(SpheresRadius, "SpheresRadius", 1, 100, 1, false);
FSLIDER(SpheresRadiusVariation, "SpheresRadiusVariation", 0, 1, 100, false);
// show the transform gizmo, both TransformObject and Transform defined in the parent class
if(TransformObject) {
// generate random value with variation
float random(float value, float variation) {
return value * (1.0f + variation * rand() / 32768.0f);
void generate(Volume v) {
coat::Mesh summ;
for (int i = 0; i < numSpheres; i++) {
// create transformation matrix.
// scaling to the sphere radius
coat::mat4 transform = coat::mat4::Scaling(random(SpheresRadius, SpheresRadiusVariation));
// translate using the random vector
transform *= coat::mat4::Translation(coat::vec3::RandNormal() * random(FigureRadius, FigureRadiusVariation));
// add the sphere to the summary mesh
summ.addTransformed(m, transform);
v.mergeMesh(summ, mat4::Identity, coat::BOOL_MERGE);
// save settings
// fast ang rough generating, the function is overriden from the SculptGenerator
virtual void GeneratePreview() override {
SceneElement s(getObject());
// we remove previously generates object
// we add the new object as sub-object to the current
SceneElement t = s.addChild("Simple");
Volume v = t.Volume();
// turen the new volume to surface
// we generate as if there is no transform
// we apply the transform that user made manually
// we select the generated object
// generage the object in "final quality", the function is overriden from the SculptGenerator
virtual void GenerateFinalObject() override {
// in simplest case the final quality object is same as preview
EXPORT_EXTENSION(SimpleGenerator) {
// we create the generator object
SimpleGenerator* tg = new SimpleGenerator;
// read settings
// register the tool in the toolset
// activate the tool
The mesh reference.
Definition CoreAPI.h:401
void addTransformed(const Mesh &m, const mat4 &t)
concatenate the transformed mesh with the current one
bool Read(const char *name)
Load the mesh from the file.
The scene element, like sculpt object or curve.
Definition CoreAPI.h:1609
void selectOne() const
unselect all similar elements and select this one
mat4 getTransform() const
get the scene element transform
void removeSubtree() const
remove the whole subtree
const SceneElement & setTransform(const mat4 &Transform) const
Set the transform matrix.
SceneElement addChild(const char *name) const
add the child element of the same nature
The class allows to operate over voxels/surface on the relatively low-level.
Definition CoreAPI.h:1911
void mergeMesh(Mesh &mesh, const mat4 &transform=mat4::Identity, BoolOpType op=BOOL_MERGE)
merge the mesh into scene
void toSurface()
turn to surface mode, the triangles will be tangentially relaxed
The coat namespace used for most 3DCoat API calls except low-level internal structures.
Definition CoreAPI.h:43
just merge, no booleans, it may be used only in surface mode
Definition CoreAPI.h:89
comms::cMat4 mat4
4x4 float matrix, see the cMat4
Definition CoreAPI.h:59