Keeping your code compatible with recent changes

This thread is for OPENRNDR users who update openrndr and orx to version 0.4.5 or later. At this point it’s not yet officially released, but the changes may affect users building SNAPSHOT versions.

In most cases upstream changes to those repositories only bring new features or optimizations and require no changes in our code. But sometimes the changes do require us to update our programs.

I plan to document such changes in this thread to make it easier to live on the edge :slight_smile:

3 Likes

SVG, Composition and Writer moved from openrndr to orx

This change will make it possible to eventually use filters and gradients in SVG content and facilitate further improvements.

Related commits: SVG and Composition , writer

Required changes

  • Add orx-composition, orx-svg and orx-text-writer to your template
  • Update imports / code:
Find Replace with
import org.openrndr.shape.ClipMode import org.openrndr.extra.composition.ClipMode
import org.openrndr.shape.Composition import org.openrndr.extra.composition.Composition
import org.openrndr.shape.CompositionDimensions import org.openrndr.extra.composition.CompositionDimensions
import org.openrndr.shape.CompositionDrawer import org.openrndr.extra.composition.CompositionDrawer
import org.openrndr.shape.draw import org.openrndr.extra.composition.draw
import org.openrndr.drawComposition import org.openrndr.extra.composition.drawComposition
import org.openrndr.shape.GroupNode import org.openrndr.extra.composition.GroupNode
import org.openrndr.svg.loadSVG import org.openrndr.extra.svg.loadSVG
import org.openrndr.svg.saveToFile import org.openrndr.extra.svg.saveToFile
import org.openrndr.svg.toSVG import org.openrndr.extra.svg.toSVG
import org.openrndr.svg.writeSVG import org.openrndr.extra.svg.writeSVG
import org.openrndr.draw.Cursor import org.openrndr.extra.textwriter.Cursor
import org.openrndr.draw.Writer import org.openrndr.extra.textwriter.TextWriter
import org.openrndr.draw.writer import org.openrndr.extra.textwriter.writer
Writer(...) TextWriter(...) {

Example commit to an openrndr-template making these changes.

Segment becomes Segment2D

With this change code shared by the 2D and 3D segments is de-duplicated making it easier to maintain (and I suspect adding missing functionality to Segment3D).

Related commits: Generalize segments and paths · openrndr/openrndr@09af6eb · GitHub

Required changes

Find Replace with
Segment(...) Segment2D(...)
import org.openrndr.shape.Segment import org.openrndr.shape.Segment2D

Refactor orx-shapes package layout

Since orx-shapes has grown quite a lot, its various shape constructors were organized into logical packages. The functionality is still the same but the import statements in existing projects must be updated if switching to orx 0.4.5.

Related commits: [orx-shapes] Refactor package layout · openrndr/orx@8fbb106 · GitHub

Required changes

Find Replace with
import org.openrndr.extra.shapes.AlphaShape import org.openrndr.extra.shapes.alphashape.AlphaShape
import org.openrndr.extra.shapes.Arrangement import org.openrndr.extra.shapes.arrangement.Arrangement
import org.openrndr.extra.shapes.BoundedFace import org.openrndr.extra.shapes.arrangement.BoundedFace
import org.openrndr.extra.shapes.bezierPatch import org.openrndr.extra.shapes.bezierpatches.bezierPatch
import org.openrndr.extra.shapes.drawers.bezierPatches import org.openrndr.extra.shapes.bezierpatches.bezierPatches
import org.openrndr.extra.shapes.distort import org.openrndr.extra.shapes.bezierpatches.distort
import org.openrndr.extra.shapes.hobbyCurve import org.openrndr.extra.shapes.hobbycurve.hobbyCurve
import org.openrndr.extra.shapes.Arc import org.openrndr.extra.shapes.primitives.Arc
import org.openrndr.extra.shapes.grid import org.openrndr.extra.shapes.primitives.grid
import org.openrndr.extra.shapes.Net import org.openrndr.extra.shapes.primitives.Net
import org.openrndr.extra.shapes.Pulley import org.openrndr.extra.shapes.primitives.Pulley
import org.openrndr.extra.shapes.regularPolygon import org.openrndr.extra.shapes.primitives.regularPolygon
import org.openrndr.extra.shapes.regularPolygonBeveled import org.openrndr.extra.shapes.primitives.regularPolygonBeveled
import org.openrndr.extra.shapes.regularPolygonRounded import org.openrndr.extra.shapes.primitives.regularPolygonRounded
import org.openrndr.extra.shapes.regularStar import org.openrndr.extra.shapes.primitives.regularStar
import org.openrndr.extra.shapes.regularStarRounded import org.openrndr.extra.shapes.primitives.regularStarRounded
import org.openrndr.extra.shapes.RoundedRectangle import org.openrndr.extra.shapes.primitives.RoundedRectangle
import org.openrndr.extra.shapes.Tear import org.openrndr.extra.shapes.primitives.Tear

Rectangle.uniform

The .uniform() methods were recently updated to make use of better hash functions. We can now query random uniform points from Rectangle, Triangle, Circle and Box. In these changes Rectangle.uniform was moved into a shapes package, which requires to update the import as listed below (otherwise the IDE will complain about the wrong number of arguments).

Related commits: [orx-noise] Add hash functions · openrndr/orx@fba1e5b · GitHub

Required changes

Find Replace with
import org.openrndr.extra.noise.uniform import org.openrndr.extra.noise.shapes.uniform

In this change, the .uniform() method lost it’s distance-to-edge argument. If you made use of it, first offset the rectangle and the call .uniform().

drawer.bounds.uniform(100.0)

becomes

drawer.bounds.offsetEdges(-100.0).uniform()

OPENRNDR Random class now deprecated

The OPENRNDR Random class gets often confused with the Kotlin Random class. Less experienced users may import the wrong one using the IDE’s automatic import feature, and the code would fail to run.

Therefore it has been recently deprecated. Here alternatives to commonly used methods:

Before After Import
Random.double() Double.uniform(-1.0, 1.0) import org.openrndr.extra.noise.uniform
Random.double(0.0) Double.uniform(0.0, 1.0) import org.openrndr.extra.noise.uniform
Random.double0() Double.uniform(0.0, 1.0) import org.openrndr.extra.noise.uniform
Random.bool(0.01) Boolean.random(0.01) import org.openrndr.extra.noise.primitives.random
Random.simplex(0.1) simplex(1234, 0.1, 0.0) import org.openrndr.extra.noise.simplex
Random.simplex(vec2 * 0.1) simplex(1234, vec2 * 0.1) import org.openrndr.extra.noise.simplex
Random.int(12, 34) Int.uniform(12, 34) import org.openrndr.extra.noise.primitives.random
Random.int0(123) Int.uniform(0, 123) import org.openrndr.extra.noise.primitives.random

Random.seed

If your program uses Random.seed because you want reproducible randomness, there are some required changes. Random.seed expected a String, but we will now use Kotlin’s Random, which expects an Int for a seed in its constructor. You will need to figure out an approach for switching to integer seeds. Once you do,

Before: Random.seed = "friendly parrot"
Now: val rnd = Random(seed = 2) // remember to import kotlin.random.Random

Then pass the rnd variable to any of the methods in the table above, or to methods like Rectangle.scatter():

Double.uniform(0.0, 1.0, rnd)
Int.uniform(12, 34, rnd)
drawer.bounds.scatter(width * 0.1, random = rnd)

By passing a seeded Random generator the sequence of produced values will be the same each time you run the program.

Random.unseeded { }

Since random functions are now unseeded by default, Random.unseeded can be safely deleted from your code (openrndr/orx 0.4.5-alpha8 or later).

1 Like

Color transform methods moved into orx

The functions used to generate a colorMatrix have been moved into orx, affecting the usage of these methods

  • tint()
  • invert()
  • grayscale()
  • constant()

Related commits: openrndr and orx.

Required changes

Find Replace with
import org.openrndr.draw.tint import org.openrndr.extra.color.colormatrix.tint
import org.openrndr.draw.grayscale import org.openrndr.extra.color.colormatrix.grayscale
import org.openrndr.draw.invert import org.openrndr.extra.color.colormatrix.invert
import org.openrndr.draw.constant import org.openrndr.extra.color.colormatrix.constant

Here the relevant section in the guide.

Hey everybody,

first off, thanks for this AWESOME work :slight_smile: I’ve just updated a bigger project from 0.4.4 to 0.4.5-alpha9. Besides the things in this thread, it seemed to me that somehow my system (OSX, M1) switched to OpenGL ES instead of plain OpenGL - which broke all my custom shaders :slight_smile: .

After some debugging, it turned out to be this line where it switched to OpenGL ES for M1 OSX Mac.

=> so for me setting -Dorg.openrndr.gl3.gl_type=gl was crucial.

All the best, and keep up the great work,
Sebastian

1 Like

Thank you @skurfuerst for your kind words and for sharing your discovery. Welcome to the forum! :slight_smile: I’ll share it with the team in case we can give more info about this change.