Ejemplo n.º 1
0
    def test_array_curvemap_extend(self):
        bpy.ops.wm.open_mainfile(filepath=test_blend_file)

        light_name = "Light"
        light = D.lights["Light"]
        light_type = light.type
        # extend the source curvemap to 3 points
        src_points = [(0.111, 0.222), (0.333, 0.444), (0.555, 0.666)]
        curve0 = light.falloff_curve.curves[0]
        curve0.points.new(*src_points[2])
        for i, point in enumerate(src_points):
            curve0.points[i].location = point

        self.proxy = BpyBlendProxy()
        self.proxy.load(context)

        light.name = "light_bak"

        light = D.lights.new(light_name, light_type)

        # the dst curvemap has 2 points by default
        # save() needs to extend
        light_proxy = self.proxy.data("lights").data(light_name)
        light_proxy.save(D.lights, light_name, self.proxy.visit_state())
        dst_curve = light.falloff_curve.curves[0]
        self.assertEqual(len(src_points), len(dst_curve.points))
        for i, point in enumerate(src_points):
            for dst, expected in zip(dst_curve.points[i].location, point):
                self.assertAlmostEqual(dst, expected)
Ejemplo n.º 2
0
    def test_write_array_curvemap(self):
        bpy.ops.wm.open_mainfile(filepath=test_blend_file)

        light_name = "Light"
        light = D.lights["Light"]
        points = [(0.111, 0.222), (0.333, 0.444)]
        curve0 = light.falloff_curve.curves[0]
        for i, point in enumerate(points):
            curve0.points[i].location = point

        self.proxy = BpyBlendProxy()
        self.proxy.load(context)

        light.name = "light_bak"
        light_bak = D.lights["light_bak"]
        light = None

        light_proxy = self.proxy.data("lights").data(light_name)
        light_proxy.save(D.lights, light_name, self.proxy.visit_state())
        light = D.lights[light_name]
        curve = light.falloff_curve.curves[0]
        for i, point in enumerate(points):
            for clone, expected in zip(curve.points[i].location, point):
                self.assertAlmostEqual(clone, expected)

        self.assertEqual(D.lights[light_name], light_bak)
Ejemplo n.º 3
0
    def test_all_soa_grease_pencil(self):
        import array

        bpy.ops.object.gpencil_add(type="STROKE")
        proxy = BpyBlendProxy()
        proxy.load(test_context)
        gp_layers = proxy.data("grease_pencils").data("Stroke").data("layers")
        gp_points = gp_layers.data("Lines").data("frames").data(0).data(
            "strokes").data(0).data("points")._data
        expected = (
            ("co", array.array, "f"),
            ("pressure", array.array, "f"),
            ("strength", array.array, "f"),
            ("uv_factor", array.array, "f"),
            ("uv_rotation", array.array, "f"),
            ("select", list, bool),
        )
        for name, type_, element_type in expected:
            self.assertIn("co", gp_points)
            item = gp_points[name]
            self.assertIsInstance(item, SoaElement)
            self.assertIsInstance(item._data, type_)
            if type_ is array.array:
                self.assertEqual(item._data.typecode, element_type)
            else:
                self.assertIsInstance(item._data[0], element_type)

        self.assertEqual(len(gp_points["pressure"]._data),
                         len(gp_points["strength"]._data))
        self.assertEqual(3 * len(gp_points["pressure"]._data),
                         len(gp_points["co"]._data))
Ejemplo n.º 4
0
    def test_extend_array_curvemap(self):
        bpy.ops.wm.open_mainfile(filepath=test_blend_file)

        src_light_name = "Light"
        src_light = D.lights["Light"]
        # extend the source curvemap to 3 points
        src_points = [(0.111, 0.222), (0.333, 0.444), (0.555, 0.666)]
        curve0 = src_light.falloff_curve.curves[0]
        curve0.points.new(*src_points[2])
        for i, point in enumerate(src_points):
            curve0.points[i].location = point

        self.proxy = BpyBlendProxy()
        self.proxy.load(context)
        light_proxy = self.proxy.data("lights").data(src_light_name)
        light_type = light_proxy.data("type")

        # Create a light then restore src_light into it
        dst_light_name = "Dst Light"
        dst_light = D.lights.new(dst_light_name, light_type)
        # patch the light name to restore the proxy into dst_light
        light_proxy.rename(dst_light_name)
        # the dst curvemap has 2 points by default
        # save() needs to extend
        light_proxy.save(D.lights, dst_light_name)
        dst_curve = dst_light.falloff_curve.curves[0]
        self.assertEqual(len(src_points), len(dst_curve.points))
        for i, point in enumerate(src_points):
            for dst, expected in zip(dst_curve.points[i].location, point):
                self.assertAlmostEqual(dst, expected)
Ejemplo n.º 5
0
 def test_camera_focus_object_none(self):
     # test_misc.TestLoadProxy.test_camera_focus_object_none
     self.proxy = BpyBlendProxy()
     self.proxy.load(test_context)
     # load into proxy
     cam_proxy = self.proxy.data("cameras").data("Camera_0")
     focus_object_proxy = cam_proxy.data("dof").data("focus_object")
     self.assertIs(focus_object_proxy, None)
Ejemplo n.º 6
0
    def setUp(self):
        bpy.ops.wm.open_mainfile(filepath=test_blend_file)

        # otherwise the loaded scene  way have curves despite use_curve_mapping==False and
        # the new one will not have curves and will not receive them as they are not send
        # use_curve_mapping == False
        D.scenes["Scene_0"].view_settings.use_curve_mapping = True

        self.proxy = BpyBlendProxy()
        self.proxy.load(context)
        register_bl_equals(self, context)
Ejemplo n.º 7
0
    def test_blenddata_filtered(self):
        blend_data = self.proxy._data
        scene = blend_data["scenes"]._data["Scene_0"]._data
        self.assertTrue("eevee" in scene)

        filter_stack = copy.copy(test_filter)
        filter_stack.append({T.Scene: TypeFilterOut(T.SceneEEVEE)})
        proxy = BpyBlendProxy()
        proxy.load(Context(filter_stack))
        blend_data_ = proxy._data
        scene_ = blend_data_["scenes"]._data["Scene_0"]._data
        self.assertFalse("eevee" in scene_)
