The forum for the two workshops "Bio Visualisation with Blender and MembraneEditor" (28.02.-01.03.2018 and 25.-26.02.2019) for "Konstanz Research School Chemical Biology", presented by Mehmood Ghaffar and Bjorn Sommer.
Post Reply
mghaffar
Posts: 2
Joined: 15.10.2014, 10:15

Blender Python Scripts

Post by mghaffar » 25.02.2018, 23:12

Part 1: Create a dummy User Interface in Blender

Code: Select all

import bpy
import sys

class UIPanel(bpy.types.Panel):
	bl_label = 'Example Panel'
	bl_space_type = "VIEW_3D" #Defines the location of Panel
	bl_region_type = "UI"
	
	def draw(self, context):
		layout = self.layout
		
# To register UIPanel
bpy.utils.register_module(__name__)
Part 2: Create a dummy User Interface with Button in Blender

Code: Select all

import bpy
import sys

class UIPanel(bpy.types.Panel):
	bl_label = 'Example Panel'
	bl_space_type = "VIEW_3D" #Defines the location of Panel
	bl_region_type = "UI"
	
	def draw(self, context):
		layout = self.layout
		row = layout.row()
		row.label ('This is your first row')
		row = layout.row()
		row.operator('hello.hello')

class OBJECT_OT_HelloButton(bpy.types.Operator):
	bl_idname = "hello.hello" #reference in code
	bl_label = "Say Hello"  # text appear on the button
		
	def execute(self, context):
		# Define some actions here
		return {'FINISHED'}

# To register UIPanel
bpy.utils.register_module(__name__)
Part 3: A simple functional script

Code: Select all

import bpy
import sys

# store scene objects to a variable object
object = bpy.context.scene.objects

def translate_XYZ(*argv): # Translate along xyz axes
    if object.active:
        bpy.ops.transform.translate(value=(argv))

class UIPanel(bpy.types.Panel):
    bl_label = 'Example Panel'
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"
    
    def draw(self, context):
        layout = self.layout
        
        row = layout.row() # First line in the Panel
        row.label ('Move LEFT / RIGHT')
        row = layout.row()
        row.operator('translate.n_x')
        row.operator('translate.p_x')
        
        row = layout.row() # Second line in the Panel
        row.label('Move Forward / Backward')
        
        row = layout.row() # First line in the Panel
        row.operator('translate.p_y')
        row.operator('translate.n_y')
        
        #Task 
        #for up and down code snippet here!
        

class Translate_PX(bpy.types.Operator): # move along +X Axis
    bl_idname = "translate.p_x"
    bl_label = "RIGHT"
    name = bpy.props.StringProperty()
    def execute(self, context):
        translate_XYZ(1,0,0)
        return{'FINISHED'}
   
class Translate_PY(bpy.types.Operator): # move along +Y Axis
    bl_idname = "translate.p_y"
    bl_label = "FORWARD"
    name = bpy.props.StringProperty()
    def execute(self, context):
        translate_XYZ(0,1,0)
        return{'FINISHED'}    
 
class Translate_NX(bpy.types.Operator): # move along -X Axis
    bl_idname = "translate.n_x"
    bl_label = "LEFT"
    name = bpy.props.StringProperty()
    def execute(self, context):
        translate_XYZ(-1,0,0)
        return{'FINISHED'}
   
class Translate_NY(bpy.types.Operator): # move along -Y Axis
    bl_idname = "translate.n_y"
    bl_label = "BACKWARD"
    name = bpy.props.StringProperty()
    def execute(self, context):
        translate_XYZ(0,-1,0)
        return{'FINISHED'}
    
#Task 
#for up and down code snippet here!
        
bpy.utils.register_module(__name__)

Post Reply

Return to “Bio Visualisation with Blender & MembraneEditor 2018-19”