Translating from C to Python.

I came across a plugin for another 3D program that when activated creates a sprial mesh & object. Think torsion spring. However, the code is in C. I would like to translate it to Python and use it as script in Blender. I looked at the existing AddMesh scripts, but did not see one that created a sprial/spring.

Does anyone know of any resources that would guide me in translating C code to Python? I’m not familiar enough with C to correctly translate it. I found ctopy online, a program that “…automates the parts of translating C source code to Python source code that are difficult for a human but easy for a machine…” However, it’s a .rpm extension which doesn’t appear to run on Ubuntu as it uses DEB (dpkg).

Thanks for any assist,
David C.

I guess the most easy way would be just extending Python with the C routines you have usable, see:

http://docs.python.org/extending/

Can you show the original code, it may be possible to find the logics behind the programm even without “translating” it from a language to an another.

" However, it’s a .rpm extension which doesn’t appear to run on Ubuntu as it uses DEB (dpkg).

Try alien. From the site:

Alien is a program that converts between the rpm, dpkg, stampede slp, and slackware tgz file formats. If you want to use a package from another distribution than the one you have installed on your system, you can use alien to convert it to your preferred package format and install it.

http://kitenet.net/~joey/code/alien/

You should be able to install it from the repo with a “sudo apt-get install alien” command. Good luck.

This from the Anim8r 3D program and according to their site, the scripting language (called ASL for now) “is an interpreted language loosely based on the C programming language but with a somewhat restricted set of functionality. It also borrows a few simple syntactic constructs from C++.”

 
/* spring_plugin_1.a8s */
 
/*
* This script constructs a Mesh in the shape of a coiled spring
* aligned along the Y-axis.
*
* Parameters:
*
* sides - sides on polygon extruded to from the coil.
* diameter - diameter of polygon.
* offset - radius of center of coil from center of Y-axis.
* segments - total number of segments along coil.
* cycles - number of revolutions for coil.
* spread - vertical displacement between center of coils.
*
* Copyright 2006 R. Steven Glanville. Permission granted for
* modification and use, including commercial use. Source
* distribution must include this copyright.
*
*/
/*
* Directives to install as Mesh plugin in Object editor.
*
* Plugin kind and name - defined by #plugin directive:
*
* arg1: string, editor kind. "object" is for the Object editor
* arg2: string, plug-in kind. "mesh" is for a parameteric shape
* arg3: string, name used in Anim8or interface to feref to this shape
*
* Parameters - defined by #parameter directives:
*
* arg1: string, parameter name
* arg2: type name (int or float)
* arg3: initial value
* arg4: minimum value
* arg5: maximum value
* arg6+: Zero or more semantics for interaction:
*             scale - Scale commands apply to this value
*             scale_x - Non-uniform X scaling applies
*             scale_y - Non-uniform Y scaling applies
*             scale_z - Non-uniform Z scaling applies
*
* Return value - #return directive gives variable that will hold the result
* when the script is finished. It must be declared in the scripts. For a
* Parameteric Shape it must be of type shape.
*
* Icon for toolbar - defined by #button directive.
*
* arg1: width in pixels. Max value of 32 but anything above 25 doesn't
* all show up. 
* arg2: height in pixels Mas value of 32 but again it's best to stay
* at 25 or less.
* arg3: number of colors - must be 2
* arg4+ "height" number of 32 bit hex values. The right most "width" bits
* are used as a bit map with 1's being the icon and 0's the background.
*
*/
#plugin("object", "mesh", "spring");
#parameter("sides", int, 6, 3, 16);
#parameter("diameter", float, 10.0, 0.001, 99999, scale);
#parameter("offset", float, 20.0, 0.0, 99999, scale, scale_x);
#parameter("segments", int, 30, 1, 1000);
#parameter("cycles", float, 3, 0.01, 100.0, scale_z);
#parameter("spread", float, 15.0, 0.01, 100.0, scale, scale_y);
#return($spring);
#button(17, 25, 2,
       0x00000fc6, 0x00007079, 0x00008009, 0x00010f89, 
       0x00013871, 0x000107e1, 0x0000c003, 0x0000300d, 
       0x00000ff9, 0x00007079, 0x00008009, 0x00010f89, 
       0x00013871, 0x000107e1, 0x0000c003, 0x0000300d, 
       0x00000ff9, 0x00007079, 0x00008009, 0x00010f89, 
       0x00013871, 0x000107e1, 0x0000c002, 0x0000300c, 
       0x00000ff0);
shape $spring; /* Will hold the resulting mesh */
 
int $numSides; /* Number of sides on cross section */
float $diameter; /* Diameter of cross section */
float $offset; /* Radius of spring coil */
int $segments; /* Segments along length of coil */
float $cycles; /* Times coil wraps around */
float $spread; /* Distance between centers of coils */
 
/* Get spring parameters from Anim8or: */
 
$numSides = parameter("sides");
$diameter = parameter("diameter");
$offset = parameter("offset");
$segments = parameter("segments");
$cycles = parameter("cycles");
$spread = parameter("spread");
 