Ejemplo n.º 8
0
 def test_camera_focus_object_idref(self):
     # test_misc.TestLoadProxy.test_camera_focus_object_idref
     cam = D.cameras["Camera_0"]
     cam.dof.focus_object = D.objects["Cube"]
     self.proxy = BpyBlendProxy()
     self.proxy.load(test_context)
     # load into proxy
     cam_proxy = self.proxy.data("cameras").data("Camera_0")
     focus_object_proxy = cam_proxy.data("dof").data("focus_object")
     self.assertIsInstance(focus_object_proxy, BpyIDRefProxy)
     self.assertEqual(focus_object_proxy.collection, "objects")
     self.assertEqual(focus_object_proxy.key, "Cube")
Ejemplo n.º 9
0
class TestCodec(unittest.TestCase):
    def setUp(self):
        bpy.ops.wm.open_mainfile(filepath=test_blend_file)
        self.proxy = BpyBlendProxy()
        register_bl_equals(self, test_context)

    def test_camera(self):
        # test_codec.TestCodec.test_camera

        # prepare camera
        transmit_name = "transmit_camera"
        cam_sent = D.cameras["Camera_0"]

        cam_sent.dof.focus_object = D.objects["Cube"]

        # load into proxy
        self.proxy.load(test_context)

        # patch the name so that it does not get mixed up as we restore later in the same scene
        cam_proxy_sent = self.proxy.data("cameras").data("Camera_0")
        cam_proxy_sent._data["name"] = transmit_name
        self.assertIsInstance(cam_proxy_sent, BpyIDProxy)

        # encode
        codec = Codec()
        message = codec.encode(cam_proxy_sent)

        #
        # transmit
        #

        # create
        cam_received = D.cameras.new(transmit_name)

        # decode into proxy
        cam_proxy_received = codec.decode(message)

        focus_object_proxy = cam_proxy_received.data("dof").data(
            "focus_object")
        self.assertIsInstance(focus_object_proxy, BpyIDRefProxy)
        self.assertEqual(focus_object_proxy._datablock_uuid,
                         cam_sent.dof.focus_object.mixer_uuid)

        # save into blender
        cam_proxy_received.save(D.cameras, transmit_name,
                                self.proxy.visit_state())
        self.assertEqual(cam_sent, cam_received)
        pass
Ejemplo n.º 10
0
 def set_experimental_sync(self, experimental_sync: bool):
     if experimental_sync:
         logger.warning("Experimental sync in ON")
         self.proxy = BpyBlendProxy()
     else:
         if self.proxy:
             logger.warning("Experimental sync in OFF")
             self.proxy = None
Ejemplo n.º 11
0
    def test_write_datablock_with_reference_camera_dof_target(self):
        # Write the whole camera datablock, including its reference to dof target

        camera_name = "Camera_0"
        camera = D.cameras[camera_name]

        # setup the scene and reload
        focus_object = D.objects["Cube"]
        camera.dof.focus_object = focus_object
        self.proxy = BpyBlendProxy()
        self.proxy.load(context)

        camera.name = "camera_bak"

        camera_proxy = self.proxy.data("cameras").data(camera_name)
        camera_proxy.save(D.cameras, camera_name, self.proxy.visit_state())
        self.assertEqual(D.cameras[camera_name].dof.focus_object, focus_object)
Ejemplo n.º 12
0
    def test_array_curvemap_shrink(self):
        bpy.ops.wm.open_mainfile(filepath=test_blend_file)

        light_name = "Light"
        light = D.lights["Light"]
        src_points = [(0.666, 0.777), (0.888, 0.999)]
        curve0 = light.falloff_curve.curves[0]
        for i, point in enumerate(src_points):
            curve0.points[i].location = point

        self.proxy = BpyBlendProxy()
        self.proxy.load(context)

        light.name = "light_bak"
        light = None

        light_proxy = self.proxy.data("lights").data(light_name)

        light_proxy.save(D.lights, light_name, self.proxy.visit_state())
        light = D.lights[light_name]

        dst_curve = light.falloff_curve.curves[0]
        self.assertEqual(len(src_points), len(dst_curve.points))

        # extend the dst curvemap to 3 points
        dst_points = [(0.111, 0.222), (0.333, 0.444), (0.555, 0.666)]
        curve0 = light.falloff_curve.curves[0]
        curve0.points.new(*dst_points[2])
        for i, point in enumerate(dst_points):
            curve0.points[i].location = point
        self.assertEqual(len(dst_points), len(dst_curve.points))

        # restore again, save needs to shrink
        light_proxy.save(D.lights, light_name, self.proxy.visit_state())
        light = D.lights[light_name]

        dst_curve = light.falloff_curve.curves[0]
        self.assertEqual(len(src_points), len(dst_curve.points))
        for i, point in enumerate(src_points):
            for dst, expected in zip(dst_curve.points[i].location, point):
                self.assertAlmostEqual(dst, expected)
Ejemplo n.º 13
0
    def test_write_array_curvemap(self):
        bpy.ops.wm.open_mainfile(filepath=test_blend_file)

        light_name = "Light"
        light = D.lights["Light"]
        points = [(0.111, 0.222), (0.333, 0.444)]
        curve0 = light.falloff_curve.curves[0]
        for i, point in enumerate(points):
            curve0.points[i].location = point

        self.proxy = BpyBlendProxy()
        self.proxy.load(context)
        clone_name = f"Clone of {light_name}"
        light_proxy = self.proxy._data["lights"]._data[light_name]
        light_type = light_proxy._data["type"]
        light_proxy.rename(clone_name)
        clone_light = D.lights.new(clone_name, light_type)
        light_proxy.save()
        clone_curve = clone_light.falloff_curve.curves[0]
        for i, point in enumerate(points):
            for clone, expected in zip(clone_curve.points[i].location, point):
                self.assertAlmostEqual(clone, expected)
