I would like to understand how to pass an object of type bpy.data
to an operator, but I don't understand how to do it. I can pass a string, which will then retrieve what I'm looking for. In this example I show how I pass a string to the operator. This string will retrieve the object, but I don't like it that way and I don't know how to pass a bpy.data
object to that operator directly.
I want to clarify that this question isn't related to how to get the context.object
, but rather I'm interested in getting any type of object, so this is just an example to better define my question.
import bpy
from bpy.props import StringProperty
class SimpleOperator(bpy.types.Operator):
bl_idname = "object.simple_operator"
bl_label = "Simple Object Operator"
object_name: StringProperty()
#How to:
#object: <---------------------
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
#I can find the object like this:
ob = bpy.data.objects[self.object_name]
#But i want:
# ob = self.object <---------------------
return {'FINISHED'}
class HelloWorldPanel(bpy.types.Panel):
"""Creates a Panel in the Object properties window"""
bl_label = "Hello World Panel"
bl_idname = "OBJECT_PT_hello"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "object"
def draw(self, context):
ob = context.object
layout = self.layout
op = layout.operator("object.simple_operator",text="Test")
op.object_name = ob.name
op.object = ob
def register():
bpy.utils.register_class(SimpleOperator)
bpy.utils.register_class(HelloWorldPanel)
def unregister():
bpy.utils.unregister_class(SimpleOperator)
bpy.utils.unregister_class(HelloWorldPanel)
if __name__ == "__main__":
register()
object: PointerProperty(type=bpy.types.Object)
only to get the error message saying you can't assign datablock pointers to an operator.... possibly explains why you never see this used in any operator. The idea si to use the operator / context paradigm and use context to reference objects. Going to have to grit your teeth and just put up with using method outlined in question, despite not liking it. $\endgroup$group = bpy.data.node_groups[self.node_group_name]
andinput=group.inputs[self.input_int_idx]
$\endgroup$def register
&def unregister
are now incorrectly indented into the classHelloWorldPanel
$\endgroup$