mitsuba-renderer / mitsuba2

Mitsuba 2: A Retargetable Forward and Inverse Renderer
Other
2.05k stars 266 forks source link

Dictionary to xml for ,shapegroup #607

Closed peroveh closed 2 years ago

peroveh commented 2 years ago

Summary

Struggling to make a python dictionary which is accepted,, and or converted to an accepted xml file with a shape group. In particular the instantiation seems problematic (i.e. the reference)..

Do you have a python dictionary example representation to the xml shapegroup example? https://mitsuba2.readthedocs.io/en/latest/generated/plugins.html?highlight=shape_group

def mksphere(yloc,ind,radius=1.):
    sp_dict={
        "id": "mysphere"+str(ind),
        "type": "sphere",
        "center": [0,0,yloc],
        "radius": radius,
        "flip_normals": False,
        "bsdf":{
            "type": "diffuse" #"dielectric"
        }
    }
    return sp_dict

group1 = {
    "type":"shapegroup",
    "id": "my_sphere_group",
    "mysphere1" : mksphere(1.,0),
    "mysphere2" : mksphere(3.,1)
    }

# then creating entries in a larger dictionary: 
    "mygroup":group1,

    #"myinstance":{    failed attempt 
    #    "type": "instance", 
    #    "ref" : "my_sphere_group",
    #    "to_world" : ScalarTransform4f.translate([0,10,0])
    #},
    "myinstance":{  also failed
        "instance": "my_sphere_group",
        "to_world" : ScalarTransform4f.translate([0,10,0]),
    }
merlinND commented 2 years ago

Hi @peroveh,

Looks like you were almost there, it works with this syntax:

import mitsuba
mitsuba.set_variant('scalar_rgb')

from mitsuba.core import ScalarTransform4f

def mksphere(yloc, ind, radius=1.):
    sp_dict = {
        "id": "mysphere" + str(ind),
        "type": "sphere",
        "center": [0, 0, yloc],
        "radius": radius,
        "flip_normals": False,
        "bsdf": {
            "type": "diffuse"
        }
    }
    return sp_dict

group1 = {
    "type": "shapegroup",
    "id": "my_sphere_group",
    "mysphere1": mksphere(1.,0),
    "mysphere2": mksphere(3.,1)
}

scene_dict = {
    "type": "scene",

    "group1": group1,

    "myinstance1": {
       "type": "instance",
       "my_ref": {
            "type": "ref",
            "id": "my_sphere_group",
       },
       "to_world" : ScalarTransform4f.translate([0,10,0])
    },
    "myinstance2": {
       "type": "instance",
       "my_ref": {
            "type": "ref",
            "id": "my_sphere_group",
       },
       "to_world" : ScalarTransform4f.translate([0,-5,2])
    },
}

scene = mitsuba.core.xml.load_dict(scene_dict)
print(scene)