Ejemplo n.º 14
0
    def execute(self, context):
        # Cannot import at module level, since it requires access to bpy.data which is not
        # accessible during module load
        from mixer.blender_data.proxy import BpyBlendProxy
        from mixer.blender_data.filter import test_context
        import cProfile
        import io
        import pstats
        from pstats import SortKey

        profile_cumulative = get_props().profile_cumulative
        profile_callers = get_props().profile_callers
        profile = profile_callers or profile_cumulative
        proxy = BpyBlendProxy()
        if profile:
            pr = cProfile.Profile()
            pr.enable()
        t1 = time.time()
        proxy.load(test_context)
        t2 = time.time()
        if profile:
            pr.disable()
            s = io.StringIO()
            sortby = SortKey.CUMULATIVE
            ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
            if profile_cumulative:
                ps.print_stats()
            if profile_callers:
                ps.print_callers()
            print(s.getvalue())

        logger.warning(f"Elapse: {t2 - t1} s.")
        non_empty = proxy.get_non_empty_collections()
        logger.info(
            f"Number of non empty collections in proxy: {len(non_empty)}")

        # Put breakpoint here and examinate non_empty dictionnary
        return {"FINISHED"}
Ejemplo n.º 15
0
    def test_write_camera_dof_target(self):
        # test_write.TestWriteAttribute.test_write_camera_dof_target
        # write an ID ref
        bpy.ops.wm.open_mainfile(filepath=test_blend_file)

        src_camera_name = "Camera_0"
        src_camera = D.cameras[src_camera_name]
        focus_object = D.objects["Cube"]
        src_camera.dof.focus_object = focus_object

        self.proxy = BpyBlendProxy()
        self.proxy.load(context)
        camera_proxy = self.proxy.data("cameras").data(src_camera_name)

        # Create a light then restore src_light into it
        dst_camera_name = "Dst Camera"
        dst_camera = D.cameras.new(dst_camera_name)

        # patch the light name to restore the proxy into dst_light
        camera_proxy.rename(dst_camera_name)
        # save() needs to shrink the dst curvemap
        camera_proxy.save(D.cameras)
        self.assertEqual(dst_camera.dof.focus_object, focus_object)
Ejemplo n.º 16
0
 def test_load_as(self):
     proxy = BpyBlendProxy()
     proxy.load(test_context)
     root_ids = proxy.root_ids
     self.assertEqual(
         LoadElementAs.STRUCT,
         load_as_what(T.Scene.bl_rna.properties["display"],
                      bpy.data.scenes[0].display, root_ids),
     )
     self.assertEqual(
         LoadElementAs.ID_REF,
         load_as_what(T.Scene.bl_rna.properties["objects"],
                      bpy.data.scenes[0].objects, root_ids),
     )
     self.assertEqual(
         LoadElementAs.ID_REF,
         load_as_what(T.Scene.bl_rna.properties["world"],
                      bpy.data.scenes[0].world, root_ids),
     )
     self.assertEqual(
         LoadElementAs.ID_DEF,
         load_as_what(T.Scene.bl_rna.properties["collection"],
                      bpy.data.scenes[0].collection, root_ids),
     )
