コード例 #1
0
    def LoadJson(self, json_file):
        self.LoadResource()
        f = open(json_file)
        scene_info = json.loads(f.read())
        #print(scene_info)

        if "Render" in scene_info.keys():
            render_info = scene_info["Render"]
            scene = bpy.context.scene
            scene.render.image_settings.file_format = render_info["format"]
            scene.frame_end = render_info["frame"]
            scene.cycles.samples = render_info["sample"]

        if 'Library' in scene_info.keys():
            for library in scene_info['Library']:
                self.LoadLibrary(Config.resource_path + library + '.blend')

        if 'Materials' in scene_info.keys():
            for material_info in scene_info['Materials']:
                mat_type = material_info['type']
                name = material_info['id']
                if mat_type == "Lego":
                    material = LegoPlasticMaterial(name,
                                                   material_info['color'])
                elif mat_type == "CarPaint":
                    material = CarPaintMaterial(name, material_info['color'])
                elif mat_type == "Glass":
                    material = SimpleGlassMaterial(name,
                                                   material_info['color'], 0.0)
                elif mat_type == "PianoPaint":
                    material = PianoPaintMaterial(name, material_info['color'])
                elif mat_type == "Lollipop":
                    material = LollipopMaterial(name, material_info['color'])
                elif mat_type == "Led":
                    material = LedMaterial(name, material_info['color'])
                elif mat_type == "Diffuse":
                    material = DiffuseMaterial(name, material_info['color'])
                elif mat_type == "Metal":
                    material = MetalMaterial(name, material_info['color'])
                elif mat_type == "Subsurface":
                    material = SubsurfaceMaterial(name, material_info['color'],
                                                  3)
                elif mat_type == "Emission":
                    material = EmissionMaterial(name, material_info['color'],
                                                3)
                else:
                    material = Material(name, material_info['color'])
                self.AddMaterial(material)
                print('Add a material' + name)

        #self.AddMaterial(GlassMaterial('Glass'))
        #self.AddMaterial(SimpleGlassMaterial((0, 1, 0.4, 1.0), 0.0))
        #self.AddMaterial(MetalMaterial((0, 1, 0.4, 1.0)))
        #self.AddMaterial(RandomMaterial("Rand"))

        if 'Objects' in scene_info.keys():
            add_objects = []
            for object_info in scene_info['Objects']:
                entity = None
                if (object_info['type'] == 'cube'):
                    entity = Entity()
                    if self.template_cube == None:
                        bpy.ops.mesh.primitive_cube_add(
                            radius=1,
                            view_align=False,
                            enter_editmode=False,
                            location=object_info['position'],
                            layers=(True, False, False, False, False, False,
                                    False, False, False, False, False, False,
                                    False, False, False, False, False, False,
                                    False, False))
                        #scale = object_info['size']
                        self.template_cube = bpy.context.object
                        self.template_cube.hide_render = True
                        #entity.baseObject = bpy.context.object

                    copy = self.template_cube.copy()
                    copy.hide_render = False
                    copy.data = copy.data.copy(
                    )  # also duplicate mesh, remove for linked duplicate
                    copy.location = object_info['position']
                    copy.scale = (object_info['size'], object_info['size'],
                                  object_info['size'])
                    add_objects.append(copy)
                    entity.baseObject = copy
                    print('Add a Cube' + object_info['id'])

                elif (object_info['type'] == 'lamp'):
                    lamp_data = bpy.data.lamps.new(name=object_info['id'],
                                                   type='POINT')
                    lamp_object = bpy.data.objects.new(name=object_info['id'],
                                                       object_data=lamp_data)
                    # Link lamp object to the scene so it'll appear in this scene
                    bpy.context.scene.objects.link(lamp_object)
                    # Place lamp to a specified location
                    lamp_object.location = object_info['position']
                    lamp_data.color = object_info['color']
                    print('Add a Lamp')
                elif (object_info['type'] == 'model'):
                    entity = Model(object_info['id'] + '.obj')
                    state = State.instance
                    state.model = object_info['id']
                if entity != None:
                    entity.SetMaterial(self.Materials[object_info['material']])
                    self.AddObject(entity)
            for ob in add_objects:
                bpy.context.scene.objects.link(ob)
            bpy.context.scene.update()

        if 'HDR' in scene_info.keys():
            hdr = scene_info['HDR']
            if hdr != None:
                if hdr == 'rand':
                    self.SetEnvironmentMap('{0:02d}.hdr'.format(
                        random.randint(1, 10)))
                else:
                    self.SetEnvironmentMap(hdr + ".hdr")
        else:
            bpy.context.scene.cycles.film_transparent = True

        if 'Camera' in scene_info.keys():
            camera_info = scene_info['Camera']
            camera = bpy.data.objects['Camera']
            camera.location = camera_info['position']
            self.LookAt(camera, camera_info['look'])

        if 'Animation' in scene_info.keys():
            bpy.ops.object.empty_add()
            empty_object = bpy.context.object