a-rotation

Übersicht: Python & Blender


The animation of an object is shown while the location of the camera is fixed and every side of the cube has its own image.


Instructions

Tasks:
  1. Check your setup, so all scripts are working without errors.
  2. Add and move an object from a to b and start rotating it after the move.
  3. The images on a cube sometimes are placed in a wrong direction, do you have a solution for this problem? Tell us...

Explanations

To work with angles in Blender the unit is Radians is used. Those who want to brush up his knowledge again, can read on Wikipedia:

Radian

For short the most important facts to solve our problems.

Conversion

After the formula the amount can be calculated for a degree:

1° = 2 * pi / 360

The good news are, there is no need to calculate the values like this:

>>> 2 * math.pi / 360
0.017453292519943295

>>> 10 * 2 * math.pi/360
0.17453292519943295

Use the the following formulas where you need a rotation. For your orientation some special formulas for quarter, half, three quarter and full turn.

45° pi * 1/4
90° pi * 1/2
180° pi * 1
270° pi * 3/2
360° pi * 2

Now it should no problem to rotate the cube.

blender-moves/a-rotation/rotation.py (Source)

#! bpy
"""
Name: 'rotate.py'
Blender: 27x
Group: 'Example: rotation, materials, textures, uv-mapping'
Tooltip: 'move an object'
"""

import bpy
import os
import math

def uvMapper4Cube(obj):
    """ Put images on each side of an cube"""

    # die verwendeten Namen bauen
    namen = {'kopf.jpg': ('mat01', 'tex01'),
             'boot.jpg': ('mat02', ' tex02'),
             'blume.jpg': ('mat03', 'tex03'),
             'monster.jpg': ('mat04', 'tex04'),
             'vocaword.jpg': ('mat05', 'tex05'),
             'telefon.jpg': ('mat06', 'tex06'),
             }

    for i in namen.keys():

        # new baterial
        matname = namen[i][0]
        if not matname in bpy.data.materials:
            material = bpy.data.materials.new(matname)
            material.diffuse_color = (0, .5, .4)
            obj.data.materials.append(material)

        # new texture
        texUV = bpy.data.textures.new(namen[i][1], type="IMAGE")
        image_path = os.path.expanduser("//images//{}".format(i))
        image = bpy.data.images.load(image_path)
        texUV.image = image

        # connect texture with material
        bpy.data.materials[matname].texture_slots.add()
        bpy.data.materials[matname].active_texture = texUV
        bpy.data.materials[matname].texture_slots[0].texture_coords = "ORCO"
        bpy.data.materials[matname].texture_slots[0].mapping = "CUBE"

        # set a material to a polygon
        for face in obj.data.polygons:
            print(face.index)
            face.material_index = face.index

#    # set a material to a polygon
#    for face in obj.data.polygons:
#        print(face.index)
#        face.material_index = face.index


def cleanup():

    # quit edit mode
    if bpy.ops.object.mode_set.poll():
        bpy.ops.object.mode_set(mode='OBJECT')

    # find all mesh objects and delete
    bpy.ops.object.select_by_type(type='MESH')
    bpy.ops.object.delete()

    # delete all materials
    for i in bpy.data.materials.values():
        bpy.data.materials.remove(i)

    # delete all textures
    for i in bpy.data.textures.values():
        bpy.data.textures.remove(i)

    # delete all  images
    for i in bpy.data.images.values():
        # delete image (only possible if the image has no user!
        # except the name is  "Render Result"!
        if i.name != "Render Result":
            i.user_clear()
            bpy.data.images.remove(i)

def anim_rotation(obj):
    obj = bpy.context.active_object
    startanim = 0
    endeanim =222
    bpy.data.scenes['Scene'].frame_start = startanim
    bpy.data.scenes['Scene'].frame_end = endeanim

    # rotate at the z-axis
    obj.rotation_euler=(0.0,0.0,0.0)
    obj.keyframe_insert(data_path='rotation_euler',frame=0)
    obj.rotation_euler=(0.0,0.0,math.pi * 2)
    obj.keyframe_insert(data_path='rotation_euler',frame=100)

    # rotate at the x-axis
    obj.rotation_euler=(0.0,0.0,0.0)
    obj.keyframe_insert(data_path='rotation_euler',frame=101)
    obj.rotation_euler=(math.pi *2,0.0,0.0)
    obj.keyframe_insert(data_path='rotation_euler',frame=200)

if __name__ == "__main__":
    if bpy.ops.object.mode_set.poll():
        bpy.ops.object.mode_set(mode='OBJECT')

    cleanup()

    bpy.ops.mesh.primitive_cube_add(location=(0, 0, 0))
    obj = bpy.context.scene.objects.active
    obj.name = 'image-as-uvmapping'
    obj = bpy.context.scene.objects['image-as-uvmapping']
    uvMapper4Cube(obj)
    anim_rotation(obj)

    # switch to the timeline
    for area in bpy.context.window.screen.areas:
        if area.type == "CONSOLE":
            area.type = 'TIMELINE'
    # Und Animation starten
    bpy.ops.screen.animation_play()

If you fail to get the result as shown in the video you can download all files:

  1. Download all files (rotation.zip)
  2. start the blend file ...
  3. load the script rotation.py into the text window
  4. run the script
  5. render the animation (configuration of the render setting is described here)

Sample video and how it could look like...

Comments