Ejemplo n.º 17
0
class TestWriteAttribute(unittest.TestCase):
    def setUp(self):
        bpy.ops.wm.open_mainfile(filepath=test_blend_file)

        # otherwise the loaded scene  way have curves despite use_curve_mapping==False and
        # the new one will not have curves and will not receive them as they are not send
        # use_curve_mapping == False
        D.scenes["Scene_0"].view_settings.use_curve_mapping = True

        self.proxy = BpyBlendProxy()
        self.proxy.load(context)
        register_bl_equals(self, context)

    def test_write_simple_types(self):
        scene = D.scenes[0]
        object_ = D.objects[0]
        # matrix = [10.0, 20.0, 30.0, 40.0, 11.0, 21.0, 31.0, 41.0, 12.0, 22.0, 32.0, 42.0, 14.0, 24.0, 34.0, 44]
        matrix2 = [[10.0, 20.0, 30.0, 40], [11.0, 21.0, 31.0, 41],
                   [12.0, 22.0, 32.0, 42], [14.0, 24.0, 34.0, 44]]
        values = [
            # (scene, "name", "Plop"),
            (scene, "frame_current", 99),
            (scene, "use_gravity", False),
            (scene, "gravity", [-1, -2, -3]),
            (scene, "gravity", Vector([-10, -20, -30])),
            (scene, "sync_mode", "FRAME_DROP"),
            # (object_, "matrix_world", matrix),
            (object_, "matrix_world", Matrix(matrix2)),
        ]
        for bl_instance, name, value in values:
            write_attribute(bl_instance, name, value, self.proxy.visit_state())
            stored_value = getattr(bl_instance, name)
            stored_type = type(stored_value)
            self.assertEqual(stored_type(value), stored_value)

    def test_write_bpy_struct_scene_eevee(self):
        scene = D.scenes[0]
        eevee_proxy = self.proxy._data["scenes"]._data["Scene_0"]._data[
            "eevee"]
        eevee_proxy._data["gi_cubemap_resolution"] = "64"
        eevee_proxy.save(scene, "eevee", self.proxy.visit_state())
        self.assertEqual("64", scene.eevee.gi_cubemap_resolution)

    def test_write_bpy_property_group_scene_cycles(self):
        # Not very useful it derives from struct
        scene = D.scenes[0]
        cycles_proxy = self.proxy._data["scenes"]._data["Scene_0"]._data[
            "cycles"]
        cycles_proxy._data["shading_system"] = True
        cycles_proxy.save(scene, "cycles", self.proxy.visit_state())
        self.assertEqual(True, scene.cycles.shading_system)

    def test_write_array_of_struct_with_vec(self):
        # self.addTypeEqualityFunc(D.bpy_struct, bl_equalityfunc)
        cube = D.meshes["Cube"]
        vertices_proxy = self.proxy._data["meshes"]._data["Cube"]._data[
            "vertices"]

        # loaded as SOA into array.array
        co_proxy = vertices_proxy._data["co"]._data

        # first vertex
        co_proxy[0] *= 2
        co_proxy[1] *= 2
        co_proxy[2] *= 2

        vertices_proxy.save(cube, "vertices", self.proxy.visit_state())
        self.assertListEqual(list(cube.vertices[0].co[0:3]),
                             co_proxy[0:3].tolist())

    # explicit test per data type , including addition in collections

    def test_write_datablock_light(self):
        # Write a whole scene datablock
        light_name = "Light"
        light = D.lights[light_name]
        light_type = light.type
        light_proxy = self.proxy.data("lights").data(light_name)

        light.name = "light_bak"
        light_bak = D.lights["light_bak"]

        light = D.lights.new(light_name, light_type)
        light_proxy.save(D.lights, light_name, self.proxy.visit_state())
        self.assertEqual(D.lights[light_name], light_bak)

    def test_write_datablock_world(self):
        # Write a whole scene datablock
        world_name = "World"
        world = D.worlds[world_name]
        world_proxy = self.proxy.data("worlds").data(world_name)

        world.name = "world_bak"
        world_bak = D.worlds["world_bak"]

        world = D.worlds.new(world_name)
        world_proxy.save(D.worlds, world_name, self.proxy.visit_state())
        self.assertEqual(D.worlds[world_name], world_bak)

    def test_write_array_curvemap(self):
        bpy.ops.wm.open_mainfile(filepath=test_blend_file)

        light_name = "Light"
        light = D.lights["Light"]
        points = [(0.111, 0.222), (0.333, 0.444)]
        curve0 = light.falloff_curve.curves[0]
        for i, point in enumerate(points):
            curve0.points[i].location = point

        self.proxy = BpyBlendProxy()
        self.proxy.load(context)

        light.name = "light_bak"
        light_bak = D.lights["light_bak"]
        light = None

        light_proxy = self.proxy.data("lights").data(light_name)
        light_proxy.save(D.lights, light_name, self.proxy.visit_state())
        light = D.lights[light_name]
        curve = light.falloff_curve.curves[0]
        for i, point in enumerate(points):
            for clone, expected in zip(curve.points[i].location, point):
                self.assertAlmostEqual(clone, expected)

        self.assertEqual(D.lights[light_name], light_bak)

    def test_array_curvemap_shrink(self):
        bpy.ops.wm.open_mainfile(filepath=test_blend_file)

        light_name = "Light"
        light = D.lights["Light"]
        src_points = [(0.666, 0.777), (0.888, 0.999)]
        curve0 = light.falloff_curve.curves[0]
        for i, point in enumerate(src_points):
            curve0.points[i].location = point

        self.proxy = BpyBlendProxy()
        self.proxy.load(context)

        light.name = "light_bak"
        light = None

        light_proxy = self.proxy.data("lights").data(light_name)

        light_proxy.save(D.lights, light_name, self.proxy.visit_state())
        light = D.lights[light_name]

        dst_curve = light.falloff_curve.curves[0]
        self.assertEqual(len(src_points), len(dst_curve.points))

        # extend the dst curvemap to 3 points
        dst_points = [(0.111, 0.222), (0.333, 0.444), (0.555, 0.666)]
        curve0 = light.falloff_curve.curves[0]
        curve0.points.new(*dst_points[2])
        for i, point in enumerate(dst_points):
            curve0.points[i].location = point
        self.assertEqual(len(dst_points), len(dst_curve.points))

        # restore again, save needs to shrink
        light_proxy.save(D.lights, light_name, self.proxy.visit_state())
        light = D.lights[light_name]

        dst_curve = light.falloff_curve.curves[0]
        self.assertEqual(len(src_points), len(dst_curve.points))
        for i, point in enumerate(src_points):
            for dst, expected in zip(dst_curve.points[i].location, point):
                self.assertAlmostEqual(dst, expected)

    def test_array_curvemap_extend(self):
        bpy.ops.wm.open_mainfile(filepath=test_blend_file)

        light_name = "Light"
        light = D.lights["Light"]
        light_type = light.type
        # extend the source curvemap to 3 points
        src_points = [(0.111, 0.222), (0.333, 0.444), (0.555, 0.666)]
        curve0 = light.falloff_curve.curves[0]
        curve0.points.new(*src_points[2])
        for i, point in enumerate(src_points):
            curve0.points[i].location = point

        self.proxy = BpyBlendProxy()
        self.proxy.load(context)

        light.name = "light_bak"

        light = D.lights.new(light_name, light_type)

        # the dst curvemap has 2 points by default
        # save() needs to extend
        light_proxy = self.proxy.data("lights").data(light_name)
        light_proxy.save(D.lights, light_name, self.proxy.visit_state())
        dst_curve = light.falloff_curve.curves[0]
        self.assertEqual(len(src_points), len(dst_curve.points))
        for i, point in enumerate(src_points):
            for dst, expected in zip(dst_curve.points[i].location, point):
                self.assertAlmostEqual(dst, expected)

    def test_write_datablock_scene(self):
        # Write a whole scene datablock
        scene_name = "Scene_0"
        scene = D.scenes[scene_name]
        scene_proxy = self.proxy.data("scenes").data(scene_name)
        self.assertIsInstance(scene_proxy, BpyIDProxy)

        scene.name = "scene_bak"
        scene_bak = D.scenes["scene_bak"]

        scene_proxy.save(D.scenes, scene_name, self.proxy.visit_state())
        self.assertEqual(D.scenes[scene_name], scene_bak)

    def test_write_datablock_reference_scene_world(self):
        # just write the Scene.world attribute
        scene_name = "Scene_0"
        scene = D.scenes[scene_name]
        expected_world = scene.world
        assert expected_world is not None

        world_ref_proxy = self.proxy.data("scenes").data(scene_name).data(
            "world")
        self.assertIsInstance(world_ref_proxy, BpyIDRefProxy)

        scene.world = None
        assert scene.world != expected_world

        world_ref_proxy.save(scene, "world", self.proxy.visit_state())
        self.assertEqual(scene.world, expected_world)

    def test_write_datablock_with_reference_camera_dof_target(self):
        # Write the whole camera datablock, including its reference to dof target

        camera_name = "Camera_0"
        camera = D.cameras[camera_name]

        # setup the scene and reload
        focus_object = D.objects["Cube"]
        camera.dof.focus_object = focus_object
        self.proxy = BpyBlendProxy()
        self.proxy.load(context)

        camera.name = "camera_bak"

        camera_proxy = self.proxy.data("cameras").data(camera_name)
        camera_proxy.save(D.cameras, camera_name, self.proxy.visit_state())
        self.assertEqual(D.cameras[camera_name].dof.focus_object, focus_object)
