码迷,mamicode.com
首页 > 其他好文 > 详细

The Bpy Module

时间:2020-07-19 15:54:00      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:form   function   different   des   action   obj   scripts   object   point   

  1. Programmatically Selecting Objects
import bpy

def mySelector(objName, additive=False):

	# By default, clear other selections
	if not additive:
		bpy.ops.object.select_all(action=‘DESELECT‘)

	# Set the ‘select‘ property of the datablock to True
	bpy.data.objects[objName].select_set(True)

# Select only ‘Cube‘
mySelector(‘Cube‘)

# Select ‘Sphere‘, keeping other selections
mySelector(‘Sphere‘, additive=True)

# Translate selected objects 1 unit along the x-axis
bpy.ops.transform.translate(value=(1, 0, 0))
  1. Minimal Toolkit for Creation and Transformation(ut.py)
import bpy

# Selecting objects by name
def select(objName):
	bpy.ops.object.select_all(action=‘DESELECT‘)
	bpy.data.objects[objName].select_set(True)

# Activating objects by name
def activate(objName):
	bpy.context.scene.objects.active = bpy.data.objects[objName]

class sel:
	"""Function Class for operation on SELECTED objects"""

	# Differential
	def translate(v):
		bpy.ops.transform.translate(
			value=v, constraint_axis=(True, True, True))

	# Differential
	def scale(v):
		bpy.ops.transform.resize(
			value=v, constraint_axis=(True, True, True))

	# Differential
	def rotate_x(v):
		bpy.ops.transform.rotate(value=v, orient_axis=‘X‘)

	# Differential
	def rotate_y(v):
		bpy.ops.transform.rotate(value=v, orient_axis=‘Y‘)

	# Differential
	def rotate_z(v):
		bpy.ops.transform.rotate(value=v, orient_axis=‘Z‘)

class act:
	"""Function Class for operation on ACTIVE objects"""

	# Declarative
	def location(v):
		bpy.context.object.location = v

	# Declarative
	def scale(v):
		bpy.context.object.scale = v

	# Declarative
	def rotation(v):
		bpy.context.object.rotation_euler = v

	# Rename the active object
	def rename(objName):
		bpy.context.object.name = objName

class spec:
	"""Function Class for operation on SPECIFIED objects"""

	# Declarative
	def scale(objName, v):
		bpy.data.objects[objName].scale = v

	# Declarative
	def location(objName, v):
		bpy.data.objects[objName].location = v

	# Declarative
	def roration(objName, v):
		bpy.data.objects[objName].rotation_euler = v

class create:
	"""Function Class for CREATING Objects"""

	def cube(objName):
		bpy.ops.mesh.primitive_cube_add(size=0.5, location=(0, 0, 0))
		act.rename(objName)

	def sphere(objName):
		bpy.ops.mesh.primitive_uv_sphere_add(radius=0.5, location=(0, 0, 0))
		act.rename(objName)

	def cone(objName):
		bpy.ops.mesh.primitive_cone_add(radius1=0.5, location=(0, 0, 0))
		act.rename(objName)

	# Delete an object by name
	def delete(objName):

		select(objName)
		bpy.ops.object.delete(use_global=False)

	# Delete all objects
	def delete_all():

		if(len(bpy.data.objects) != 0):
			bpy.ops.object.select_all(action=‘SELECT‘)
			bpy.ops.object.delete(use_global=False)

if __name__ == "__main__":

	# Create a cube
	create.cube(‘PerfectCube‘)

	# Differential transformations combine
	sel.translate((0, 1, 2))

	sel.scale((1, 1, 2))
	sel.scale((0.5, 1, 1))

	sel.rotate_x(3.1415 / 8)
	sel.rotate_x(3.1415 / 7)

	sel.rotate_z(3.1415 / 3)

	# Create a cone
	create.cone(‘PointyCone‘)

	# Declarative transformations overwrite
	act.location((-2, -2, 0))
	spec.scale(‘PointyCone‘, (1.5, 2.5, 2))

	# Create a Sphere
	create.sphere(‘SmoothSphere‘)

	# Declarative transformations overwrite
	spec.location(‘SmoothSphere‘, (2, 0, 0))
	act.rotation((0, 0, 3.1415 / 3))
	act.scale((1, 3, 1))
  1. Visualizing Multivariate Data with the Minimal Toolkit
import sys
sys.path.append("/home/zhuhaoneng/Documents/blender-scripts")
import ut

# Will reload the module from the live script of ut.py

import importlib
importlib.reload(ut)
import csv
import urllib.request

###################
# Reading in Data #
###################

# Read iris.csv from file respository
url_str = ‘http://blender.chrisconlan.com/iris.csv‘
iris_csv = urllib.request.urlopen(url_str)
iris_ob = csv.reader(iris_csv.read().decode(‘utf-8‘).splitlines())

# Store header as list, and data as list of lists
iris_header = []
iris_data = []

for v in iris_ob:
	if not iris_header:
		iris_header = v
	else:
		v = [float(v[0]),
			 float(v[1]),
			 float(v[2]),
			 float(v[3]),
			 str(v[4])]
		iris_data.append(v)

# Columns:
# ‘Sepal.Length‘, ‘Sepal.Width‘,
# ‘Petal.Length‘, ‘Petal.Width‘, ‘Species‘

# Visualize 3 dimensions
# Sepal.Length, Sepal.Width, and ‘Petal.Length‘

# Clear scene
ut.delete_all()

# Place data
for i in range(0, len(iris_data)):
	ut.create.sphere(‘row-‘ + str(i))
	v = iris_data[i]
	ut.act.scale((0.25, 0.25, 0.25))
	ut.act.location((v[0], v[1], v[2]))

# Columns:
# ‘Sepal.Length‘, ‘Sepal.Width‘,
# ‘Petal.Length‘, ‘Petal.Width‘, ‘Species‘

# Visualize 4 dimensions
# Sepal.Length, Sepal.Width, ‘Petal.Length‘,
# and scale the object by a factor of ‘Petal.Width‘

# Clear scene
ut.delete_all()

#Place data
for i in range(0, len(iris_data)):
	ut.create.sphere(‘row-‘ + str(i))
	v = iris_data[i]
	scale_factor = 0.2
	ut.act.scale((v[3] * scale_factor,) * 3)
	ut.act.location((v[0], v[1], v[2]))

# Columns:
# ‘Sepal.Length‘, ‘Sepal.Width‘,
# ‘Petal.Length‘, ‘Petal.Width‘, ‘Species‘

# Visualize 5 dimensions
# Sepal.Length, Sepal.Width, ‘Petal.Length‘,
# and scale the object by a factor of ‘Petal.Width‘
# setosa = sphere, versicolor = cube, virginica = cone

# Clear scene
ut.delete_all()

# Place data
for i in range(0, len(iris_data)):

	v = iris_data[i]

	if v[4] == ‘setosa‘:
		ut.create.sphere(‘setosa-‘ + str(i))
	if v[4] == ‘versicolor‘:
		ut.create.cube(‘versicolor-‘ + str(i))
	if v[4] == ‘virginica‘:
		ut.create.cone(‘virginica-‘ + str(i))

	scale_factor = 0.2
	ut.act.scale((v[3] * scale_factor,) * 3)
	ut.act.location((v[0], v[1], v[2]))

The Bpy Module

标签:form   function   different   des   action   obj   scripts   object   point   

原文地址:https://www.cnblogs.com/jeremy-zhuhn/p/13339323.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!