TylerGubala/blenderpy

installing bpy in anaconda python 3.7.11

monajalal opened this issue · 10 comments

(UnseenObjectPose) mona@ard-gpu-01:~/Unseen_Object_Pose$ pip install bpy
Collecting bpy
  Downloading bpy-2.82.1.tar.gz (19 kB)
  Preparing metadata (setup.py) ... error
  error: subprocess-exited-with-error
  
  × python setup.py egg_info did not run successfully.
  │ exit code: 1
  ╰─> [17 lines of output]
      Traceback (most recent call last):
        File "/home/mona/anaconda3/envs/UnseenObjectPose/lib/python3.7/tokenize.py", line 397, in find_cookie
          codec = lookup(encoding)
      LookupError: unknown encoding: future_fstrings
      
      During handling of the above exception, another exception occurred:
      
      Traceback (most recent call last):
        File "<string>", line 36, in <module>
        File "<pip-setuptools-caller>", line 28, in <module>
        File "/home/mona/anaconda3/envs/UnseenObjectPose/lib/python3.7/tokenize.py", line 449, in open
          encoding, lines = detect_encoding(buffer.readline)
        File "/home/mona/anaconda3/envs/UnseenObjectPose/lib/python3.7/tokenize.py", line 436, in detect_encoding
          encoding = find_cookie(second)
        File "/home/mona/anaconda3/envs/UnseenObjectPose/lib/python3.7/tokenize.py", line 405, in find_cookie
          raise SyntaxError(msg)
      SyntaxError: unknown encoding for '/tmp/pip-install-nzffr3bp/bpy_e73cbe00c9bb419cab5813be27267dc1/setup.py': future_fstrings
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.
(UnseenObjectPose) mona@ard-gpu-01:~/Unseen_Object_Pose$ pythpn
pythpn: command not found
(UnseenObjectPose) mona@ard-gpu-01:~/Unseen_Object_Pose$ python
Python 3.7.11 (default, Jul 27 2021, 14:32:16) 
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
(UnseenObjectPose) mona@ard-gpu-01:~/Unseen_Object_Pose$ uname -a
Linux ard-gpu-01 5.19.0-38-generic #39~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Fri Mar 17 21:16:15 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
(UnseenObjectPose) mona@ard-gpu-01:~/Unseen_Object_Pose$ lsb_release -a
LSB Version:	core-11.1.0ubuntu4-noarch:security-11.1.0ubuntu4-noarch
Distributor ID:	Ubuntu
Description:	Ubuntu 22.04.1 LTS
Release:	22.04
Codename:	jammy
(UnseenObjectPose) mona@ard-gpu-01:~/Unseen_Object_Pose$ conda info

     active environment : UnseenObjectPose
    active env location : /home/mona/anaconda3/envs/UnseenObjectPose
            shell level : 2
       user config file : /home/mona/.condarc
 populated config files : /home/mona/.condarc
          conda version : 23.3.1
    conda-build version : 3.24.0
         python version : 3.9.13.final.0
       virtual packages : __archspec=1=x86_64
                          __cuda=11.6=0
                          __glibc=2.35=0
                          __linux=5.19.0=0
                          __unix=0=0
       base environment : /home/mona/anaconda3  (writable)
      conda av data dir : /home/mona/anaconda3/etc/conda
  conda av metadata url : None
           channel URLs : https://conda.anaconda.org/pytorch/linux-64
                          https://conda.anaconda.org/pytorch/noarch
                          https://repo.anaconda.com/pkgs/main/linux-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/r/linux-64
                          https://repo.anaconda.com/pkgs/r/noarch
          package cache : /home/mona/anaconda3/pkgs
                          /home/mona/.conda/pkgs
       envs directories : /home/mona/anaconda3/envs
                          /home/mona/.conda/envs
               platform : linux-64
             user-agent : conda/23.3.1 requests/2.28.1 CPython/3.9.13 Linux/5.19.0-38-generic ubuntu/22.04.1 glibc/2.35
                UID:GID : 1002:1002
             netrc file : None
           offline mode : False
(UnseenObjectPose) mona@ard-gpu-01:~/Unseen_Object_Pose$ pip install bpy==3.5.0
ERROR: Could not find a version that satisfies the requirement bpy==3.5.0 (from versions: 2.82.1, 2.91a0)
ERROR: No matching distribution found for bpy==3.5.0
(UnseenObjectPose) mona@ard-gpu-01:~/Unseen_Object_Pose$ python
Python 3.7.11 (default, Jul 27 2021, 14:32:16) 
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()

Thank you @TylerGubala
Is this warning I should fix?
WARN (bgl): source/blender/python/generic/bgl.c:2654 BPyInit_bgl: 'bgl' imported without an OpenGL backend. Please update your add-ons to use the 'gpu' module. In Blender 4.0 'bgl' will be removed.

@TylerGubala I also got this error:


(testbpy) mona@ard-gpu-01:~/Unseen_Object_Pose/Render$ python data_generation.py
WARN (bgl): source/blender/python/generic/bgl.c:2654 BPyInit_bgl: 'bgl' imported without an OpenGL backend. Please update your add-ons to use the 'gpu' module. In Blender 4.0 'bgl' will be removed.
---------- image generation------------
Generating src image for 000001
  0%|                                                                                                                                                                                      | 0/185 [00:00<?, ?it/s]