Ejemplo n.º 18
0
 def setUp(self):
     file = test_blend_file
     # file = r"D:\work\data\test_files\BlenderSS 2_82.blend"
     bpy.ops.wm.open_mainfile(filepath=file)
     self.proxy = BpyBlendProxy()
     self.proxy.load(test_context)
Ejemplo n.º 19
0
class TestWorld(unittest.TestCase):
    def setUp(self):
        self.bpy_data_proxy = BpyBlendProxy()
        self.diff = BpyBlendDiff()
        bpy.data.worlds[0].name = "World"
        register_bl_equals(self, safe_context)

    def test_world(self):
        world = bpy.data.worlds[0]
        world.use_nodes = True
        self.assertGreaterEqual(len(world.node_tree.nodes), 2)

        self.diff.diff(self.bpy_data_proxy, safe_context)
        sent_ids = {}
        sent_ids.update({("worlds", world.name): world})

        changeset = self.bpy_data_proxy.update(self.diff, safe_context)
        updates = changeset.creations
        # avoid clash on restore
        world.name = world.name + "_bak"

        codec = Codec()
        for update in updates:
            key = (update.collection_name, update.data("name"))
            sent_id = sent_ids.get(key)
            if sent_id is None:
                continue

            encoded = codec.encode(update)
            # sender side
            #######################
            # receiver side
            decoded = codec.decode(encoded)
            created = self.bpy_data_proxy.update_datablock(decoded)
            self.assertEqual(created, sent_id)

    def test_non_existing(self):
        world = bpy.data.worlds[0]

        self.diff.diff(self.bpy_data_proxy, safe_context)
        sent_ids = {}
        sent_ids.update({("worlds", world.name): world})

        changeset = self.bpy_data_proxy.update(self.diff, safe_context)
        creations = changeset.creations
        # avoid clash on restore
        world.name = world.name + "_bak"

        codec = Codec()
        for update in creations:
            key = (update.collection_name, update.data("name"))
            sent_id = sent_ids.get(key)
            if sent_id is None:
                continue

            # create a property on the send proxy and test that is does not fail on the receiver
            # property on ID
            update._data["does_not_exist_property"] = ""
            update._data["does_not_exist_struct"] = BpyStructProxy()
            update._data["does_not_exist_ID"] = BpyIDProxy()

            encoded = codec.encode(update)
            # sender side
            #######################
            # receiver side
            decoded = codec.decode(encoded)
            created = self.bpy_data_proxy.update_datablock(decoded)
            self.assertEqual(created, sent_id)
