Last active
January 3, 2016 15:39
-
-
Save dertom95/2db69b7fed6cbae73f9a to your computer and use it in GitHub Desktop.
One way to bind data to specific blender spaces.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# snippet to add data to specific space-views by dertom | |
# | |
# - alter SpaceContext-PropertyGroup to fit your needs | |
# - import the SpaceContext to your script: import SpaceContext | |
# - when needed get the space-context (your SpaceContext-PropertyGroup) via: | |
# currentSpaceContext = SpaceContext.retrieveSpaceContext(context) | |
# | |
# For now you have to press a button to trigger the operation that creates the context for a single space | |
# | |
# This sample is indended to be run from the text-editor and will create a panel in each view_3d spaces ui-region. | |
# For testing open multiple view3d-spaces, add the space-context via button and then you can choose one object which | |
# will be set in the new space-context (independ from the view data) | |
import bpy | |
# The spacecontext: define per space data you want to keep in each space-view: | |
class SpaceContext(bpy.types.PropertyGroup): | |
objectName = bpy.props.StringProperty() | |
# create a data-slot for the current space. not sure how to call this automatically from | |
# panel's draw-method. For now you will have to press the button once to init spacecontext | |
class SpaceContextOp(bpy.types.Operator): | |
"""Tooltip""" | |
bl_idname = "utils.spacecontext" | |
bl_label = "Create SpaceContext" | |
@classmethod | |
def poll(cls, context): | |
return True | |
def execute(self, context): | |
spaceSignature = getCurrentSpaceSignature(context) | |
# check if there is already data for this space | |
world = bpy.data.worlds[0] | |
pos = world.spaceMapping.find(spaceSignature) | |
data = None | |
if pos==-1: # no slot for this space-view? create one | |
data = world.spaceMapping.add() | |
data.name = spaceSignature | |
return {'FINISHED'} | |
# get the spacecontext-signature,a unique-string,for the given context | |
def getCurrentSpaceSignature(context): | |
current_space = context.space_data | |
for screenIdx in range(0,len(bpy.data.screens)): | |
screen = bpy.data.screens[screenIdx] | |
for areaIdx in range(0,len(screen.areas)): | |
area = screen.areas[areaIdx] | |
for spaceIdx in range(0,len(area.spaces)): | |
space = area.spaces[spaceIdx] | |
if (space == current_space): | |
name = str(screenIdx)+str(areaIdx)+str(spaceIdx) | |
return name | |
# This is the call you want to do to receive the current space-context | |
# get the spacecontext or None | |
def getCurrentSpaceContext(context): | |
signature = getCurrentSpaceSignature(context) | |
world = bpy.data.worlds[0] | |
pos = world.spaceMapping.find(signature) | |
if pos == -1: | |
# print("NO CONTEXT FOUND FOR SIG:"+signature) | |
return None | |
else: | |
# print("FOUND CONTEXT FOR SIG:"+signature) | |
return world.spaceMapping[pos] | |
class SpaceContextPanel(bpy.types.Panel): | |
"""Testing Space-dependent data""" | |
bl_label = "SpaceContext Demo" | |
bl_idname = "spacecontext_demo" | |
bl_space_type = 'VIEW_3D' | |
bl_region_type = 'UI' | |
def draw(self, context): | |
layout = self.layout | |
spaceCtx = getCurrentSpaceContext(context) | |
if not spaceCtx: # no spacecontext bound to this space? show button to create one | |
layout.operator("utils.spacecontext") | |
else: # spaceCtx found | |
layout.prop_search(spaceCtx,"objectName",bpy.data,"objects") | |
def register(): | |
bpy.utils.register_class(SpaceContext) | |
bpy.types.World.spaceMapping = bpy.props.CollectionProperty(type=bpy.types.SpaceContext) | |
bpy.utils.register_class(SpaceContextOp) | |
bpy.utils.register_class(SpaceContextPanel) | |
def unregister(): | |
bpy.utils.unregister_class(bpy.types.SpaceContextOp) | |
bpy.utils.unregister_class(bpy.types.SpaceContext) | |
bpy.utils.unregister_class(bpy.types.SpaceContextPanel) | |
if __name__ == "__main__": | |
register() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment