def __init__(self): self.display_draw = None self.pipeline = None self.scene = Scene.Scene() self.view_matrix = None self.request_new_frame = True self.request_scene_update = True self.profiling_data = io.StringIO()
def __init__(self): self.display_draw = None self.scene = Scene.Scene() self.view_matrix = None self.request_new_frame = True self.request_scene_update = True self.bridge = MaltPipeline.get_bridge() self.bridge_id = self.bridge.get_viewport_id() if self.bridge else None self.last_frame_time = 0
def __init__(self): self.display_draw = None self.scene = Scene.Scene() self.view_matrix = None self.request_new_frame = True self.request_scene_update = True self.profiling_data = io.StringIO() self.bridge = MaltPipeline.get_bridge() self.bridge_id = self.bridge.get_viewport_id()
def get_parameters(self, overrides, resources): if '_RNA_UI' not in self.keys(): return {} rna = self.get_rna() parameters = {} for key in rna.keys(): if '@' in key: continue if rna[key]['active'] == False: continue result_key = key for override in reversed(overrides): override_key = key + ' @ ' + override if override_key in rna.keys(): if rna[override_key]['active']: key = override_key if rna[key]['type'] in (Type.INT, Type.FLOAT): try: parameters[result_key] = tuple(self[key]) except: parameters[result_key] = self[key] elif rna[key]['type'] == Type.BOOL: parameters[result_key] = bool(self.bools[key].boolean) elif rna[key]['type'] == Type.TEXTURE: texture = self.textures[key].texture if texture: parameters[result_key] = MaltTextures.get_texture(texture) else: parameters[result_key] = None elif rna[key]['type'] == Type.GRADIENT: #TODO: Only works for materials color_ramp = get_color_ramp(self.id_data, key).color_ramp parameters[result_key] = MaltTextures.get_gradient( color_ramp, self.id_data.name_full, key) elif rna[key]['type'] == Type.MATERIAL: material = self.materials[key].material extension = self.materials[key].extension if material: materials = resources['materials'] material_name = material.name_full if material_name not in materials.keys(): shader = { 'path': bpy.path.abspath(material.malt.shader_source), 'parameters': material.malt.parameters.get_parameters( overrides, resources) } material_parameters = material.malt_parameters.get_parameters( overrides, resources) materials[material_name] = Scene.Material( shader, material_parameters) parameters[result_key] = materials[material_name] else: parameters[result_key] = None return parameters
def add_object(obj, matrix): if obj.display_type in [ 'TEXTURED', 'SOLID' ] and obj.type in ('MESH', 'CURVE', 'SURFACE', 'FONT'): material = None if len(obj.material_slots ) > 0 and obj.material_slots[0].material: blend_material = obj.material_slots[0].material material_name = blend_material.name_full if material_name not in materials.keys(): #load material shader = blend_material.malt.get_shader() if shader: pipeline_shaders = shader[ self.get_pipeline().__class__.__name__] parameters = blend_material.malt_parameters.get_parameters( ) materials[material_name] = Scene.Material( pipeline_shaders, parameters) else: materials[material_name] = None material = materials[material_name] if obj.name_full not in meshes: # (Uses obj.original) Malt Parameters are not present in the evaluated mesh parameters = obj.original.data.malt_parameters.get_parameters( ) meshes[obj.name_full] = Scene.Mesh(None, parameters) if depsgraph.mode == 'VIEWPORT': meshes[obj.name_full].mesh = MaltMeshes.get_mesh(obj) else: #always load the mesh for final renders meshes[obj.name_full].mesh = MaltMeshes.load_mesh(obj) mesh = meshes[obj.name_full] if mesh is None: return scale = matrix.to_scale() matrix = flatten_matrix(matrix) result = Scene.Object(matrix, mesh, material, obj.malt_parameters.get_parameters()) if scale[0] * scale[1] * scale[2] < 0.0: result.negative_scale = True scene.objects.append(result) elif obj.type == 'LIGHT': if obj.data.type == 'AREA': return #Not supported malt_light = obj.data.malt light = Scene.Light() light.color = tuple(malt_light.color) light.position = tuple(obj.matrix_world.translation) light.direction = tuple( obj.matrix_world.to_quaternion() @ Vector( (0.0, 0.0, -1.0))) light.radius = malt_light.radius light.spot_angle = malt_light.spot_angle light.spot_blend = malt_light.spot_blend_angle light.parameters = obj.malt_parameters.get_parameters() types = { 'SUN': 1, 'POINT': 2, 'SPOT': 3, } light.type = types[obj.data.type] if light.type == types['SUN']: light.matrix = flatten_matrix( matrix.to_quaternion().to_matrix().to_4x4().inverted()) else: #Scaling too ???? light.matrix = flatten_matrix(matrix.inverted()) scene.lights.append(light)
def load_scene(self, context, depsgraph): def flatten_matrix(matrix): return list(chain.from_iterable(matrix.transposed())) scene = Scene.Scene() scene.parameters = depsgraph.scene_eval.malt_parameters.get_parameters( ) scene.world_parameters = depsgraph.scene_eval.world.malt_parameters.get_parameters( ) scene.frame = depsgraph.scene_eval.frame_current r = depsgraph.scene_eval.render fps = r.fps / r.fps_base remap = r.frame_map_new / r.frame_map_old scene.time = (scene.frame / fps) * remap #Camera if depsgraph.mode == 'VIEWPORT': view_3d = context.region_data camera_matrix = flatten_matrix(view_3d.view_matrix) projection_matrix = flatten_matrix(view_3d.window_matrix) if view_3d.perspective_matrix != self.view_matrix: self.view_matrix = view_3d.perspective_matrix.copy() self.request_new_frame = True scene.camera = Scene.Camera(camera_matrix, projection_matrix) else: camera = depsgraph.scene_eval.camera camera_matrix = flatten_matrix(camera.matrix_world.inverted()) projection_matrix = flatten_matrix( camera.calc_matrix_camera( depsgraph, x=depsgraph.scene_eval.render.resolution_x, y=depsgraph.scene_eval.render.resolution_y)) scene.camera = Scene.Camera(camera_matrix, projection_matrix) #Objects materials = {} meshes = {} def add_object(obj, matrix): if obj.display_type in [ 'TEXTURED', 'SOLID' ] and obj.type in ('MESH', 'CURVE', 'SURFACE', 'FONT'): material = None if len(obj.material_slots ) > 0 and obj.material_slots[0].material: blend_material = obj.material_slots[0].material material_name = blend_material.name_full if material_name not in materials.keys(): #load material shader = blend_material.malt.get_shader() if shader: pipeline_shaders = shader[ self.get_pipeline().__class__.__name__] parameters = blend_material.malt_parameters.get_parameters( ) materials[material_name] = Scene.Material( pipeline_shaders, parameters) else: materials[material_name] = None material = materials[material_name] if obj.name_full not in meshes: # (Uses obj.original) Malt Parameters are not present in the evaluated mesh parameters = obj.original.data.malt_parameters.get_parameters( ) meshes[obj.name_full] = Scene.Mesh(None, parameters) if depsgraph.mode == 'VIEWPORT': meshes[obj.name_full].mesh = MaltMeshes.get_mesh(obj) else: #always load the mesh for final renders meshes[obj.name_full].mesh = MaltMeshes.load_mesh(obj) mesh = meshes[obj.name_full] if mesh is None: return scale = matrix.to_scale() matrix = flatten_matrix(matrix) result = Scene.Object(matrix, mesh, material, obj.malt_parameters.get_parameters()) if scale[0] * scale[1] * scale[2] < 0.0: result.negative_scale = True scene.objects.append(result) elif obj.type == 'LIGHT': if obj.data.type == 'AREA': return #Not supported malt_light = obj.data.malt light = Scene.Light() light.color = tuple(malt_light.color) light.position = tuple(obj.matrix_world.translation) light.direction = tuple( obj.matrix_world.to_quaternion() @ Vector( (0.0, 0.0, -1.0))) light.radius = malt_light.radius light.spot_angle = malt_light.spot_angle light.spot_blend = malt_light.spot_blend_angle light.parameters = obj.malt_parameters.get_parameters() types = { 'SUN': 1, 'POINT': 2, 'SPOT': 3, } light.type = types[obj.data.type] if light.type == types['SUN']: light.matrix = flatten_matrix( matrix.to_quaternion().to_matrix().to_4x4().inverted()) else: #Scaling too ???? light.matrix = flatten_matrix(matrix.inverted()) scene.lights.append(light) for obj in depsgraph.objects: add_object(obj, obj.matrix_world) for instance in depsgraph.object_instances: if instance.instance_object: add_object(instance.instance_object, instance.matrix_world) return scene
def get_scene(self, context, depsgraph, request_scene_update, overrides): if request_scene_update == True: scene = Scene.Scene() self.scene = scene scene = self.scene if hasattr(scene, 'proxys') == False: scene.proxys = {} scene.parameters = depsgraph.scene_eval.malt_parameters.get_parameters( overrides, scene.proxys) scene.world_parameters = depsgraph.scene_eval.world.malt_parameters.get_parameters( overrides, scene.proxys) override_material = scene.world_parameters['Material.Override'] default_material = scene.world_parameters['Material.Default'] scene.frame = depsgraph.scene_eval.frame_current r = depsgraph.scene_eval.render fps = r.fps / r.fps_base remap = r.frame_map_new / r.frame_map_old scene.time = (scene.frame / fps) * remap def flatten_matrix(matrix): return [e for v in matrix.transposed() for e in v] #Camera if depsgraph.mode == 'VIEWPORT': view_3d = context.region_data camera_matrix = flatten_matrix(view_3d.view_matrix) projection_matrix = flatten_matrix(view_3d.window_matrix) if view_3d.perspective_matrix != self.view_matrix: self.view_matrix = view_3d.perspective_matrix.copy() self.request_new_frame = True scene.camera = Scene.Camera(camera_matrix, projection_matrix) #TODO if view_3d.view_perspective == 'CAMERA': camera = depsgraph.scene_eval.camera frame = camera.data.view_frame(scene=depsgraph.scene_eval) screen_space_frame = [] for v in frame: v = view_3d.window_matrix @ view_3d.view_matrix @ camera.matrix_world @ Vector( (v.x, v.y, v.z, 1.0)) v /= v.w v = v * 0.5 + Vector((0.5, 0.5, 0.5, 0.0)) v.w = 0 screen_space_frame.append(v) tr, br, bl, tl = screen_space_frame w = (tr.x - tl.x) * context.region.width h = (tr.y - br.y) * context.region.height else: camera = depsgraph.scene_eval.camera camera_matrix = flatten_matrix(camera.matrix_world.inverted()) projection_matrix = flatten_matrix( camera.calc_matrix_camera( depsgraph, x=depsgraph.scene_eval.render.resolution_x, y=depsgraph.scene_eval.render.resolution_y)) scene.camera = Scene.Camera(camera_matrix, projection_matrix) if request_scene_update == False: return scene meshes = {} #Objects def add_object(obj, matrix, id): if obj.display_type in [ 'TEXTURED', 'SOLID' ] and obj.type in ('MESH', 'CURVE', 'SURFACE', 'META', 'FONT'): name = MaltMeshes.get_mesh_name(obj) if depsgraph.mode == 'RENDER': name = '___F12___' + name if name not in meshes: # (Uses obj.original) Malt Parameters are not present in the evaluated mesh parameters = obj.original.data.malt_parameters.get_parameters( overrides, scene.proxys) malt_mesh = None if depsgraph.mode == 'VIEWPORT': malt_mesh = MaltMeshes.get_mesh(obj) else: #always load the mesh for final renders malt_mesh = MaltMeshes.load_mesh(obj, name) if malt_mesh: meshes[name] = [ Scene.Mesh(submesh, parameters) for submesh in malt_mesh ] for i, mesh in enumerate(meshes[name]): scene.proxys[('mesh', name, i)] = mesh.mesh else: meshes[name] = None mesh = meshes[name] if mesh is None: return scale = matrix.to_scale() mirror_scale = scale[0] * scale[1] * scale[2] < 0.0 matrix = flatten_matrix(matrix) obj_parameters = obj.malt_parameters.get_parameters( overrides, scene.proxys) obj_parameters['ID'] = id if len(obj.material_slots) > 0: for i, slot in enumerate(obj.material_slots): material = default_material if slot.material and slot.material.malt.get_source_path( ) != '': material_name = slot.material.name_full material_key = ('material', material_name) if material_key not in scene.proxys.keys(): path = slot.material.malt.get_source_path() shader_parameters = slot.material.malt.parameters.get_parameters( overrides, scene.proxys) material_parameters = slot.material.malt_parameters.get_parameters( overrides, scene.proxys) from Bridge.Proxys import MaterialProxy scene.proxys[material_key] = MaterialProxy( path, shader_parameters, material_parameters) material = scene.proxys[material_key] if override_material: material = override_material result = Scene.Object(matrix, mesh[i], material, obj_parameters, mirror_scale) scene.objects.append(result) else: material = default_material if override_material: material = override_material result = Scene.Object(matrix, mesh[0], material, obj_parameters, mirror_scale) scene.objects.append(result) elif obj.type == 'LIGHT': if obj.data.type == 'AREA': return #Not supported malt_light = obj.data.malt light = Scene.Light() light.color = tuple(obj.data.color * malt_light.strength) light.position = tuple(obj.matrix_world.translation) light.direction = tuple( obj.matrix_world.to_quaternion() @ Vector( (0.0, 0.0, -1.0))) light.radius = malt_light.radius light.spot_angle = malt_light.spot_angle light.spot_blend = malt_light.spot_blend_angle light.parameters = obj.data.malt_parameters.get_parameters( overrides, scene.proxys) types = { 'SUN': 1, 'POINT': 2, 'SPOT': 3, } light.type = types[obj.data.type] if light.type == types['SUN']: light.matrix = flatten_matrix( matrix.to_quaternion().to_matrix().to_4x4().inverted()) else: #Scaling too ???? light.matrix = flatten_matrix(matrix.inverted()) scene.lights.append(light) is_f12 = depsgraph.mode == 'RENDER' for obj in depsgraph.objects: if is_f12 or obj.visible_in_viewport_get(context.space_data): id = abs(hash(obj.name_full)) % (2**16) add_object(obj, obj.matrix_world, id) for instance in depsgraph.object_instances: if instance.instance_object: if is_f12 or instance.parent.visible_in_viewport_get( context.space_data): id = abs(instance.random_id) % (2**16) add_object(instance.instance_object, instance.matrix_world, id) return scene
def add_object(obj, matrix, id): if obj.display_type in [ 'TEXTURED', 'SOLID' ] and obj.type in ('MESH', 'CURVE', 'SURFACE', 'META', 'FONT'): name = MaltMeshes.get_mesh_name(obj) if depsgraph.mode == 'RENDER': name = '___F12___' + name if name not in meshes: # (Uses obj.original) Malt Parameters are not present in the evaluated mesh parameters = obj.original.data.malt_parameters.get_parameters( overrides, scene.proxys) malt_mesh = None if depsgraph.mode == 'VIEWPORT': malt_mesh = MaltMeshes.get_mesh(obj) else: #always load the mesh for final renders malt_mesh = MaltMeshes.load_mesh(obj, name) if malt_mesh: meshes[name] = [ Scene.Mesh(submesh, parameters) for submesh in malt_mesh ] for i, mesh in enumerate(meshes[name]): scene.proxys[('mesh', name, i)] = mesh.mesh else: meshes[name] = None mesh = meshes[name] if mesh is None: return scale = matrix.to_scale() mirror_scale = scale[0] * scale[1] * scale[2] < 0.0 matrix = flatten_matrix(matrix) obj_parameters = obj.malt_parameters.get_parameters( overrides, scene.proxys) obj_parameters['ID'] = id if len(obj.material_slots) > 0: for i, slot in enumerate(obj.material_slots): material = default_material if slot.material and slot.material.malt.get_source_path( ) != '': material_name = slot.material.name_full material_key = ('material', material_name) if material_key not in scene.proxys.keys(): path = slot.material.malt.get_source_path() shader_parameters = slot.material.malt.parameters.get_parameters( overrides, scene.proxys) material_parameters = slot.material.malt_parameters.get_parameters( overrides, scene.proxys) from Bridge.Proxys import MaterialProxy scene.proxys[material_key] = MaterialProxy( path, shader_parameters, material_parameters) material = scene.proxys[material_key] if override_material: material = override_material result = Scene.Object(matrix, mesh[i], material, obj_parameters, mirror_scale) scene.objects.append(result) else: material = default_material if override_material: material = override_material result = Scene.Object(matrix, mesh[0], material, obj_parameters, mirror_scale) scene.objects.append(result) elif obj.type == 'LIGHT': if obj.data.type == 'AREA': return #Not supported malt_light = obj.data.malt light = Scene.Light() light.color = tuple(obj.data.color * malt_light.strength) light.position = tuple(obj.matrix_world.translation) light.direction = tuple( obj.matrix_world.to_quaternion() @ Vector( (0.0, 0.0, -1.0))) light.radius = malt_light.radius light.spot_angle = malt_light.spot_angle light.spot_blend = malt_light.spot_blend_angle light.parameters = obj.data.malt_parameters.get_parameters( overrides, scene.proxys) types = { 'SUN': 1, 'POINT': 2, 'SPOT': 3, } light.type = types[obj.data.type] if light.type == types['SUN']: light.matrix = flatten_matrix( matrix.to_quaternion().to_matrix().to_4x4().inverted()) else: #Scaling too ???? light.matrix = flatten_matrix(matrix.inverted()) scene.lights.append(light)
def add_object(obj, matrix): if obj.display_type in ['TEXTURED','SOLID'] and obj.type in ('MESH','CURVE','SURFACE','FONT'): name = MaltMeshes.get_mesh_name(obj) if name not in meshes: # (Uses obj.original) Malt Parameters are not present in the evaluated mesh parameters = obj.original.data.malt_parameters.get_parameters(overrides, resources) malt_mesh = None if depsgraph.mode == 'VIEWPORT': malt_mesh = MaltMeshes.get_mesh(obj) else: #always load the mesh for final renders malt_mesh = MaltMeshes.load_mesh(obj) if malt_mesh: meshes[name] = [Scene.Mesh(submesh, parameters) for submesh in malt_mesh] else: meshes[name] = None mesh = meshes[name] if mesh is None: return scale = matrix.to_scale() mirror_scale = scale[0]*scale[1]*scale[2] < 0.0 matrix = flatten_matrix(matrix) obj_parameters = obj.malt_parameters.get_parameters(overrides, resources) if len(obj.material_slots) > 0: for i, slot in enumerate(obj.material_slots): material = None if slot.material: material_name = slot.material.name_full if material_name not in materials.keys(): shader = { 'path': bpy.path.abspath(slot.material.malt.shader_source), 'parameters': slot.material.malt.parameters.get_parameters(overrides, resources) } parameters = slot.material.malt_parameters.get_parameters(overrides, resources) materials[material_name] = Scene.Material(shader, parameters) material = materials[material_name] result = Scene.Object(matrix, mesh[i], material, obj_parameters, mirror_scale) scene.objects.append(result) else: result = Scene.Object(matrix, mesh[0], None, obj_parameters, mirror_scale) scene.objects.append(result) elif obj.type == 'LIGHT': if obj.data.type == 'AREA': return #Not supported malt_light = obj.data.malt light = Scene.Light() light.color = tuple(malt_light.color) light.position = tuple(obj.matrix_world.translation) light.direction = tuple(obj.matrix_world.to_quaternion() @ Vector((0.0,0.0,-1.0))) light.radius = malt_light.radius light.spot_angle = malt_light.spot_angle light.spot_blend = malt_light.spot_blend_angle light.parameters = obj.data.malt_parameters.get_parameters(overrides, resources) types = { 'SUN' : 1, 'POINT' : 2, 'SPOT' : 3, } light.type = types[obj.data.type] if light.type == types['SUN']: light.matrix = flatten_matrix(matrix.to_quaternion().to_matrix().to_4x4().inverted()) else: #Scaling too ???? light.matrix = flatten_matrix(matrix.inverted()) scene.lights.append(light)
def get_scene(self, context, depsgraph, request_scene_update, overrides): def flatten_matrix(matrix): return [e for v in matrix.transposed() for e in v] materials = {} meshes = {} resources = { 'materials': materials, } if request_scene_update == True: scene = Scene.Scene() self.scene = scene scene = self.scene scene.parameters = depsgraph.scene_eval.malt_parameters.get_parameters(overrides, resources) scene.world_parameters = depsgraph.scene_eval.world.malt_parameters.get_parameters(overrides, resources) scene.frame = depsgraph.scene_eval.frame_current r = depsgraph.scene_eval.render fps = r.fps / r.fps_base remap = r.frame_map_new / r.frame_map_old scene.time = (scene.frame / fps) * remap #Camera if depsgraph.mode == 'VIEWPORT': view_3d = context.region_data camera_matrix = flatten_matrix(view_3d.view_matrix) projection_matrix = flatten_matrix(view_3d.window_matrix) if view_3d.perspective_matrix != self.view_matrix: self.view_matrix = view_3d.perspective_matrix.copy() self.request_new_frame = True scene.camera = Scene.Camera(camera_matrix, projection_matrix) else: camera = depsgraph.scene_eval.camera camera_matrix = flatten_matrix(camera.matrix_world.inverted()) projection_matrix = flatten_matrix( camera.calc_matrix_camera( depsgraph, x=depsgraph.scene_eval.render.resolution_x, y=depsgraph.scene_eval.render.resolution_y )) scene.camera = Scene.Camera(camera_matrix, projection_matrix) if request_scene_update == False: return scene #Objects def add_object(obj, matrix): if obj.display_type in ['TEXTURED','SOLID'] and obj.type in ('MESH','CURVE','SURFACE','FONT'): name = MaltMeshes.get_mesh_name(obj) if name not in meshes: # (Uses obj.original) Malt Parameters are not present in the evaluated mesh parameters = obj.original.data.malt_parameters.get_parameters(overrides, resources) malt_mesh = None if depsgraph.mode == 'VIEWPORT': malt_mesh = MaltMeshes.get_mesh(obj) else: #always load the mesh for final renders malt_mesh = MaltMeshes.load_mesh(obj) if malt_mesh: meshes[name] = [Scene.Mesh(submesh, parameters) for submesh in malt_mesh] else: meshes[name] = None mesh = meshes[name] if mesh is None: return scale = matrix.to_scale() mirror_scale = scale[0]*scale[1]*scale[2] < 0.0 matrix = flatten_matrix(matrix) obj_parameters = obj.malt_parameters.get_parameters(overrides, resources) if len(obj.material_slots) > 0: for i, slot in enumerate(obj.material_slots): material = None if slot.material: material_name = slot.material.name_full if material_name not in materials.keys(): shader = { 'path': bpy.path.abspath(slot.material.malt.shader_source), 'parameters': slot.material.malt.parameters.get_parameters(overrides, resources) } parameters = slot.material.malt_parameters.get_parameters(overrides, resources) materials[material_name] = Scene.Material(shader, parameters) material = materials[material_name] result = Scene.Object(matrix, mesh[i], material, obj_parameters, mirror_scale) scene.objects.append(result) else: result = Scene.Object(matrix, mesh[0], None, obj_parameters, mirror_scale) scene.objects.append(result) elif obj.type == 'LIGHT': if obj.data.type == 'AREA': return #Not supported malt_light = obj.data.malt light = Scene.Light() light.color = tuple(malt_light.color) light.position = tuple(obj.matrix_world.translation) light.direction = tuple(obj.matrix_world.to_quaternion() @ Vector((0.0,0.0,-1.0))) light.radius = malt_light.radius light.spot_angle = malt_light.spot_angle light.spot_blend = malt_light.spot_blend_angle light.parameters = obj.data.malt_parameters.get_parameters(overrides, resources) types = { 'SUN' : 1, 'POINT' : 2, 'SPOT' : 3, } light.type = types[obj.data.type] if light.type == types['SUN']: light.matrix = flatten_matrix(matrix.to_quaternion().to_matrix().to_4x4().inverted()) else: #Scaling too ???? light.matrix = flatten_matrix(matrix.inverted()) scene.lights.append(light) for obj in depsgraph.objects: add_object(obj, obj.matrix_world) for instance in depsgraph.object_instances: if instance.instance_object: add_object(instance.instance_object, instance.matrix_world) #TODO: for i, obj in enumerate(scene.objects): obj.parameters['ID'] = i+1 scene.meshes = list(meshes.values()) scene.materials = list(materials.values()) return scene