/* Declare some working variables: */
 
point3 $p[16];
float $u[17];                    /* U component of texture coordinates */
point3 $p0;
int $vtxIndex[34];
int $texIndex[34];
float $radius, $t;
float $angle, $cosa, $sina;
int $faceIndex, $i, $edgeIndex;
 
/* Now build it: */
 
$radius = $diameter*0.5;
 
/* $numSides = min(16, $numSides);                /* Only room for 16 sides */
 
/* Define a '$numSides' sided N-gon: */
 
for $i = 0 to $numSides - 1 do {
    $u[$i] = (1.0*$i)/$numSides;
    $t = (2*3.14159*$i)/$numSides;
    $p[$i] = (sin($t)*$radius + $offset, cos($t)*$radius, 0);
}
$u[$numSides] = 1.0;
 
/* Tilt it to face along the coil: */
 
$angle = atan($spread/(2*3.14159*$offset));
$cosa = cos($angle);
$sina = sin($angle);
for $i = 0 to $numSides - 1 do {
$p0 = $p[$i];
$p[$i].y = $cosa*$p0.y - $sina*$p0.z;
$p[$i].z = $sina*$p0.y + $cosa*$p0.z;
}
 
/* A new, empty mesh is assigned to $spring before the script runs. */
 
/* Open a mesh or you can't edit it. */
 
$spring.Open();
for $i = 0 to $numSides - 1 do {
$texIndex[$i] = $spring.AddTexCoord(($u[$i], 0));
$vtxIndex[$i] = $spring.AddPoint($p[$i]);
}
 
$vtxIndex[$numSides] = $vtxIndex[0];
$texIndex[$numSides] = $texIndex[0];
 
/* First end cap: */
 
$faceIndex = $spring.OpenFace(0, 4);
for $i = 0 to $numSides - 1 do {
$spring.TexCoordN($texIndex[$i]);
$spring.VertexN($vtxIndex[$i]);
}
$spring.CloseFace();
 
/* Set edge sharpness to rounded after 3 subdivisions: */
 
for $i = 0 to $spring.GetNumSides($faceIndex) do {
$edgeIndex = $spring.GetEdgeIndex($faceIndex, $i);
$spring.SetEdgeSharpness($edgeIndex, 3);
}
 
int $seg;
point3 $p1;
int $oldIndex, $newIndex, $temp;
float $y, $percent;
$oldIndex = 0;
$newIndex = 17;
for $seg = 1 to $segments do {
 
/* Transform points in cross section to next position: */
 
$percent = ($seg*1.0)/$segments;
$angle = (2*3.14159*$cycles)*$percent;
$sina = sin($angle);
$cosa = cos($angle);
$y = ($spread*$cycles)*$percent;
for $i = 0 to $numSides - 1 do {
    $p0 = $p[$i];
    $p1.x = $cosa*$p0.x + $sina*$p0.z;
    $p1.y = $p0.y + $y;
    $p1.z = -$sina*$p0.x + $cosa*$p0.z;
    $vtxIndex[$newIndex + $i] = $spring.AddPoint($p1);
    $texIndex[$newIndex + $i] = $spring.AddTexCoord(($u[$i], $percent));
}
$vtxIndex[$newIndex + $numSides] = $vtxIndex[$newIndex];
$texIndex[$newIndex + $numSides] = $spring.AddTexCoord(($u[$numSides], 1));
 
/* Define faces for current segment: */
 
for $i = 0 to $numSides - 1 do {
    $spring.OpenFace(0, 4);
    $spring.TexCoordN($texIndex[$oldIndex + $i]);
    $spring.VertexN($vtxIndex[$oldIndex + $i]);
    $spring.TexCoordN($texIndex[$newIndex + $i]);
    $spring.VertexN($vtxIndex[$newIndex + $i]);
    $spring.TexCoordN($texIndex[$newIndex + $i + 1]);
    $spring.VertexN($vtxIndex[$newIndex + $i + 1]);
    $spring.TexCoordN($texIndex[$oldIndex + $i + 1]);
    $spring.VertexN($vtxIndex[$oldIndex + $i + 1]);
    $spring.CloseFace();
}
/* Swap indexes */
 
$temp = $oldIndex;
$oldIndex = $newIndex;
$newIndex = $temp;
}
/* Final end cap: */
 
$faceIndex = $spring.OpenFace(0, 4);
for $i = 0 to $numSides - 1 do {
 
/* $oldIndex is last face */
 
$spring.TexCoordN($texIndex[$oldIndex + $i]);
$spring.VertexN($vtxIndex[$oldIndex + $i]);
}
$spring.CloseFace();
 
/* Set edge sharpness to rounded after 3 subdivisions: */
 
for $i = 0 to $spring.GetNumSides($faceIndex) do {
$edgeIndex = $spring.GetEdgeIndex($faceIndex, $i);
$spring.SetEdgeSharpness($edgeIndex, 3);
}
 
/* Don't forget to close the shape or your changes might be lost! */
 
$spring.Close();
 
/* Fall off the end and your finished. */

And the end result is this:

