analitics

Pages

Sunday, June 5, 2022

Blender 3D and python scripting - part 012.

In this tutorial I will show you how you can add an extra branch to the existing one.
The source code was structured a bit with two createBranch and createNewBranch functions.
However, it is still in a raw structured format because such source code is aimed at using classes.
I did not solve the new branch size for the added vertex.
In the function that creates the new branch, the second parameter receives a number that represents the vertex from where the new edge will be created.
createNewBranch(obj_branch, 1)
In the source code we have the variable branch = 4, because the number of vertices starts in the list from 0 then argument 1 immediately means the next vertex from the first one in the list.
A random peak with close values ​​is generated and an edge is added to link it to the previously selected one.
Here is the source code that adds a new branch.
import bpy
import random

# import bmesh 
import bmesh

MinNubmer = -10
MaxNumber = 10

# Clean up the area , uncoment the next two row to keep
# branch after running the script
bpy.ops.object.select_all(action="SELECT")
bpy.ops.object.delete()

# Number of branches
branch = 4
# Create the verts array
verts = [(0,0,0)]
# Create the edges array
edges = [(0,0)]
# Create the faces array
faces = []

# define random number for X and Y axis 
def RN():
    return  random.randint(MinNubmer, MaxNumber) / 20 

# define random number for positive Z axis
def RNZ():
    return  random.randint(10, 50) / 10  

# create a list of branch thicknesses
rand_list = []

name_branch = "TreeMesh"
# define createBranch 
def createBranch(branch, name_branch):
    # Create the mesh for branch 
    mesh = bpy.data.meshes.new(name_branch) 
    for i in range(1,branch):
        rand_list.append(RNZ()/30)
        # sort all reverse by thicknesses
        rand_list.sort(reverse=True)

    # generate vertices list for drawing the branch
    for i in range(1,branch):
        rand_list.append(RN())
        verts.append((rand_list[i-1] +0.1,rand_list[i-1]+0.1,RNZ()))
        edges.append((i-1,i))
    
    # sort the list of vertices by last number witch is Z axis 
    verts.sort(key=lambda x: x[2])
    # create branch update and validate, see documentation
    mesh.from_pydata(verts, edges, faces) 
    mesh.update()
    mesh.validate()
    # Create object to hold the mesh branch with the new name for object
    obj = bpy.data.objects.new(name_branch+'_Obj', mesh)
    return obj

# use the createBranch
obj_branch = createBranch(branch, name_branch)

# ... and add it to the scene
scene = bpy.context.scene
scene.collection.objects.link(obj_branch)

# create a new branch     
def createNewBranch(obj_branch, n):
    bpy.ops.object.mode_set(mode="EDIT", toggle=False)
    me = obj_branch.data
    bm = bmesh.from_edit_mesh(me)
    bm.select_mode = {'VERT'}

    for i,v in enumerate(bm.verts):
        # select only by the index of list 
        if i == n:
            v.select = ( v.co.x > 0.0 )
            v2 = v    
        else: 
            v.select = False
    # flush and update view 
    v1 = bm.verts.new( (RN()+(v.co.x) + 0.1 , RN()+(v.co.y) + 0.1 , (v.co.z) - (v.co.z)/3) )
    #v1 = bm.verts.new(1, 1, 3)
    bm.edges.new((v1, v2))
    # update 
    bm.select_flush_mode()   
    me.update()
    #bmesh.update_edit_mesh(obj_branch.data)

# add the skin modifier - NOT FIXED FOR THE LAST BRANC ADDED
obj_branch.modifiers.new(name="SK", type="SKIN")
bpy.context.view_layer.objects.active = obj_branch  
# get the skin vertices layers
skin_vertices = obj_branch.data.skin_vertices
# get the layer
skin_layer = skin_vertices[0]

for i in range(0,branch):
    # assigns radius for each vertice to sized the branch 
    skin_layer.data[i-1].radius = (rand_list[i-1], rand_list[i-1]) 
    #Indices 0 and 1 are the vertex indices
    skin_layer.data[i].radius = (rand_list[i-1],rand_list[i-1])

# set modes for user 
bpy.ops.object.mode_set(mode="EDIT", toggle=False)
bpy.ops.object.skin_root_mark()

createNewBranch(obj_branch, 1)

bpy.ops.object.mode_set(mode="OBJECT", toggle=False)

Saturday, June 4, 2022

Blender 3D and python scripting - part 011.