Traceback (most recent call last):
  File "/home/mona/Unseen_Object_Pose/Render/data_generation.py", line 226, in <module>
    src_image_generate(cfg, 'train')
  File "/home/mona/Unseen_Object_Pose/Render/data_generation.py", line 138, in src_image_generate
    _, depth = render_objects([meshes[int(key.split('_')[0])-1]], [0], [pose], dst_K, \
NameError: name 'render_objects' is not defined

These are the packages imported by the author:

import os
import sys
import yaml
import torch
import math
import numpy as np
import cv2
from tqdm import tqdm
import json
import trimesh
import argparse
import glob
import pickle
from tqdm import trange
from bpy_render import render_ply
from pytorch3d.transforms import euler_angles_to_matrix, matrix_to_euler_angles, rotation_6d_to_matrix

I see there is bpy_render.

What do you think?

Can you do these steps to see if you can reproduce the error?

git clon ehttps://github.com/sailor-z/Unseen_Object_Pose.git
cd UnseenObjectPose
conda create -n testbpy python=3.10 anaconda
 conda activate testbpy
pip install bpy==3.5.0
conda install pytorch=1.13.0 torchvision pytorch-cuda=11.6 -c pytorch -c nvidia
conda install -c fvcore -c iopath -c conda-forge fvcore iopath
git clone https://github.com/facebookresearch/pytorch3d.git
cd pytorch3d && pip install -e .
cd ..
cd Render
(testbpy) mona@ard-gpu-01:~/Unseen_Object_Pose/Render$ python data_generation.py

as you see they are using bpy

sailor-z/Unseen_Object_Pose#6
I have CUDA 11.6

They have a render_objects method defined in the same level as the other python code is. It seems an import error in Python 3.10?

import pyrender
import numpy as np
import cv2

def render_objects(meshes, ids, poses, K, w, h):
    assert(K[0][1] == 0 and K[1][0] == 0 and K[2][0] ==0 and K[2][1] == 0 and K[2][2] == 1)
    fx = K[0][0]
    fy = K[1][1]
    cx = K[0][2]
    cy = K[1][2]
    objCnt = len(ids)
    assert(len(poses) == objCnt)

    # set background with 0 alpha, important for RGBA rendering
    scene = pyrender.Scene(bg_color=np.array([0.0, 0.0, 0.0, 1.0]), ambient_light=np.array([0.02, 0.02, 0.02, 1.0]))
    # pyrender.Viewer(scene, use_raymond_lighting=True)
    # camera = pyrender.PerspectiveCamera(yfov=np.pi / 3.0, aspectRatio=1.0)
    camera = pyrender.IntrinsicsCamera(fx=fx,fy=fy,cx=cx,cy=cy,znear=0.05,zfar=100000)
    camera_pose = np.eye(4)
    # reverse the direction of Y and Z, check: https://pyrender.readthedocs.io/en/latest/examples/cameras.html
    camera_pose[1][1] = -1
    camera_pose[2][2] = -1
    scene.add(camera, pose=camera_pose)
    #light = pyrender.SpotLight(color=np.ones(3), intensity=4.0, innerConeAngle=np.pi/16.0, outerConeAngle=np.pi/6.0)
    light = pyrender.DirectionalLight(color=np.ones(3), intensity=4.0)
    #light = pyrender.PointLight(color=np.ones(3), intensity=4.0)
    scene.add(light, pose=camera_pose)
    for i in range(objCnt):
        clsId = int(ids[i])
        mesh = pyrender.Mesh.from_trimesh(meshes[clsId])
        H = np.zeros((4,4))
        H[0:3] = poses[i][0:3]
        H[3][3] = 1.0
        scene.add(mesh, pose=H)
    # pyrender.Viewer(scene, use_raymond_lighting=True)

    r = pyrender.OffscreenRenderer(w, h)
    # flags = pyrender.RenderFlags.OFFSCREEN | pyrender.RenderFlags.DEPTH_ONLY
    #flags = pyrender.RenderFlags.OFFSCREEN
    #flags = pyrender.RenderFlags.OFFSCREEN | pyrender.RenderFlags.RGBA
    #color, depth = r.render(scene, flags=flags)
    color, depth = r.render(scene)

    color = cv2.cvtColor(color, cv2.COLOR_RGB2BGR) # RGB to BGR (for OpenCV)
    #color = cv2.cvtColor(color, cv2.COLOR_RGBA2BGRA) # RGBA to BGRA (for OpenCV)

    return color, depth

It is in py_render.py

I also have no problem importing the following

(testbpy) mona@ard-gpu-01:~/Unseen_Object_Pose/Render$ python
Python 3.10.10 (main, Mar 21 2023, 18:45:11) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

from bpy_render import render_ply
WARN (bgl): source/blender/python/generic/bgl.c:2654 BPyInit_bgl: 'bgl' imported without an OpenGL backend. Please update your add-ons to use the 'gpu' module. In Blender 4.0 'bgl' will be removed.

I got it fixed.

I added
from py_render import render_objects
in
data_generation.py
and
also installed
pip install pyrender