Search This Blog

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))

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

Saturday 18 September 2021

BRIDGE EDGE LOOPS


 Let us say we want to add  faces between the two edge loops in the gear shown in image 1 to get a final output as shown in image 2. We can use the Bridge Edge loops option to get this result.

STEPS
1. In Image -1, select the gear and go to edit mode. Set selection to Edge selection mode. Select an edge on one of the circular edge. Then on the 3D header, click on "Select" and then click on "Edge Loop". The complete circular edge will be selected.

 


                                                                Image-1

 
2. Holding Shift key down, click on an edge on the second circular edge, Click on "Select" and then click on "Edge Loop". Now both the circular edges would be selected.
3. Now on 3D header click on "Mesh" and then click on "Edge" and select "Bridge Edge Loop" in the popup menu. Now faces will be added between the two circular edges as shown in Image -2.

 


                                                                Image -2

 
4. Note this option  works only  if the two edges are part of a single object.

Thursday 16 September 2021

DECIMATE MODIFIER

  Let us say we created a mesh object, but ended up with a lot more number of vertices than you think is the optimal number. To maintain the overall shape and still reduce the vertex count, decimate modifier can be used. The way to use it is explained below:

  Add a Monkey object and change to edit mode. In the info editor header the number of vertices is shown as 507. Change to solid mode. Having the monkey object selected, go to Properties Editor and click on the Modifier icon(Spanner), then click on Add modifier button.  In the drop down menu under "Generate" click on "Decimate". Under the "Collapse-Un-Subdivide-Planar" buttons,  "Ratio    1.0000" is visible  which is a slider control. Click LMB and drag to change the value from 1.0000 to say 0.700. Next click on Apply button. The number of vertices would have reduced to 360.

  As you reduce the ratio value, the vertex count will reduce, but beyond a point the details of the mesh shape will tend to be lost.



ROUND EDGES- BEVEL TOOL

Blender version 3.6 To make any edge to be rounded smoothly, we can use the bevel tool in Edit Mode. Look at the cube in Image 1 where all t...