Is it possible to convert a .pyde file to a .pde file ?

edited January 2018 in Python Mode

Hi,

The question says it all. I have sketches coded in Python via Python Mode that I'd like to convert to raw Processing programming language (.pde file). Is that possible ? If so, how could I make the conversion ?

Regards

Answers

  • There isn't an automatic method that I'm aware of. @jdf might know If contacted through processing.py on GitHub.

    In some cases, manual conversion is as simple as adding semicolons to the end of lines and changing function declarations -- it depends on how complex your .py sketch is and if you are mainly just calling the processing API (easy) or if you are using Python-specific imports (like itertools) and idioms (like list comprehensions) (harder).

    To get Java classes that you could adapt back into Processing (although this might be harder than hand editing) you can run your code through Jython and inspect the class files it generates, or you could try (untested) calling the processing.py Jython and use compileall or py_compile; see:

  • Thank you. I personnaly use a lot of list comprehensions so I guess the task won't be easy.

  • edited January 2018

    In that case, if there are any generic methods of automatically converting Python into Java, they might work for you. That sounds like it might already be a StackOverflow question somewhere....

  • edited January 2018

    This is how I've converted this array initialization loop from this Java Mode sketch below: ~O)

    http://Studio.ProcessingTogether.com/sp/pad/export/ro.9AqEIV-5Q9sCx

    // Trig lookup tables borrowed from Toxi; cryptic but effective.
    static final float PRECISION = 2.0, RADS = .5 * DEG_TO_RAD*PRECISION;
    static final int LENGTH = (int) (360/PRECISION), LUTS = LENGTH<<1;
    
    static final float[] COS_SIN = new float[LUTS];
    static // Comment this out for JS Mode!
    { // Fill the tables
      for (int i = 0; i < LUTS; i += 2) {
        COS_SIN[i]   = cos(RADS*i);
        COS_SIN[i+1] = sin(RADS*i);
      }
    }
    

    To Python Mode; turning that whole loop as a 1-statement comprehension: :ar!

    https://Forum.Processing.org/two/discussion/14071/converting-java-code-to-python-for-dataviz#Item_13

    PRECISION = 1.5; LENGTH = int(360//PRECISION)
    LUTS, RADS = LENGTH<<1, .5 * DEG_TO_RAD*PRECISION
    
    COS_SIN = tuple(sin(RADS*(i-1)) if i&1 else cos(RADS*i) for i in range(LUTS))
    
Sign In or Register to comment.