analitics

Pages

Showing posts with label 3d. Show all posts
Showing posts with label 3d. Show all posts

Friday, October 13, 2023

Blender 3D and python scripting - part 026.

Today I tested the bpy python module from Blender 3D software version 3.5 and I made this lite addon that showed me a modal dialog and checked and installed the Pillow python module.
The script don't install Pillow because is not fixed.
The main reason was to add my Python tools and features to Blender 3D and share with you.
bl_info = {
    "name": "Tools by catafest",
    "blender": (3, 0, 0),
    "category": "3D View",
}

import bpy
from bpy.types import Operator, Panel
from bpy.props import StringProperty

try:
    import importlib
    importlib.import_module("Pillow")
    PIL_installed = True
except ImportError:
    PIL_installed = False

def install_pillow():
    import subprocess
    try:
        subprocess.run([bpy.app.binary_path, '--python-exit-code', '1', '-m', 'ensurepip'])
        subprocess.check_call([bpy.app.binary_path, '-m', 'pip', 'install', 'Pillow'])
    except subprocess.CalledProcessError as e:
        print("Eroare la instalarea Pillow:", e)

# Operator pentru a afișa fereastra modală cu informații despre instalarea Pillow
class CATAFEST_IMAGES_OT_show_pillow_message(Operator):
    bl_idname = "catafest.show_pillow_message"
    bl_label = "Show Pillow Message"

    def execute(self, context):
        global PIL_installed
        message = "Pillow este instalat." if PIL_installed else "Pillow nu este instalat."

        # Dacă Pillow nu este instalat, încercați să-l instalați
        if not PIL_installed:
            install_pillow()
            try:
                import importlib
                importlib.import_module("Pillow")
                PIL_installed = True
                message = "Pillow a fost instalat cu succes!" if PIL_installed else "Eroare la instalarea Pillow."
            except ImportError:
                PIL_installed = False

        # Afișați fereastra modală în centrul ecranului
        bpy.ops.catafest.show_modal_message('INVOKE_DEFAULT', title="Starea Pillow", message=message)
        return {'FINISHED'}

    def invoke(self, context, event):
        return self.execute(context)

# Operator pentru a afișa fereastra modală personalizată
class CATAFEST_IMAGES_OT_show_modal_message(Operator):
    bl_idname = "catafest.show_modal_message"
    bl_label = "Show Modal Message"

    title: bpy.props.StringProperty(default="Message")
    message: bpy.props.StringProperty(default="")

    def execute(self, context):
        return {'FINISHED'}

    def invoke(self, context, event):
        wm = context.window_manager
        return wm.invoke_props_dialog(self, width=400)

    def draw(self, context):
        layout = self.layout
        layout.label(text=self.message)

# Panel pentru bara laterală din 3D View
class VIEW3D_PT_tools_image(Panel):
    bl_label = "Images"
    bl_idname = "VIEW3D_PT_tools_image"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = 'Tools by catafest'

    def draw(self, context):
        layout = self.layout
        layout.operator(CATAFEST_IMAGES_OT_show_modal_message.bl_idname)
        layout.operator(CATAFEST_IMAGES_OT_show_pillow_message.bl_idname)

def register():
    bpy.utils.register_class(CATAFEST_IMAGES_OT_show_modal_message)
    bpy.utils.register_class(CATAFEST_IMAGES_OT_show_pillow_message)
    bpy.utils.register_class(VIEW3D_PT_tools_image)

def unregister():
    bpy.utils.unregister_class(CATAFEST_IMAGES_OT_show_modal_message)
    bpy.utils.unregister_class(CATAFEST_IMAGES_OT_show_pillow_message)
    bpy.utils.unregister_class(VIEW3D_PT_tools_image)

if __name__ == "__main__":
    register()

Monday, August 15, 2022

Blender 3D and python scripting - part 025.

In this tutorial I will show a simple python script for add a String interface to the Group Output.
You can see in the next image the result of this script.
You need to have an modifier Geometry Nodes or add new one.
This is the source script with comments for each step I used:
import bpy

#get active object - default 
obj = bpy.context.active_object

# set the default start for working with Geometry Nodes modifier
# you need to have a Geometry Nodes modifier
node_group = obj.modifiers['GeometryNodes'].node_group
nodes = node_group.nodes

