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

Normalizing an imported object #1100

Closed
cs-mshah opened this issue May 25, 2024 · 2 comments
Closed

Normalizing an imported object #1100

cs-mshah opened this issue May 25, 2024 · 2 comments
Labels
question Question, not yet a bug ;)

Comments

@cs-mshah
Copy link

Describe the issue

I am trying to normalize a .glb object using the following code:

def objs_bbox(objs: List[MeshObject]):
    bbox_min = np.array((math.inf,) * 3)
    bbox_max = np.array((-math.inf,) * 3)
    for obj in objs:
        bbox = obj.get_bound_box()
        min_bbox = np.min(bbox, axis=0)
        max_bbox = np.max(bbox, axis=0)
        bbox_min = np.minimum(bbox_min, min_bbox)
        bbox_max = np.maximum(bbox_max, max_bbox)
    return bbox_min, bbox_max


def normalise_objs(objs: List[MeshObject]):
    """Normalise the objects to have a scale of 1 and be centered at the origin.

    :param objs: The objects to normalise
    """
    bbox_min, bbox_max = objs_bbox(objs)
    scale_obj = bbox_max - bbox_min
    scale_factor = 1 / max(scale_obj)
    offset = - (bbox_max + bbox_min) / 2
    root_obj = get_root_obj(objs[0])
    root_obj.set_location(root_obj.get_location() + offset)
    root_obj.set_scale(root_obj.get_scale() * scale_factor)
    
objs = bproc.loader.load_obj(args.object)
normalise_objs(objs)

But the results are weird:

Using both translation and scaling, the final object's location is not correct. Whereas commenting out each one individually gives the correct scale and correct position.

image

image

image

Minimal code example

No response

Files required to run the code

No response

Expected behavior

The object should be scaled and at the correct position being normalized.

BlenderProc version

2.7.0

@cs-mshah cs-mshah added the question Question, not yet a bug ;) label May 25, 2024
@cornerfarmer
Copy link
Member

Hey,

the bug is in your code. The offset depends on the scaling factor. Multiplying the offset with the scale factor should solve your problem:

 obj.set_location(obj.get_location() + offset * scale_factor)

@cs-mshah
Copy link
Author

cs-mshah commented Jun 4, 2024

@cornerfarmer Hey thanks for pointing it out. I had one more question: How should I apply rotation to an object? I tried using the obj.set_rotation on the root node of the gltf object but that didn't seem to work. It is quite confusing. Could you help?

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