Open
Description
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
BlenderProc version
Github man branch