5
$\begingroup$

In Python I want to know what bpy_struct is for? Can you give me an example of its use in programming?

$\endgroup$
4
$\begingroup$

Note that bpy.types.bpy_struct is not actually available from within Blender, it only exists for the purpose of documentation.

In Object Oriented Programming when you define a class you can specify a parent class, your new class then adds some functionality to what the parent class provides.

In blender the bpy_struct is essentially the base class for all classes in blender and provides the core functionality that is common to everything. You cannot use it directly in python but you will often use the functionality it provides in it's many subclasses that you do use.

Examples would be obj.keyframe_insert('location') or pythons standard keys() and values() methods which are implemented in bpy_struct.

$\endgroup$
  • $\begingroup$ In c++ all members (data fields or methods) of a class are private by default (only accessible within the class or its derived classes). Instead of declaring a class, the language also allows declaring structs. In most cases structs are used for data collections (composite objects) in an application, because their members are public by default and thus easy to access without getter or setter methods, similar like accessing properties in blender. I'm not quite sure (not a developer), but I guess that the name is based on that analogy of holding objects rather than providing generic functions $\endgroup$ – p2or Mar 13 '17 at 17:52
  • $\begingroup$ @poor it may also be that in C (which is what most of blender is written in, not C++), there are no classes, only structs. $\endgroup$ – Sazerac Mar 14 '17 at 2:59
1
$\begingroup$

In Blender's data model, bpy.types.bpy_struct is the base class for all data types. One of the core features of the data model is properties (which in turn are typed) and bpy_struct provides the interface and implementation for setting/getting/resolving of properties. Resolution is a interesting term because some data types can hold relative data paths to properties and by using the interface provided by bpy_struct one can safely try and resolve the property. If you look at the documentation for bpy_struct, you will notice that it has a lot of subclasses!

An example: the following code snippet uses bpy_struct.path_resolve on a data_path stored in a FCurve for a pose bone in an armature and then selects the bone in the viewport.

armature = bpy.context.scene.objects['Armature']

# Provided you have keyframe the above armature in pose mode
action = bpy.data.actions['ArmatureAction.002']

# Get the first FCurve
fcurve = action.fcurves.values()[0]

# Get the data_path of the pose_bone property that was keyframed
pose_bone_path = fcurve.data_path.rpartition('.')[0]

# path_resolve is the method provided by bpy_struct of which Armature is a subclass of
pose_bone = armature.path_resolve(pose_bone_path)

# Get the actual bone of the armature (has the same name as pose bone)
bone = armature.bones[pose_bone.name]

# Select the bone
bone.select = True
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.