http://www.anim8or.com/scripts/spring.jpg

I spent this morning looking through and reading about ASL at http://www.anim8or.com/main/index.html differentiating between ASL, C & C++ syntax, but I lack the knowledge base of C/C++ programming languages. The author identified some of the C & C++ syntax and some I recognized like if, while and for, which (obviously) is used in Python.

I’ve been examining some of the AddMesh scripts figuring out how I can “reverse engineer” an existing script to create the coil spring. I haven’t yet started to experiment with them, for now I’m writing it out by hand and refering back to the original AddMesh codes and the above code to figure it out, removing what (I hope) is the ASL code and looking for similiarities within the C/C++ to translate to Python.

I read through a couple of pages of the link you provided, but I don’t believe extending will work–though I could be wrong–because this doesn’t appear to be a true C or C++ module. The code is a mix of C, C++ and the ASL.

And thanks linuxpimp, I’ll take a look at it at the site you recommended.

David C.'s most recent post was caught in the moderation queue for a couple days. I’m bumping this thread to bring it back to the top for consideration.

David,

Is making a spring all you are hoping to accomplish? If so, you can use the Screw tool which is in the Edit buttons to do this. Here are the instructions from the Blender wiki:

http://wiki.blender.org/index.php/Doc:Manual/Modelling/Meshes/Basic_Tools#Screw

Blender comes with a add_mesh_torus.py which is a simple script for making a torus (donut). A spring is basically a torus that loops more than once while moving in the Z axis.

So you could just get the torus script and increase the PREF_MAJOR_SEG for loop, which is the main loop for number of rings the torus have. And just increase that loop to whatever number of loops you want your spring to have. Then you go where it appends the verts to the mesh and add (the verts.append() method) and increment the z axis each major loop.

And you’d have a spring.

Unfortunally I still suck with the Blender API to do it myself, but that’s the idea. Shouldn’t be hard.

Blendenzo - Yes, creating a coiled spring mesh is what I’m hoping to accomplish. I’ll check out the screw tool. Though I am looking to actually write a script simply for the experience. I’ve been copying code from a couple Python books as exercises for the last couple of months, but haven’t attempted to write or modify an existing script. Thanks

Vibrunazo - I’ve read through the torus script and it makes sense to use it as a base. I was getting confused with the rotation matrix and angle that are mentioned in the script. I’ll play around with the PREF_MAJ_SEG and see what happens. Thanks and don’t feel too bad about the Blender API, I’m learning it myself.

David C.

I’d be interested in translating Borland Pascal (Delphi) in Python… Does anyone knows how to?

Abidos, can you tell us what kind of project you have you like to translate? Is it about a big amount of code, or just some algorithms, or something? It can be hard to find any automated tools, so i guess most of the work has do be done by hand. For some years ago Delphi was my personal favourite, then I changed totally to open source world, and now I mainly use Python. But I did not take much of code with me from Delphi, because the frameworks in both languages are so different. It was easier to learn Python from scratch than trying to translate code from Delphi.

May be you can try using smth like this?

from ctypes import *

# Load mytest.dll
mydll =  CDLL("mytest")
mydll.func(params)

SnifiX - I agree with the approach you’re proposing. Actually I tried it for translating a project of mine for a semi-interactive map in an .EXE when I wanted to install / incorporate it into a web site. In fact, I reworked everything in JAVA… Which was a bit painful but the mar was implemented to work about 30 times faster then in the .EXE, whih is nice, right? :slight_smile: I didnt have database access at this provider so managed to optimise the required data and include it in the .java file so it became just one and only file (applet) that run nicely on the web. One learns a lot of JAVA while doing this!!! :wink:

Now Im working on some scripts for Blender where, obviously, I need to use Python… heh :spin:

SnifiX - I agree with the approach you’re proposing. Actually I tried it for translating a project of mine for a semi-interactive map in an .EXE when I wanted to install / incorporate it into a web site. In fact, I reworked everything in JAVA… Which was a bit painful but the mar was implemented to work about 30 times faster then in the .EXE, whih is nice, right? :slight_smile: I didnt have database access at this provider so managed to optimise the required data and include it in the .java file so it became just one and only file (applet) that run nicely on the web. One learns a lot of JAVA while doing this!!! :wink:

Now Im working on some scripts for Blender where, obviously, I need to use Python… heh :spin:

andreimy - Did you write to me? What does your code do? Please, explain… :eek:

SnifiX - I agree with the approach you’re proposing. Actually I tried it for translating a project of mine for a semi-interactive map in an .EXE when I wanted to install / incorporate it into a web site. In fact, I reworked everything in JAVA… Which was a bit painful but the mar was implemented to work about 30 times faster then in the .EXE, whih is nice, right? :slight_smile: I didnt have database access at this provider so managed to optimise the required data and include it in the .java file so it became just one and only file (applet) that run nicely on the web. One learns a lot of JAVA while doing this!!! :wink:

Now Im working on some scripts for Blender where, obviously, I need to use Python… heh :spin:

andreimy - Did you write to me? What does your code do? Please, explain… :eek: