Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gwens committed May 18, 2017
0 parents commit b4b929e
Show file tree
Hide file tree
Showing 68 changed files with 2,943 additions and 0 deletions.
27 changes: 27 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Freeware License, some rights reserved

Copyright (c) 2017 Chris Conlan

Permission is hereby granted, free of charge, to anyone obtaining a copy
of this software and associated documentation files (the "Software"),
to work with the Software within the limits of freeware distribution and fair use.
This includes the rights to use, copy, and modify the Software for personal use.
Users are also allowed and encouraged to submit corrections and modifications
to the Software for the benefit of other users.

It is not allowed to reuse, modify, or redistribute the Software for
commercial use in any way, or for a user�s educational materials such as books
or blog articles without prior permission from the copyright holder.

The above copyright notice and this permission notice need to be included
in all copies or substantial portions of the software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Apress Source Code

This repository accompanies [*The Blender Python API*](http://www.apress.com/9781484228012) by Chris Conlan (Apress, 2017).

[comment]: #cover


Download the files as a zip using the green button, or clone the repository to your machine using Git.

## Releases

Release v1.0 corresponds to the code in the published book, without corrections or updates.

## Contributions

See the file Contributing.md for more information on how you can contribute to this repository.
3 changes: 3 additions & 0 deletions ch01_code/1-1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bpy.ops.transform.translate(value=(3.05332, 0, 0), constraint_axis=(True, False, False),
constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED',
proportional_edit_falloff='SMOOTH', proportional_size=1, release_confirm=True)
13 changes: 13 additions & 0 deletions ch01_code/1-2.batch
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Assuming you are starting from C:\Users\%USERNAME%
cd Desktop\blender-2.78c-windows64
blender

# Navigating from anywhere on the Windows
# filesystem to Blender on the Desktop
cd C:\Users\%USERNAME%\Desktop\blender-2.78c-windows64
blender

# If an existing Blender install causes
# the wrong version to open, use blender.exe
cd C:\Users\%USERNAME%\Desktop\blender-2.78c-windows64
blender.exe
9 changes: 9 additions & 0 deletions ch01_code/1-3.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Navigating to Blender on the Desktop from
# anywhere in the filesystem for Linux
cd ~/Desktop/blender-2.78c-linux-glibc211-x86_64
./blender


# Navigating to Blender in the home directory for OSX
cd ~/Desktop/blender-2.78c-OSX-10.6-x86_64
./blender
3 changes: 3 additions & 0 deletions ch01_code/1-4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bpy.ops.mesh.primitive_cube_add(radius=1, view_align=False, enter_editmode=False,
location=(0, 0, 0), layers=(True, False, False, False, False, False, False, False,
False, False, False, False, False, False, False, False, False, False, False, False))
2 changes: 2 additions & 0 deletions ch01_code/1-5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Make a bigger cube sitting in the first quadrant
bpy.ops.mesh.primitive_cube_add(radius=3, location=(5, 5, 5))
6 changes: 6 additions & 0 deletions ch01_code/1-6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import bpy

for k in range(5):
for j in range(5):
for i in range(5):
bpy.ops.mesh.primitive_cube_add(radius=0.25, location=(i, j, k))
2 changes: 2 additions & 0 deletions ch02_code/2-1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Outputs bpy.data.objects datablocks
bpy.context.selected_objects
27 changes: 27 additions & 0 deletions ch02_code/2-10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import ut
import csv
import urllib.request

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

# Read iris.csv from file repository
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)
16 changes: 16 additions & 0 deletions ch02_code/2-11.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 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]))
18 changes: 18 additions & 0 deletions ch02_code/2-12.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 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]))
27 changes: 27 additions & 0 deletions ch02_code/2-13.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 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]))
6 changes: 6 additions & 0 deletions ch02_code/2-2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Return the names of selected objects
[k.name for k in bpy.context.selected_objects]

# Return the locations of selected objects
# (location of origin assuming no pending transformations)
[k.location for k in bpy.context.selected_objects]
20 changes: 20 additions & 0 deletions ch02_code/2-3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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 = 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))
9 changes: 9 additions & 0 deletions ch02_code/2-4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Returns bpy.data.objects datablock
bpy.context.object

# Longer synonym for the above line
bpy.context.active_object

# Accessing the 'name' and 'location' values of the datablock
bpy.context.object.name
bpy.context.object.location
16 changes: 16 additions & 0 deletions ch02_code/2-5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import bpy

def myActivator(objName):

# Pass bpy.data.objects datablock to scene class
bpy.context.scene.objects.active = bpy.data.objects[objName]


# Acivate the object named 'Sphere'
myActivator('Sphere')

# Verify the 'Sphere' was activated
print("Active object:", bpy.context.object.name)

# Selected objects were unaffected
print("Selected objects:", bpy.context.selected_objects)
5 changes: 5 additions & 0 deletions ch02_code/2-6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# bpy.data.objects datablock for an object named 'Cube'
bpy.data.objects['Cube']

# bpy.data.objects datablock for an object named 'eyeballSphere'
bpy.data.objects['eyeballSphere']
15 changes: 15 additions & 0 deletions ch02_code/2-7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import bpy

def mySpecifier(objName):
# Return the datablock
return bpy.data.objects[objName]

# Store a reference to the datablock
myCube = mySpecifier('Cube')

# Output the location of the origin
print(myCube.location)

# Works exactly the same as above
myCube = bpy.data.objects['Cube']
print(myCube.location)
18 changes: 18 additions & 0 deletions ch02_code/2-8.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Each line will return the same object type and memory address
bpy.data
bpy.data.objects.data
bpy.data.objects.data.objects.data
bpy.data.objects.data.objects.data.objects.data

# References to the same object can be made across datablock types
bpy.data.meshes.data
bpy.data.meshes.data.objects.data
bpy.data.meshes.data.objects.data.scenes.data.worlds.data.materials.data


# Different types of datablocks also nest
# Each of these lines returns the bpy.data.meshes datablock for 'Cube'
bpy.data.meshes['Cube']
bpy.data.objects['Cube'].data
bpy.data.objects['Cube'].data.vertices.data
bpy.data.objects['Cube'].data.vertices.data.edges.data.materials.data
Loading

0 comments on commit b4b929e

Please sign in to comment.