/scenes/scenekit-demo.py

https://github.com/tdamdouni/Pythonista · Python · 57 lines · 48 code · 7 blank · 2 comment · 0 complexity · 6ca6b56fdd4734008b71b1dbde74e761 MD5 · raw file

  1. # coding: utf-8
  2. # https://forum.omz-software.com/topic/1686/3d-in-pythonista/7
  3. from objc_util import *
  4. import ui
  5. import math
  6. SCNView, SCNScene, SCNBox, SCNText, SCNNode, SCNLight, SCNAction, UIFont = map(ObjCClass, ['SCNView', 'SCNScene', 'SCNBox', 'SCNText', 'SCNNode', 'SCNLight', 'SCNAction', 'UIFont'])
  7. class SCNVector3 (Structure):
  8. _fields_ = [('x', c_float), ('y', c_float), ('z', c_float)]
  9. @on_main_thread
  10. def demo():
  11. main_view = ui.View()
  12. main_view_objc = ObjCInstance(main_view)
  13. scene_view = SCNView.alloc().initWithFrame_options_(((0, 0),(400, 400)), None).autorelease()
  14. scene_view.setAutoresizingMask_(18)
  15. scene_view.setAllowsCameraControl_(True)
  16. scene = SCNScene.scene()
  17. root_node = scene.rootNode()
  18. text_mesh = SCNText.textWithString_extrusionDepth_('@tdamdouni', 6.0)
  19. text_mesh.setFlatness_(0.2)
  20. text_mesh.setChamferRadius_(0.4)
  21. text_mesh.setFont_(UIFont.fontWithName_size_('HelveticaNeue-Bold', 18))
  22. bbox_min, bbox_max = SCNVector3(), SCNVector3()
  23. text_mesh.getBoundingBoxMin_max_(byref(bbox_min), byref(bbox_max), restype=None, argtypes=[POINTER(SCNVector3), POINTER(SCNVector3)])
  24. text_width = bbox_max.x - bbox_min.x
  25. text_node = SCNNode.nodeWithGeometry_(text_mesh)
  26. text_node.setCastsShadow_(True)
  27. text_container = SCNNode.node()
  28. text_container.addChildNode_(text_node)
  29. text_container.setPosition_((0, 40, 0))
  30. text_node.setPosition_((-text_width/2, 0, 0))
  31. box = SCNBox.boxWithWidth_height_length_chamferRadius_(100, 4, 100, 1)
  32. box_node = SCNNode.nodeWithGeometry_(box)
  33. root_node.addChildNode_(box_node)
  34. rotate_action = SCNAction.repeatActionForever_(SCNAction.rotateByX_y_z_duration_(0, math.pi*2, math.pi*2, 10))
  35. text_container.runAction_(rotate_action)
  36. root_node.addChildNode_(text_container)
  37. light_node = SCNNode.node()
  38. light_node.setPosition_((0, 100, 10))
  39. light_node.setRotation_((1, 0, 0, -math.pi/2))
  40. light = SCNLight.light()
  41. light.setType_('spot')
  42. light.setCastsShadow_(True)
  43. light.setColor_(UIColor.cyanColor().CGColor())
  44. light_node.setLight_(light)
  45. root_node.addChildNode_(light_node)
  46. scene_view.setScene_(scene)
  47. main_view_objc.addSubview_(scene_view)
  48. main_view.name = 'SceneKit Demo'
  49. main_view.present()
  50. demo()