#get the node named 'Group Output'
geom_out = nodes.get('Group Output')

#create a string node 
string_node = nodes.new('FunctionNodeInputString')
# set the name to 'String'
string_out = string_node.outputs['String']
# set the value to "This is a string"
string_node.string = "This is a string"

# link to the Group Output
node_group.links.new(string_out, geom_out.inputs[-1])

Wednesday, August 10, 2022

Blender 3D and python scripting - part 024.

In this tutorial I will show you how to use GeometryNodes with python script and Blender A.P.I.
You can see the result in the next image.
The Object Info node gets information from objects. This can be useful to control parameters in the geometry node tree with an external object, either directly by using its geometry, or via its transformation properties. An Object Info node can be added quickly by dragging an object into the node editor.
Another information can be found on the manual link.
In the next script you can see I created a simple BezierCurve object.
The definition named new_GeometryNodes_group is used to create two nodes GroupInit and GroupOutput.
I commente the source code to see some steps.
# import python packages
import bpy
from mathutils import Vector

# create a simpple BezierCurve and rename it with 'BezierCurveGeormetryNode'
bpy.ops.curve.primitive_bezier_curve_add()
bpy.ops.object.modifier_add(type='NODES')  

curve = bpy.context.active_object
curve.name = 'BezierCurveGeormetryNode'

# define a function for GroupInit and GroupOutput
def new_GeometryNodes_group():
    ''' Create a new empty node group that can be used
        in a GeometryNodes modifier.
    '''
    node_group = bpy.data.node_groups.new('GeometryNodes', 'GeometryNodeTree')
    inNode = node_group.nodes.new('NodeGroupInput')
    inNode.outputs.new('NodeSocketGeometry', 'Geometry')
    outNode = node_group.nodes.new('NodeGroupOutput')
    outNode.inputs.new('NodeSocketGeometry', 'Geometry')
    node_group.links.new(inNode.outputs['Geometry'], outNode.inputs['Geometry'])
    # the -3.5 is value for how far will be set the GroupInit and GroupOutput in the area of GeormetryNodes
    inNode.location = Vector((-3.5*inNode.width, 0))
    outNode.location = Vector((3.5*outNode.width, 0))
    return node_group

# the default curve modifier has no node group set, you need to set :
if curve.modifiers[-1].node_group:
    node_group = curve.modifiers[-1].node_group    
else:
    node_group = new_GeometryNodes_group()
    curve.modifiers[-1].node_group = node_group

# set default grup node as nodes
nodes = node_group.nodes

# get both nodes for each one 
group_in = nodes.get('Group Input')
group_out = nodes.get('Group Output')

# add the GeometryNodeObjectInfo to the GeometryNode area 
new_node_obj = nodes.new('GeometryNodeObjectInfo')
new_node_obj.inputs[0].default_value = bpy.data.objects["BezierCurveGeormetryNode"]

Saturday, July 23, 2022

Blender 3D and python scripting - part 023.

This script will add a submenu to the main Help menu with an icon with a folder that will open the explorer from the Windows operating system.
I have not solved the tooltip of this button, it is set to show a message about opening a URL.
This is the source code:
import bpy

def menu_func(self, context):
    '''Open explorer in windows systems'''
    self.layout.operator(
            "wm.url_open", text="Open explorer", icon='FILE_FOLDER').url = "C:/"
def register():
    bpy.types.TOPBAR_MT_help.append(menu_func)

def unregister():
    bpy.types.TOPBAR_MT_help.remove(menu_func)

if __name__ == "__main__":
    register()
If you want to change the tooltip then need to create a class OpenOperator and wrapper for this operator function and set the bl_label and all you need to have it.
The menu_func will get the layout operator with all defined in the class OpenOperator and will set the tooltip with the text: Open explorer in windows systems.
See this new source code:
import bpy

def menu_func(self, context):
    self.layout.operator(
            OpenOperator.bl_idname, text="Open explorer", icon='FILE_FOLDER')

class OpenOperator(bpy.types.Operator):
    """Open explorer in windows systems"""
    bl_idname = "wm.open_explorer"
    bl_label = "Open explorer"

    def execute(self, context):
        bpy.ops.wm.url_open(url="C:/")
        return {'FINISHED'}


