bracelet

Übersicht: Python & Blender


Objectives

Jewelry, personal and individual? Inspired by the Japanese author * Takuro Wada * an bracelet will be prepeard. As so often, a Python script helps to automate many work steps.


Instructions:
  1. use this script (end of article)

  2. improve the script and creaet your own version e.g. with holes

    c

  3. preataere the 3D printing

  4. send me a photo for your bracelet to (pkoppatz[at]web.de)

Explanation of the scripts

The basic element

The first step is to remove all objects from the scene, so we are able to run the script multiple times. The example consists of a ball (uv_sphere) as the basic form. The position for each uv_sphere must be calculated.

Delete: See function delete_all () Create objects: see function create_objects ()

Merge all the individual objects

When the first step is completed, you get a tape. For the Next step, all parts are merged to one object.

Connect objects: see function glue_all ()

An object that can be bent

To control the bending process, a second object is required. An "EMPTY" of the type arrow is used for this.

Connect objects: see function bend_object_add ()

Bend the arrow object

The "imaginary/invisible" arrow is now bent with a modifier. A conversion function can be used to specify the angle in degrees used.

Conversion: see function deg_to_rad (d)

Bending (the active object) with a second modifier.

Bending: see function bend ()

Result

b

Corlor

For the color, the object can be selected and a new material Created and assigned (not shown here).

Preview image

If you want render an image, as shown in the pictures, you still need a camera, a light source and a background image. All three elements are created with a second script. The camera is connected to an empty (camera_control) and is always aligned with this. Move Empty and/or camera to show the desired cutout.

Scripts

Generator script for the bracelet

Run this script first.

use-cases/art/bracelet/gen-bracelet-en.py (Source)

#!bpy
"""
Name: 'Generator for bracelts'
Blender: 2.7x
Group: 'Generators'
Tooltip: 'Creat a basic shape for a bracelet'
"""
import bpy

def create_objects(x=0, y=0, z=0, anz=20):

   for i in range(anz):
      bpy.ops.mesh.primitive_uv_sphere_add(size=1,
                                           segments=64,
                                           ring_count=32,
                                           view_align=False,
                                           enter_editmode=False,
                                           location=(i*1.4, y, z+2))
      # maybe more flat...
      # bpy.ops.transform.resize(value=(0.65, 0.65, 0.65),
      #                         constraint_axis=(False, False, True))
def deg_to_rad(d):
    import math
    return d * math.pi / 180.0

def glue_all():
   scn = bpy.context.scene
   teil = scn.objects['Sphere']
   scn.objects.active = teil
   for i, sphere in enumerate(bpy.data.objects):
      if i == 0:
         continue
      boolean = teil.modifiers.new('SphereBoolean', 'BOOLEAN')
      boolean.object = sphere
      boolean.operation = 'UNION'
      bpy.ops.object.modifier_apply(apply_as='DATA', modifier='SphereBoolean')
      bpy.context.scene.objects.unlink(sphere)


def bend_object_add():
   rx = deg_to_rad(90)
   ry = deg_to_rad(-180)
   rz = deg_to_rad(-90)

   bpy.ops.object.empty_add(
      type='ARROWS',
      radius=1,
      view_align=False,
      location=(0, 0, 0),
      rotation=(rx, ry, rz)
   )
   return

def bend():
   scn = bpy.context.scene
   teil = scn.objects['Sphere']
   scn.objects.active = teil
   bpy.ops.object.modifier_add(type='SIMPLE_DEFORM')
   bpy.context.object.modifiers["SimpleDeform"].deform_method = 'BEND'
   bpy.context.object.modifiers["SimpleDeform"].angle = deg_to_rad(350)
   bpy.ops.object.modifier_apply(apply_as='DATA', modifier="SimpleDeform")

def delete_all():
    for item in bpy.context.scene.objects:
        bpy.context.scene.objects.unlink(item)

    for item in bpy.data.objects:
        bpy.data.objects.remove(item)

    for item in bpy.data.meshes:
        bpy.data.meshes.remove(item)

    for item in bpy.data.materials:
        bpy.data.materials.remove(item)

if __name__ == "__main__":
   delete_all()
   # first ring
   create_objects(1,3,1,40)
   # second ring
   create_objects(1,3,2,40)
   # third ring
   create_objects(1,3,3,40)

   glue_all()
   bend_object_add()
   bend()

Setup for rendering an image

Run this script after the bracelet script.

use-cases/art/bracelet/settings.py (Source)

#!bpy
"""
Name: 'SceneConfigurator'
Blender: 2.7x
Group: 'Low poly'
Tooltip: 'Configure standard settings in a new scene'
"""
import bpy

class SceneConfigurator:
    """Configure a scene"""

    def __init__(self):
        pass

    def light(self):

        bpy.ops.object.lamp_add(type='HEMI',
        radius=1, view_align=False, location=(1, -30, 90))

    def cam2empty(self):
        """Trac camera to empty

        Move the empty and the camera will point in this direction
        """
        bpy.ops.object.empty_add(type='PLAIN_AXES',
                                 view_align=False,
                                 location=(0, 11, 4))
        bpy.context.object.name = "camera_control"

        bpy.ops.object.camera_add(view_align=True,
                                  enter_editmode=False,
                                  location=(0, -40, 24),
                                  rotation=(0, 0, 0))
        #bpy.ops.object.select_pattern(pattern='Empty')
        #bpy.ops.object.select_pattern(pattern='Camera')
        bpy.ops.object.constraint_add(type='TRACK_TO')
        bpy.context.object.constraints["Track To"].target = bpy.data.objects["camera_control"]
        bpy.context.object.constraints["Track To"].track_axis = 'TRACK_NEGATIVE_Z'
        bpy.context.object.constraints["Track To"].up_axis = 'UP_Y'

    def sky(self):
        bpy.context.scene.world.use_sky_paper = True
        bpy.context.scene.world.use_sky_blend = True
        bpy.context.scene.world.horizon_color = (0.606907, 0.613911, 0.837134)

    def remove(self, name=None):
        """ Delete an object by name"""

        if name:
            bpy.ops.object.select_pattern(pattern=name)
        else:
            print('nothing to do')

        bpy.ops.object.delete()

if __name__ == "__main__":

    # switch to object mode, if nessasary
    if bpy.ops.object.mode_set.poll():
        bpy.ops.object.mode_set(mode='OBJECT')
    ctrl = SceneConfigurator()
    ctrl.remove(name="camera_control")
    ctrl.remove(name="Camera")
    ctrl.remove(name="Hemi")
    ctrl.light()
    ctrl.cam2empty()
    ctrl.sky()

Comments