Showing posts with label Scripting. Show all posts
Showing posts with label Scripting. Show all posts

Friday, December 7, 2012

Python Smoothing

This tutorial entry will show how to script in the sub-surface modifier and the smooth_shader operator.  Note that the following code would be used within an Operator.  For simply performing these operations on an already selected object, see the Python Macros entry.

# add a subsurf modifier, setting level to 2
ssMod = obj.modifiers.new("MyName", type='SUBSURF')
ssMod.levels = 2

# set a selected active object (operations must be in this order)
obj.select = True
bpy.context.scene.objects.active = obj

# add smooth shading
bpy.obs.object.shade_smooth()

Scripting Externally

There are a handful of reasons not to script using the internal script editor built into Blender.  One of the main reasons is undo-ing.  If you undo, something in your scene while testing, it will also undo the last set of edits you made to your script.  This may not be obvious and you'll find yourself re-typing scripts sections over and over again.

Helper Script
The suggested method is to edit your scripts externally, and use the following script to run it in your Blender scene.

import bpy
import os

filename = os.path.join(os.path.dirname(bpy.data.filepath), "my-script-name.py")

exec(compile(open(filename).read(), filename, 'exec'))

Friday, November 30, 2012

Basic Operator

This entry covers scripting a basic Operator in Blender.  An operator essentially is an operation which can be performed in/on your project.  Some existing examples are Smooth Shading, or moving objects in the scene. The move operator is one of many ways of integrating custom functionality (or add-ons) into Blender.

For this example, I'll just show a conventional "Hello World" approach.  Below is an example of all the basic code required for a custom operator.



Monday, November 12, 2012

Python Macros

This entry covers the creation of Python Macros.  These are basically the same as python scripts, however these are much simpler scripts that encapsulate actions.    In this example we'll show how you can combine the action of adding a Sub-Surface Modifier along with the Smooth Shader operation.  An example macro can be seen below.

Saturday, November 10, 2012

Python Interpreter

This entry will be the starting point for any and all Python Scripting in Blender.  What's nice is that Blender also installs a full fledged Python interpreter/compiler so you can test regular python scripts (not just Blender ones) in this environment.

Blender has a view dedicated to Python Scripting which can be found by [Ctrl + Left] or by selecting Scripting from the drop down menu at the top.  The scripting view looks like the image below.