Code Monkey home page Code Monkey logo

openpyscad's Introduction

Build Status Coverage Status Python2 Python3

OpenPySCAD

Python library to generate OpenSCAD source code. This library provides intuitive interface when you handle 3D data. OpenPySCAD supports both python2(2.7) and python3(3.4+).

Install

pip install openpyscad

How to use

  • Write python code as follows:
import openpyscad as ops
c1 = ops.Cube([10, 20, 10])
c2 = ops.Cube([20, 10, 10])
(c1 + c2).write("sample.scad")
  • Generated code will be written in the "sample.scad". OpenSCAD can detect the change of your code and reload automatically. That's so cool :D
union(){
    cube([10, 20, 10]);
    cube([20, 10, 10]);
};

Generated code examples

3D Shapes

Python:

Sphere(r=10, _fn=100)
Cube([10, 10, 10])
Cylinder(h=10, r=10)
p = Polyhedron(
    points=[
        [10, 10, 0], [10, -10, 0], [-10, -10, 0], [-10, 10, 0],  [0, 0, 10]
    ],
    faces=[
        [0, 1, 4], [1, 2, 4], [2, 3, 4], [3, 0, 4],  [1, 0, 3], [2, 1, 3]
    ]
)

Generated OpenSCAD code:

sphere(r=10, $fn=100);
cube(size=[10, 10, 10]);
cylinder(h=10, r=10);
polyhedron(points=[[10, 10, 0], [10, -10, 0], [-10, -10, 0], [-10, 10, 0], [0, 0, 10]], faces=[[0, 1, 4], [1, 2, 4], [2, 3, 4], [3, 0, 4], [1, 0, 3], [2, 1, 3]]);

Boolean Operations

Python:

# Union
Cube([20, 10, 10]) + Cube([10, 20, 10])

# You can also write like this
u = Union()
u.append(Cube[20, 10, 10])
u.append(Cube[10, 20, 10])

# Difference
Cube([20, 10, 10]) - Cube([10, 20, 10])

# You can also write like this
i = Difference()
i.append(Cube[20, 10, 10])
i.append(Cube[10, 20, 10])

# Intersection
Cube([20, 10, 10]) & Cube([10, 20, 10])

# You can also write like this
i = Intersection()
i.append(Cube[20, 10, 10])
i.append(Cube[10, 20, 10])

Generated OpenSCAD code:

// Union
union(){
    cube([20, 10, 10])
    cube([10, 20, 10])
};

// Difference
difference(){
    cube([20, 10, 10]);
    cube([10, 20, 10]);
};

// Intersection
intersection(){
    cube([20, 10, 10]);
    cube([10, 20, 10]);
};

Transformations

Python:

# Translate
Cube([20, 10, 10]).translate([10, 10, 10])

# Rotate
Cube([20, 10, 10]).rotate([0, 0, 45])

# Scale
Cube([20, 10, 10]).scale([2, 1, 1])

# Resize
Cube([20, 10, 10]).resize([2, 1, 1])

# Mirror
Cube([20, 10, 10]).mirror([1, 1, 1])

# Color
Cube([20, 10, 10]).color("Red")

# Offset
Circle(10).offset(10)

Generated OpenSCAD code:

// Translate
translate(v=[10, 10, 10]){
    cube([20, 10, 10]);
};

// Rotate
rotate(v=[0, 0, 45]){
    cube([20, 10, 10]);
};

// Scale
scale(v=[2, 1, 1]){
    cube([20, 10, 10]);
};

// Resize
resize(newsize=[2, 1, 1]){
    cube(size=[20, 10, 10]);
};

// Mirror
mirror([1, 1, 1]){
    cube(size=[20, 10, 10]);
};

// Color
color("Red"){
    cube(size=[20, 10, 10]);
};

// Offset
offset(r=10){
    circle(r=10);
};

Modifiers

OpenPySCAD provides modifiers interfaces ("*", "!", "#" and "%").

Python:

c1 = Cube(10)
c1.disable()         # add "*" character
c1.show_only()       # add "!" character
c1.is_debug()        # add "#" character
c1.is_transparent()  # add "&" character

LICENSE

MIT

openpyscad's People

Contributors

taxpon avatar jpoullet2000 avatar owebeeone avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.