Ejemplo n.º 20
0
class TestWriteAttribute(unittest.TestCase):
    def setUp(self):
        bpy.ops.wm.open_mainfile(filepath=test_blend_file)

        # otherwise the loaded scene  way have curves despite use_curve_mapping==False and
        # the new one will not have curves and will not receive them as they are not send
        # use_curve_mapping == False
        D.scenes["Scene_0"].view_settings.use_curve_mapping = True

        self.proxy = BpyBlendProxy()
        self.proxy.load(context)
        register_bl_equals(self, context)

    def test_write_simple_types(self):
        scene = D.scenes[0]
        object_ = D.objects[0]
        # matrix = [10.0, 20.0, 30.0, 40.0, 11.0, 21.0, 31.0, 41.0, 12.0, 22.0, 32.0, 42.0, 14.0, 24.0, 34.0, 44]
        matrix2 = [[10.0, 20.0, 30.0, 40], [11.0, 21.0, 31.0, 41],
                   [12.0, 22.0, 32.0, 42], [14.0, 24.0, 34.0, 44]]
        values = [
            # (scene, "name", "Plop"),
            (scene, "frame_current", 99),
            (scene, "use_gravity", False),
            (scene, "gravity", [-1, -2, -3]),
            (scene, "gravity", Vector([-10, -20, -30])),
            (scene, "sync_mode", "FRAME_DROP"),
            # (object_, "matrix_world", matrix),
            (object_, "matrix_world", Matrix(matrix2)),
        ]
        for bl_instance, name, value in values:
            write_attribute(bl_instance, name, value)
            stored_value = getattr(bl_instance, name)
            stored_type = type(stored_value)
            self.assertEqual(stored_type(value), stored_value)

    def test_write_bpy_struct_scene_eevee(self):
        scene = D.scenes[0]
        eevee_proxy = self.proxy._data["scenes"]._data["Scene_0"]._data[
            "eevee"]
        eevee_proxy._data["gi_cubemap_resolution"] = "64"
        eevee_proxy.save(scene, "eevee")
        self.assertEqual("64", scene.eevee.gi_cubemap_resolution)

    def test_write_bpy_property_group_scene_cycles(self):
        # Not very useful it derives from struct
        scene = D.scenes[0]
        cycles_proxy = self.proxy._data["scenes"]._data["Scene_0"]._data[
            "cycles"]
        cycles_proxy._data["shading_system"] = True
        cycles_proxy.save(scene, "cycles")
        self.assertEqual(True, scene.cycles.shading_system)

    def test_write_array_of_struct_with_vec(self):
        # self.addTypeEqualityFunc(D.bpy_struct, bl_equalityfunc)
        cube = D.meshes["Cube"]
        vertices_proxy = self.proxy._data["meshes"]._data["Cube"]._data[
            "vertices"]

        # loaded as SOA into array.array
        co_proxy = vertices_proxy._data["co"]._data

        # first vertex
        co_proxy[0] *= 2
        co_proxy[1] *= 2
        co_proxy[2] *= 2

        vertices_proxy.save(cube, "vertices")
        self.assertListEqual(list(cube.vertices[0].co[0:3]),
                             co_proxy[0:3].tolist())

    # explicit test per data type , including addition in collections

    def test_write_light(self):
        light_name = "Light"
        clone_name = f"Clone of {light_name}"
        light_proxy = self.proxy._data["lights"]._data[light_name]
        expected_energy = 666
        light_proxy._data["energy"] = expected_energy
        light_type = light_proxy._data["type"]
        light_proxy.rename(clone_name)
        clone_light = D.lights.new(clone_name, light_type)
        light_proxy.save()
        self.assertEqual(clone_light.energy, expected_energy)

    def test_write_world(self):
        # test_write.TestWriteAttribute.test_write_world
        world_name = "World"
        clone_name = f"Clone of {world_name}"
        world_clone = D.worlds.new(clone_name)
        world_proxy = self.proxy._data["worlds"]._data[world_name]
        world_proxy.rename(clone_name)
        world_proxy.save()
        self.assertEqual(world_clone, D.worlds[world_name])

    def test_write_array_curvemap(self):
        bpy.ops.wm.open_mainfile(filepath=test_blend_file)

        light_name = "Light"
        light = D.lights["Light"]
        points = [(0.111, 0.222), (0.333, 0.444)]
        curve0 = light.falloff_curve.curves[0]
        for i, point in enumerate(points):
            curve0.points[i].location = point

        self.proxy = BpyBlendProxy()
        self.proxy.load(context)
        clone_name = f"Clone of {light_name}"
        light_proxy = self.proxy._data["lights"]._data[light_name]
        light_type = light_proxy._data["type"]
        light_proxy.rename(clone_name)
        clone_light = D.lights.new(clone_name, light_type)
        light_proxy.save()
        clone_curve = clone_light.falloff_curve.curves[0]
        for i, point in enumerate(points):
            for clone, expected in zip(clone_curve.points[i].location, point):
                self.assertAlmostEqual(clone, expected)

    def test_write_camera_dof_target(self):
        # test_write.TestWriteAttribute.test_write_camera_dof_target
        # write an ID ref
        bpy.ops.wm.open_mainfile(filepath=test_blend_file)

        src_camera_name = "Camera_0"
        src_camera = D.cameras[src_camera_name]
        focus_object = D.objects["Cube"]
        src_camera.dof.focus_object = focus_object

        self.proxy = BpyBlendProxy()
        self.proxy.load(context)
        camera_proxy = self.proxy.data("cameras").data(src_camera_name)

        # Create a light then restore src_light into it
        dst_camera_name = "Dst Camera"
        dst_camera = D.cameras.new(dst_camera_name)

        # patch the light name to restore the proxy into dst_light
        camera_proxy.rename(dst_camera_name)
        # save() needs to shrink the dst curvemap
        camera_proxy.save(D.cameras)
        self.assertEqual(dst_camera.dof.focus_object, focus_object)

    def test_shrink_array_curvemap(self):
        bpy.ops.wm.open_mainfile(filepath=test_blend_file)

        src_light_name = "Light"
        src_light = D.lights["Light"]
        src_points = [(0.666, 0.777), (0.888, 0.999)]
        curve0 = src_light.falloff_curve.curves[0]
        for i, point in enumerate(src_points):
            curve0.points[i].location = point

        self.proxy = BpyBlendProxy()
        self.proxy.load(context)
        light_proxy = self.proxy.data("lights").data(src_light_name)
        light_type = light_proxy.data("type")

        # Create a light then restore src_light into it
        dst_light_name = "Dst Light"
        dst_light = D.lights.new(dst_light_name, light_type)
        # extend the dst curvemap to 3 points
        dst_points = [(0.111, 0.222), (0.333, 0.444), (0.555, 0.666)]
        curve0 = dst_light.falloff_curve.curves[0]
        curve0.points.new(*dst_points[2])
        for i, point in enumerate(dst_points):
            curve0.points[i].location = point

        # patch the light name to restore the proxy into dst_light
        light_proxy.rename(dst_light_name)
        # save() needs to shrink the dst curvemap
        light_proxy.save(D.lights, dst_light_name)
        dst_curve = dst_light.falloff_curve.curves[0]
        self.assertEqual(len(src_points), len(dst_curve.points))
        for i, point in enumerate(src_points):
            for dst, expected in zip(dst_curve.points[i].location, point):
                self.assertAlmostEqual(dst, expected)

    def test_extend_array_curvemap(self):
        bpy.ops.wm.open_mainfile(filepath=test_blend_file)

        src_light_name = "Light"
        src_light = D.lights["Light"]
        # extend the source curvemap to 3 points
        src_points = [(0.111, 0.222), (0.333, 0.444), (0.555, 0.666)]
        curve0 = src_light.falloff_curve.curves[0]
        curve0.points.new(*src_points[2])
        for i, point in enumerate(src_points):
            curve0.points[i].location = point

        self.proxy = BpyBlendProxy()
        self.proxy.load(context)
        light_proxy = self.proxy.data("lights").data(src_light_name)
        light_type = light_proxy.data("type")

        # Create a light then restore src_light into it
        dst_light_name = "Dst Light"
        dst_light = D.lights.new(dst_light_name, light_type)
        # patch the light name to restore the proxy into dst_light
        light_proxy.rename(dst_light_name)
        # the dst curvemap has 2 points by default
        # save() needs to extend
        light_proxy.save(D.lights, dst_light_name)
        dst_curve = dst_light.falloff_curve.curves[0]
        self.assertEqual(len(src_points), len(dst_curve.points))
        for i, point in enumerate(src_points):
            for dst, expected in zip(dst_curve.points[i].location, point):
                self.assertAlmostEqual(dst, expected)

    def test_write_scene(self):
        scene_name = "Scene_0"
        scene = D.scenes[scene_name]
        clone_name = f"Clone of {scene_name}"
        scene_proxy = self.proxy._data["scenes"]._data[scene_name]
        scene_proxy.rename(clone_name)
        clone = D.scenes.new(clone_name)
        scene_proxy.save(D.scenes, clone_name)
        self.assertEqual(scene, clone)

    def test_write_scene_world(self):
        scene_name = "Scene_0"
        scene = D.scenes[scene_name]
        # scene.world = D.worlds["World_1"]
        clone_name = f"Clone of {scene_name}"
        world_proxy = self.proxy._data["scenes"]._data[scene_name]._data[
            "world"]
        clone = D.scenes.new(clone_name)
        world_proxy.save(clone, "world")
        self.assertEqual(scene.world, clone.world)
