Writing scripts in Blender we often need to know the full list of object properties to access them through the Blender API. Consider how we can get a list of available object methods and properties.
- “dir” command
The simplest way to get the complete structure of the object you need is to run the “dir” command in the Python Console window, specifying the object as the argument.
Let’s get the properties and methods of the Glossy BSDF node. Add it to the Node Editor, select it (make it active) and in the Python Console window execute the “dir” command, specifying the active node as the argument:
1 |
dir(bpy.context.active_object.active_material.node_tree.nodes.active) |
After executing the command, a list of properties and methods of the selected node will be displayed in the Python Console window:
The “dir” command gives a complete structure of the object but does not show its current properties values.
- Saving the “rna” structure of the object to a file
After the “dir” command execution we can see that bl_rna and rna_type structures exist in the node object. This means that the rna-structure of this node can be stored in a file.
To do this, let’s write the following simple script:
1 2 3 4 5 6 7 8 9 |
import bpy import os from rna_xml import rna2xml file = open(os.path.dirname(bpy.data.filepath) + os.sep + 'glossy_rna.xml', 'w') rna2xml(file.write, root_node = 'glossy_bsdf', root_rna = bpy.context.active_object.active_material.node_tree.nodes.active) file.close() |
This script creates a file named glossy_rna.xml in the blender project’s directory. Then, with the rna_xml module and the rna2xml command use, it saves the structure of the selected node (specified in the root_rna parameter) to this file.
The contents of the glossy_rna.xml file after script execution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
<glossy_bsdf> <inputs> <NodeSocketColor name="Color" identifier="Color" is_output="FALSE" hide="FALSE" enabled="TRUE" link_limit="1" is_linked="FALSE" show_expanded="FALSE" hide_value="FALSE" node="ShaderNodeBsdfGlossy::Glossy BSDF" type="RGBA" bl_idname="NodeSocketColor" default_value="0.8 0.8 0.8 1"> </NodeSocketColor> <NodeSocketFloatFactor name="Roughness" identifier="Roughness" is_output="FALSE" hide="FALSE" enabled="TRUE" link_limit="1" is_linked="FALSE" show_expanded="FALSE" hide_value="FALSE" node="ShaderNodeBsdfGlossy::Glossy BSDF" type="VALUE" bl_idname="NodeSocketFloatFactor" default_value="0.2"> </NodeSocketFloatFactor> <NodeSocketVector name="Normal" identifier="Normal" is_output="FALSE" hide="FALSE" enabled="TRUE" link_limit="1" is_linked="FALSE" show_expanded="FALSE" hide_value="TRUE" node="ShaderNodeBsdfGlossy::Glossy BSDF" type="VECTOR" bl_idname="NodeSocketVector" default_value="0 0 0"> </NodeSocketVector> </inputs> <internal_links> </internal_links> <outputs> <NodeSocketShader name="BSDF" identifier="BSDF" is_output="TRUE" hide="FALSE" enabled="TRUE" link_limit="4095" is_linked="FALSE" show_expanded="FALSE" hide_value="FALSE" node="ShaderNodeBsdfGlossy::Glossy BSDF" type="SHADER" bl_idname="NodeSocketShader"> </NodeSocketShader> </outputs> </glossy_bsdf> |
The structure of the node properties saved to file with their current values.
Combining this two methods of getting Blender objects structure, we can receive the complete information about their methods and properties with the current values.