Esempio n. 1
0
 def main():
     init()
     for frame in range(250):
         substep()
         mciso.clear()
         voxel.voxelize(mciso.m, pos, 8)
         mciso.march()
         vs, fs = mciso.get_mesh()
         nb.delete_object(f'mciso_output_{frame}')
         mesh = nb.new_mesh(f'mciso_output_{frame}', vs, [], fs)
         yield nb.object_mesh_update(object, mesh)
Esempio n. 2
0
N = 16

pos = ti.Vector.field(3, float, (N, N))


@ti.kernel
def init():
    for i, j in pos:
        pos[i, j] = ti.Vector([i - N / 2, j - N / 2, 0])


@ti.kernel
def update(t: float):
    for i, j in pos:
        pos[i, j].z = ti.sin(pos[i, j].xy.norm() * 0.5 - t * 2)


verts, edges, faces, uv = nb.meshgrid(N)
nb.delete_mesh('point_cloud')
nb.delete_object('point_cloud')
mesh = nb.new_mesh('point_cloud', verts, edges, faces, uv)
nb.new_object('point_cloud', mesh)


@nb.add_animation
def main():
    init()
    for frame in range(250):
        update(frame * 0.03)
        yield nb.mesh_update(mesh, pos=pos.to_numpy().reshape(N**2, 3))
Esempio n. 3
0
dt = 5e-4
steps = 144
gravity = 9.8
wind = 0.1
ball_radius = 0.4

pos_, edges_, faces_, uv_ = nb.meshgrid(N, eight=True)
pos_[:, :2] = pos_[:, :2] * 2 - 1
pos_ = pos_[:, (2, 1, 0)]

rest_ = np.sqrt(np.sum((pos_[edges_[:, 0]] - pos_[edges_[:, 1]])**2, axis=1))

nb.delete_mesh('cloth')
nb.delete_object('cloth')
nb.delete_object('ball')
cloth_mesh = nb.new_mesh('cloth', pos_, nb.meshgrid(N)[1], faces_, uv_)
cloth_object = nb.new_object('cloth', cloth_mesh)

bpy.ops.mesh.primitive_ico_sphere_add(subdivisions=3, radius=ball_radius)
ball_object = bpy.context.object
ball_object.name = 'ball'

pos = ti.Vector.field(3, float, pos_.shape[0])
vel = ti.Vector.field(3, float, pos_.shape[0])
edges = ti.Vector.field(2, int, edges_.shape[0])
rest = ti.field(float, rest_.shape[0])
pos.from_numpy(pos_)
edges.from_numpy(edges_)
rest.from_numpy(rest_)

@ti.kernel
Esempio n. 4
0
@ti.kernel
def substep():
    for i in x:
        acc = ti.Vector.zero(float, 3)
        for j in range(N):
            r = x[i] - x[j]
            acc -= r / r.norm(1e-2)**3
        v[i] += gravity * acc * dt
    for i in x:
        x[i] += v[i] * dt


# delete old mesh & object (if any)
nb.delete_mesh('point_cloud')
nb.delete_object('point_cloud')

# create a new point cloud
mesh = nb.new_mesh('point_cloud', np.zeros((N, 3)))
object = nb.new_object('point_cloud', mesh)


# define animation iterator body
@nb.add_animation
def main():
    init()
    while True:
        for s in range(6):
            substep()
        yield nb.mesh_update(mesh, x.to_numpy())
Esempio n. 5
0
N = 16

pos = ti.Vector.field(3, float, (N, N))


@ti.kernel
def init():
    for i, j in pos:
        pos[i, j] = ti.Vector([i - N / 2, j - N / 2, 0])


@ti.kernel
def update(t: float):
    for i, j in pos:
        pos[i, j].z = ti.sin(pos[i, j].xy.norm() * 0.5 - t * 2)


nb.delete_mesh('point_cloud')
nb.delete_object('point_cloud')
mesh = nb.new_mesh('point_cloud')
nb.new_object('point_cloud', mesh)


@nb.add_animation
def main():
    init()
    for frame in range(250):
        update(frame * 0.03)
        yield nb.mesh_update(mesh, pos=pos.to_numpy().reshape(N**2, 3))
Esempio n. 6
0
        x[p] += dt * v[p]
        J[p] *= 1 + dt * new_C.trace()
        C[p] = new_C


@ti.kernel
def init():
    for i in range(n_particles):
        x[i] = ti.Vector([ti.random() for i in range(dim)]) * 0.4 + 0.15
        J[i] = 1


nb.delete_mesh('point_cloud')
nb.delete_object('point_cloud')

mesh = nb.new_mesh('point_cloud', np.zeros((n_particles, 3)))
object = nb.new_object('point_cloud', mesh)


@nb.add_animation
def main():
    def T(x):
        return np.array([x[:, 0] * 2 - 1, x[:, 2] * 2 - 1,
                         x[:, 1] * 2]).swapaxes(0, 1)

    init()
    while True:
        for s in range(steps):
            substep()
        yield nb.mesh_update(mesh, T(x.to_numpy()))
Esempio n. 7
0
    def init():
        for i in pos:
            pos[i] = ti.Vector([ti.random() for t in range(3)]) * 0.2 + 0.4
            vel[i] = ti.Vector([ti.random() for t in range(3)]) * 2 - 1

    @ti.kernel
    def substep():
        for i in pos:
            pos[i] += vel[i] * dt

    import numblend as nb
    import bpy
    bpy.context.scene.frame_current = 0
    nb.init()

    nb.delete_object('mciso_output')
    nb.delete_mesh('mciso_output')
    object = nb.new_object('mciso_output', nb.new_mesh('mciso_output'))

    @nb.add_animation
    def main():
        init()
        for frame in range(250):
            substep()
            mciso.clear()
            voxel.voxelize(mciso.m, pos, 8)
            mciso.march()
            vs, fs = mciso.get_mesh()
            nb.delete_object(f'mciso_output_{frame}')
            mesh = nb.new_mesh(f'mciso_output_{frame}', vs, [], fs)
            yield nb.object_mesh_update(object, mesh)
Esempio n. 8
0
        v[i].y -= gravity * dt
        v[i] = tl.ballBoundReflect(x[i], v[i], tl.vec(+0.0, +0.2, -0.0), 0.4,
                                   6)
    for i in ti.grouped(x):
        v[i] *= ti.exp(-damping * dt)
        x[i] += dt * v[i]


### Blender

_, edges, faces, uv = nb.meshgrid(N)

nb.delete_mesh('point_cloud')
nb.delete_object('point_cloud')

mesh = nb.new_mesh('point_cloud', np.zeros((N**2, 3)), edges, faces)
object = nb.new_object('point_cloud', mesh)


@nb.add_animation
def main():
    def T(x):
        return np.array([x[:, 0], x[:, 2], x[:, 1]]).swapaxes(0, 1)

    init()
    yield T(x.to_numpy().reshape(N**2, 3))
    while True:
        for s in range(steps):
            substep()
        yield nb.mesh_update(mesh, T(x.to_numpy().reshape(N**2, 3)))