Panjea Python Tutorials

https://gitlab.com/hartsantler/pyjea
Not Another PyGame Tutorial.

Most other Python tutorials try to teach you how use Python for general programming, several libraries, or PyGame. We are skipping PyGame, and moving you quickly into Blender scripting, where you can generate graphics, test logic, and export to a real game engine.

Dive Into Python

If you are new to Python, its good to get started with the basics learning about: variables, functions, dicts, lists, and strings. Below are the minimal links you should read to have a basic understanding of these concepts.

The Terminal


These tutorials must be run from the command line terminal. On Apple OSX, search for terminal, python2 is installed by default, so you can run: python tutorial1.py. Note that these tutorials work best with Python3, which you can install by running: brew install python3. Note on OSX do not replace Python2 with Python3, later we will be using the PanjeaPlugin, which requires Python2.
On MS Windows10 we recommend you use WSL (Windows Subsystem for Linux) and install Ubuntu, then from the WSL terminal, run: sudo apt-get install python3

Lesson 1 - Dictionary

A dictionary is a key to value pair mapping, you can get or set an item in a dictionary using the brackets DICT[KEY]


lesson1.py

line 12: symbols={0:"🌲",1:"🌳",2:"🌴",3:"🌵",4:"🌿",5:"🍀",6:"🍁",7:"🌾",8:"🌻",9:"🌺"}
symbols is a global variable, a dictionary mapping, it maps line numbers to unicode symbols. This dictionary is used on line 23, in the drawbackground function.

Lesson 2 - Strings

Strings are made of letters and words, they are enclosed in quotes, and a single letter can be index by a number using brackets STRING[N]


lesson2.py

line 24: faces = "😁😂😆😝"
faces is a global variable, a string, this holds the four frames of animation for the NPCs (non-playable-characters). The animation frames are updated starting at line 72, using random.random() a frame is picked at random.

Lesson 3 - Threads

Threads allow for multiple loops of logic to run at the same time. Normally you should avoid using them, as they are only required in some cases when polling hardware devices, or network IO.


lesson3.py

line 111: threading._start_new_thread(redrawloop,tuple([]))
threading is a python module, using _start_new_thread allows for running a secondary loop that will not block our main loop. This is used so that the background can update and display animation without the user having to give keyboard input. Note that tuple([]) creates an empty tuple, this is required because the redrawloop function takes no arguments.

Lesson 4 - ASCII Art

lesson4.py
			Mario = [
				'     RRRRR      ',
				'    RRRRRRRRR   ',
				'    HHH--0-     ',
				'   H-H---0---   ',
				'   H-HH---0---- ',
				'   HH----00000  ',
				'     --------   ',
				'  RRRRBBRRR     ',
				'--RRRRBBBRRRR---',
				'--- RRBYBBBBRR--',
				'-- BBBBBBBBBB   ',
				'  BBBBBBBBBBBB  ',
				' BBBBB    BBBB  ',
				'HHBBB      BBB  ',
				'HHHH       HHHH ',
				' HHHHH     HHHHH',
			]
			

The easy way to print a fake pixel into a terminal is to use the full-block symbol "█". Then to color each fake pixel, we use terminal escape codes. See below how each line in the Mario ASCII art list is looped over, with each letter being replaced by the escape code.

			for ln in Mario:
				ln = ln.replace( '0', ECODE % BLACK )
				ln = ln.replace( 'R', ECODE % RED )
				ln = ln.replace( 'B', ECODE % BLUE )
				ln = ln.replace( '-', ECODE % YELLOW )
				ln = ln.replace( 'H', ECODE % GRAY )
				ln = ln.replace( 'Y', ECODE % WHITE )
				print( offset + ln)
			

Lesson 5 - Terminal Tricks


lesson5.py

Inorder to redraw the background and take keyboard input without requiring the user press the enter key, we must modify the terminal settings using the modules: tty and termios.

			fd = sys.stdin.fileno()
			old_settings = termios.tcgetattr(fd)
			tty.setraw(fd)
			

Above sets the terminal into a raw processing mode, where the OS will not buffer anything, and instead directly pass all key events to the program. Note: lesson5 requires Linux or Apple OSX


Dive into Blender

Note: you need to install Blender and the PanjeaPlugin to run the following examples.
Note: the PanjeaPlugin requires: PyGame, PyODE, and PyQt5
pip install pygame pyode pyqt5

Lesson 6 - Cubes


lesson6.py

To create a cube in Blender, the command is:
bpy.ops.mesh.primitive_cube_add()
Then to get a reference to the cube:
cube = bpy.context.active_object

Now that we have a single cube, we can loop over the Mario ASCII drawing, and replace each character with a cube.
ob = bpy.data.objects.new(name='mario', object_data=cube.data.copy() )
bpy.context.collection.objects.link(ob)
Note that bpy is the Blender Python module, this allows you to call blender operators, and reference data inside of Blender, like: objects, materials, lights, etc.

Lesson 7 - Physics

This lesson requires you have a dual analog gamepad.

lesson7.py
			root.use_ode_physics = True
			root.use_ode_gamepad = True
			root.gamepad_lin_force_x = -50
			root.gamepad_lin_force_z = -50
			

The code above enables ODE Physics and sets the gamepad to apply linear force to the root object. The root object is the parent of all the cubes.

Lesson 8 - Grease Pencil



lesson8.py
			gmat = bpy.data.materials.new(name='volume')
			bpy.data.materials.create_gpencil_data(gmat)
			gmat.grease_pencil.show_fill = True
			gmat.grease_pencil.fill_color = [0.1,0.1,0.8, 0.01]
			

The code above makes a grease pencil material that is very transparent (0.01). Because we are making 360 twisted circles that overlap, this produces a volumetric effect.