Ejemplo n.º 21
0
 def setUp(self):
     self.bpy_data_proxy = BpyBlendProxy()
     self.diff = BpyBlendDiff()
     bpy.data.worlds[0].name = "World"
     register_bl_equals(self, safe_context)
Ejemplo n.º 22
0
 def setUp(self):
     for w in D.worlds:
         D.worlds.remove(w)
     self.proxy = BpyBlendProxy()
Ejemplo n.º 23
0
class TestDiff(unittest.TestCase):
    def setUp(self):
        for w in D.worlds:
            D.worlds.remove(w)
        self.proxy = BpyBlendProxy()

    def test_create(self):
        # test_diff.TestDiff.test_create
        self.proxy.load(test_context)
        new_worlds = ["W0", "W1"]
        new_worlds.sort()
        for w in new_worlds:
            D.worlds.new(w)
        diff = BpyBlendDiff()
        diff.diff(self.proxy, test_context)
        for name, delta in diff.collection_deltas:
            self.assertEqual(0, len(delta.items_removed),
                             f"removed count mismatch for {name}")
            self.assertEqual(0, len(delta.items_renamed),
                             f"renamed count mismatch for {name}")
            if name == "worlds":
                self.assertEqual(len(new_worlds), len(delta.items_added),
                                 f"added count mismatch for {name}")
                found = list(delta.items_added.keys())
                found.sort()
                self.assertEqual(new_worlds, found,
                                 f"added count mismatch for {name}")
            else:
                self.assertEqual(0, len(delta.items_added),
                                 f"added count mismatch for {name}")

    def test_remove(self):
        # test_diff.TestDiff.test_create
        new_worlds = ["W0", "W1", "W2"]
        new_worlds.sort()
        for w in new_worlds:
            D.worlds.new(w)

        self.proxy.load(test_context)

        removed = ["W0", "W1"]
        removed.sort()
        for w in removed:
            D.worlds.remove(D.worlds[w])

        diff = BpyBlendDiff()
        diff.diff(self.proxy, test_context)
        for name, delta in diff.collection_deltas:
            self.assertEqual(0, len(delta.items_added),
                             f"added count mismatch for {name}")
            self.assertEqual(0, len(delta.items_renamed),
                             f"renamed count mismatch for {name}")
            if name == "worlds":
                self.assertEqual(len(removed), len(delta.items_removed),
                                 f"removed count mismatch for {name}")
                items_removed = [
                    proxy.data("name") for proxy in delta.items_removed
                ]
                items_removed.sort()
                self.assertEqual(removed, items_removed,
                                 f"removed count mismatch for {name}")
            else:
                self.assertEqual(0, len(delta.items_added),
                                 f"added count mismatch for {name}")

    def test_rename(self):
        # test_diff.TestDiff.test_create
        new_worlds = ["W0", "W1", "W2"]
        new_worlds.sort()
        for w in new_worlds:
            D.worlds.new(w)

        self.proxy.load(test_context)

        renamed = [("W0", "W00"), ("W2", "W22")]
        renamed.sort(key=sort_renamed_item)
        for old_name, new_name in renamed:
            D.worlds[old_name].name = new_name

        diff = BpyBlendDiff()
        diff.diff(self.proxy, test_context)
        for name, delta in diff.collection_deltas:
            self.assertEqual(0, len(delta.items_added),
                             f"added count mismatch for {name}")
            self.assertEqual(0, len(delta.items_removed),
                             f"removed count mismatch for {name}")
            if name == "worlds":
                self.assertEqual(len(renamed), len(delta.items_renamed),
                                 f"renamed count mismatch for {name}")
                items_renamed = list(delta.items_renamed)
                items_renamed.sort(key=sort_renamed_item)
                items_renamed = [(proxy.data("name"), new_name)
                                 for proxy, new_name in items_renamed]
                self.assertEqual(renamed, items_renamed,
                                 f"removed count mismatch for {name}")
            else:
                self.assertEqual(0, len(delta.items_added),
                                 f"added count mismatch for {name}")

    def test_create_delete_rename(self):
        # test_diff.TestDiff.test_create
        new_worlds = ["W0", "W1", "W2", "W4"]
        new_worlds.sort()
        for w in new_worlds:
            D.worlds.new(w)

        self.proxy.load(test_context)

        renamed = [("W0", "W00"), ("W2", "W22"), ("W4", "W44")]
        renamed.sort(key=sort_renamed_item)
        for old_name, new_name in renamed:
            D.worlds[old_name].name = new_name

        added = ["W0", "W5"]
        added.sort()
        for w in added:
            D.worlds.new(w)

        removed = ["W1", "W00"]
        removed.sort()
        for w in removed:
            D.worlds.remove(D.worlds[w])

        diff = BpyBlendDiff()
        diff.diff(self.proxy, test_context)
        for name, delta in diff.collection_deltas:
            if name == "worlds":
                items_added = list(delta.items_added.keys())
                items_added.sort()
                self.assertEqual(items_added, ["W0", "W5"],
                                 f"added count mismatch for {name}")

                items_renamed = delta.items_renamed
                items_renamed.sort(key=sort_renamed_item)
                items_renamed = [(proxy.data("name"), new_name)
                                 for proxy, new_name in items_renamed]
                self.assertEqual(items_renamed, [("W2", "W22"), ("W4", "W44")],
                                 f"renamed count mismatch for {name}")

                items_removed = [
                    proxy.data("name") for proxy in delta.items_removed
                ]
                items_removed.sort()
                self.assertEqual(items_removed, ["W0", "W1"],
                                 f"removed count mismatch for {name}")
            else:
                self.assertEqual(0, len(delta.items_renamed),
                                 f"renamed count mismatch for {name}")
                self.assertEqual(0, len(delta.items_removed),
                                 f"removed count mismatch for {name}")
                self.assertEqual(0, len(delta.items_added),
                                 f"added count mismatch for {name}")
