Recent Posts

[Trouble Shooting] NullFunctionError of glutInit in OpenGL

Problem

When trying to call “glutInit()” of PyOpenGL, I got the error “NullFunctionError: Attempt to call an undefined function glutInit, check for bool(glutInit) before calling”.

# python source code
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *

glutInit()
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB) 
 
Error Detail

---------------------------------------------------------------------------
NullFunctionError                         Traceback (most recent call last)
/tmp/ipykernel_14/3289264930.py in <module>
      3 from OpenGL.GLU import *
      4 
----> 5 glutInit()
      6 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB)
      7 

/usr/local/lib/python3.9/site-packages/OpenGL/GLUT/special.py in glutInit(*args)
    331     try:
    332         # XXX need to check for error condition here...
--> 333         _base_glutInit( ctypes.byref(count), holder )
    334     finally:
    335         os.chdir( currentDirectory )

/usr/local/lib/python3.9/site-packages/OpenGL/platform/baseplatform.py in __call__(self, *args, **named)
    421                 pass
    422             else:
--> 423                 raise error.NullFunctionError(
    424                     """Attempt to call an undefined function %s, check for bool(%s) before calling"""%(
    425                         self.__name__, self.__name__,

NullFunctionError: Attempt to call an undefined function glutInit, check for bool(glutInit) before calling

Environment

Windows10
Python 3.9.7

Cause

There are some Recommended Enhancements for PyOpenGL. And some of them, such as GLUT or FreeGLUT, are not contained in PyOpenGL itself.

Solution

Solution1 use binary installer for windows.

The documentation says “Win32 and Win64 binary installers for PyOpenGL include a copy of GLUT”.
So uninstall current pyopengl first.
And download binary installer for windows. Put the downloaded file in some directory, then run the command “pip install <file name>” in the directory where the file is saved.

pip uninstall PyOpenGL
pip install PyOpenGL‑3.1.6‑cp39‑cp39‑win_amd64.whl

Solution2 install GLUT or FreeGLUT separately

Installing and adding GLUT or FreeGLUT to PATH solves this problem.

Build FreeGLUT from source code or download built FreeGLUT from “Download freeglut 3.0.0 for MSVC” in transmissionzero.co.uk/software/freeglut-devel.

Put freeglut\bin\x64\freeglut.dll into C:\Windows\System32.

Add “C:\Windows\System32” to environment variable “PATH” to enable python find the library.

If the error still occurred, check if your library name is in Win32Platform.GLUT() <python>\Lib\site-packages\OpenGL\platform\win32.py

    @baseplatform.lazy_property
    def GLUT( self ):
        for possible in ('freeglut%s.%s'%(size,vc,), 'freeglut', 'glut%s.%s'%(size,vc,)):  # Added 'freeglut' because the library name is freeglut.dll
            # Prefer FreeGLUT if the user has installed it, fallback to the included 
            # GLUT if it is installed
            try:
                return ctypesloader.loadLibrary(
                    ctypes.windll, possible, mode = ctypes.RTLD_GLOBAL
                )
            except WindowsError:
                pass
        return None

These links might help you.
python – Attempt to call an undefined function glutInit
#219 glutInit fails on windows 64-bit
Attempt to call an undefined function glutInit
Python and PyOpenGL Installation

Appendix: How to install GLUT or FreeGLUT on Linux

Use “sudo apt-get install python-opengl” command to install PyOpenGL with dependent library at once.

How To Get Mapping of UV and Vertex

GOAL

To get correspondence of UV coordinates and vertex id in the object in Blender scene by Blender Python. The following is output of the script.

uv: 0.625 0.5 vertex id: 0
uv: 0.875 0.5 vertex id: 4
uv: 0.875 0.75 vertex id: 6
uv: 0.625 0.75 vertex id: 2
uv: 0.375 0.75 vertex id: 3
.
.
.

Environment

Blender 2.83 (Python 3.7.4)
Windows 10

Method

Mesh Loop

Use Mesh Loop to get UV and vertex data.

In unfolded UV, the correspondence between the point of UV map and the vertex of 3D object is not one-to-one. Thus we use ‘Mesh Loop’ instead of vertex or edge to distinct point of UV map.

Mesh Loop is an element surrounding a face, which consists of edges and vertices. Each quad face consists of 4 Mesh Loops as below. For example, cube objects have 6face, 24 mesh loops.

import bpy

obj = bpy.data.objects[obj_name]
mesh_loops = obj.data.loops 
for mesh_loop in mesh_loops:
    print(mesh_loop)

Mesh UV Loop

UVs have Mesh UV Loops as a counterpart to the Mesh Loops. And the correspondence between Mesh Loop and MeshUVLoop is one-to-one.

import bpy

obj = bpy.data.objects[obj_name]
mesh_uv_loop = obj.data.uv_layers[0].data[loop_index]

Source Code

import bpy

def get_uvs(obj_name):
    obj = bpy.data.objects[obj_name]
    mesh_loops = obj.data.loops

    for i, mesh_loop in enumerate(mesh_loops):
        mesh_uv_loop = obj.data.uv_layers[0].data[i]
        print("uv:", mesh_uv_loop.uv[0], mesh_uv_loop.uv[1], "vertex id:", mesh_loop.vertex_index)

get_uvs("Cube")

How To Start Anaconda

GOAL

Today’s goal is to construct development environment for data analytics and machine learning with Anaconda.

What is Anaconda?

Anaconda is open source Python distribution for data science. You can see the list of package lists here.

the open-source Individual Edition (Distribution) is the easiest way to perform Python/R data science and machine learning on a single machine. Developed for solo practitioners, it is the toolkit that equips you to work with thousands of open-source packages and libraries.

from anaconda.com/products/individual

Environment

macOS Catalina 10.15.5
Python 3.9.7

Method

Download & Install

Access the websiteanaconda.com/products/individual and click “Download” button to download the installer of the edition that you want.

Start the installer and click “Continue”.

Check if anaconda is completely installed with terminal.

Start Anaconda Navigator

Start applications > Anaconda-Navigator.

Create virtual environment

Click Environments and create new environment.

I named new environment “data_analysis”.

Install libraries or modules with conda

Conda is an open source package management system and environment management system.

Open terminal in the environment where you want to install libraries.

Then put the “conda install” command to install libraries.
For example, I installed pytorch in the “pytorch” environment. The option “-c” is the channel (What is a “conda channel”?).

conda install pytorch torchvision -c pytorch

Start Application

Select environment what you want to use and install or launch application.

I launched “Jupyter notebook” and check that “pythorch” library is installed successfully.

Categories

AfterEffects Algorithm Artificial Intelligence Blender C++ Computer Graphics Computer Science Daily Life DataAnalytics Event Game ImageProcessing JavaScript Kotlin mathematics Maya PHP Python SoftwareEngineering Tips Today's paper Tools TroubleShooting Unity Visual Sudio Web Windows WordPress 未分類