2

I have a little problem :) I need to do a program in python , blender , in wich i must built a structure of many cubes. i am a absolutely beginner , and i have no experience in python. I realize a functional program in python and i succeded to built the structure , but i don't know how to make a mini-movie in which i would see how really my structure construct itself. this is the program i have made :

import bpy

for i in range(6):
    for x in range(-i,i+1):
        for y in range(-i,i+1):
            for z in range(-i,i+1):
                if(x<0):
                    xx=-x
                else:
                    xx=x

                if(y<0):
                    yy=-y
                else:
                    yy=y

                if(z<0):
                    zz=-z
                else:
                    zz=z

                if(xx+yy+zz==i):
                    bpy.ops.mesh.primitive_cube_add(radius=0.5,location=(x,y,z))

That is all.. I have been looking for a tutorial for more than a mouth , and i can't see one who would be useful for my program . Help meeee!!!

zeffii
  • 39,634
  • 9
  • 103
  • 186
user21553
  • 35
  • 6
  • you want to render a frame for each new block being added? – zeffii Feb 05 '16 at 13:05
  • yes , but i talk about more than 400 cubes – user21553 Feb 05 '16 at 13:09
  • i was thinking about puting sleeptime after every iteration , but my program waited and show me nothing until the program is finished – user21553 Feb 05 '16 at 13:12
  • related, possible duplicate: http://blender.stackexchange.com/questions/28673/update-viewport-while-running-script – zeffii Feb 05 '16 at 14:27
  • sleep() wont work, but if you want to see this code running in 3dview in realtime then the above link shows a few approaches. it's not exactly clear what you want to produce as an outcome. Please elaborate. – zeffii Feb 05 '16 at 14:40
  • and i should make – user21553 Feb 05 '16 at 15:18
  • def modal(self, context, event): if event.type in {'RIGHTMOUSE', 'ESC'} or self.limits > 30: self.limits = 0 self.cancel(context) return {'FINISHED'} – user21553 Feb 05 '16 at 15:18
  • for every for i have ? – user21553 Feb 05 '16 at 15:18
  • Starting from a cube i place an identical cube on each of its sides to obtain a crystal . Then i place cubes on each free sides of the crystal to obtain a bigger crystal , and so on . The rule is that cubes coordonate taken with positive sign , need to be equal with the number of iteration. I will show you an example : at iteration 1 i will have 6 cubes with coordonates :(0.0.1)(0.0.-1)(0.1.0)(0.-1.0)(1.0.0)(-1.0.0) . I want exactly the model with sphere , but i don't know how to manipulate my functions for – user21553 Feb 05 '16 at 15:41
  • i succeded !!!!!! Thanks you a lot @@@@ Five stars from me :) you're the best !!!!! – user21553 Feb 05 '16 at 19:20

1 Answers1

2
import bpy
from itertools import product

mesh = bpy.data.meshes['Cube']
objects_scene = bpy.context.scene.objects
objects_data  = bpy.data.objects
actions_data  = bpy.data.actions

def create_cube(name, location):
    obj = objects_data.new(name, mesh)
    objects_scene.link(obj)
    obj.location = location
    return obj

def create_animation(obj, time):
    obj.animation_data_create()
    obj.animation_data.action = action = actions_data.new("Action")
    fcurves = [action.fcurves.new(data_path) for data_path in ("hide", "hide_render")]
    for fcu in fcurves:
        fcu.keyframe_points.insert(   0, 1, {'FAST'}).interpolation = "CONSTANT"
        fcu.keyframe_points.insert(time, 0, {'FAST'}).interpolation = "CONSTANT"
        fcu.extrapolation = "CONSTANT"

n = 6
time = 1    
for i in range(n):
    for indices in product(range(-i, i+1), repeat=3):
        if sum(abs(j) for j in indices) == i:
            cube = create_cube("Cube", indices)
            create_animation(cube, time)
            time += 1

The result rendered using the OpenGL Viewport Renderer:

result


Note

I scaled down the default cube manually and deleted it afterwards before i ran the script.

pink vertex
  • 9,896
  • 1
  • 25
  • 44