Search This Blog

Thursday 30 December 2021

SINE WAVES-RED,YELLOW AND BLUE SCRIPT

#THIS SCRIPT WORKS WITH BLENDER 2.8 AND ABOVE.

 import bpy
from math import sin,cos,pi

radius=100
deg=2*pi

#TO DELETE ALL EXISTING OBJECTS
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_by_type(type='MESH')
bpy.ops.object.delete(use_global=False)


for t in range (0,360):
    
    #RED
    z=radius*sin(t*deg/360)
    x=t
    y=0
    bpy.ops.mesh.primitive_cube_add(location=(x,y,z))
    bpy.ops.transform.resize(value=(0.9,0.9,0.9))
    obj_matl=bpy.data.materials.new("obj_clr")
    obj_matl.diffuse_color=(1,0,0,1)
    mesh=bpy.context.object.data
    mesh.materials.clear()
    mesh.materials.append(obj_matl)
    #YELLOW
    z=radius*sin(t*deg/360)
    x=t+120
    y=0
    bpy.ops.mesh.primitive_cube_add(location=(x,y,z))
    bpy.ops.transform.resize(value=(0.9,0.9,0.9))
    obj_matl=bpy.data.materials.new("obj_clr")
    obj_matl.diffuse_color=(1,0.6,0,1)
    mesh=bpy.context.object.data
    mesh.materials.clear()
    mesh.materials.append(obj_matl)
    #BLUE
    z=radius*sin(t*deg/360)
    x=t+240
    y=0
    bpy.ops.mesh.primitive_cube_add(location=(x,y,z))
    bpy.ops.transform.resize(value=(0.9,0.9,0.9))
    obj_matl=bpy.data.materials.new("obj_clr")
    obj_matl.diffuse_color=(0,0,1,1)
    mesh=bpy.context.object.data
    mesh.materials.clear()
    mesh.materials.append(obj_matl)

The wave form generated by the script can be seen by clicking on the link given  below:

SINE WAVE LINK 

 


   


    
   

Sunday 26 December 2021

BPY SCRIPT TO ADD 50 CUBES AND ANIMATE THEM

In this script we add 50 cubes at random location, set random color to each cube and set key frames to each cube to animate them.

The number of cubes added can be changed by setting the range value in the first for loop.Total number of frames is set in the beginning by setting the value for the variable total_frames. Key frame interval is adjusted by setting the third variable in the range setting of the second for loop.Here it is set to 10. The diffuse color setting has four values for Red, Green, Blue and Alpha.

import bpy
from math import sin, pi
from random import randint

total_frames=100

# clear meshes in the scene
for obj in bpy.data.objects:
    if obj.type == 'MESH':
        bpy.data.objects.remove(obj)

#CREATE 10 CUBES        
for i in range (50):
    x=randint(-10,10)
    y=randint(-10,10)
    z=randint(-10,10)    
    bpy.ops.mesh.primitive_cube_add( location=(x,y,z))
    cube=bpy.context.object
    # TO ADD NEW MATERIAL TO EACH CUBE
    matl=bpy.data.materials.new("mat_clr")
    matl.diffuse_color = (x*0.6,y*0.3,z*0.1,1)
    mesh=cube.data
    mesh.materials.clear()
    mesh.materials.append(matl)
    
    # SET KEYFRAMES FOR EACH CUBE
    for frame in range(0, total_frames,10):
        bpy.context.scene.frame_set(frame)
        x=randint(-10,10)
        y=randint(-10,10)
        z=randint(-10,10)

        cube.location=(x,y,z)
        cube.keyframe_insert(data_path='location')

 

A link to the rendered video file is given below:

https://drive.google.com/file/d/1ZSFBqbky9fEUKng0ZanPFgLFlcWgkLO1/view?usp=sharing

 

Tuesday 21 December 2021

BPY SCRIPT TO ADD MULTIPLE CUBES IN CIRCLES

This script adds multiple Cubes in concentric circles

-----------------------------------------------------------------------------------------------------------------------------
import bpy
import csv
from math import sin,cos,pi

d =pi/180 # FORMULA TO CHANGE RADIAN TO DEGREE
i=0 #  LOOP COUNTER


radius=4*1.1
x=0
y=0
z=0


for i in range (12):
  x=radius*sin(30*i*d)
  y=0
  z=radius*cos(30*i*d)
  bpy.ops.mesh.primitive_cube_add(radius=1, view_align=False, enter_editmode=False, location=(x,y,z))
 
radius+=5

for i in range (24):
  x=radius*sin(15*i*d)
  y=0
  z=radius*cos(15*i*d)
  bpy.ops.mesh.primitive_cube_add(radius=1, view_align=False, enter_editmode=False, location=(x,y,z))
radius+=5

for i in range (48):
  x=radius*sin(7.5*i*d)
  y=0
  z=radius*cos(7.5*i*d)
  bpy.ops.mesh.primitive_cube_add(radius=1, view_align=False, enter_editmode=False, location=(x,y,z))
