From Python to C++

bernard5BABD
Advocate

From Python to C++

bernard5BABD
Advocate
Advocate

Hello,

I have an add-in written in Python, which has been downloaded numerous times. I am thinking about expanding it (or writing a new one based on it), but I would like the new version to be paid for. I believe that to protect the code, I have to write in C++ and compile it. 

Is there a way to "translate" Python to C++, or should I prepare myself to rewrite everything in C++?
The correlative question follows: is there a way to compile and protect Python code? I know you can compile it for standalone programs, but not sure with Fusion 360 API.

Thanks for your help, advice, pointers, and best regards,

Bernard

Bernard Grosperrin, Retired, Maker, and Autodesk Certified Instructor
Blog : Le Bear CNC | Forum : Le Bear CNC Forum | Addin : Airfoil Sketch from file
0 Likes
Reply
Accepted solutions (1)
487 Views
2 Replies
Replies (2)

JeromeBriot
Mentor
Mentor
Accepted solution

Hello,

 


@bernard5BABD  a écrit :

Is there a way to "translate" Python to C++,


AFAIK, no.

 


@bernard5BABD  a écrit :

should I prepare myself to rewrite everything in C++?

Well, that depends on your skill in C++ programming. Distribute compiled C++ code is the safer way for sure. But does it worth the effort and the time spent in rewriting the all code ?

 


@bernard5BABD  a écrit :

 is there a way to compile and protect Python code?


You can compile Python code in .pyc files using compileall or py_compile modules. The .pyc files are binary files not readable by humans (read limitations here: Important Python Version Change Information).

 

I attached a script example to this message.

 

First I wrote the code in the source/message.py file. Then I opened a terminal, I went to the folder where the py file is, and I runnned the following command (replace <USERNAME> and <FUSION360INSTALLID>)

 :

 

 

C:\Users\<USERNAME>\AppData\Local\Autodesk\webdeploy\production\<FUSION360INSTALLID>\Python\python.exe -m py_compile message.py

 

 

The __pycache__ folder was then created with the .pyc file inside. I copied the file message.cpython-37.pyc in the root folder and I renamed it as message.pyc

 

Here is the script that calls the pyc file:

 

import adsk.core, adsk.fusion, adsk.cam, traceback

from . import message

def run(context):

    app = adsk.core.Application.get()
    ui  = app.userInterface

    try:

        message.hello()

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

Don't forget to remove the source file when you distribute the add-in.

 

All the process can be automated using more Python code. But I keep it short here…

 

Hope it helps.

 

1 Like

bernard5BABD
Advocate
Advocate

Thank you Jérôme. I'll look at this solution as soon as I have some time..... 😉

Bernard Grosperrin, Retired, Maker, and Autodesk Certified Instructor
Blog : Le Bear CNC | Forum : Le Bear CNC Forum | Addin : Airfoil Sketch from file
0 Likes