Skip to content

Instantly share code, notes, and snippets.

@iRi-E
Created November 10, 2015 14:02
Show Gist options
  • Save iRi-E/5ed5451046aae27f6880 to your computer and use it in GitHub Desktop.
Save iRi-E/5ed5451046aae27f6880 to your computer and use it in GitHub Desktop.
Blender script that displays a stock price on 3D viewport
import bpy, blf
import re, urllib.request
def getPrice(stockNum):
url = 'http://stocks.finance.yahoo.co.jp/stocks/detail/?code={}.T'.format(stockNum)
price = ''
name = ''
with urllib.request.urlopen(url) as f:
for line in f.readlines():
text = line.decode('UTF-8')
match = re.search('"stoksPrice">(.*?)<', text)
if match:
price = match.group(1)
match = re.search('"symbol"><h1>(.*?)<', text)
if match:
name = match.group(1)
if price and name:
break
return price, name
def drawPrice(price, label, font_id, x, y, size):
blf.position(font_id, x, y, 0)
blf.size(font_id, size, 72)
blf.draw(font_id, "{}: {}".format(label, price))
class RenderPriceHUD(bpy.types.Operator):
bl_idname = 'view3d.stock_price_hud'
bl_label = 'Display Render Times'
last_activity = 'NONE'
_handle = None
_price = ''
_name = ''
enable = bpy.props.BoolProperty(default=True)
co_code = bpy.props.IntProperty(default=4689) # defaults Yahoo! Japan
font_id = bpy.props.IntProperty(default=0)
x = bpy.props.IntProperty(default=400)
y = bpy.props.IntProperty(default=0)
size = bpy.props.IntProperty(default=16)
@staticmethod
def handle_add(self, context):
RenderPriceHUD._handle = bpy.types.SpaceView3D.draw_handler_add(
drawPrice, (self._price, self._name, self.font_id,
self.x, self.y, self.size), 'WINDOW', 'POST_PIXEL')
@staticmethod
def handle_remove():
if RenderPriceHUD._handle is not None:
bpy.types.SpaceView3D.draw_handler_remove(RenderPriceHUD._handle, 'WINDOW')
RenderPriceHUD._handle = None
def execute(self, context):
self.handle_remove()
if self.enable:
self._price, self._name = getPrice(self.co_code)
self.handle_add(self, context)
for area in context.window.screen.areas:
if area.type == 'VIEW_3D':
area.tag_redraw()
return {'FINISHED'}
if __name__ == '__main__':
bpy.utils.register_class(RenderPriceHUD)
bpy.ops.view3d.stock_price_hud()
# To update the price, run the following command in the Python console:
#
# >>> bpy.ops.view3d.stock_price_hud()
#
# Disable the HUD:
#
# >>> bpy.ops.view3d.stock_price_hud(enable=False)
#
# Change the company to display (ex. Nintendo):
#
# >>> bpy.ops.view3d.stock_price_hud(co_code=7974)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment