Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why my objects appear in a really werid pose when using random_position and applying physics properties? #1177

Open
shengjieH opened this issue Dec 28, 2024 · 3 comments
Labels
question Question, not yet a bug ;)

Comments

@shengjieH
Copy link

shengjieH commented Dec 28, 2024

Describe the issue

When i tried to use random position and apply physics properties, the objects appear at a really strange position that far away from the expected position and exceed the random range. Please see the screen shot, i print the origin position and final position after randomlization of all objects. You could see the first render is fine, but the second results is really werid. Below you could find my code, could you please help me with this issue?

Thank you in advance!!

Minimal code example

import blenderproc as bproc
import argparse
import os
import numpy as np

parser = argparse.ArgumentParser()
parser.add_argument('camera', nargs='?', default="examples/camera_positions_test", help="Path to the camera file")
parser.add_argument('scene', nargs='?', default="examples/scene.blend", help="Path to the scene.blend file")
parser.add_argument('output_dir', nargs='?', default="examples/output", help="Path to where the final files will be saved")
parser.add_argument('--num_images', type=int, default=3, help="Number of images to render")
args = parser.parse_args()

bproc.init()

objs = bproc.loader.load_blend(args.scene)

print("Current category IDs for objects:")
for obj in objs:
    category_id = obj.get_cp("category_id") if obj.has_cp("category_id") else None
    print(f"Object: {obj.get_name()}, Category ID: {category_id}")


print("Objects in the scene:")
for obj in objs:
    print(obj.get_name())

plane = None
for obj in objs:
    if obj.get_name() == "plane":
        plane = obj
        break

if plane is None:
    raise ValueError("No object named 'plane' was found in the scene. Please check your scene file.")


plane.set_location([0, 0, 0])
plane.enable_rigidbody(active=False, collision_shape="MESH")  

movable_objs = [obj for obj in objs if obj.get_name() != "plane"]


for obj in movable_objs:
    obj.enable_rigidbody(active=True, mass=1.0, collision_shape="CONVEX_HULL")


for obj in objs:
    origin = obj.get_location() 
    print(f"Object: {obj.get_name()}, Origin: {origin}")


def randomize_objects(objects):
    for obj in objects:  
        random_position = np.random.uniform(low=[-10,-10, 10], high=[0, 0, 20])  
        obj.set_location(random_position)
       
        random_rotation = np.random.uniform(low=[np.pi/2, 0, 0], high=[np.pi/2, 0, np.pi])
        obj.set_rotation_euler(random_rotation)

for j, obj in enumerate(objs):
    if obj.get_name() == "plane": 
        obj.set_cp("category_id", 0)
    else:
        obj.set_cp("category_id", j + 1)

print("Current category IDs for objects:")
for obj in objs:
    category_id = obj.get_cp("category_id") if obj.has_cp("category_id") else None
    print(f"Object: {obj.get_name()}, Category ID: {category_id}")

# light
light = bproc.types.Light()
light.set_type("AREA")
light.set_location([5, -5, 100])
light.set_energy(100000)

# camera size
bproc.camera.set_resolution(640, 640)

with open(args.camera, "r") as f:
    for line in f.readlines():
        line = [float(x) for x in line.split()]
        position, euler_rotation = line[:3], line[3:6]
        matrix_world = bproc.math.build_transformation_mat(position, euler_rotation)
        bproc.camera.add_camera_pose(matrix_world)


bproc.renderer.enable_normals_output()
bproc.renderer.enable_segmentation_output(map_by=["category_id", "instance", "name"])


for i in range(args.num_images):

    randomize_objects(movable_objs)
    
    bproc.object.simulate_physics_and_fix_final_poses(
        min_simulation_time=4,
        max_simulation_time=20,
        check_object_interval=1
    )

    for obj in movable_objs:
        final_position = obj.get_location()
        print(f"Object: {obj.get_name()}, Final Position: {final_position}")


    data = bproc.renderer.render()

    bproc.writer.write_coco_annotations(
        os.path.join(args.output_dir, f'coco_data_{i}'),
        instance_segmaps=data["instance_segmaps"],
        instance_attribute_maps=data["instance_attribute_maps"],
        colors=data["colors"],
        color_file_format="JPEG"
    )

print(f"Rendered {args.num_images} images with randomized 'PlanetaryGearSystem' positions.")

Files required to run the code

No response

Expected behavior

d31e8939-e4a4-4872-af12-a3c600853922

BlenderProc version

Github man branch

@shengjieH shengjieH added the question Question, not yet a bug ;) label Dec 28, 2024
@shengjieH
Copy link
Author

shengjieH commented Dec 28, 2024

And i just found, when i decrease the number of object until there is only one object. The simulation was stable, the object didn't appear at a far position.

@shengjieH
Copy link
Author

I was thinking it could be the problem of corrputed mesh. So i change to linux system and apply vhacd algorithm, but unfortunately the results are still the same, some objects appear at really far away position after rendering. After several days' try, i can also provide additional information:

  1. It caused by physical simulation, because when i shut down this function, the positions of objects are normal, but they are floating on the air because of no gravity.
  2. When i set the plane(passive object) large enough, and decrease the scale of objects, increase the range of random_position, which means the objects have enough space to move, in this case, the possiblity of problem decrease apparently.

@cornerfarmer
Copy link
Member

Hey @shengjieH,

it might be that your objects are in collision when placing them randomly. If that is the case, the physics simulation can behave explosively, as it tries to quickly resolve the collisions.
So I would recommend to always place the objects with collision checks as it is done here using bproc.object.sample_poses: https://github.com/DLR-RM/BlenderProc/blob/main/examples/advanced/physics_convex_decomposition/main.py#L29

You can also use the blenderproc gui mode (https://github.com/DLR-RM/BlenderProc?tab=readme-ov-file#debugging-in-the-blender-gui) together with bproc.object.simulate_physics to see what the physics simulation is actually doing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question, not yet a bug ;)
Projects
None yet
Development

No branches or pull requests

2 participants