Halloween Costume ideas 2015

PYTHON / Object, Mesh and Components Collection

This post will be quite short.

Basic stuff, basically snippets of BPY and information on how to collect information using what Blender Python already provide and could be accessed by digging the Dictionary, Library, Object, whatever it is really called.

We will focus on POLYGON MESH data and its components (face, edge, vertex). There will be some overlaps of information with previous post. I will list some useful ones and example usages.

(The list grows as I learned more about Blender Python and Python...)


UPDATE: Random Vertex Color on N-Gons Faces

With some helps from Blender Scripting Guru and his elegant script:
http://blenderscripting.blogspot.com.au/2013/03/vertex-color-map.html


Apparently applying color is a much simpler process than sampling the color.

I have made an update to my previous script to Randomize Face Color and this time it works fine on any mesh, not just Quads only. The script loops over every face, including Tris and N-Gons:

BLENDER SUSHI SCRIPT: Randomize Face Vertex Color for all N-Gons


'''
BLENDER SUSHI SCRIPT
blendersushi.blogspot.com.au

Give random solid color for every face.
Last update: 2013.03.06

NOTE: This updated Script works on any N-Gons Faces

Thanks to Dealga MrArdle for his elegant script
http://blenderscripting.blogspot.com.au/2013/03/vertex-color-map.html
'''

# bpy.data.objects['Cube'].data.polygons[0].loop_indices

import bpy
import random

# Get selected objects
selected_objects = bpy.context.selected_objects

# Create a single Material that respect Vertex Color
mat = bpy.data.materials.new('VertexMat')
mat.use_vertex_color_paint = True
mat.use_vertex_color_light = True

for object in selected_objects:

# Get Mesh Data instead of Objects
mesh = object.data
object.data.materials.append(mat)

# Create new 'Col' Vertex Color Layer
mesh.vertex_colors.new()

# Vertex colour data
vertexColor = mesh.vertex_colors[0].data
faces = mesh.polygons

# Assign colours to verts (loop every faces)
# Script Snippet from Blender Artist
i = 0
for face in faces:
rgb = [random.random() for i in range(3)]
for idx in face.loop_indices:
vertexColor[i].color = rgb
i += 1







TO DIG AND COLLECT INFO

Like I mentioned in the previous post, typically during programming we started by LISTING objects.

Once we get the list and right information, we can do further manipulations and processing of those data:
- Colorize List of Faces
- Randomize Vertex Color
- Delete all animation
- Extrude selected faces
- Change attributes with certain values for selected objects
- etc

You can get into data path of an object in few different ways, whether you are pointing directly to the DATA or you can use CONTEXT (currently selected, active, etc).

For example:
mySelections = bpy.context.selected_objects
myObjects = bpy.data.objects
myMeshes = bpy.data.meshes

Getting myMeshes list using the above, you will get all the meshes in the scene, including the Mesh that are not being used. So, the better way is to do something like below:

myMeshes = myObjects.data 

Further on, you can then:
1. Specify which object or mesh data by its name or keyword.

bpy.data.objects['Cube'].data 
# this is accessing the Mesh data of object named Cube

2. And further down, you can access the components (face, edge, vertices)

That kind of things (object inside object inside object) happens a lot while programming with Python and Blender Python. Remember we can always dir() and list() the bpy as well.

You can also every now and then MOUSE HOVER over certain Attribute or Value you like to access via bpy and it will more often give you the answer.

I guess, it is nicer if everything is documented nicely one day. In the meantime, we do it like this.

Good reading:
http://code.blender.org/index.php/2011/05/scripting-%E2%80%93-change-settings-the-fast-way/

_BY CONTEXT

SELECTED OBJECT(S)
bpy.context.selected_objects

CURRENTLY ACTIVE OBJECT
bpy.context.active_object

MAKE OBJECT ACTIVE
bpy.context.scene.objects.active = bpy.data.objects[{name}]

MORE IN DEPTH ON MAKE ACTIVE:
http://blenderscripting.blogspot.com.au/2011/07/how-to-set-activeobject-via-python.html

_OBJECT

OBJECT(S) ALL
bpy.data.objects

OBJECT COLOR
bpy.data.objects[name].color

OBJECT TYPE
bpy.data.objects[{name}].type


OBJECT KEYS --> list all Custom Properties

bpy.data.objects[{name}].keys()
bpy.data.objects['Cube'].keys()
bpy.data.objects['Cube']['props']

