Need addon coded but have C++ equivalent code

I know Python 3 fundamentally, but Blender’s scripting API just confuses the heck out of me. I need an addon that is able to generate 3D coordinates from 2D coordinates in a mirror symmetric model. I have a rather outdated program that is able to do this that conveniently enough distributes the algorithm needed as what I believe is an MIT licensed C++ code file (says it’s copyrighted but can be freely used in commercial products)

I know C++ and Python, but my real trouble with either or is wrapping my mind around APIs, which in this case is Blender’s.

I’ve pasted the C++ code at I’m hoping someone out there may be able to help my dilemma.

I glanced over the C++ code and it just seems to be doing a number of matrix operations to transform points.

What part of Blender’s API is preventing you from using this code? Are you trying to take an existing “2D” model that is in Blender and run this code against all the points in the existing model?

As far as getting this code running you have some options.

  1. Turn this code into a Python module and expose the class that does the work you are interested in. If you change platforms, you will have to recompile the C++ code, but there are lots of Python modules that are backed up by C or C++ like this. The big advantage is you just have to find a way to feed your input points into the module and you won’t have to port complicated matrix code.

  2. Re-write that file using Blender’s built-in Matrix and Vector classes. A lot of that code is definitions for basic Matrix operations that Blender’s Matrix class already implements. Doing this would make it easier to move between platforms, but it is pretty easy to introduce bugs with complicated code like that.

I’d go for the option of the re-write. But the first step is to understand what the code actually does. code rewriting without understanding the code is a very tricky business.

I’m not sure which part of the blender API you find difficult. I can imagine that you find the GUI part difficult. I would suggest to just implement the core of the algorithm in python using the Vector, Matrix and Euler classes from mathutils (blender math library). After that I think it is easier to find someone to help you integrate it into blender as a plugin.

I did a quick attempt to convert the algorithm. It should take the selected path consisting of 2 splines, and should reconstruct a path with 2 3d splines from it.
Alas, the result does not work (or at least, it does not do the magic I expected it to do)

where my implementation could have failed:

  • the algorithm seems to apply rotations in Y,X,Z order. In that kind of thing it is easy to make mistakes
  • The Rqranalysis method was using 1-based indexing, I changed it to 0-based. That could introduce a bug
  • The order of matrix multiplication could be wrong. I just guessed, but could have guessed wrong
  • At several places I used what I guessed is the blender version of the matrix operation, but I did not check if the results are actually the same.
  • The main method seems stripped down. I tried to just get the idea from there and wrote my own version.

3d_from_2d_reconstruction.blend (591 KB)

I put the code and my test in the attached file. If anybody else wants to give it a try, I wish him/her good luck.

Well it is 22 year old code, so I’ll see… Hmm… this new “result” mesh is a simple angle of vertices. Looks like there’s a bug or two. I’ll need to read up on this. Thanks though!