User Tools

Site Tools


Sidebar

Dan's Wiki

DokuWiki Instructions (local) DokuWiki Manual
Site Checker (Orphans Wanted)

Edit Sidebar

blender:python:addonnotes:start

This is an old revision of the document!


Blender Python Addon Notes

Doing research to make a panel to control Baking Texture assignments.

Quick Tips

list(bpy.data.objects)
   # -> Lists objects array
type(bpy.data.objects)
<class 'bpy_prop_collection'>
   # -> Get the Python type
dir(bpy.data.objects)
   # -> Get the variables and methods on this class

Operators

The menu item: Help → Operator Cheat Sheet” gives a list of all operators and their default values in Python syntax, along with the generated docs. This is a good way to get an overview of all blender’s operators.

Operators don’t have return values as you might expect, instead they return a set() which is made up of: {'RUNNING_MODAL', 'CANCELLED', 'FINISHED', 'PASS_THROUGH'}. Common return values are {'FINISHED'} and {'CANCELLED'}.

Operator Property

https://docs.blender.org/api/blender_python_api_2_65_5/info_tutorial_addon.html#operator-property

Example:

# moved assignment from execute() to the body of the class...
total = bpy.props.IntProperty(name="Steps", default=2, min=1, max=100)
 
# and this is accessed on the class
# instance within the execute() function as...
self.total

How to Make Meshes with Python in Blender

from https://youtu.be/mljWBuj0Gho (Curtis Holt)

so = bpy.context.active_object
verts = so.data.vertices
edges = so.data.edges
faces = so.data.polygons
# coordinates:
# verts[i].co
for v in verts:
   print(v.co)
# edges[i].vertices[j]
for e in edges:
  # v is an index, 0 - N
  for v in e.vertices:
    print(v)

Creating a mesh

import bpy
 
verts = []
edges = []
faces = []
 
verts.append([ #index 0
  0.0, #x
  1.0, #y
  0.0 #z
])
verts.append([ #index 1
  1.0, #x
  0.0, #y
  0.0 #z
])
verts.append([ #index 2
  0.0, #x
  0.0, #y
  1.0 #z
])
verts.append([ #index 3
  -1.0, #x
  0.0, #y
  0.0 #z
])
 
# values are indexes
edges.append([0,1]) # but this isn't needed, because edges are added automatically
 
# values are index of vertexes
faces.append([0,1,2])
faces.append([2,0,3])
# The following works as long as there is a Collection called "Objects"
 
name = "New Object"
mesh = bpy.data.meshes.new(name)
obj = bpy.data.objects.new(name, mesh) # Object contains meta data, not just the mesh
col = bpy.data.collections.get("Objects")
col.objects.link(obj) # This will make the object actually display
bpy.context.view_layer.objects.active = obj # makes the object active (isn't working for me)
mesh.from_pydata(verts, edges, faces) # actually makes the mesh
blender/python/addonnotes/start.1644260059.txt.gz · Last modified: 2022/02/07 18:54 by dwheele