Read this:
http://wiki.blender.org/index.php/Doc:2.6/Manual/Extensions/Python/Properties#Properties.2C_ID-Properties_and_their_differences
http://blenderartists.org/forum/showthread.php?235702-How-to-make-a-property-pointing-to-an-object

OBJECT NAME
bpy.data.objects[name].name

OBJECT MATERIAL
bpy.data.objects[name].data.materials

OBJECT MATERIAL NAME
bpy.data.objects[{name}].data.materials[{name}].name

OBJECT ACTIVE MATERIAL INDEX
bpy.data.objects['Cube'].active_material_index


OBJECT TRANSFORMATION (Location, Rotation, Scale)

_MESH


MESH via OBJECT
bpy.data.objects[name].data

This will give you access to the actual Mesh data.

MESH ALL
bpy.data.meshes

_FACE

FACE POLYGONS
bpy.data.objects['Cube'].data.polygons




FACE LOOP VERTICES INDEX
bpy.data.objects['Cube'].data.polygons[0].loop_indices



FACE OF ACTIVE OBJECT CURRENTLY ACTIVE IN EDIT MODE
bpy.context.active_object.data.polygons.active

Read:
http://blenderartists.org/forum/showthread.php?207542-Selecting-a-face-through-the-API

FACE OF ACTIVE OBJECT TO BE SELECTED (True or False)
bpy.context.active_object.data.polygons[index].select

_VERTEX

VERTICES VECTOR COORDINATE (LOCAL)
bpy.data.vertices[idx].co[vector]

Remember to multiple World Matrix in order to get XYZ position in world.

VERTEX COLOR
No direct access at the moment.

Getting vertex color is apparently not that simple, basically one needs to look up into the available Vertex Color layer (usually called 'Col' by default) that is connected to the Mesh.

Also the fact that the actual Vertex Color displayed by Face of polygons is really the combination of many Color values.

We need to create our own function to get the "color of vertex".

Read:
http://blenderscripting.blogspot.com.au/2013/03/vertex-color-map.html

GET TOTAL VERTEX NUMBER OF SELECTED OBJECT
len (bpy.context.active_object.data.vertices)

VERTEX SELECT VIA ACTIVE OBJECT
bpy.context.active_object.data.vertices[index].select = True
bpy.context.active_object.data.vertices[index].select = False

Read:
http://blenderartists.org/forum/showthread.php?200305-How-to-select-individual-vertices-via-python

_ANIMATION

bpy.data.objects['Cube'].animation_data_clear()
bpy.data.objects['Cube'].animation_data_create()




_OBJECT AND EDIT MODE

bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.mode_set(mode='EDIT')

_MODIFIER

Modify property of existing Modifier on Object with specific name
bpy.data.objects['name'].modifiers['name'].levels = 2


Checking if Modifier by specific NAME is EXISTS

def existsName(modifiername):
    if bpy.context.active_object.modifiers.get(modifiername):
        return True
    else:
        return False

Checking if Modifier by specific TYPE is EXISTS
def existsType(modifiertype):
    
    modifiers = bpy.context.active_object.modifiers
    
    for modifier in modifiers:
        if modifier.type != modifiertype:
            pass
        else: 
            print("Yes, exists!")
            return True

Create New Modifier on Active Object
bpy.context.active_object.modifiers.new("Sushi", "MIRROR")


_DYNAMICS (BULLET)

CHANGE RIGID BODY TYPE
bpy.context.object.rigid_body.collision_shape = 'BOX'

example usage:


import bpy

selected_objects = bpy.context.selected_objects

for object in selected_objects:
    bpy.context.scene.objects.active = object
    bpy.context.object.rigid_body.collision_shape = 'BOX'


_PARTICLE

PARTICLE POSITION
bpy.context.active_object.particle_systems.active.particles[{index}].location
bpy.context.active_object.particle_systems.active.particles[0].location


_VERTEX_RADIUS_OF_SKIN_MODIFIER

SKIN MODIFIER - SKIN VERTICES RADIUS
skin = bpy.context.active_object.data.skin_vertices[0].data
skin[0].radius[0]
skin[0].radius[1]

bpy.data.meshes['Mesh'].skin_vertices[""].data[0].radius[0]
bpy.data.meshes['Mesh'].skin_vertices[""].data[0].radius[1]

OTHER REFERENCES

http://wiki.blender.org/index.php/Doc:2.6/Manual/Extensions/Python/Console

Post a Comment

MKRdezign

Contact Form

Name

Email *

Message *

Powered by Blogger.
Javascript DisablePlease Enable Javascript To See All Widget