Today I show you how to create a branch using the Blender 3D A.P.I. together with the python programming language.
The source code seems complicated but if you follow the attached comments then you will understand how it works.
It contains two parts, one for creating the coordinates on the initial position of the branch (0,0,0) and one for creating the skin with different thicknesses.
Of note is the sorting of lists generated in the python language to get a nice increase as well as gradual thicknesses.
I've used the script several times to show you in the screenshot below some branches created with it.
Here is the source code used.
import bpy
import random
MinNubmer = -10
MaxNumber = 10

# Clean up the area , uncoment the next two row to keep
# branch after running the script
#bpy.ops.object.select_all(action="SELECT")
#bpy.ops.object.delete()

# Number of branches
branch = 6
# Create the verts array
verts = [(0,0,0)]
# Create the edges array
edges = [(0,0)]
# Create the faces array
faces = []

# Create the mesh for branch 
mesh = bpy.data.meshes.new("TreeMesh") 

# define random number for X and Y axis 
def RN():
    return  random.randint(MinNubmer, MaxNumber) / 20 

# define random number for positive Z axis
def RNZ():
    return  random.randint(10, 50) / 10  

# create a list of branch thicknesses
rand_list =[]
for i in range(1,branch):
    rand_list.append(RNZ()/30)
    # sort all reverse by thicknesses
    rand_list.sort(reverse=True)

# generate vertices list for drawing the branch
for i in range(1,branch):
    rand_list.append(RN())
    verts.append((rand_list[i-1] +0.1,rand_list[i-1]+0.1,RNZ()))
    edges.append((i-1,i))

# sort the list of vertices by last number witch is Z axis 
verts.sort(key=lambda x: x[2])

# create branch update and validate, see documentation
mesh.from_pydata(verts, edges, faces) 
mesh.update()
mesh.validate()

# Create object to hold the mesh branch 
obj = bpy.data.objects.new('Tree', mesh)

# ... and add it to the scene
scene = bpy.context.scene
scene.collection.objects.link(obj)

# add the skin modifier
obj.modifiers.new(name="SK", type="SKIN")
bpy.context.view_layer.objects.active = obj  
# get the skin vertices layers
skin_vertices = obj.data.skin_vertices
# get the layer
skin_layer = skin_vertices[0]

for i in range(1,branch):
    # assigns radius for each vertice to sized the branch 
    skin_layer.data[i-1].radius = (rand_list[i-1], rand_list[i-1]) #Indices 0 and 1 are the vertex indices
    skin_layer.data[i].radius = (rand_list[i-1],rand_list[i-1])

# set modes for user 
bpy.ops.object.mode_set(mode="EDIT", toggle=False)
bpy.ops.object.skin_root_mark()
bpy.ops.object.mode_set(mode="OBJECT", toggle=False)

Thursday, June 2, 2022

Blender 3D and python scripting - part 010.

In this tutorial, I will use the source code from the previous tutorial and with the selected mesh I will resize and translate it on an axis then I will select the newly selected mesh and I will rotate it to obtain a roof shape.
This is the source code I used:
bpy.ops.object.mode_set(mode = 'EDIT') 

# let set the object mode 
bpy.ops.object.mode_set(mode="OBJECT")
# resize the selected areas 
bpy.ops.transform.resize(value=(1, 2, 1))
# translate 
bpy.ops.transform.translate(value=(0, 0.25, 0.31))

# rotate selected only if not is the initial mesh 
for ob in bpy.context.selected_objects:
    if ob.name != 'Plane-Y+Z':
        ob.rotation_euler[0] = pi/-4
        ob.convert_space(from_space='LOCAL', to_space='WORLD')

# define the new camera named NewCamera
See the result of this source code:

Wednesday, June 1, 2022

Blender 3D and python scripting - part 009.

In this tutorial I will show you a source code in python that allows the selection of vertices by a coordinate, and separates this selection into a new object according to the faces.
The source code is presented below and is commented on accordingly to understand how it works.
bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.mesh.subdivide(number_cuts=3)
bpy.ops.object.mode_set(mode="OBJECT")

# add this source code     
bpy.ops.object.mode_set(mode = 'EDIT') 
# need to use bmesh
import bmesh
# select the plane and get data mesh 
plane_obj = bpy.data.objects['Plane-Y+Z']
plane_mesh = plane_obj.data
bm = bmesh.from_edit_mesh(plane_mesh)

# select vertices by points 
for v in bm.verts:
    v.select_set(v.co.y < 0.5)
#get mode 
bm.select_mode = {'VERT', 'EDGE', 'FACE'}
# this will update the selection 
bm.select_flush_mode()
# select by FACE   
bpy.context.tool_settings.mesh_select_mode = (False, False, True)
# separate selection by face
bpy.ops.mesh.separate(type='SELECTED')
# select by EDGE
bpy.context.tool_settings.mesh_select_mode = (True, False, False)

