Coder Social home page Coder Social logo

louiscarl / pysomo Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 0.0 1.15 MB

A small solid modeling library. With pysomo you can create 3D models in Python and export them to various 3D file formats. https://pypi.org/project/pysomo/

License: MIT License

Python 100.00%
obj stl cad csg 3d modeling solid

pysomo's Introduction

pysomo

PyPI version Tests

pysomo (for solid modeling) is a small solid modeling library. This library allows you to create 3D models in Python and export them to various 3D file formats.

Requirements

pysomo creates an xcsg file, an XML file that can be parsed by the xcsg application to create export your models in various file formats. pysomo does not include xcsg, it can be downloaded separately: download xcsg.

Installation

  1. To install, simply run pip install pysomo
  2. To export from xcsg format to a 3D model file, the xcsg application must be in the same directory as your application.

Examples

Extrusion

The following code creates a coin with a square hole in the middle.

import pysomo as somo


# First let's create the round part of the coin.
coin_circle = somo.Circle(30)
# This creates the base solid of the coin.
coin = coin_circle.linear_extrude(2)
# Create the solid to use as the extrusion. Note that we use the offset method
# to create a smaller circle from the base. This will give us the rim.
coin_extr = coin_circle.offset(-5, True).linear_extrude(1)


# Now we use the subtraction operator to exclude our shapes from the coin.
coin = coin - coin_extr.translate(0, 0, 1.5) - coin_extr.translate(0, 0, -0.5)


# Let's now create the square hole in the coin.
square = somo.Square(20)
square_rim = square.linear_extrude(2)
square_hole = square.offset(-2, False).linear_extrude(2)


# Our final coin is the base coin with a square removed.
coin = coin + square_rim - square_hole


# Now we export to a file. The Root is responsible for building the xcsg file.
root = somo.Root(coin)
# The Exporter reads the root file and uses the xcsg application.
somo.Exporter(r"coin.obj").export_obj(root)

This creates the coin below.

Extrusion solid

Stairs

The following code builds a staircase up to the maximum height allowed by a building code, if we were to use the maximum step height allowed. It demonstrates modelisation via code. It also demonstrates that figures do not mutate when an operation is applied. Instead, every operation returns a new figure.

import pysomo as somo


def to_meters(inches):
    return 0.0254 * inches


# Let's say these are the building code requirements for stairs, in inches.
min_clear_width = to_meters(3 * 12)
max_stair_height = to_meters(163)
max_riser_height = to_meters(7 + 1 / 2)
min_tread_depth = to_meters(11)


# Build the staircase steps by union of every step.
step = somo.Cuboid(
    min_clear_width,
    max_riser_height,
    min_tread_depth,
    center='false')
steps = step  # Initially, the stairs are a single step
step_count = 1  # Number of steps in the stairs


# Let's keep adding steps while the height is code compliant.
while (step_count + 1) * max_riser_height < max_stair_height:
    # Note that every operation in fact returns a new solid and does not
    # modify the original step, so you can reuse solids for every translation.
    steps += step.translate(
        0,
        max_riser_height * step_count,
        min_tread_depth * step_count)
    step_count += 1


# Build the stringers. Vertices are calculated from the steps we built above.
stairs_height = max_riser_height * step_count
stairs_depth = min_tread_depth * step_count
vertices = [
    (min_clear_width, 0, 0),
    (min_clear_width, 0, min_tread_depth),
    (min_clear_width, stairs_height, stairs_depth),
    (min_clear_width, stairs_height - max_riser_height, stairs_depth),
    (0, 0, 0),
    (0, 0, min_tread_depth),
    (0, stairs_height, stairs_depth),
    (0, stairs_height - max_riser_height, stairs_depth),
]
stringers = somo.Polyhedron(vertices)


# The union of steps and stringers creates the stairwell.
root = somo.Root(steps + stringers)


# Export to obj format.
somo.Exporter(r"stairs.obj").export_obj(root)

This creates the staircase below.

Stairs

An advantage in this style of 3d modeling is the simplicity of changing your models through variables. Let's say we added a zero to the maximum height allowed:

Stairs

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.