Ejemplo n.º 24
0
class TestLoadProxy(unittest.TestCase):
    def setUp(self):
        file = test_blend_file
        # file = r"D:\work\data\test_files\BlenderSS 2_82.blend"
        bpy.ops.wm.open_mainfile(filepath=file)
        self.proxy = BpyBlendProxy()
        self.proxy.load(test_context)

    def check(self, item, expected_elements):
        self.assertSetEqual(set(item._data.keys()), set(expected_elements))

    # @unittest.skip("")
    def test_blenddata(self):
        blend_data = self.proxy._data
        expected_data = {
            "scenes", "collections", "objects", "materials", "lights"
        }
        self.assertTrue(all([e in blend_data.keys() for e in expected_data]))

        self.check(self.proxy._data["scenes"], {"Scene_0", "Scene_1"})
        self.check(self.proxy._data["cameras"], {"Camera_0", "Camera_1"})
        self.check(self.proxy._data["objects"],
                   {"Camera_obj_0", "Camera_obj_1", "Cone", "Cube", "Light"})
        self.check(
            self.proxy._data["collections"], {
                "Collection_0_0", "Collection_0_1", "Collection_0_0_0",
                "Collection_1_0"
            })

    def test_blenddata_filtered(self):
        blend_data = self.proxy._data
        scene = blend_data["scenes"]._data["Scene_0"]._data
        self.assertTrue("eevee" in scene)

        filter_stack = copy.copy(test_filter)
        filter_stack.append({T.Scene: TypeFilterOut(T.SceneEEVEE)})
        proxy = BpyBlendProxy()
        proxy.load(Context(filter_stack))
        blend_data_ = proxy._data
        scene_ = blend_data_["scenes"]._data["Scene_0"]._data
        self.assertFalse("eevee" in scene_)

    # @unittest.skip("")
    def test_scene(self):
        # test_misc.TestLoadProxy.test_scene
        scene = self.proxy._data["scenes"]._data["Scene_0"]._data
        # will vary slightly during tiune tuning of the default filter
        self.assertGreaterEqual(len(scene), 45)
        self.assertLessEqual(len(scene), 55)

        # objects = scene["objects"]._data
        # self.assertEqual(4, len(objects))

        # for o in objects.values():
        #     self.assertEqual(type(o), BpyIDRefProxy, o)

        # builtin attributes (floats)
        frame_properties = [
            name for name in scene.keys() if name.startswith("frame_")
        ]
        self.assertEqual(7, len(frame_properties))

        # bpy_struct
        eevee = scene["eevee"]._data
        self.assertEqual(58, len(eevee))

        # Currently mot loaded
        # # PropertiesGroup
        # cycles_proxy = scene["view_layers"]._data["View Layer"]._data["cycles"]
        # self.assertIsInstance(cycles_proxy, BpyPropertyGroupProxy)
        # self.assertEqual(32, len(cycles_proxy._data))

        # # The master collection
        # master_collection = scene["collection"]
        # self.assertIsInstance(master_collection, BpyIDProxy)

    def test_collections(self):
        collections = self.proxy._data["collections"]
        coll_0_0 = collections._data["Collection_0_0"]._data

        coll_0_0_children = coll_0_0["children"]
        self.check(coll_0_0_children, {"Collection_0_0_0"})
        for c in coll_0_0_children._data.values():
            self.assertIsInstance(c, BpyIDRefProxy)

        coll_0_0_objects = coll_0_0["objects"]
        self.check(coll_0_0_objects,
                   {"Camera_obj_0", "Camera_obj_1", "Cube", "Light"})
        for o in coll_0_0_objects._data.values():
            self.assertIsInstance(o, BpyIDRefProxy)

        pass

    def test_camera_focus_object_idref(self):
        # test_misc.TestLoadProxy.test_camera_focus_object_idref
        cam = D.cameras["Camera_0"]
        cam.dof.focus_object = D.objects["Cube"]
        self.proxy = BpyBlendProxy()
        self.proxy.load(test_context)
        # load into proxy
        cam_proxy = self.proxy.data("cameras").data("Camera_0")
        focus_object_proxy = cam_proxy.data("dof").data("focus_object")
        self.assertIsInstance(focus_object_proxy, BpyIDRefProxy)
        self.assertEqual(focus_object_proxy.collection, "objects")
        self.assertEqual(focus_object_proxy.key, "Cube")

    def test_camera_focus_object_none(self):
        # test_misc.TestLoadProxy.test_camera_focus_object_none
        self.proxy = BpyBlendProxy()
        self.proxy.load(test_context)
        # load into proxy
        cam_proxy = self.proxy.data("cameras").data("Camera_0")
        focus_object_proxy = cam_proxy.data("dof").data("focus_object")
        self.assertIs(focus_object_proxy, None)
Ejemplo n.º 25
0
 def setUp(self):
     bpy.ops.wm.open_mainfile(filepath=test_blend_file)
     self.proxy = BpyBlendProxy()
     register_bl_equals(self, test_context)