# define the new camera named NewCamera

Python 3.7.13 : My colab tutorials - part 024.

In this colab notebook I test how to install pytorch and torchvision python packages on colab notebook and save the model to Google drive.
I tried to save the model.ptl file but I got a network error and uploaded the file to googe drive and then downloaded it.
You can see the full source code on this GitHub repo.

Monday, May 30, 2022

Blender 3D and python scripting - part 008.

In this tutorial I will show you how to use the subdivision operation using the python language and A.P.I from the Blender 3D software.
To use the subdivision operation you must have an active object and be in edit mode.
This is the source code used for this operation with tree cuts for subdivision operation.
bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.mesh.subdivide(number_cuts=3)
bpy.ops.object.mode_set(mode="OBJECT")
You can see in the screenshot below where I added this source code in the old script used in the last tutorial and how the subdivision operation was performed.

Thursday, May 26, 2022

Blender 3D and python scripting - part 007.

In this tutorial I will show you how you create and use lights.
I used the same old source sode from the last tutorial.
Depending on the type of light created, their properties may change. Changing the on-fly light type cannot be done by a simple source code. Obviously you can recreate a new type of light with the new type you want.
This source code I added after this line of source code: scene = bpy.context.scene
# this create a light by type ['POINT', 'SUN', 'SPOT', 'HEMI', 'AREA']
light_data = bpy.data.lights.new('light', type='POINT')
# set light object 
light = bpy.data.objects.new('light', light_data)
# link light to collections
bpy.context.collection.objects.link(light)

light.location[0] = -1
light.location[1] = 3
light.location[2] = 3

light.data.color = (1.0, 0.0, 0.0)
light.data.energy=200.0
light.data.specular_factor = 0.5
# if you use another type like 'SUN' 
# then you can change properties like: angle 
#light.data.angle = pi * 10.0 / 180.0 

# get the name of the object light 
lamp = bpy.data.lights[light.name]

Wednesday, May 25, 2022

Blender 3D and python scripting - part 006.

In this tutorial I will show you how to use the camera and render an image.
I kept the source code from the old tutorial 005 and made the following changes:
I defined the global PI constant because I used it outside the definition.
import bpy

#define the pi global 
pi = 3.1415926
I added to the old source code the part of adding camera, translation, rotation, rendering settings and rendering an image called box_640_480.png in the 3D folder on the local disk.
You can see in the example below the added source code:
# define the new camera named NewCamera
camera_data = bpy.data.cameras.new(name='NewCamera')
# set camera_data to object 
camera_object = bpy.data.objects.new('ObjectCamera', camera_data)
# link camera object to scene
bpy.context.scene.collection.objects.link(camera_object)
# set active camera in the current scene by object
bpy.context.scene.camera = bpy.data.objects['ObjectCamera']
# set location 
camera_object.location = [0,-5,1]
# set rotation mode
camera_object.rotation_mode = 'XYZ'
# set the rotate the camerea using rotation_euler
bpy.data.objects[camera_object.name_full].rotation_euler = (90*(pi/180),0,0)

scene = bpy.context.scene

#these settings will set the render output
bpy.context.scene.cycles.samples = 1
scene.render.resolution_x = 640
scene.render.resolution_y = 480
scene.render.resolution_percentage = 100
scene.render.use_border = False
scene.render.image_settings.file_format='PNG'
scene.render.filepath='C:/3D/box_640_480.png'
bpy.ops.render.render(write_still=1)

Tuesday, May 24, 2022

Blender 3D and python scripting - part 005.

In this tutorial I will recreate the same box but with a more complex source code.
In the previous tutorial I used the same source code several times...
    obj = bpy.ops.mesh.primitive_plane_add(size=2, 
    calc_uvs=True, 
    enter_editmode=False, 
    align='CURSOR', 
    location=location, 
    rotation=(0, 0, 0), 
    scale=(0,0,0)
    )
    
    # rename the object
    bpy.context.object.name = obj_name
    # return the object reference
    return bpy.context.object
It is easier to understand the steps taken and then move on to optimizing them in complex forms.
Obviously a presentation of the source code in this tutorial will show you the differences.
I will add that the source code does not include scalar transformations and is limited to a box with size 1.
import bpy

