2
$\begingroup$

Got this def I would like to use for multiple other defs in the same file

def matrix1():
    # create a location matrix
    mat_loc = mathutils.Matrix.Translation((-0.0, -0.0, -0.0))

    # create an identitiy matrix
    mat_sca = mathutils.Matrix.Scale(0.5, 4, (0.0, 0.0, 1.0))
    mat_sca1 = mathutils.Matrix.Scale(0.0, 4, (0.0, 0.0, 0.0))

    # create a rotation matrix
    mat_rot = mathutils.Matrix.Rotation(math.radians(90.0), 4, "Y")

    # combine transformations
    mat_out = mat_loc @ mat_rot @ mat_sca
    mat_out1 = mat_loc @ mat_rot

How can I "import" the combined transformation (mat_out or mat_out1) into another def?

def add_mesh(self, context):

    mesh = bpy.data.meshes.new("mesh")
    bm = bmesh.new()

    ret = bmesh.ops.create_circle(
        bm,
        segments=32,
        radius=1,
        matrix=mat_out1, #<-- this one gives error that mat_out1 is not defined
    )

I also tried to import def matrix but that also did not work.

$\endgroup$
2
  • $\begingroup$ This is a python question and not specific to Blender, so you should probably ask it on StackExchage with the python tag rather than here. But basically, have your first method return mat_out1 or make mat_out1 be global to the matrix1 rather than local. $\endgroup$ Commented Dec 31, 2021 at 22:06
  • 1
    $\begingroup$ @MartyFouts This question is according the rules. Python scripting for Blender addons. And if I ask this on stackexchange they say to go here because its Blender specific. $\endgroup$
    – tasey
    Commented Dec 31, 2021 at 23:37

2 Answers 2

3
$\begingroup$

Although Marty Fouts' solution works, you shouldn't use global variables. Why not? Because you have no "control" over these variables. It is very hard to debug and find mistakes in your program (if it gets bigger and bigger) and everybody can change them. Of course, if you have just 1 script with 20 lines of code, it's ok. But if you want to reuse your scripts, you should use a clean solution.

So you should work with e.g. a def which return values like so:

import bpy
import mathutils
import math

def matrix1():
    # create a location matrix
    mat_loc = mathutils.Matrix.Translation((-0.0, -0.0, -0.0))

    # create an identitiy matrix
    mat_sca = mathutils.Matrix.Scale(0.5, 4, (0.0, 0.0, 1.0))
    mat_sca1 = mathutils.Matrix.Scale(0.0, 4, (0.0, 0.0, 0.0))

    # create a rotation matrix
    mat_rot = mathutils.Matrix.Rotation(math.radians(90.0), 4, "Y")

    # combine transformations
    return mat_loc @ mat_rot @ mat_sca, mat_loc @ mat_rot




def add_mesh(self, context):

    mesh = bpy.data.meshes.new("mesh")
    bm = bmesh.new()

    matout, matout1 = matrix1()

    ret = bmesh.ops.create_circle(
        bm,
        segments=32,
        radius=1,
        matrix=matout1
    )
$\endgroup$
0
2
$\begingroup$

Here is one solution:

def matrix1():
    global mat_out
    global mat_out1
    # create a location matrix
    mat_loc = mathutils.Matrix.Translation((-0.0, -0.0, -0.0))

    # create an identitiy matrix
    mat_sca = mathutils.Matrix.Scale(0.5, 4, (0.0, 0.0, 1.0))
    mat_sca1 = mathutils.Matrix.Scale(0.0, 4, (0.0, 0.0, 0.0))

    # create a rotation matrix
    mat_rot = mathutils.Matrix.Rotation(math.radians(90.0), 4, "Y")

    # combine transformations
    mat_out = mat_loc @ mat_rot @ mat_sca
    mat_out1 = mat_loc @ mat_rot

By declaring the two matrices as global they will become available to use in the add_mesh function.

It really is a question about Python and not Blender.

$\endgroup$
2
  • $\begingroup$ I should learn how to formulate my questions correct. But tbh the stack exchange community is pretty harsh. (not the Blender Community) They downvote and close questions very easy or just commenting very rude. I asked questions there also that are python related like this one and the reactions are like "go to Blender (exchange) coz your using there API. Sometimes due to that I become afraid to ask at all since it is also affecting my account and sometime my mood. I am just asking some help you know and it feels like unwanted question.... Anyway your help is appreciated. Thanks :-) $\endgroup$
    – tasey
    Commented Jan 1, 2022 at 14:58
  • $\begingroup$ I'm sorry to hear that StackExchange community is so harsh. They should have seen that this is really a question about Python. In their defense they do get a lot of questions that aren't about language usage but about using some tool or another. Still, that's no excuse for rudeness. If you're going to do a lot of Python programming for Blender, you might want to watch "Scripting for Artists" series, at least the free parts, from Blender Studio. You can find it on YouTube. $\endgroup$ Commented Jan 1, 2022 at 15:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .