﻿<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ja">
	<id>https://wiki.blender.jp/index.php?action=history&amp;feed=atom&amp;title=%E5%88%A9%E7%94%A8%E8%80%85%3AZ0r%2FPyDevAndProfiling</id>
	<title>利用者:Z0r/PyDevAndProfiling - 版の履歴</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.blender.jp/index.php?action=history&amp;feed=atom&amp;title=%E5%88%A9%E7%94%A8%E8%80%85%3AZ0r%2FPyDevAndProfiling"/>
	<link rel="alternate" type="text/html" href="https://wiki.blender.jp/index.php?title=%E5%88%A9%E7%94%A8%E8%80%85:Z0r/PyDevAndProfiling&amp;action=history"/>
	<updated>2026-05-12T12:03:48Z</updated>
	<subtitle>このウィキのこのページに関する変更履歴</subtitle>
	<generator>MediaWiki 1.31.0</generator>
	<entry>
		<id>https://wiki.blender.jp/index.php?title=%E5%88%A9%E7%94%A8%E8%80%85:Z0r/PyDevAndProfiling&amp;diff=105613&amp;oldid=prev</id>
		<title>Yamyam: 1版 をインポートしました</title>
		<link rel="alternate" type="text/html" href="https://wiki.blender.jp/index.php?title=%E5%88%A9%E7%94%A8%E8%80%85:Z0r/PyDevAndProfiling&amp;diff=105613&amp;oldid=prev"/>
		<updated>2018-06-28T19:44:32Z</updated>

		<summary type="html">&lt;p&gt;1版 をインポートしました&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;ja&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;← 古い版&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;2018年6月28日 (木) 19:44時点における版&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-notice&quot; lang=&quot;ja&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(相違点なし)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Yamyam</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.blender.jp/index.php?title=%E5%88%A9%E7%94%A8%E8%80%85:Z0r/PyDevAndProfiling&amp;diff=105612&amp;oldid=prev</id>
		<title>wiki&gt;Ideasman42: moved User:Z0r to User:Z0r/PyDevAndProfiling</title>
		<link rel="alternate" type="text/html" href="https://wiki.blender.jp/index.php?title=%E5%88%A9%E7%94%A8%E8%80%85:Z0r/PyDevAndProfiling&amp;diff=105612&amp;oldid=prev"/>
		<updated>2012-11-11T05:47:41Z</updated>

		<summary type="html">&lt;p&gt;moved &lt;a href=&quot;/%E5%88%A9%E7%94%A8%E8%80%85:Z0r&quot; title=&quot;利用者:Z0r&quot;&gt;User:Z0r&lt;/a&gt; to &lt;a href=&quot;/%E5%88%A9%E7%94%A8%E8%80%85:Z0r/PyDevAndProfiling&quot; title=&quot;利用者:Z0r/PyDevAndProfiling&quot;&gt;User:Z0r/PyDevAndProfiling&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新規ページ&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Using PyDev for Blender Script Development ==&lt;br /&gt;
&lt;br /&gt;
[http://pydev.org/ PyDev] is an extension for [http://www.eclipse.org/ Eclipse] that adds support for editing Python code. It's a good way to develop scripts for Blender and the Blender Game Engine: it has syntax highlighting, code completion (to some extent), and an interactive debugger.&lt;br /&gt;
&lt;br /&gt;
After installing a minimal version of Eclipse (the standard &amp;quot;Eclipse IDE for Java Developers&amp;quot; will do), you will need to [http://pydev.org/manual_101_install.html install PyDev].&lt;br /&gt;
&lt;br /&gt;
=== Configuring PyDev for Game Development ===&lt;br /&gt;
&lt;br /&gt;
First, set up your directory structure. It should look something like this:&lt;br /&gt;
&lt;br /&gt;
  MyGame/&lt;br /&gt;
    Game/&lt;br /&gt;
      *.blend&lt;br /&gt;
      src/&lt;br /&gt;
        __init__.py&lt;br /&gt;
        *.py&lt;br /&gt;
      textures/&lt;br /&gt;
      audio/&lt;br /&gt;
      etc/&lt;br /&gt;
&lt;br /&gt;
The nesting of the ''MyGame'', ''Game'' and ''src'' directories are important, but you can name them whatever you like. When you start Eclipse, it will ask you which workspace to use; choose the ''MyGame'' directory. The ''Game'' directory will be known to Eclipse as your project.&lt;br /&gt;
&lt;br /&gt;
Before importing your project, make sure you have a Python 3 interpreter configured in PyDev. In choose ''Window &amp;gt; Preferences &amp;gt; Pydev &amp;gt; Interpreter''. If you don't see &amp;quot;Python 3&amp;quot; or similar in the top panel, click on ''New'' to add it. Then, because the BGE API is unknown to PyDev, turn off some error markers: still in the Preferences dialogue, choose ''Pydev &amp;gt; Editor &amp;gt; Code Analysis''. In the ''Undefined'' tab, set ''Undefined variable from import'' to ''Warning''. In the ''Imports'' tab, set ''Import not found'' to ''Ignore''.&lt;br /&gt;
&lt;br /&gt;
Now you can import your project.&lt;br /&gt;
# Switch to the PyDev perspective: choose ''Window &amp;gt; Open Perspective &amp;gt; Other &amp;gt; Pydev''.&lt;br /&gt;
# Right-click in the Pydev Package Explorer view, and choose ''New &amp;gt; Project &amp;gt; Pydev &amp;gt; Project''.&lt;br /&gt;
# Click ''Next''. In the ''Project name'' field, enter ''Game'' (or whatever you have called your project directory). If it doesn't exist yet, it will be created; but it shouldn't be overwritten (but make a backup first). Set the ''Grammar Version'' to ''3.0'', and the ''Interpreter'' to the one configured above (''Python 3''). Un-tick ''Create adefault 'src' folder''; it will be added later.&lt;br /&gt;
# Right-click on your new project directory in the Pydev Package Explorer, and choose ''Properties &amp;gt; PyDev - PYTHONPATH &amp;gt; Source Folders''. Click ''Add source folder'', and choose the ''Game'' directory itself - this sets up the modules to mimic how they are imported within Blender.&lt;br /&gt;
&lt;br /&gt;
That's it! Now any .py files that you have in the ''src'' directory will be available as modules in the game engine. For example, a Python controller set to run ''src.foo.bar'' would run the ''bar()'' function in ''src/foo.py''.&lt;br /&gt;
&lt;br /&gt;
=== Debugging ===&lt;br /&gt;
&lt;br /&gt;
PyDev comes with an interactive debugger. This section explains how to use it in Blender; for detailed usage see [http://pydev.org/manual_adv_remote_debugger.html the official PyDev documentation].&lt;br /&gt;
&lt;br /&gt;
First, Switch to the Debug perspective in Eclipse: choose ''Window &amp;gt; Open Perspective &amp;gt; Other &amp;gt; Debug''. Then create an ''External Tool'' to start Blender: choose ''Run &amp;gt; External Tools &amp;gt; External Tools Configurations''. Right-click on ''Program'' on the left, and choose ''New''. Configure the tool as follows:&lt;br /&gt;
* ''Main &amp;gt; Name'': ''Blender PyDebug''&lt;br /&gt;
* ''Main &amp;gt; Location'': ''/usr/bin/blender'' (or wherever your Blender executable is).&lt;br /&gt;
* ''Environment&amp;gt; Vaiable &amp;gt; PYTHONPATH'': ''/path/to/.eclipse/org.eclipse.platform_VERSION/plugins/org.python.pydev.debug_VERSION/pysrc'', i.e. create a new environment variable to set the PYTHONPATH. For this variable, you will need to find the directory that contains ''pydevd.py'', which is distributed with PyDev. This allows Blender to import the ''pydevd'' module.&lt;br /&gt;
&lt;br /&gt;
Now add this code to the start of one of your files, e.g. ''src/__init__.py'':&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang='python'&amp;gt;&lt;br /&gt;
try:&lt;br /&gt;
	import pydevd&lt;br /&gt;
	pydevd.settrace(stdoutToServer=True, stderrToServer=True, suspend=False)&lt;br /&gt;
except ImportError:&lt;br /&gt;
	pass&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
That code will happily run without the debugger if you don't start Blender using the external tool created above.&lt;br /&gt;
&lt;br /&gt;
Now you're all set. To debug your code:&lt;br /&gt;
# Set breakpoints by double-clicking in the grey side-bar on the left of the text editor view.&lt;br /&gt;
# Choose ''Pydev &amp;gt; Start Debug Server''.&lt;br /&gt;
# Choose ''Run &amp;gt; External Tools &amp;gt; Blender PyDebug'' (you may need to run it from the ''Configure External Tools'' dialogue if it's not in the menu).&lt;br /&gt;
# Start your game normally. Execution should stop whenever the interpreter reaches one of your breakpoints.&lt;br /&gt;
&lt;br /&gt;
== Profiling Blender Games ==&lt;br /&gt;
&lt;br /&gt;
=== Instrumented ===&lt;br /&gt;
One of the most basic ways to measure the speed of your program is to surround interesting blocks of it with timing collection code. The following code lets you time the execution of some functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import time&lt;br /&gt;
prof = {}&lt;br /&gt;
&lt;br /&gt;
class profile:&lt;br /&gt;
	'''Function decorator for code profiling.'''&lt;br /&gt;
&lt;br /&gt;
	def __init__(self, name):&lt;br /&gt;
		self.name = name&lt;br /&gt;
&lt;br /&gt;
	def __call__(self, fun):&lt;br /&gt;
		def profile_fun(*args, **kwargs):&lt;br /&gt;
			start = time.clock()&lt;br /&gt;
			try:&lt;br /&gt;
				return fun(*args, **kwargs)&lt;br /&gt;
			finally:&lt;br /&gt;
				duration = time.clock() - start&lt;br /&gt;
				if not fun in prof:&lt;br /&gt;
					prof[fun] = [self.name, duration, 1]&lt;br /&gt;
				else:&lt;br /&gt;
					prof[fun][1] += duration&lt;br /&gt;
					prof[fun][2] += 1&lt;br /&gt;
		return profile_fun&lt;br /&gt;
&lt;br /&gt;
def print_stats(c):&lt;br /&gt;
	'''Prints profiling results to the console. Run from a Python controller.'''&lt;br /&gt;
&lt;br /&gt;
	if not c.sensors[0].positive:&lt;br /&gt;
		return&lt;br /&gt;
&lt;br /&gt;
	def timekey(stat):&lt;br /&gt;
		return stat[1] / float(stat[2])&lt;br /&gt;
	stats = sorted(prof.values(), key=timekey, reverse=True)&lt;br /&gt;
&lt;br /&gt;
	print('=== Execution Statistics ===')&lt;br /&gt;
	print('Times are in milliseconds.')&lt;br /&gt;
	print('{:&amp;lt;55} {:&amp;gt;6} {:&amp;gt;7} {:&amp;gt;6}'.format('FUNCTION', 'CALLS', 'SUM(ms)', 'AV(ms)'))&lt;br /&gt;
	for stat in stats:&lt;br /&gt;
		print('{:&amp;lt;55} {:&amp;gt;6} {:&amp;gt;7.0f} {:&amp;gt;6.2f}'.format(&lt;br /&gt;
				stat[0], stat[2],&lt;br /&gt;
				stat[1] * 1000,&lt;br /&gt;
				(stat[1] / float(stat[2])) * 1000))&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then you need to decorate the functions you are interested in with the ''profile'' decorator:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
@profile('foo_func')&lt;br /&gt;
def foo_func(c):&lt;br /&gt;
	c.owner.worldPosition.z += 1&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, hook up the ''print_stats'' function to a Python controller so that it prints the statistics when a key is pressed (for example).&lt;br /&gt;
&lt;br /&gt;
=== Statistical ===&lt;br /&gt;
Profiling a game can be tricky: your code may be called via several Python controller logic bricks, so there is no single entry point. Therefore you can't use the built-in [http://docs.python.org/py3k/library/profile.html profile] module - and instrumenting each function manually can be a pain.&lt;br /&gt;
&lt;br /&gt;
Another method that works very well is statistical (or ''stochastic'') profiling. This method uses interrupts to stop the interpreter at regular intervals and record what piece of code is executing. To do this, you can use the 3rd-party ''statprof'' module.&lt;br /&gt;
&lt;br /&gt;
First, download [[File:Statprof.3k.txt]] and place it next to your .blend file. Remember to rename it to ''statprof.py''. Then add the following code to one of your scripts (or create a new text block for it, e.g. &amp;quot;profiling.py&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import statprof&lt;br /&gt;
statprof.start()&lt;br /&gt;
&lt;br /&gt;
def start_profiling(c):&lt;br /&gt;
	if c.sensors[0].positive:&lt;br /&gt;
		statprof.start()&lt;br /&gt;
&lt;br /&gt;
def print_stats(c):&lt;br /&gt;
	if c.sensors[0].positive:&lt;br /&gt;
		statprof.stop()&lt;br /&gt;
		statprof.display()&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then all you need to do is bind those functions to a Python controller. ''start_profiling'' should probably be run on the first frame; ''print_stats'' should be bound to some later event such as a key-press. Because the profiler collects samples periodically, the longer you let your game run for, the more accurate the results will be. But in practical terms, ''print_stats'' can be run 20-30 seconds after ''start_profiling''. Note that in its current form, ''print_stats'' will only return useful results for the first time it is run.&lt;/div&gt;</summary>
		<author><name>wiki&gt;Ideasman42</name></author>
		
	</entry>
</feed>