def create_plane_XYZ(loc, obj_name):
    #define the pi 
    pi = 3.1415926
    # this is a definition like a tuple
    rot = (0,0,0)
    # need to convert it to a list in order to add new values
    rot_list = list(rot)
    # this ang variable will rotate obj to the 90-degree angle 
    ang = -90*(pi/180)
    ang_inc = 45*(pi/180)
    if loc[0] < 0:
        rot_list[1]=ang

    if loc[0] > 0: 
        rot_list[1]=ang

    if loc[1] < 0:
        rot_list[0]=ang
        
    if loc[1] > 0: 
        rot_list[0]=ang
    # this check if the two value from position of plane is not zero
    if loc[1] != loc[2] != 0: 
        rot_list[0]=ang_inc
    # this convert a list back to tuple 
    rot=tuple(rot_list)
    # this create the plane 
    obj = bpy.ops.mesh.primitive_plane_add(size=2, 
    calc_uvs=True, 
    enter_editmode=False, 
    align='CURSOR', 
    location=loc, 
    rotation= rot, 
    scale=(0,0,0)
    )
    # rename the object
    bpy.context.object.name = obj_name
    # return the object reference
    return bpy.context.object

# this will create a plane on X and translate with -1 on Y 
planeX001 = create_plane_XYZ((0,-1,0), "Plane-X")
# this will create a plane on X and translate with 1 on Y 
planeX002 = create_plane_XYZ((0,1,0), "Plane+X")
# this will create a plane on Y and translate with -1 on X 
planeY001 = create_plane_XYZ((-1,0,0), "Plane-Y")
# this will create a plane on X and translate with 1 on Y 
planeY002 = create_plane_XYZ((1,0,0), "Plane+Y")
# this will create a plane with 45 degree because two value on tuple is not zero
planeZ001 = create_plane_XYZ((0,-0.25,1.66), "Plane-Y+Z")

Monday, May 23, 2022

Blender 3D and python scripting - part 004.

In this tutorial, I will show you how to create a box from planes, see the screenshot:
You can see I used the math python package and I created rotation by radians and rotation object for three custom planes.
This is the source code:
import bpy

import math

def DegToRad(angle):
    """convert to radians"""
    return angle*(math.pi/180)


def RotOBJ(name, angles):
    """rotate obj to the specified angles"""
    rotation = [DegToRad(angle) for angle in angles]
    bpy.data.objects[name].rotation_euler = rotation
    
def create_plane_X(location, obj_name):

    obj = bpy.ops.mesh.primitive_plane_add(size=2, 
    calc_uvs=True, 
    enter_editmode=False, 
    align='CURSOR', 
    location=location, 
    rotation=(0, 0, 0), 
    scale=(0,0,0)
    )
    
    # rename the object
    bpy.context.object.name = obj_name
    # return the object reference
    return bpy.context.object

def create_plane_Y(location, obj_name):

    obj = bpy.ops.mesh.primitive_plane_add(size=2, 
    calc_uvs=True, 
    enter_editmode=False, 
    align='CURSOR', 
    location=location, 
    rotation=(0, 0, 0), 
    scale=(0,0,0)
    )
    
    # rename the object
    bpy.context.object.name = obj_name
    # return the object reference
    return bpy.context.object

def create_plane_Z(location, obj_name):

    obj = bpy.ops.mesh.primitive_plane_add(size=2, 
    calc_uvs=True, 
    enter_editmode=False, 
    align='CURSOR', 
    location=location, 
    rotation=(0, 0, 0), 
    scale=(0,0,0)
    )
    
    # rename the object
    bpy.context.object.name = obj_name
    # return the object reference
    return bpy.context.object

n = 1

planeX001 = create_plane_X((0,-1,0), "PlaneX-{:02d}".format(n))
RotOBJ(planeX001.name, [-90, 0, 0])
planeX002 = create_plane_X((0,1,0), "PlaneX-{:02d}".format(n))
RotOBJ(planeX002.name, [-90, -0, 0])

planeY001 = create_plane_Y((-1,0,0), "PlaneY-{:02d}".format(n))
RotOBJ(planeY001.name, [0, -90, 0])
planeY002 = create_plane_Y((1,0,0), "PlaneY-{:02d}".format(n))
RotOBJ(planeY002.name, [0, -90, 0])

planeZ001 = create_plane_Z((0,-0.25,1.66), "PlaneZ-{:02d}".format(n))
RotOBJ(planeZ001.name, [45, 0, 0])

Sunday, May 22, 2022

Blender 3D and python scripting - part 003.

In the first tutorial I presented a simple script and in this one I improved it with a way to create lines with a number of points, to use pressurization and coloring according to these points.
I added comments in the source code to make it easier to understand.
Here is the result obtained for nineteen points:
This is the source code:
import bpy 
import random

#this is a for lines with N poins 
N = 19 