def register():
    bpy.utils.register_class(OpenOperator)
    bpy.types.TOPBAR_MT_help.append(menu_func)


def unregister():
    bpy.utils.unregister_class(OpenOperator)
    bpy.types.TOPBAR_MT_help.remove(menu_func)


if __name__ == "__main__":
    register()

Thursday, July 7, 2022

Blender 3D and python scripting - part 022.

In the last tutorial, we exemplified with the default template from Blender 3D how to create a panel in the Object area.
Today I will show you how to modify this panel with some useful elements for developing an addon.
The purpose of the old tutorial on this is the differences and changes that must be made to the template source code to introduce the following functions:
    StringProperty(
    BoolProperty( 
    IntProperty(
    IntVectorProperty(
    FloatProperty(
    FloatVectorProperty(
    BoolVectorProperty(
Some arguments need to be modified to have different input data, see the selection of colors in the attached image:
I commented on the source code areas in the template and added my changes:
The class also called SceneSettingItem and CollectionProperty is currently being tested and is not finalized to be implemented, it can be seen in panel: 0 items.
It can be seen that any defined class must be registered and unregistered
Here is the source code used to get the new screenshot changes:
import bpy

# Assign a collection
class SceneSettingItem(bpy.types.PropertyGroup):
    name = bpy.props.StringProperty(name="Cube")
    mesh = bpy.props.PointerProperty(type=bpy.types.Mesh)
    


PROPS = [
            ('myString', bpy.props.StringProperty(name='myString', default='this is my string!')),
            ('myBoolean', bpy.props.BoolProperty(name='myBoolean', default=False)),
            ('myInt', bpy.props.IntProperty(name='myInt', default=1)),
            ('myIntVectorXYZ', bpy.props.IntVectorProperty(subtype='XYZ')),
            ('myFloat', bpy.props.FloatProperty(name='myFloat', default=1)),
            ('myFloatVectorXYZ', bpy.props.FloatVectorProperty(subtype='XYZ')),
            ('myBooleanVector', bpy.props.BoolVectorProperty(size=3)),
            ('myBooleanVectorXYZ', bpy.props.BoolVectorProperty(size=3,subtype='XYZ')),
            ('myBooleanVectorColor', bpy.props.FloatVectorProperty(name="Edit Mode Color", subtype='COLOR',  default=(0.76, 0.0, 0.0), size=3, min=0, max=1)),
            ('myCollectionProperty', bpy.props.CollectionProperty(type=SceneSettingItem)),
        ]    

class HelloWorldPanelVariables(bpy.types.Panel):
    """Creates a Panel in the Object properties window"""
    bl_label = "Hello World Panel Variables"
    bl_idname = "OBJECT_PT_hello"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "object"

#    def draw(self, context):
#        layout = self.layout
#        obj = context.object
#        row = layout.row()
#        row.label(text="Hello world!", icon='WORLD_DATA')
#        row = layout.row()
#        row.label(text="Active object is: " + obj.name)
#        row = layout.row()
#        row.prop(obj, "name")
#        row = layout.row()
#        row.operator("mesh.primitive_cube_add")

    def draw(self, context):
        col = self.layout.column()
        for (prop_name, _) in PROPS:
            row = col.row()
            row.prop(context.scene, prop_name)

        
def register():
    bpy.utils.register_class(SceneSettingItem)
    bpy.utils.register_class(HelloWorldPanelVariables)
    for (prop_name, prop_value) in PROPS:
        setattr(bpy.types.Scene, prop_name, prop_value)

def unregister():
    bpy.utils.unregister_class(SceneSettingItem)
    bpy.utils.unregister_class(HelloWorldPanelVariables)    
    for (prop_name, _) in PROPS:
        delattr(bpy.types.Scene, prop_name)

if __name__ == "__main__":
    register()

Wednesday, July 6, 2022

Blender 3D and python scripting - part 021.

I will continue the series of tutorials with python and the Blender 3D software interface.
From the main menu we can get to the scripting part and here we choose Templates - Python - Ui Panel Simple.
The source code will be added to the python editor.
Save this source code with a name and load it as an addon.
After loading this source code it can be found at Properties at Object, see screenshot.
You can see the source code from the Ui Panel Simple template that I used.
import bpy

class HelloWorldPanel(bpy.types.Panel):
    """Creates a Panel in the Object properties window"""
    bl_label = "Hello World Panel"
    bl_idname = "OBJECT_PT_hello"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "object"

    def draw(self, context):
        layout = self.layout

        obj = context.object

        row = layout.row()
        row.label(text="Hello world!", icon='WORLD_DATA')

        row = layout.row()
        row.label(text="Active object is: " + obj.name)
        row = layout.row()
        row.prop(obj, "name")

        row = layout.row()
        row.operator("mesh.primitive_cube_add")

def register():
    bpy.utils.register_class(HelloWorldPanel)


def unregister():
    bpy.utils.unregister_class(HelloWorldPanel)

if __name__ == "__main__":
    register()

Sunday, June 26, 2022

Blender 3D and python scripting - part 020.

So far I have added or branched used python scripts in the blender and created a UV texture for this use UV Smart projection.
The resulting script is quite large in content.
I thought I should create an addon to use and show you how to do it.
Let's follow the basic steps when working with python scripts in Blender 3D.
  1. go to the tag named: Scripting;
  2. use main menu to create a new script from Text - New;
  3. use main menu - Template - Python - Addon Add Object;
  4. save the script with a good name, I used: addon_catafest_add_branch.py;
  5. save the blend file with a good name: I used: addon_catafest_add_branch.blend;
See this screenshot:
After these changes you can modify in the template file the parts related to the names, descriptions and other elements that will be viewed in Blender 3D
Restart the Blender 3D software, open the python script with all changes and run it.
In the 3D Viewport area press the shortkeys: Shift + A and use Mesh menu to see this addon feature, see screenshot:
This is source code I used:
bl_info = {
    "name": "New branch",
    "author": "Your Name Here",
    "version": (1, 0),
    "blender": (3, 3, 0),
    "location": "View3D > Add > Mesh > New Object",
    "description": "Adds a new branch Mesh Object",
    "warning": "",
    "doc_url": "",
    "category": "Add Mesh",
}


import bpy
from bpy.types import Operator
from bpy.props import FloatVectorProperty
from bpy_extras.object_utils import AddObjectHelper, object_data_add
from mathutils import Vector


def add_object(self, context):
    scale_x = self.scale.x
    scale_y = self.scale.y

    verts = [
        Vector((-1 * scale_x, 1 * scale_y, 0)),
        Vector((1 * scale_x, 1 * scale_y, 0)),
        Vector((1 * scale_x, -1 * scale_y, 0)),
        Vector((-1 * scale_x, -1 * scale_y, 0)),
    ]

    edges = []
    faces = [[0, 1, 2, 3]]

    mesh = bpy.data.meshes.new(name="New Object Mesh")
    mesh.from_pydata(verts, edges, faces)
    # useful for development when the mesh may be invalid.
    # mesh.validate(verbose=True)
    object_data_add(context, mesh, operator=self)


class OBJECT_OT_add_object(Operator, AddObjectHelper):
    """Create a new branch Mesh Object"""
    bl_idname = "mesh.add_object"
    bl_label = "Add Mesh Object"
    bl_options = {'REGISTER', 'UNDO'}

    scale: FloatVectorProperty(
        name="scale",
        default=(1.0, 1.0, 1.0),
        subtype='TRANSLATION',
        description="scaling",
    )

    def execute(self, context):

        add_object(self, context)

        return {'FINISHED'}


# Registration

def add_object_button(self, context):
    self.layout.operator(
        OBJECT_OT_add_object.bl_idname,
        text="catafest - add branch",
        icon='PLUGIN')


# This allows you to right click on a button and link to documentation
def add_object_manual_map():
    url_manual_prefix = "https://docs.blender.org/manual/en/latest/"
    url_manual_mapping = (
        ("bpy.ops.mesh.add_object", "scene_layout/object/types.html"),
    )
    return url_manual_prefix, url_manual_mapping


def register():
    bpy.utils.register_class(OBJECT_OT_add_object)
    bpy.utils.register_manual_map(add_object_manual_map)
    bpy.types.VIEW3D_MT_mesh_add.append(add_object_button)


def unregister():
    bpy.utils.unregister_class(OBJECT_OT_add_object)
    bpy.utils.unregister_manual_map(add_object_manual_map)
    bpy.types.VIEW3D_MT_mesh_add.remove(add_object_button)


if __name__ == "__main__":
    register()

Saturday, June 18, 2022

Blender 3D and python scripting - part 019.

Today I'm going to show you how to have a UV map for the entire mesh of the object created with the smart projection mode.
It's the same when you create a UV map using UV Mapping - Smart UV Project in the UV Editing view.
In principle, everything created in the Blender 3D interface has an equivalent in the A.P.I. of the 3D Blender.
You can see a screenshot with this script:
Here you see the source code that includes this option.
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):
        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

# 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) + 1.0 , RN()+(v.co.y) + 1.0 , (v.co.z) - (v.co.z)/3) )
    #v1 = bm.verts.new(1, 1, 3)
    bm.edges.new((v1, v2))
    rand_list.append(0.01)
    rand_list.sort(reverse=True)
    # update 
    bm.select_flush_mode()   
    me.update()
    #mesh.validate()
    #bmesh.update_edit_mesh(obj_branch.data)
    

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


## now set up shape key in Blender
#mesh=obj_branch.data
#sk_basis = obj_branch.shape_key_add(name='Basis',from_mix=False)
#sk_basis.interpolation = 'KEY_LINEAR'
## must set relative to false here
#obj_branch.data.shape_keys.use_relative = False

## create new shape key
#sk = obj_branch.shape_key_add(name='Deform',from_mix=False)
#sk.interpolation = 'KEY_LINEAR'
#sk.slider_min = 0
#sk.slider_max = 2

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

# this will fix the error ...  mode_set_poll()
bpy.context.view_layer.objects.active = obj_branch  

createNewBranch(obj_branch, 1)

# print tool for developing area 
def print_python_console(data):
    for window in bpy.context.window_manager.windows:
        screen = window.screen
        for area in screen.areas:
            if area.type == 'CONSOLE':
                override = {'window': window, 'screen': screen, 'area': area}
                bpy.ops.console.scrollback_append(override, text=str(data), type="OUTPUT")

# used to see the size of radius skin for each vertices
print_python_console(rand_list)

# fix error :  skin modifier is locked when using edit mode.
bpy.ops.object.mode_set(mode="OBJECT", toggle=False)
# 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(1,branch+1):
    # 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])

# this will apply the modifier named 'SK'
bpy.ops.object.modifier_apply( modifier = 'SK' )
#
bpy.ops.object.mode_set(mode="EDIT", toggle=True)
bpy.ops.object.skin_root_mark()

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

mesh = bpy.data.meshes.new(name_branch+'_Obj') 
mesh.update()
mesh.validate()

import math 

def get_dimension(normal):
    x_abs = math.fabs(normal[0])
    y_abs = math.fabs(normal[1])
    z_abs = math.fabs(normal[2])
    if z_abs >= x_abs and z_abs >= y_abs:
        return 2
    elif x_abs >= y_abs:
        return 0
    else:
        return 1

texture_scale = 1.0
bpy.ops.object.mode_set(mode="EDIT", toggle=True)  
bpy.ops.mesh.select_all(action='SELECT')  
me = obj_branch.data
bm = bmesh.from_edit_mesh(me)

bpy.ops.uv.sphere_project()

uv_layer = bm.loops.layers.uv.verify()

# adjust uv coordinates
for face in bm.faces:
    for l in face.loops:
        luv = l[uv_layer]
        # select UV vertex if these are in certain range
        if 0 <= luv.uv.x <= 1 and 0 <= luv.uv.y <= 1:
            luv.select = True
            luv = l[uv_layer]
            luv.uv = l.vert.co.yz * texture_scale
            luv.uv = l.vert.co.xz * texture_scale
            luv.uv = l.vert.co.xy * texture_scale
                
bmesh.update_edit_mesh(me) 
me.update()  

bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.uv.smart_project(angle_limit=1.15192, island_margin=0, area_weight=0, correct_aspect=True, scale_to_bounds=False)
# use this for cylinder project for UV map 
#bpy.ops.uv.cylinder_project(direction='ALIGN_TO_OBJECT',
#align='POLAR_ZX',
#radius=1.0,
#correct_aspect=True,
#clip_to_bounds=False,
#scale_to_bounds=True)


bmesh.update_edit_mesh(me)
bpy.ops.object.mode_set(mode="OBJECT", toggle=True)

Thursday, June 16, 2022

Blender 3D and python scripting - part 018.

In this tutorial I will show you how to create a UV map.
I searched the web and the documentation briefly and I didn't find anything very concrete, but here I created a functional example.
The example contains a function that calculates dimensions, which are then processed and added to the UV Editing workspace.
This is what the source code added to the initial script tested looks like:
mesh.validate()

import math 

def get_dimension(normal):
    x_abs = math.fabs(normal[0])
    y_abs = math.fabs(normal[1])
    z_abs = math.fabs(normal[2])
    if z_abs >= x_abs and z_abs >= y_abs:
        return 2
    elif x_abs >= y_abs:
        return 0
    else:
        return 1

texture_scale = 1.0
bpy.ops.object.mode_set(mode="EDIT", toggle=True)    
me = obj_branch.data
bm = bmesh.from_edit_mesh(me)

uv_layer = bm.loops.layers.uv.verify()


for f in bm.faces:
    largest_index = get_dimension(f.normal)
    for l in f.loops:
        luv = l[uv_layer]
        luv.uv = l.vert.co.yz * texture_scale
        luv.uv = l.vert.co.xz * texture_scale
        luv.uv = l.vert.co.xy * texture_scale


me.update()
Here is a screenshot of the result, you can see that it only shows the node, you can select for all nodes to see a final result.

Tuesday, June 14, 2022

Blender 3D and python scripting - part 017.

In this tutorial I will show you how you can apply an modifier in Blender 3D using the python script.
First, you need to comment these source code rows in order to allow to apply the modifier.
## now set up shape key in Blender
#mesh=obj_branch.data
#sk_basis = obj_branch.shape_key_add(name='Basis',from_mix=False)
#sk_basis.interpolation = 'KEY_LINEAR'
## must set relative to false here
#obj_branch.data.shape_keys.use_relative = False

## create new shape key
#sk = obj_branch.shape_key_add(name='Deform',from_mix=False)
#sk.interpolation = 'KEY_LINEAR'
#sk.slider_min = 0
#sk.slider_max = 2
You cannot aply an modifier if you have skape keys.
To apply an modifyer you can use this line of source code:
# this will apply the modifier named 'SK'
bpy.ops.object.modifier_apply( modifier = 'SK' )
If you go to the UV Editing area you will see the modifier is apply and you can create an UV map.

Monday, June 13, 2022

Blender 3D and python scripting - part 016.

On this day, I will digress from the series of tutorials started and presented and show you how to install other python packages in Blender 3D.
Go to the bin folder where the python is install, see my path of Blender 3D.
C:\blender-3.3.0-alpha+master.add1da52ad78-windows.amd64-release\3.3\python\bin
Use these commands in to window command shell to install OpenCv python module.
python.exe -m ensurepip
python.exe -m pip install --upgrade pip
python.exe -m pip install opencv-python 
python.exe -m pip install opencv-contrib-python
Run in the blender script area these commands, in order to see if this python package working.
import cv2
cv2.version
You can create a simple script and test it, see the next example:
import numpy as np
import cv2
  
# Creating a black image with 3 channels
# RGB and unsigned int datatype
img = np.zeros((400, 400, 3), dtype = "uint8")
  
# Creating line
cv2.line(img, (21, 167), (100, 99), (0, 0, 255), 8)
  
cv2.imshow('dark', img)
  
# Allows us to see image
# until closed forcefully
cv2.waitKey(0)
cv2.destroyAllWindows()
You can see a screenshot with this python script:

Sunday, June 12, 2022

Blender 3D and python scripting - part 015.

The tutorial for today is about adding armature to the skin, and has a single line of source code:
bpy.ops.object.skin_armature_create(modifier="SK")
This will add an armature with bones for each edge based on the skin modifier.
See the result of this ...
The full source code is this:
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):
        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

# 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) + 1.0 , RN()+(v.co.y) + 1.0 , (v.co.z) - (v.co.z)/3) )
    #v1 = bm.verts.new(1, 1, 3)
    bm.edges.new((v1, v2))
    rand_list.append(0.01)
    rand_list.sort(reverse=True)
    # update 
    bm.select_flush_mode()   
    me.update()
    #mesh.validate()
    #bmesh.update_edit_mesh(obj_branch.data)

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


# now set up shape key in Blender
mesh=obj_branch.data
sk_basis = obj_branch.shape_key_add(name='Basis',from_mix=False)
sk_basis.interpolation = 'KEY_LINEAR'
# must set relative to false here
obj_branch.data.shape_keys.use_relative = False

# create new shape key
sk = obj_branch.shape_key_add(name='Deform',from_mix=False)
sk.interpolation = 'KEY_LINEAR'
sk.slider_min = 0
sk.slider_max = 2

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

# this will fix the error ...  mode_set_poll()
bpy.context.view_layer.objects.active = obj_branch  

createNewBranch(obj_branch, 1)

# print tool for developing area 
def print_python_console(data):
    for window in bpy.context.window_manager.windows:
        screen = window.screen
        for area in screen.areas:
            if area.type == 'CONSOLE':
                override = {'window': window, 'screen': screen, 'area': area}
                bpy.ops.console.scrollback_append(override, text=str(data), type="OUTPUT")

# used to see the size of radius skin for each vertices
print_python_console(rand_list)

# fix error :  skin modifier is locked when using edit mode.
bpy.ops.object.mode_set(mode="OBJECT", toggle=False)
# add the skin modifier
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(1,branch+1):
    # 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])

bpy.ops.object.skin_armature_create(modifier="SK")

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

Saturday, June 11, 2022

Blender 3D and python scripting - part 014.

In this tutorial I will show you how can add a shape key to the branch using this source code:
# now set up shape key in Blender
mesh=obj_branch.data
sk_basis = obj_branch.shape_key_add(name='Basis',from_mix=False)
sk_basis.interpolation = 'KEY_LINEAR'
# must set relative to false here
obj_branch.data.shape_keys.use_relative = False

# create new shape key
sk = obj_branch.shape_key_add(name='Deform',from_mix=False)
sk.interpolation = 'KEY_LINEAR'
sk.slider_min = 0
sk.slider_max = 2

# ... and add it to the scene

Tuesday, June 7, 2022

Blender 3D and python scripting - part 013.

In today's tutorial I will present the source code with some minor fixes and an early way to fix the skin for the added branch.
Minor fixes are related to some errors in creating and passing data - I added comments.
It is interesting to see how I created and modified the source code step by step because it cannot be moved from one area to another because it is restrictive to the way it works in Blender 3D.
If I had used classes, this would not have been understood.
There are also minor technical details related to the skin, the random function for the thickness of the branches ...
For a source code written on the fly and without a pseudocode defined at the beginning I could say that the transitions between the source code between the tutorials is quite legible.
Here is a screenshot with some skin generated examples for the second branch for vertex position one.
This is the source basket used to create the 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):
        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

# 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) + 1.0 , RN()+(v.co.y) + 1.0 , (v.co.z) - (v.co.z)/3) )
    #v1 = bm.verts.new(1, 1, 3)
    bm.edges.new((v1, v2))
    rand_list.append(0.01)
    rand_list.sort(reverse=True)
    # update 
    bm.select_flush_mode()   
    me.update()
    #mesh.validate()
    #bmesh.update_edit_mesh(obj_branch.data)

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

# this will fix the error ...  mode_set_poll()
bpy.context.view_layer.objects.active = obj_branch  

createNewBranch(obj_branch, 1)

# print tool for developing area 
def print_python_console(data):
    for window in bpy.context.window_manager.windows:
        screen = window.screen
        for area in screen.areas:
            if area.type == 'CONSOLE':
                override = {'window': window, 'screen': screen, 'area': area}
                bpy.ops.console.scrollback_append(override, text=str(data), type="OUTPUT")

# used to see the size of radius skin for each vertices
print_python_console(rand_list)

# fix error :  skin modifier is locked when using edit mode.
bpy.ops.object.mode_set(mode="OBJECT", toggle=False)
# 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(1,branch+1):
    # 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)

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

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]