radius+=5

for i in range (96):
  x=radius*sin(3.75*i*d)
  y=0
  z=radius*cos(3.75*i*d)
  bpy.ops.mesh.primitive_cube_add(radius=1, view_align=False, enter_editmode=False, location=(x,y,z))
radius+=5

-----------------------------------------------------------------------------------------------------------------------------





BPY SCRIPT TO ADD MULTIPLE CUBES

This script must have a cube object to start with. This object is duplicated and assigned random material.

-------------------------------------------------------------------------------------------------------------------------------------------------------
import bpy
from random import randint
import random

number=100


#TO DELETE ALL EXISTING OBJECTS

bpy.ops.object.select_by_type(type='MESH')
bpy.ops.object.delete(use_global=False)

#TO ADD CAMERA
bpy.ops.object.camera_add(view_align=True, location=(0,-45,0), rotation=(1.5708,0,0))
bpy.ops.transform.resize(value=(9.42627, 9.42627, 9.42627))


#TO ADD BG PLANE
bpy.ops.mesh.primitive_plane_add( location=(0,10,0))
bpy.ops.transform.resize(value=(10, 10, 10))
bpy.ops.transform.rotate(value=-1.55203, axis=(-1, 0,0))
bpy.ops.transform.resize(value=(2,2,2))

#TO ADD MATERIAL TO PLANE
obj_matl=bpy.data.materials.new("obj_clr")
obj_matl.diffuse_color=(0,0.9,0.8)
mesh=bpy.context.object.data
mesh.materials.clear()
mesh.materials.append(obj_matl)

#TO CREATE MULTIPLE TORUS AND ASSIGN MATERIAL
for i in range(0,number):
    x=randint(-10,10)
    y=randint(-5,5)
    z=randint(-10,10)
    p=randint(1,10)
    bpy.ops.mesh.primitive_cube_add(location=(x,y,z))
    bpy.ops.transform.resize(value=(p*0.1,p*0.1,p*0.1))
    bpy.ops.transform.rotate(value=1.5708, axis=(1, 1, 0))
    p=randint(0,9)
    #print(p)
    obj_matl=bpy.data.materials.new("obj_clr")
    obj_matl.diffuse_color=(0.8,p*0.3,0)
    mesh=bpy.context.object.data
    mesh.materials.clear()
    mesh.materials.append(obj_matl)

--------------------------------------------------------------------------------------------------------------------------------

Monday 20 December 2021

BPY SCRIPT TO DRAW A SPIRAL

The script will draw a spiral using the cube object which is repeatedly positioned along a spiral path. The spiral is visible in the Top ortho view(7).

-----------------------------------------------------------------------------------------------------------------------------

import bpy
from math import sin,cos,pi

bigdeg=pi*2
smalldeg=pi/100
radius=2
for i in range(0,500):
    x=radius*sin(smalldeg*i)
    y=radius*cos(smalldeg*i)
    z=0
    bpy.ops.mesh.primitive_cube_add(location=(x,y,z))
    bpy.ops.transform.resize(value=(1,1,1))
    mat_red=bpy.data.materials.new("red")
    mat_red.diffuse_color=(0.8, 0, 0)
    mesh=bpy.context.object.data
    mesh.materials.clear()
    mesh.materials.append(mat_red)
    radius +=.07
 
-----------------------------------------------------------------------------------------------------------------------------

 


BPY SCRIPT TO ADD MATERIAL TO AN EXISTING OBJECT.

To run this script open blender, add a cube object and select it.Copy and paste these codes into you text editor and run it to add a material to the cube. Here Red material(color) is added to the Cube.

-------------------------------------------------------------------------------------------------------------------------------

 import bpy

mat_red=bpy.data.materials.new("red")

mat_red.diffuse_color=(0.8, 0, 0)

mesh=bpy.context.object.data

mesh.materials.clear()

mesh.materials.append(mat_red)

-------------------------------------------------------------------------------------------------------------------------------

BPY SCRIPT TO POSITION AN OBJECT AT RANDOM LOCATION.

 The script below positions the monkey object at random position between _20 and +20 units along the X and Y axis. Along Z axis the location varies between -2 and +2 units.

----------------------------------------------------------------------------------------------------------------------------

'''POSITIONS MONKEY OBJECT AT RANDOM LOCATIONS BETWEEN -20 AND +20 UNITS ALONG X & Y AXIS, -2 TO +2 UNITS ALONG Z AXIS '''

import bpy

bpy.ops.object.select_all(action="SELECT")
bpy.ops.object.delete(use_global=False)


from random import randint
number=200
for i in range(0,number):
    x=randint(-20,20)
    y=randint(-20,20)    
    z=randint(-2,2)
    bpy.ops.mesh.primitive_monkey_add(location=(x,y,z))

----------------------------------------------------------------------------------------------------------------------------
  

TO MIRROR ARMATURE BONES ON X AXIS 1. Assume that we are making a human rig in standing position. 2. Set the view to front view. 3. Create t...