# this is default python script from the first tutorial
gpencil_data = bpy.data.grease_pencils.new("GPencil")
gpencil = bpy.data.objects.new(gpencil_data.name, gpencil_data)
bpy.context.collection.objects.link(gpencil)

gp_layer = gpencil_data.layers.new("lines")

gp_frame = gp_layer.frames.new(bpy.context.scene.frame_current)

gp_stroke = gp_frame.strokes.new()

gp_stroke.points.add(count=N)

# let's create a new material for pencil stroke 
gp_material_001 = bpy.data.materials.new(name="Grease pencil material 001")

# if you want to use Nodes 
gp_material_001.use_nodes = True

#this will add a diffuse color for this material 
gp_material_001.diffuse_color = (0.0, 0.0, 0.0, 1)

# create a new material for this grease pencil
bpy.data.materials.create_gpencil_data(gp_material_001)
# add the material to the grese pencil defined like gpencil 
gpencil.data.materials.append(gp_material_001)

for i in range (N):
    rand1 = random.randint(-3, 3)
    rand2 = random.randint(-3, 3)
    rand_size = random.randint(70, 76)
    gp_stroke.line_width = rand_size
    gp_stroke.points[i].co = (rand1,rand2,rand1)
    gp_stroke.points[i].co = (rand2,rand1,rand2)

    #this will create a random pressure 
    rand_pressure = random.randint(-3, 3) * 3
    #create random color for Red Green and Blue 
    rand_color_R = random.randint(0, 1)
    rand_color_G = random.randint(0, 1)
    rand_color_B = random.randint(0, 1)
    # set the pressure 
    gp_stroke.points[i].pressure = rand_pressure
    # set the color RGB with transparency 1
    gp_stroke.points[i].vertex_color = (rand_color_R,rand_color_G,rand_color_B, 1) 

Thursday, May 19, 2022

Blender 3D and python scripting - part 002.

In today's tutorial I will show you how to create a sphere and how to add a material to it.
The source code is very simple with two functions one is for the sphere and the second one is the material of this, see:
import bpy

def create_sphere(radius, distance_to_center, obj_name):

    obj = bpy.ops.mesh.primitive_uv_sphere_add(
        radius=radius,
        location=(distance_to_center, 0, 0),
        scale=(1, 1, 1)
    )
    # rename the object
    bpy.context.object.name = obj_name
    # return the object reference
    return bpy.context.object


def create_emission_shader(color, strength, mat_name):
    # create a new material shader
    mat = bpy.data.materials.new(mat_name)
    # enable the node-graph edition mode
    mat.use_nodes = True
    
    # clear all starter nodes
    nodes = mat.node_tree.nodes
    nodes.clear()

    # add the Emission node
    node_emission = nodes.new(type="ShaderNodeEmission")
    # (input[0] is the color)
    node_emission.inputs[0].default_value = color
    # (input[1] is the strength)
    node_emission.inputs[1].default_value = strength
    
    # add the Output node
    node_output = nodes.new(type="ShaderNodeOutputMaterial")
    
    # link the two nodes
    links = mat.node_tree.links
    link = links.new(node_emission.outputs[0], node_output.inputs[0])

    # return the material reference
    return mat

n = 1
r = 1.0
d = 1.5

sphere001 = create_sphere(r, d, "Sphere-{:02d}".format(n))

sphere001.data.materials.append(
    create_emission_shader(
        (1, 1, 1, 1), 100, "SphereMat001"
    )
)

Wednesday, May 18, 2022

Blender 3D and python scripting - part 001.

Today I started a series of tutorials on python scripting and Blender 3D.
The first tutorial is how to draw using the Blender 3D features with the grease pencil utility using the Python scripting language.
Open Blender 3D in the scripting section and enter the source code below.
import bpy 
import random
rand1 = random.randint(-3, 3)
rand2 = random.randint(-3, 3)
rand_size = random.randint(70, 76)

gpencil_data = bpy.data.grease_pencils.new("GPencil")
gpencil = bpy.data.objects.new(gpencil_data.name, gpencil_data)
bpy.context.collection.objects.link(gpencil)

gp_layer = gpencil_data.layers.new("lines")

gp_frame = gp_layer.frames.new(bpy.context.scene.frame_current)

gp_stroke = gp_frame.strokes.new()
gp_stroke.line_width = rand_size

gp_stroke.points.add(count=4)

gp_stroke.points[0].co = (rand1,rand2,rand1)
gp_stroke.points[1].co = (rand2,rand1,rand2)
Run the script several times to see the effect produced.
Here is the result of running this script: