cheind / pytorch-blender

:sweat_drops: Seamless, distributed, real-time integration of Blender into PyTorch data pipelines
MIT License
562 stars 44 forks source link

TypeError: bpy_struct: item.attr = val: RigidBodyConstraint.motor_lin_target_velocity expected a float type, not numpy.ndarray #20

Closed rafaveguim closed 2 years ago

rafaveguim commented 2 years ago

I just installed the project with the latest versions of pytorch (1.10.1), gym (0.21.0), and Blender (3.0.0).

I'm getting the following error when trying to run the cartpole example:

Traceback (most recent call last):
  File "/Users/rafa/Documents/Dev/pytorch-blender/pkg_blender/blendtorch/btb/animation.py", line 209, in _on_pre_frame
    self.pre_frame.invoke()
  File "/Users/rafa/Documents/Dev/pytorch-blender/pkg_blender/blendtorch/btb/signal.py", line 54, in invoke
    s(*args, **kwargs)
  File "/Users/rafa/Documents/Dev/pytorch-blender/pkg_blender/blendtorch/btb/env.py", line 108, in _pre_frame
    self._env_prepare_step(action)
  File "/Users/rafa/Documents/Dev/pytorch-blender/examples/control/cartpole_gym/envs/cartpole.blend.py", line 33, in _env_prepare_step
    self._apply_motor_force(action)
  File "/Users/rafa/Documents/Dev/pytorch-blender/examples/control/cartpole_gym/envs/cartpole.blend.py", line 55, in _apply_motor_force
    self.motor.motor_lin_target_velocity = self.motor.motor_lin_target_velocity + \
TypeError: bpy_struct: item.attr = val: RigidBodyConstraint.motor_lin_target_velocity expected a float type, not numpy.ndarray

It seems that the value of f being passed to def _apply_motor_force(self, f): is a numpy.ndarray of 3 values.

Is there an easy way to fix this or it's a deeper issue due to changes in the dependencies?

cheind commented 2 years ago

Hey,

hmm I woudn't have expected f to be 3-dimensional in this example. There seems to be some unwanted broadcasting along the processing pipeline. Could you check what x_cart and x_pole is in https://github.com/cheind/pytorch-blender/blob/develop/examples/control/cartpole.py?`

I don't have blender 3.x around currently

cheind commented 2 years ago

So, I've installed latest Blender 3.0, gym 0.21 and pytorch 10.1. I however can't reproduce what you are seeing. The control example runs fine. Are you on latest develop of blendtorch?

rafaveguim commented 2 years ago

Yes, my branch is up to date with develop. I printed xcart and xpole in cartpole.py:

xcart: 0.0 xpole: [0. 0. 1.2]

Do these seem sane?

I'm on Mac, with python 3.9.7

cheind commented 2 years ago

xpole is incorrect, it should be just the x-coordinate of the pole. I have no idea how that happens and I cannot reproduce it. The data you see is read from Blender when you reset/step the env. In both methods just the x coordinate of the world_matrix is returned.

I've tested the code on Ubuntu as well and it works as expected. Would you mind creating a fresh virtual environment to install pytorch-blender?

cheind commented 2 years ago

@rafaveguim any updates on this?

rafaveguim commented 2 years ago

Is this correct in _env_reset?

p = np.array([0.0, 0, 1.2])

cheind commented 2 years ago

Ah, you're on the right track here!

I believe the code should be

def _env_reset(self):
    self.motor.motor_lin_target_velocity = 0.
    self.cart.location = np.array([0.0, 0, 1.2])
    self.polerot.rotation_euler[1] = np.random.uniform(-0.6, 0.6)
    return self._env_post_step()

def _env_post_step(self):
    c = self.cart.matrix_world.translation[0]
    p = self.pole.matrix_world.translation[0]
    a = self.pole.matrix_world.to_euler('XYZ')[1]
    return dict(
        obs=(c, p, a),
        reward=0.,
        done=bool(
            abs(a) > 0.6
            or abs(c) > 4.0
        )
    )

in examples/control/cartpole_gym/envs/cartpole.blend.py. Could you try this? Not sure, why it works on other platforms though.

rafaveguim commented 2 years ago

Problem solved!

cheind commented 2 years ago

nice, will move the updated code to develop. thanks for your help!