├── Arduino └── BlenderController.ino ├── Blender ├── BlenderController.blend └── BlenderController.py ├── README.md ├── Screenshot.png ├── Video.gif └── doc └── data.json /Arduino/BlenderController.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | Servo s1, s2; 4 | 5 | void setup(){ 6 | Serial.begin(115200); 7 | Serial.flush(); 8 | s1.attach(9); 9 | s2.attach(8); 10 | } 11 | 12 | String readString(){ 13 | String inString =""; 14 | char inChar; 15 | while(Serial.available()>0){ 16 | inChar =(char) Serial.read(); 17 | inString+=inChar; 18 | delay(1); 19 | } 20 | return inString; 21 | } 22 | 23 | int* parseString(String msg){ 24 | static int a[2]; 25 | int commaIndex = msg.indexOf(','); 26 | a[0] = msg.substring(0, commaIndex).toInt(); 27 | a[1] = msg.substring(commaIndex+1).toInt(); 28 | return a; 29 | } 30 | 31 | 32 | 33 | void writeValues(int b[]){ 34 | if(b[0]<20)b[0]=20; 35 | if(b[0]>150)b[0]=150; 36 | if(b[1]<40)b[1]=40; 37 | if(b[1]>160)b[1]=160; 38 | 39 | //s1.write(b[0]); 40 | s1.write(map(b[0],0,180,150,20)); 41 | s2.write(map(b[1],0,180,40,160)); 42 | Serial.print(b[0]); 43 | Serial.print(" "); 44 | Serial.println(b[1]); 45 | } 46 | 47 | 48 | void loop(){ 49 | //Serial.print("a"); 50 | if(Serial.available()){ 51 | String incoming=readString(); 52 | int *angles=parseString(incoming); 53 | writeValues(angles); 54 | } 55 | } -------------------------------------------------------------------------------- /Blender/BlenderController.blend: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alvaroferran/BlenderController/005a9785bf3215863d44514f0de73f4d65c8aee3/Blender/BlenderController.blend -------------------------------------------------------------------------------- /Blender/BlenderController.py: -------------------------------------------------------------------------------- 1 | import bpy 2 | import math 3 | import time 4 | 5 | import sys 6 | import serial 7 | import glob 8 | 9 | port=''.join(glob.glob("/dev/ttyUSB*")) 10 | ser = serial.Serial(port,115200) 11 | print("connected to: " + ser.portstr) 12 | 13 | 14 | ob = bpy.data.objects['Armature'] 15 | bpy.context.scene.objects.active = ob 16 | 17 | bpy.ops.object.mode_set(mode='POSE') 18 | 19 | offset1=30 20 | offset2=140 21 | 22 | 23 | 24 | def get_local_orientation(pose_bone): 25 | local_orientation = pose_bone.matrix_channel.to_euler() 26 | if pose_bone.parent is None: 27 | return local_orientation 28 | else: 29 | x=local_orientation.x-pose_bone.parent.matrix_channel.to_euler().x 30 | y=local_orientation.y-pose_bone.parent.matrix_channel.to_euler().y 31 | z=local_orientation.z-pose_bone.parent.matrix_channel.to_euler().z 32 | return(x,y,z) 33 | 34 | 35 | def sendAngles(): 36 | 37 | bone1=ob.pose.bones['Link1IK'] 38 | bone2=ob.pose.bones['Link2IK'] 39 | 40 | angle1=str(round(math.degrees(get_local_orientation(bone1)[2])+offset1))#[0]=x,[1]=y,[2]=z 41 | angle2=str(round(math.degrees(get_local_orientation(bone2)[2])+offset2)) 42 | 43 | print( "%s %s \n" %( angle1, angle2 ) ) 44 | 45 | ser.write((angle1+','+angle2).encode('UTF-8')) 46 | 47 | 48 | 49 | 50 | 51 | def frameChange(passedScene): 52 | 53 | sendAngles() 54 | 55 | bpy.app.handlers.frame_change_pre.append(frameChange) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # BlenderController 2 | 3 | 4 |
5 | 6 |
7 | 8 |
9 | 10 | This repo shows how to control a robot from blender using python. 11 | To execute the controller first upload the arduino code into the board. Then in the blender file, press on "Run Script", then the play icon and finally hover the mouse over the control bone and press the "G" key. 12 |
13 | 14 |

15 | 16 |

17 | The arm used in this project is available here: 18 | 19 | 20 |

21 | This project is licensed under Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) 22 | 23 | 24 | 25 |

26 | 27 |

28 | -------------------------------------------------------------------------------- /Screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alvaroferran/BlenderController/005a9785bf3215863d44514f0de73f4d65c8aee3/Screenshot.png -------------------------------------------------------------------------------- /Video.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alvaroferran/BlenderController/005a9785bf3215863d44514f0de73f4d65c8aee3/Video.gif -------------------------------------------------------------------------------- /doc/data.json: -------------------------------------------------------------------------------- 1 | { 2 | "image": "Screenshot.png", 3 | "tags": ["robotics", "blender", "python","servo"] 4 | } 5 | --------------------------------------------------------------------------------